Skip to content

Commit

Permalink
chore: Adjust loader
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 11, 2024
1 parent 21e8d00 commit d82dbb5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]
config: [Debug, Release]

env:
LD_DEBUG: libs

runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand Down
53 changes: 29 additions & 24 deletions src/Resizetizer/src/SkiaSharpTools.Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static void Initialize()

if (isNetCore)
{
SetupResolver();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
NetcoreInitializeWindows();
Expand Down Expand Up @@ -82,35 +84,38 @@ private static void NetcoreInitializeWindows()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var dllImportResolverDelegateType = Type.GetType("System.Runtime.InteropServices.DllImportResolver")!;
foreach (var runtimePath in GetRuntimesFolder())
{
_ = AddDllDirectory(runtimePath);
}
}
}

// We're building with netstandard 2.0 which does not provide those APIs, but we
// know we're running on netcore. Let's use them through reflection.
_setDllImportResolver = Type
.GetType("System.Runtime.InteropServices.NativeLibrary")
?.GetMethod("SetDllImportResolver", [typeof(Assembly), dllImportResolverDelegateType]);
private static void SetupResolver()
{
var dllImportResolverDelegateType = Type.GetType("System.Runtime.InteropServices.DllImportResolver")!;

_tryLoad = (TryLoadDelegate)Type
.GetType("System.Runtime.InteropServices.NativeLibrary")
?.GetMethod("TryLoad", [typeof(string), typeof(Assembly), typeof(DllImportSearchPath?), typeof(IntPtr).MakeByRefType()])
?.CreateDelegate(typeof(TryLoadDelegate));
// We're building with netstandard 2.0 which does not provide those APIs, but we
// know we're running on netcore. Let's use them through reflection.
_setDllImportResolver = Type
.GetType("System.Runtime.InteropServices.NativeLibrary")
?.GetMethod("SetDllImportResolver", [typeof(Assembly), dllImportResolverDelegateType]);

if (_setDllImportResolver is not null && _tryLoad is not null)
{
var importResolverMethod = typeof(SkiaSharpTools).GetMethod(nameof(ImportResolver), BindingFlags.Static | BindingFlags.NonPublic);
var importResolverDelegate = Delegate.CreateDelegate(dllImportResolverDelegateType, null, importResolverMethod!);
_tryLoad = (TryLoadDelegate)Type
.GetType("System.Runtime.InteropServices.NativeLibrary")
?.GetMethod("TryLoad", [typeof(string), typeof(Assembly), typeof(DllImportSearchPath?), typeof(IntPtr).MakeByRefType()])
?.CreateDelegate(typeof(TryLoadDelegate));

_setDllImportResolver.Invoke(null, [typeof(SkiaSharp.SKAlphaType).Assembly, importResolverDelegate]);
}
else
{
throw new InvalidOperationException($"Unable to find System.Runtime.InteropServices.NativeLibrary.SetDllImportResolver or TryLoad");
}
if (_setDllImportResolver is not null && _tryLoad is not null)
{
var importResolverMethod = typeof(SkiaSharpTools).GetMethod(nameof(ImportResolver), BindingFlags.Static | BindingFlags.NonPublic);
var importResolverDelegate = Delegate.CreateDelegate(dllImportResolverDelegateType, null, importResolverMethod!);

foreach (var runtimePath in GetRuntimesFolder())
{
_ = AddDllDirectory(runtimePath);
}
_setDllImportResolver.Invoke(null, [typeof(SkiaSharp.SKAlphaType).Assembly, importResolverDelegate]);
}
else
{
throw new InvalidOperationException($"Unable to find System.Runtime.InteropServices.NativeLibrary.SetDllImportResolver or TryLoad");
}
}

Expand Down

0 comments on commit d82dbb5

Please sign in to comment.