From 9c32f1a506747ce74812080b35a29c9cd14c49a9 Mon Sep 17 00:00:00 2001 From: chuongmep <31106432+chuongmep@users.noreply.github.com> Date: Thu, 18 Apr 2024 00:13:38 +0800 Subject: [PATCH] resolve dependencies --- AddInManager.sln | 28 +++++++++++++++ AddInManager/Command/AddinManagerBase.cs | 21 +++++++++++- AddInManager/Model/AssemblyLoadContext.cs | 42 +++++++++++++++++++++-- Test/Test.csproj | 3 ++ Test/TestEvaCommand.cs | 11 +++--- Test2/DependLib.cs | 10 ++++++ Test2/Test2.csproj | 38 ++++++++++++++++++++ 7 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 Test2/DependLib.cs create mode 100644 Test2/Test2.csproj diff --git a/AddInManager.sln b/AddInManager.sln index f4bd88f..e03418e 100644 --- a/AddInManager.sln +++ b/AddInManager.sln @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RevitElementBipChecker", "RevitElementBipChecker\RevitElementBipChecker.csproj", "{21460D85-C4AD-49D5-963F-CF13C4AE99EB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test2", "Test2\Test2.csproj", "{DA609427-F086-4C79-A5FA-202DBB5DE48D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug R22|Any CPU = Debug R22|Any CPU @@ -94,6 +96,8 @@ Global {1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release R24|Any CPU.ActiveCfg = Release R24|Any CPU {1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release R25|Any CPU.ActiveCfg = Release R25|Any CPU {1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release R25|Any CPU.Build.0 = Release R25|Any CPU + {1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R25|Any CPU.ActiveCfg = Release R25|Any CPU + {1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R25|Any CPU.Build.0 = Release R25|Any CPU {21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU {21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R22|Any CPU.Build.0 = Debug R22|Any CPU {21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Installer|Any CPU.ActiveCfg = Debug R22|Any CPU @@ -118,6 +122,30 @@ Global {21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Release R25|Any CPU.Build.0 = Release R25|Any CPU {21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R25|Any CPU.ActiveCfg = Release R25|Any CPU {21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R25|Any CPU.Build.0 = Release R25|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R22|Any CPU.Build.0 = Debug R22|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R23|Any CPU.ActiveCfg = Debug R23|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R23|Any CPU.Build.0 = Debug R23|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R24|Any CPU.ActiveCfg = Debug R24|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R24|Any CPU.Build.0 = Debug R24|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R25|Any CPU.ActiveCfg = Debug R21|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R25|Any CPU.Build.0 = Debug R21|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Installer|Any CPU.ActiveCfg = Debug R21|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Installer|Any CPU.Build.0 = Debug R21|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R19|Any CPU.ActiveCfg = Release R19|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R19|Any CPU.Build.0 = Release R19|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R20|Any CPU.ActiveCfg = Release R20|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R20|Any CPU.Build.0 = Release R20|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R21|Any CPU.ActiveCfg = Release R21|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R21|Any CPU.Build.0 = Release R21|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R22|Any CPU.ActiveCfg = Release R22|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R22|Any CPU.Build.0 = Release R22|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R23|Any CPU.ActiveCfg = Release R23|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R23|Any CPU.Build.0 = Release R23|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R24|Any CPU.ActiveCfg = Release R24|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R24|Any CPU.Build.0 = Release R24|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R25|Any CPU.ActiveCfg = Release R25|Any CPU + {DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R25|Any CPU.Build.0 = Release R25|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AddInManager/Command/AddinManagerBase.cs b/AddInManager/Command/AddinManagerBase.cs index 015c363..ded900f 100644 --- a/AddInManager/Command/AddinManagerBase.cs +++ b/AddInManager/Command/AddinManagerBase.cs @@ -97,9 +97,24 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele return 0; } Result result = Result.Failed; - var alc = new AssemblyLoadContext(); + var alc = new AssemblyLoadContext(filePath); try { + + // get list dependencies assembly at same folder + //var dependencies = alc.GetDependencies(filePath); + // List subAlcs = new List(); + // foreach (var dependency in dependencies) + // { + // string subFilePath = Path.Combine(Path.GetDirectoryName(filePath), dependency + ".dll"); + // var subAlc = new AssemblyLoadContext(subFilePath); + // if (!File.Exists(subFilePath)) continue; + // // load stream with security permission + // // var subStream = new FileStream(subFilePath, FileMode.Open, FileAccess.Read); + // subAlc.LoadFromAssemblyPath(subFilePath); + // // subStream.Close(); + // subAlcs.Add(subAlc); + // } var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); Assembly assembly = alc.LoadFromStream(stream); object instance = assembly.CreateInstance(_activeCmdItem.FullClassName); @@ -109,6 +124,10 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele _activeEc = externalCommand; result = _activeEc.Execute(data, ref message, elements); alc.Unload(); + // foreach (AssemblyLoadContext subAlc in subAlcs) + // { + // subAlc.Unload(); + // } } int counter = 0; for (counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++) diff --git a/AddInManager/Model/AssemblyLoadContext.cs b/AddInManager/Model/AssemblyLoadContext.cs index d9a0ee4..e83acaa 100644 --- a/AddInManager/Model/AssemblyLoadContext.cs +++ b/AddInManager/Model/AssemblyLoadContext.cs @@ -1,6 +1,5 @@ - - -#if R25 +#if R25 +using System.IO; using System.Reflection; using System.Runtime.Loader; @@ -10,7 +9,44 @@ class AssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext { public AssemblyLoadContext() : base(isCollectible: true) { + } + private AssemblyDependencyResolver _resolver; + + public AssemblyLoadContext(string pluginPath): base(isCollectible: true) + { + _resolver = new AssemblyDependencyResolver(pluginPath); + } + + protected override Assembly Load(AssemblyName assemblyName) + { + string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName); + if (assemblyPath != null) + { + if(assemblyPath.Contains("RevitAPI")) + { + return null; + } + if(assemblyPath.Contains("RevitAPIUI")) + { + return null; + } + var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read); + return LoadFromStream(stream); + } + + return null; + } + + protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) + { + string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName); + if (libraryPath != null) + { + return LoadUnmanagedDllFromPath(libraryPath); + } + return IntPtr.Zero; } + } #endif \ No newline at end of file diff --git a/Test/Test.csproj b/Test/Test.csproj index a10a0cf..d61fd83 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -57,5 +57,8 @@ + + + diff --git a/Test/TestEvaCommand.cs b/Test/TestEvaCommand.cs index f678d61..8d0d1a3 100644 --- a/Test/TestEvaCommand.cs +++ b/Test/TestEvaCommand.cs @@ -1,7 +1,9 @@ -using System.Windows; +using System; +using System.Windows; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; +using Test2; namespace Test; @@ -10,9 +12,10 @@ public class TestEvaCommand : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { - - UIApplication uiApplication = commandData.Application; - MessageBox.Show(uiApplication.Application.VersionBuild); + MessageBox.Show("TestEvaCommand"); + double plus = DependLib.Plus(); + Console.WriteLine(plus); + MessageBox.Show(plus.ToString()); return Result.Succeeded; } } \ No newline at end of file diff --git a/Test2/DependLib.cs b/Test2/DependLib.cs new file mode 100644 index 0000000..c35de48 --- /dev/null +++ b/Test2/DependLib.cs @@ -0,0 +1,10 @@ +namespace Test2 +{ + public static class DependLib + { + public static double Plus() + { + return 2 + 6; + } + } +} \ No newline at end of file diff --git a/Test2/Test2.csproj b/Test2/Test2.csproj new file mode 100644 index 0000000..bd31f52 --- /dev/null +++ b/Test2/Test2.csproj @@ -0,0 +1,38 @@ + + + + net48 + enable + latest + x64 + true + + + true + full + $(DefineConstants);DEBUG + + + true + none + $(DefineConstants);RELEASE + + + $(RevitVersion) + true + false + A Project Support for developer in revit + + + + + + + + + + + + + +