Files
swarm-assistent/AssistentSqliteBootstrap.cs
T

64 lines
1.9 KiB
C#

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using SQLitePCL;
namespace Mrleo1nid.SwarmAssistent;
/// <summary>
/// SwarmUI loads extensions in an isolated AssemblyLoadContext — Microsoft.Data.Sqlite
/// does not auto-init native SQLite there. Native libs ship under the extension dir
/// (runtimes/linux-x64/native/libe_sqlite3.so), but the loader searches the host
/// base dir unless we preload from the extension assembly location.
/// </summary>
internal static class AssistentSqliteBootstrap
{
static int _ready;
internal static void EnsureInitialized()
{
if (Interlocked.CompareExchange(ref _ready, 1, 0) != 0)
{
return;
}
PreloadNativeLibrary();
Batteries_V2.Init();
}
static void PreloadNativeLibrary()
{
string extDir = Path.GetDirectoryName(typeof(AssistentSqliteBootstrap).Assembly.Location)
?? AppContext.BaseDirectory
?? "";
if (string.IsNullOrWhiteSpace(extDir))
{
return;
}
string[] relPaths =
[
Path.Combine("runtimes", "linux-x64", "native", "libe_sqlite3.so"),
Path.Combine("runtimes", "linux-x64", "native", "e_sqlite3.so"),
"libe_sqlite3.so",
"e_sqlite3.so",
];
foreach (string rel in relPaths)
{
string path = Path.Combine(extDir, rel);
if (!File.Exists(path))
{
continue;
}
try
{
NativeLibrary.Load(path);
return;
}
catch (Exception ex)
{
SwarmUI.Utils.Logs.Debug($"AssistentSqliteBootstrap: NativeLibrary.Load({path}): {ex.Message}");
}
}
}
}