From cde04582acf426708b623afd6746428fcf934acf Mon Sep 17 00:00:00 2001 From: Timothy Miller Date: Wed, 31 Jul 2024 17:50:45 +0900 Subject: [PATCH] Allow manually loading SD library --- StableDiffusion.NET/Native/Native.Load.cs | 12 ++++++++++++ StableDiffusion.NET/StableDiffusionModel.cs | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/StableDiffusion.NET/Native/Native.Load.cs b/StableDiffusion.NET/Native/Native.Load.cs index 309d842..61ecb09 100644 --- a/StableDiffusion.NET/Native/Native.Load.cs +++ b/StableDiffusion.NET/Native/Native.Load.cs @@ -26,6 +26,18 @@ internal static partial class Native #region Methods + internal static bool LoadNativeLibrary(string libraryPath) + { + if (_loadedLibraryHandle != nint.Zero) return true; + if (NativeLibrary.TryLoad(libraryPath, out nint handle)) + { + _loadedLibraryHandle = handle; + return true; + } + + return false; + } + private static nint ResolveDllImport(string libraryname, Assembly assembly, DllImportSearchPath? searchpath) { if (libraryname != LIB_NAME) return nint.Zero; diff --git a/StableDiffusion.NET/StableDiffusionModel.cs b/StableDiffusion.NET/StableDiffusionModel.cs index a82c3b7..05be613 100644 --- a/StableDiffusion.NET/StableDiffusionModel.cs +++ b/StableDiffusion.NET/StableDiffusionModel.cs @@ -91,6 +91,15 @@ public sealed unsafe class StableDiffusionModel : IDisposable } } + /// + /// Manually load the native stable diffusion library. + /// Once set, it will continue to be used for all instances. + /// + /// Path to the stable diffusion library. + /// Bool if the library loaded. + public static bool LoadNativeLibrary(string libraryPath) + => Native.LoadNativeLibrary(libraryPath); + public IImage TextToImage(string prompt, StableDiffusionParameter parameter) { ObjectDisposedException.ThrowIf(_disposed, this);