Skip to content

Commit 1adc2ff

Browse files
committed
Last state
1 parent 912ef3b commit 1adc2ff

File tree

6 files changed

+115
-7
lines changed

6 files changed

+115
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Runtime.InteropServices;
4+
5+
namespace Python.Loader
6+
{
7+
public static class InternalDllImportResolver
8+
{
9+
public static IntPtr Resolve(string libraryName, Assembly assembly, int? flags) {
10+
if (libraryName == "__Internal") {
11+
12+
}
13+
}
14+
15+
}
16+
}

Python.Loader/Loader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
using System.Reflection;
66
using Mono.Cecil;
77

8-
namespace Python
8+
namespace Python.Loader
99
{
10-
public class Loader
10+
public class RuntimeLoader
1111
{
1212
AssemblyDefinition assembly;
1313

14-
static public Loader FromFile(string filename)
14+
static public RuntimeLoader FromFile(string filename)
1515
{
16-
return new Loader(File.OpenRead(filename));
16+
return new RuntimeLoader(File.OpenRead(filename));
1717
}
1818

19-
public Loader(Stream stream)
19+
public RuntimeLoader(Stream stream)
2020
{
2121
assembly = AssemblyDefinition.ReadAssembly(stream, new ReaderParameters
2222
{
@@ -74,7 +74,7 @@ public static int Initialize(IntPtr data, int size)
7474
var dllPath = splitted[0];
7575
var pythonDll = splitted[1];
7676

77-
var loader = Loader.FromFile(dllPath);
77+
var loader = RuntimeLoader.FromFile(dllPath);
7878
loader.Remap(pythonDll);
7979
var assembly = loader.LoadAssembly();
8080

Python.Loader/Python.Loader.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
</PropertyGroup>
77

88
<ItemGroup>
99
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
10+
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
1011
</ItemGroup>
1112

1213
</Project>

net_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
#os.environ["MONO_LOG_LEVEL"] = "debug"
3+
# os.environ["MONO_LOG_MASK"] = "cfg,dll"
4+
os.environ["COREHOST_DEBUG"] = "1"
5+
os.environ["COREHOST_TRACE"] = "1"
6+
os.environ["COREHOST_TRACE_VERBOSITY"] = "4"
7+
8+
import pythonnet, clr_loader
9+
10+
import sys
11+
12+
if sys.argv[1] == "mono":
13+
mono = clr_loader.get_mono()
14+
rt = mono
15+
elif sys.argv[1] == "core":
16+
core = clr_loader.get_coreclr("/home/benedikt/git/clr-loader/example/out/example.runtimeconfig.json")
17+
rt = core
18+
19+
pythonnet.set_runtime(rt)
20+
pythonnet.load()
21+
22+
print("Loaded pythonnet")
23+
24+
import clr
25+
from System import Console
26+
Console.WriteLine("Success")

tools/remap_dll_target/Program.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.ComponentModel;
2+
using System.Reflection;
3+
using System.Xml.Linq;
4+
using System;
5+
using System.Linq;
6+
using Mono.Cecil;
7+
8+
namespace RemapDllTarget
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
var filename = args[0];
15+
Console.WriteLine($"Loading existing DLL {filename}...");
16+
var def = AssemblyDefinition.ReadAssembly(filename, new ReaderParameters {
17+
ReadingMode = ReadingMode.Immediate,
18+
ReadWrite = true,
19+
InMemory = true
20+
});
21+
22+
var mappings = args.Skip(1)
23+
.Select(x => x.Split('=', 2).ToArray())
24+
.ToDictionary(x => x[0], x => new ModuleReference(x[1]));
25+
26+
Console.WriteLine($"Remapping DllImport paths in {filename}...");
27+
foreach (var kvp in mappings) {
28+
Console.WriteLine($"{kvp.Key} => {kvp.Value}");
29+
}
30+
31+
var module = def.MainModule;
32+
foreach (var mref in mappings.Values)
33+
module.ModuleReferences.Add(mref);
34+
35+
foreach (var type in def.MainModule.Types) {
36+
foreach (var func in type.Methods) {
37+
if (func.HasPInvokeInfo) {
38+
var info = func.PInvokeInfo;
39+
if (mappings.TryGetValue(info.Module.Name, out ModuleReference newRef)) {
40+
Console.WriteLine($"Remapping {info.EntryPoint}: {info.Module} => {newRef}");
41+
info.Module = newRef;
42+
}
43+
44+
func.PInvokeInfo = info;
45+
}
46+
}
47+
}
48+
49+
Console.WriteLine($"Writing result");
50+
def.Write(filename);
51+
}
52+
}
53+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)