新增 AppDomainDllLoader 和 DesignPluginEntry 类

在 AppDomainDllLoader.cs 文件中新增了 AppDomainDllLoader 类,用于处理 DLL 加载问题,解决依赖问题。
在 DesignPluginEntry.cs 文件中新增了 DesignPluginEntry 类,增加3D插件入口。
在 PluginEntry.cs 文件中,修改了 Version 属性的获取方式,并新增了对 AppDomainDllLoader.SetLaoder() 方法的调用。
在 AssemblyInfo.cs 文件中,更新了程序集的版本信息,将 AssemblyVersion、AssemblyFileVersion 和 AssemblyInformationalVersion 都更新为 1.0.0.6。
在 Sinvo.EplanHpD.Plugin.csproj 文件中,新增了对 HandyControl 库的引用,并添加了 AppDomainDllLoader.cs 和 DesignPluginEntry.cs 文件的编译项。
在 packages.config 文件中,新增了 HandyControl 库的包引用,版本为 3.2.0,目标框架为 net481。
This commit is contained in:
lihanbo 2024-11-07 12:13:19 +08:00
parent 09c4e983bd
commit c620fda442
7 changed files with 154 additions and 4 deletions

View File

@ -0,0 +1,71 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Sinvo.EplanHpD.Plugin
{
public class AppDomainDllLoader
{
public static bool isLoaded = false;
public static void SetLaoder()
{
if (isLoaded)
{
return;
}
// 接管DLL加载,解决依赖问题
// 插件被加载时,AppContext.BaseDirectory 会指向Eplan的安装目录,导致一部分依赖的DLL在加载时找不到
// 接管之后,从两个目录都查找一次
//throw new NotImplementedException();
try
{
// 防止重复加载 先卸载事件
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolveHandle;
}
catch (Exception ex)
{
Trace.TraceError(ex.Message);
}
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandle;
isLoaded = true;
}
public static Assembly AssemblyResolveHandle(object sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
// 判断是否已经被加载
var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.FullName == assemblyName.FullName);
if (loadedAssembly != null)
{
Debug.WriteLine($"Dll is loaded -> {loadedAssembly.FullName}");
return loadedAssembly;
}
// 从当前路径下加载
var path = Path.Combine(AppContext.BaseDirectory, $"{assemblyName.Name}.dll");
if (File.Exists(path))
{
Debug.WriteLine($"Load dll for path -> {path}");
return Assembly.LoadFile(path);
}
var envPath = System.Environment.CurrentDirectory;
// 从插件路径中加载依赖
path = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(AppDomainDllLoader)).Location), $"{assemblyName.Name}.dll");
if (File.Exists(path))
{
//MessageBox.Show($"Load dll for self path -> {path}");
Debug.WriteLine($"Load dll for self path -> {path}");
return Assembly.LoadFile(path);
}
//MessageBox.Show(path);
Debug.WriteLine($"File not load! -> {path}");
return null;
}
}
}

View File

@ -0,0 +1,64 @@
using EPLAN.Harness.API;
using EPLAN.Harness.API.Plugins;
using EPLAN.Harness.API.Plugins.Core;
using Sinvo.EplanHpD.Plugin.WPFUI;
using System;
using System.Drawing;
using System.IO;
using System.Reflection;
namespace Sinvo.EplanHpD.Plugin
{
public class DesignPluginEntry : IHpDPlugin
{
public string Name => "兴禾ProD插件-3D";
public string Author => "Sinvo";
public string Description => "兴禾EPLAN Harness proD 2.9 Studio插件,提供线材数据抓取功能";
/// <summary>
/// 获取DLL版本
/// </summary>
public Version Version => Assembly.GetAssembly(this.GetType()).GetName().Version;
public System.Drawing.Image ToolbarIcon
{
get
{
var imageBytes = Resource.Sinvo;
if (imageBytes != null)
{
using (var ms = new MemoryStream(imageBytes))
{
return Image.FromStream(ms);
}
}
return null;
}
}
public string ToolbarText => "抓取线材数据";
public string ToolbarTooltip => "抓取线材数据";
public HpDModule Module => HpDModule.WorkSpace;
public void Execute(HpdApi api)
{
var doc = api.CurrentProject.GetActiveDocument();
new LectotypeWindow(doc.ID).Show();
}
public void Initialize()
{
AppDomainDllLoader.SetLaoder();
}
public void Terminate()
{
//throw new NotImplementedException();
}
}
}

View File

@ -25,7 +25,7 @@ namespace Sinvo.EplanHpD.Plugin
/// <summary>
/// 获取DLL版本
/// </summary>
public Version Version => Assembly.GetExecutingAssembly().GetName().Version;
public Version Version => Assembly.GetAssembly(this.GetType()).GetName().Version;
public System.Drawing.Image ToolbarIcon
{
@ -56,6 +56,7 @@ namespace Sinvo.EplanHpD.Plugin
try
{
var doc = api.CurrentProject.GetActiveDocument();
//doc.ID
//if(doc is FlexReport)
if (doc is EPLAN.Harness.API.Projects.Documents.Report report)
{
@ -140,6 +141,8 @@ namespace Sinvo.EplanHpD.Plugin
public void Initialize()
{
//throw new NotImplementedException();
AppDomainDllLoader.SetLaoder();
}
public void Terminate()

View File

@ -28,6 +28,6 @@ using System.Runtime.InteropServices;
// 生成号
// 修订号
//
[assembly: AssemblyVersion("1.0.0.5")]
[assembly: AssemblyFileVersion("1.0.0.1")]
[assembly: AssemblyInformationalVersion("1.0.0.2")]
[assembly: AssemblyVersion("1.0.0.6")]
[assembly: AssemblyFileVersion("1.0.0.6")]
[assembly: AssemblyInformationalVersion("1.0.0.6")]

Binary file not shown.

View File

@ -68,6 +68,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>RefDLL\EPLAN.Harness.ProjectCore.dll</HintPath>
</Reference>
<Reference Include="HandyControl, Version=3.2.0.0, Culture=neutral, PublicKeyToken=45be8712787a1e5b, processorArchitecture=MSIL">
<HintPath>..\packages\HandyControl.3.2.0\lib\net48\HandyControl.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
@ -84,6 +87,8 @@
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Compile Include="AppDomainDllLoader.cs" />
<Compile Include="DesignPluginEntry.cs" />
<Compile Include="PluginEntry.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resource.Designer.cs">
@ -104,5 +109,8 @@
<Name>Sinvo.EplanHpD.Plugin.WPFUI</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HandyControl" version="3.2.0" targetFramework="net481" />
</packages>