From b0e879da5c8cd91fcb5952a90d8cb042c9531010 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 8 Nov 2025 15:06:47 +0200 Subject: [PATCH] fixed: Loading native libraries on FreeBSD and Linux --- .../Composition/AssemblyLoader.cs | 36 +++++++++++-------- .../Migration/Framework/MigrationExtension.cs | 9 ----- src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs | 4 +-- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/NzbDrone.Common/Composition/AssemblyLoader.cs b/src/NzbDrone.Common/Composition/AssemblyLoader.cs index 9d8a1afd5..06f65a2e1 100644 --- a/src/NzbDrone.Common/Composition/AssemblyLoader.cs +++ b/src/NzbDrone.Common/Composition/AssemblyLoader.cs @@ -14,7 +14,6 @@ namespace NzbDrone.Common.Composition static AssemblyLoader() { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ContainerResolveEventHandler); - RegisterSQLiteResolver(); } public static IList Load(IList assemblyNames) @@ -23,6 +22,10 @@ namespace NzbDrone.Common.Composition toLoad.Add("Prowlarr.Common"); toLoad.Add(OsInfo.IsWindows ? "Prowlarr.Windows" : "Prowlarr.Mono"); + var toRegisterResolver = new List { "System.Data.SQLite" }; + toRegisterResolver.AddRange(assemblyNames.Intersect(new[] { "Prowlarr.Core" })); + RegisterNativeResolver(toRegisterResolver); + var startupPath = AppDomain.CurrentDomain.BaseDirectory; return toLoad @@ -43,25 +46,28 @@ namespace NzbDrone.Common.Composition return AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath); } - public static void RegisterSQLiteResolver() + public static void RegisterNativeResolver(IEnumerable assemblyNames) { - // This ensures we look for sqlite3 using libsqlite3.so.0 on Linux and not libsqlite3.so which - // is less likely to exist. - var sqliteAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath( - Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System.Data.SQLite.dll")); + foreach (var name in assemblyNames) + { + // This ensures we look for sqlite3 using libsqlite3.so.0 on Linux and not libsqlite3.so which + // is less likely to exist. + var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath( + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{name}.dll")); - try - { - NativeLibrary.SetDllImportResolver(sqliteAssembly, LoadSqliteNativeLib); - } - catch (InvalidOperationException) - { - // This can only be set once per assembly - // Catch required for NzbDrone.Host tests + try + { + NativeLibrary.SetDllImportResolver(assembly, LoadNativeLib); + } + catch (InvalidOperationException) + { + // This can only be set once per assembly + // Catch required for NzbDrone.Host tests + } } } - private static IntPtr LoadSqliteNativeLib(string libraryName, Assembly assembly, DllImportSearchPath? dllImportSearchPath) + private static IntPtr LoadNativeLib(string libraryName, Assembly assembly, DllImportSearchPath? dllImportSearchPath) { ArgumentException.ThrowIfNullOrWhiteSpace(libraryName); diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationExtension.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationExtension.cs index ab390f6f7..e0ec93774 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationExtension.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationExtension.cs @@ -22,15 +22,6 @@ namespace NzbDrone.Core.Datastore.Migration.Framework return expressionRoot.Table(name).WithColumn("Id").AsInt32().PrimaryKey().Identity(); } - public static IDbCommand CreateCommand(this IDbConnection conn, IDbTransaction tran, string query) - { - var command = conn.CreateCommand(); - command.Transaction = tran; - command.CommandText = query; - - return command; - } - public static void AddParameter(this IDbCommand command, object value) { var parameter = command.CreateParameter(); diff --git a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs index a04a8b750..3ab499626 100644 --- a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs +++ b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs @@ -51,7 +51,7 @@ namespace NzbDrone.Test.Common.AutoMoq if (behavior != MockBehavior.Default && mock.Behavior == MockBehavior.Default) { - throw new InvalidOperationException("Unable to change be behaviour of a an existing mock."); + throw new InvalidOperationException("Unable to change be behaviour of an existing mock."); } return mock; @@ -139,7 +139,7 @@ namespace NzbDrone.Test.Common.AutoMoq LoadPlatformLibrary(); - AssemblyLoader.RegisterSQLiteResolver(); + AssemblyLoader.RegisterNativeResolver(new[] { "System.Data.SQLite", "Prowlarr.Core" }); } private Mock TheRegisteredMockForThisType(Type type)