using System.Reflection;
using Brutal.Logging;
namespace Brutal.SdlApi.Image;
///
/// Provides access to the SDL_image library.
///
public static unsafe partial class SdlImage
{
///
/// The name the generated P/Invoke surface imports against. The static
/// constructor registers a that normalizes
/// this name to the platform-specific SDL3_image shared library.
///
internal const string Dll = "SDL3_image";
///
/// The logging category for the per-call trace logs the generated P/Invoke surface emits. An
/// internal implementation detail mirroring core's Sdl.TraceLog; the generated
/// SdlImage.PInvoke wrappers call SdlImage.TraceLog.Trace() on every invocation.
///
internal static readonly LogCategory TraceLog = LogCategory.Make("SdlImage");
///
/// Hosts the raw generated P/Invoke surface (filled in by a later task) and, for now, the
/// native library resolver that makes loading SDL3_image work.
///
public static unsafe partial class PInvoke
{
// The native SDL3_image handle, resolved lazily by the DllImportResolver below and cached
// so the library is loaded at most once regardless of how many entry points are resolved.
private static nint _nativeHandle;
// Serializes resolution — see Brutal.Sdl's Sdl.PInvoke for the full rationale (two threads
// resolving different entry points concurrently must not race to load two handles).
private static readonly Lock ResolveLock = new();
// The resolver is registered in this static constructor rather than at first use so it is
// in place before ANY P/Invoke against this assembly is resolved. Accessing a member of the
// nested PInvoke type triggers this constructor (mirrors Brutal.Sdl's Sdl.PInvoke pattern).
static PInvoke()
{
// GUARANTEE: SDL3 must actually be loaded into the process before SDL3_image's own
// resolve runs below — SDL3_image's dylib/so/dll has a dynamic-link dependency on
// libSDL3 (@rpath/libSDL3.0.dylib on macOS, libSDL3.so.0 on Linux, SDL3.dll on
// Windows) that a nupkg-deployed app's probe layout cannot always be relied on to
// satisfy via rpath/loader-path alone (the CMake wrapper's INSTALL_RPATH is a second,
// belt-and-braces line of defense, not the primary guarantee).
//
// Merely running Sdl's or Sdl.PInvoke's static constructor is NOT sufficient: those
// constructors only *register* Brutal.Sdl's DllImportResolver
// (NativeLibrary.SetDllImportResolver) — the resolver is not invoked, and libSDL3 is
// not actually loaded, until the CLR resolves a real P/Invoke against it. So instead of
// RunClassConstructor, we make one real, side-effect-free P/Invoke call into core:
// Sdl.PInvoke.GetVersion() just reads SDL's compiled-in version number, requires no
// SDL_Init and has no observable side effects, but its LibraryImport binding is exactly
// what triggers Brutal.Sdl's resolver to actually NativeLibrary.Load libSDL3. By the
// time this call returns, libSDL3 is resident in the process, so SDL3_image's own
// dynamic-link dependency on it is satisfiable regardless of rpath/loader-path probing.
_ = Sdl.PInvoke.GetVersion();
NativeLibrary.SetDllImportResolver(typeof(SdlImage).Assembly, Resolve);
}
// Normalizes the "SDL3_image" import name to the platform-specific shared library filename
// (SDL3_image.dll on Windows, libSDL3_image.dylib on macOS, libSDL3_image.so.0 on Linux).
// SDL_image's Linux build installs a versioned soname (libSDL3_image.so.0), same convention
// as core's SDL3, so the candidate list is spelled out explicitly, most-specific first, with
// the default probing convention name as a fallback.
private static nint Resolve(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
// Only ever asked to resolve SDL3_image; anything else is left to the default resolver.
if (libraryName != Dll)
return IntPtr.Zero;
lock (ResolveLock)
{
if (_nativeHandle != IntPtr.Zero)
return _nativeHandle;
var search = searchPath ?? DllImportSearchPath.AssemblyDirectory;
// Try each candidate with Load (not TryLoad): TryLoad reports only pass/fail and
// discards the OS diagnostic, so a candidate that *exists but fails to load* (a
// missing transitive dependency, an ABI mismatch) would vanish silently. Load throws
// with the real dlerror/LoadLibrary text, which we aggregate so the final exception
// names every attempt and why it failed.
var failures = new List();
foreach (var candidate in Candidates())
{
try
{
_nativeHandle = NativeLibrary.Load(candidate, assembly, search);
return _nativeHandle;
}
catch (Exception ex)
{
failures.Add($"'{candidate}': {ex.Message}");
}
}
throw new DllNotFoundException(
"Could not load the SDL3_image native library. Attempts (most specific first): "
+ string.Join("; ", failures)
+ "."
);
}
}
private static string[] Candidates()
{
if (OperatingSystem.IsWindows())
return ["SDL3_image.dll", "SDL3_image"];
if (OperatingSystem.IsMacOS())
return ["libSDL3_image.dylib", "SDL3_image"];
// Linux and other Unix-likes.
return ["libSDL3_image.so.0", "libSDL3_image.so", "SDL3_image"];
}
}
}