Skip to content

Commit

Permalink
appdomain for deps (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjkkirschner authored Dec 12, 2023
1 parent e5ad676 commit 53e979c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
6 changes: 3 additions & 3 deletions DSIronPython/DSIronPython.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="$(SolutionDir)Config\shared.props" />
</ImportGroup>
<PropertyGroup>
<OutputPath>$(SolutionDir)\package_output\DSIronPython\bin\</OutputPath>
<OutputPath>$(SolutionDir)\package_output\DSIronPython\extra\</OutputPath>
<ProjectGuid>{9EEF4F42-6B3B-4358-9A8A-C2701539A822}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
Expand All @@ -29,7 +29,7 @@
</AssemblyAttribute>
</ItemGroup>

<Target Name="Move python libs to extra" AfterTargets="Build">
<!--<Target Name="Move python libs to extra" AfterTargets="Build">
<ItemGroup>
<MySourceFiles Include="$(OutputPath)\*lib\**;" />
</ItemGroup>
Expand All @@ -39,7 +39,7 @@
<Target Name="Remove Lib" AfterTargets="Move python libs to extra">
<RemoveDir Directories="$(OutputPath)\lib" />
</Target>
</Target>-->

<Target Name="copypkgjson" AfterTargets="Build">
<Copy SourceFiles="pkg.json" DestinationFolder="$(OutputPath)..\"/>
Expand Down
55 changes: 35 additions & 20 deletions IronPythonExtension/IronPythonExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using Dynamo.Extensions;
using Dynamo.Logging;
using Dynamo.PythonServices;
Expand Down Expand Up @@ -61,26 +62,9 @@ public void Startup(StartupParams sp)
/// <param name="sp"></param>
public void Ready(ReadyParams sp)
{
// Searches for DSIronPython engine binary in same folder with extension itself
var targetDir = Path.GetDirectoryName(Assembly.GetAssembly(typeof(IronPythonExtension)).Location);
var libraryLoader = sp.StartupParams.LibraryLoader;
Assembly pythonEvaluatorLib = null;
try
{
pythonEvaluatorLib = Assembly.LoadFrom(Path.Combine(targetDir, PythonEvaluatorAssembly + ".dll"));
}
catch (Exception ex)
{
// Most likely the IronPython engine is excluded in this case
// but logging the exception message in case for diagnose
OnMessageLogged(LogMessage.Info(ex.Message));
return;
}
// Import IronPython Engine into VM, so Python node using IronPython engine could evaluate correctly
if (pythonEvaluatorLib != null)
{
libraryLoader.LoadNodeLibrary(pythonEvaluatorLib);
}
var extraPath = Path.Combine(new FileInfo(Assembly.GetAssembly(typeof(IronPythonExtension)).Location).Directory.Parent.FullName, "extra");
var alc = new IsolatedPythoContext(Path.Combine(extraPath,"DSIronPython.dll"));
alc.LoadFromAssemblyName(new AssemblyName("DSIronPython"));
}

/// <summary>
Expand All @@ -91,4 +75,35 @@ public void Shutdown()
// Do nothing for now
}
}
internal class IsolatedPythoContext : AssemblyLoadContext
{
private AssemblyDependencyResolver resolver;

public IsolatedPythoContext(string libPath)
{
resolver = new AssemblyDependencyResolver(libPath);
}

protected override Assembly Load(AssemblyName assemblyName)
{
string assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}

return null;
}

protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string libraryPath = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}

return IntPtr.Zero;
}
}
}
4 changes: 2 additions & 2 deletions IronPythonExtension/IronPythonExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="$(SolutionDir)Config\shared.props" />
</ImportGroup>
<PropertyGroup>
<OutputPath>$(SolutionDir)\package_output\DSIronPython\bin\</OutputPath>
<OutputPath>$(SolutionDir)\package_output\DSIronPython\extra\</OutputPath>
<ProjectGuid>{182FCA4E-B6EF-451F-9EC4-7BF2C622F4F7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
Expand All @@ -21,6 +21,6 @@
<ItemGroup>
<ExtensionDefinition Include="IronPythonExtension_ExtensionDefinition.xml" />
</ItemGroup>
<Copy SourceFiles="@(ExtensionDefinition)" DestinationFolder="$(OutputPath)..\extra" />
<Copy SourceFiles="@(ExtensionDefinition)" DestinationFolder="$(OutputPath)" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ExtensionDefinition>
<AssemblyPath>..\bin\IronPythonExtension.dll</AssemblyPath>
<AssemblyPath>IronPythonExtension.dll</AssemblyPath>
<TypeName>IronPythonExtension.IronPythonExtension</TypeName>
</ExtensionDefinition>

0 comments on commit 53e979c

Please sign in to comment.