A little cleanup of DLIR
This commit is contained in:
parent
ae667eca29
commit
75e815f09e
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace BizHawk.Common
|
||||
{
|
||||
|
@ -18,29 +19,32 @@ namespace BizHawk.Common
|
|||
|
||||
public class DynamicLibraryImportResolver : IDisposable, IImportResolver
|
||||
{
|
||||
private static readonly Lazy<IEnumerable<string>> asdf = new Lazy<IEnumerable<string>>(() =>
|
||||
private static readonly IReadOnlyCollection<string> UnixSearchPaths;
|
||||
|
||||
static DynamicLibraryImportResolver()
|
||||
{
|
||||
var currDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)?.Replace("file:", "") ?? string.Empty;
|
||||
return new[] { "/usr/lib/", "/usr/lib/bizhawk/", "./", "./dll/" }.Select(dir => dir[0] == '.' ? currDir + dir.Substring(1) : dir);
|
||||
});
|
||||
UnixSearchPaths = new[]
|
||||
{
|
||||
"/usr/lib/", "/usr/lib/bizhawk/",
|
||||
$"{currDir}/", $"{currDir}/dll/"
|
||||
};
|
||||
}
|
||||
|
||||
private static string UnixResolveFilePath(string orig) => orig[0] == '/'
|
||||
? orig
|
||||
: UnixSearchPaths.Select(dir => dir + orig).FirstOrDefault(File.Exists) ?? orig;
|
||||
|
||||
private IntPtr _p;
|
||||
private bool _eternal;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="dllName"></param>
|
||||
/// <param name="eternal">
|
||||
/// If true, the DLL will never be unloaded, even by god. Use this when you need lifetime semantics similar to [DllImport]
|
||||
/// </param>
|
||||
public DynamicLibraryImportResolver(string dllName, bool eternal = false)
|
||||
public readonly bool HasLimitedLifetime;
|
||||
|
||||
/// <param name="hasLimitedLifetime">will never be unloaded iff false (like <see cref="DllImportAttribute">[DllImport]</see>)</param>
|
||||
public DynamicLibraryImportResolver(string dllName, bool hasLimitedLifetime = true)
|
||||
{
|
||||
static string ResolveFilePath(string orig) => orig[0] == '/' ? orig : asdf.Value.Select(dir => dir + orig).FirstOrDefault(File.Exists) ?? orig;
|
||||
_p = OSTailoredCode.LinkedLibManager.LoadOrThrow(OSTailoredCode.IsUnixHost ? ResolveFilePath(dllName) : dllName);
|
||||
_eternal = eternal;
|
||||
if (eternal)
|
||||
GC.SuppressFinalize(this);
|
||||
_p = OSTailoredCode.LinkedLibManager.LoadOrThrow(OSTailoredCode.IsUnixHost ? UnixResolveFilePath(dllName) : dllName); // on Windows, EmuHawk modifies its process' search path
|
||||
HasLimitedLifetime = hasLimitedLifetime;
|
||||
if (!hasLimitedLifetime) GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public IntPtr GetProcAddrOrZero(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrZero(_p, entryPoint);
|
||||
|
@ -49,8 +53,7 @@ namespace BizHawk.Common
|
|||
|
||||
private void DisposeHelper()
|
||||
{
|
||||
if (_eternal || _p == IntPtr.Zero)
|
||||
return;
|
||||
if (!HasLimitedLifetime || _p == IntPtr.Zero) return;
|
||||
OSTailoredCode.LinkedLibManager.FreeByPtr(_p);
|
||||
_p = IntPtr.Zero;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace BizHawk.Emulation.Common
|
|||
static SpeexResampler()
|
||||
{
|
||||
var resolver = new DynamicLibraryImportResolver(
|
||||
OSTailoredCode.IsUnixHost ? "libspeexdsp.so.1" : "libspeexdsp.dll", eternal: true);
|
||||
OSTailoredCode.IsUnixHost ? "libspeexdsp.so.1" : "libspeexdsp.dll", hasLimitedLifetime: false);
|
||||
NativeDSP = BizInvoker.GetInvoker<LibSpeexDSP>(resolver, CallingConventionAdapters.Native);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
|
|||
static MGBAHawk()
|
||||
{
|
||||
var resolver = new DynamicLibraryImportResolver(
|
||||
OSTailoredCode.IsUnixHost ? "libmgba.dll.so" : "mgba.dll", eternal: true);
|
||||
OSTailoredCode.IsUnixHost ? "libmgba.dll.so" : "mgba.dll", hasLimitedLifetime: false);
|
||||
LibmGBA = BizInvoker.GetInvoker<LibmGBA>(resolver, CallingConventionAdapters.Native);
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES
|
|||
static QuickNES()
|
||||
{
|
||||
var resolver = new DynamicLibraryImportResolver(
|
||||
$"libquicknes{(OSTailoredCode.IsUnixHost ? ".dll.so.0.7.0" : ".dll")}", eternal: true);
|
||||
$"libquicknes{(OSTailoredCode.IsUnixHost ? ".dll.so.0.7.0" : ".dll")}", hasLimitedLifetime: false);
|
||||
QN = BizInvoker.GetInvoker<LibQuickNES>(resolver, CallingConventionAdapters.Native);
|
||||
QN.qn_setup_mappers();
|
||||
}
|
||||
|
|
|
@ -78,7 +78,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
|
|||
static WaterboxHost()
|
||||
{
|
||||
NativeImpl = BizInvoker.GetInvoker<WaterboxHostNative>(
|
||||
new DynamicLibraryImportResolver(OSTailoredCode.IsUnixHost ? "libwaterboxhost.so" : "waterboxhost.dll", eternal: true),
|
||||
new DynamicLibraryImportResolver(OSTailoredCode.IsUnixHost ? "libwaterboxhost.so" : "waterboxhost.dll", hasLimitedLifetime: false),
|
||||
CallingConventionAdapters.Native);
|
||||
#if !DEBUG
|
||||
NativeImpl.wbx_set_always_evict_blocks(false);
|
||||
|
|
Loading…
Reference in New Issue