From 3086d12147746329c98fc149bc25e4bc03291c5c Mon Sep 17 00:00:00 2001 From: stepan_govorko Date: Mon, 10 Jan 2022 15:43:27 +0100 Subject: [PATCH] added load native libraries override --- binding/Binding.Shared/LibraryLoader.cs | 36 +++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/binding/Binding.Shared/LibraryLoader.cs b/binding/Binding.Shared/LibraryLoader.cs index 41efe7ff23..d0707a8523 100644 --- a/binding/Binding.Shared/LibraryLoader.cs +++ b/binding/Binding.Shared/LibraryLoader.cs @@ -17,6 +17,31 @@ namespace SkiaSharp #endif { #if USE_DELEGATES || USE_LIBRARY_LOADER + public static class LibraryLoaderSettings + { + private static bool _isFrozen; + private static Func _loadLibraryOverride; + + /// + /// Gets or sets the function to override the default native library loading behavior. + /// The func gets the name of the native library to be loaded as a parameter, returns the OS handle for the loaded native library. + /// Can be set only on initialization stage before the first library is loaded, otherwise an exception will be thrown. + /// + public static Func LoadLibraryOverride + { + get => _loadLibraryOverride; + set + { + if (_isFrozen) + throw new Exception ("After any native library is loaded, the load method cannot be changed."); + + _loadLibraryOverride = value; + } + } + + internal static void Freeze () => _isFrozen = true; + } + internal static class LibraryLoader { static LibraryLoader () @@ -33,9 +58,16 @@ static LibraryLoader () public static IntPtr LoadLocalLibrary (string libraryName) { - var libraryPath = GetLibraryPath (libraryName); + LibraryLoaderSettings.Freeze (); + + IntPtr handle; + if (LibraryLoaderSettings.LoadLibraryOverride != null) + handle = LibraryLoaderSettings.LoadLibraryOverride (libraryName); + else { + var libraryPath = GetLibraryPath (libraryName); + handle = LoadLibrary (libraryPath); + } - var handle = LoadLibrary (libraryPath); if (handle == IntPtr.Zero) throw new DllNotFoundException ($"Unable to load library '{libraryName}'.");