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.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace BizHawk.Common
|
namespace BizHawk.Common
|
||||||
{
|
{
|
||||||
|
@ -18,29 +19,32 @@ namespace BizHawk.Common
|
||||||
|
|
||||||
public class DynamicLibraryImportResolver : IDisposable, IImportResolver
|
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;
|
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 IntPtr _p;
|
||||||
private bool _eternal;
|
|
||||||
|
|
||||||
/// <summary>
|
public readonly bool HasLimitedLifetime;
|
||||||
///
|
|
||||||
/// </summary>
|
/// <param name="hasLimitedLifetime">will never be unloaded iff false (like <see cref="DllImportAttribute">[DllImport]</see>)</param>
|
||||||
/// <param name="dllName"></param>
|
public DynamicLibraryImportResolver(string dllName, bool hasLimitedLifetime = true)
|
||||||
/// <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)
|
|
||||||
{
|
{
|
||||||
static string ResolveFilePath(string orig) => orig[0] == '/' ? orig : asdf.Value.Select(dir => dir + orig).FirstOrDefault(File.Exists) ?? orig;
|
_p = OSTailoredCode.LinkedLibManager.LoadOrThrow(OSTailoredCode.IsUnixHost ? UnixResolveFilePath(dllName) : dllName); // on Windows, EmuHawk modifies its process' search path
|
||||||
_p = OSTailoredCode.LinkedLibManager.LoadOrThrow(OSTailoredCode.IsUnixHost ? ResolveFilePath(dllName) : dllName);
|
HasLimitedLifetime = hasLimitedLifetime;
|
||||||
_eternal = eternal;
|
if (!hasLimitedLifetime) GC.SuppressFinalize(this);
|
||||||
if (eternal)
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntPtr GetProcAddrOrZero(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrZero(_p, entryPoint);
|
public IntPtr GetProcAddrOrZero(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrZero(_p, entryPoint);
|
||||||
|
@ -49,8 +53,7 @@ namespace BizHawk.Common
|
||||||
|
|
||||||
private void DisposeHelper()
|
private void DisposeHelper()
|
||||||
{
|
{
|
||||||
if (_eternal || _p == IntPtr.Zero)
|
if (!HasLimitedLifetime || _p == IntPtr.Zero) return;
|
||||||
return;
|
|
||||||
OSTailoredCode.LinkedLibManager.FreeByPtr(_p);
|
OSTailoredCode.LinkedLibManager.FreeByPtr(_p);
|
||||||
_p = IntPtr.Zero;
|
_p = IntPtr.Zero;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ namespace BizHawk.Emulation.Common
|
||||||
static SpeexResampler()
|
static SpeexResampler()
|
||||||
{
|
{
|
||||||
var resolver = new DynamicLibraryImportResolver(
|
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);
|
NativeDSP = BizInvoker.GetInvoker<LibSpeexDSP>(resolver, CallingConventionAdapters.Native);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
|
||||||
static MGBAHawk()
|
static MGBAHawk()
|
||||||
{
|
{
|
||||||
var resolver = new DynamicLibraryImportResolver(
|
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);
|
LibmGBA = BizInvoker.GetInvoker<LibmGBA>(resolver, CallingConventionAdapters.Native);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES
|
||||||
static QuickNES()
|
static QuickNES()
|
||||||
{
|
{
|
||||||
var resolver = new DynamicLibraryImportResolver(
|
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 = BizInvoker.GetInvoker<LibQuickNES>(resolver, CallingConventionAdapters.Native);
|
||||||
QN.qn_setup_mappers();
|
QN.qn_setup_mappers();
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
|
||||||
static WaterboxHost()
|
static WaterboxHost()
|
||||||
{
|
{
|
||||||
NativeImpl = BizInvoker.GetInvoker<WaterboxHostNative>(
|
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);
|
CallingConventionAdapters.Native);
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
NativeImpl.wbx_set_always_evict_blocks(false);
|
NativeImpl.wbx_set_always_evict_blocks(false);
|
||||||
|
|
Loading…
Reference in New Issue