From d82dbb507138666788864e7b261c9491685c96cb Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Wed, 11 Sep 2024 09:40:44 -0400 Subject: [PATCH] chore: Adjust loader --- .github/workflows/ci.yml | 3 -- .../src/SkiaSharpTools.Initializer.cs | 53 ++++++++++--------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3917b12..b457bf1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/src/Resizetizer/src/SkiaSharpTools.Initializer.cs b/src/Resizetizer/src/SkiaSharpTools.Initializer.cs index 34a01b6..898caa9 100644 --- a/src/Resizetizer/src/SkiaSharpTools.Initializer.cs +++ b/src/Resizetizer/src/SkiaSharpTools.Initializer.cs @@ -32,6 +32,8 @@ public static void Initialize() if (isNetCore) { + SetupResolver(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { NetcoreInitializeWindows(); @@ -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"); } }