Cleanup OSTailoredCode
This commit is contained in:
parent
72c052622c
commit
67cbe9a5ee
|
@ -2,22 +2,20 @@ using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
//put in a different namespace for EXE so we can have an instance of this type (by linking to this file rather than copying it) built-in to the exe
|
|
||||||
//so the exe doesnt implicitly depend on the dll
|
|
||||||
#if EXE_PROJECT
|
#if EXE_PROJECT
|
||||||
namespace EXE_PROJECT
|
namespace EXE_PROJECT // Use a different namespace so the executable can still use this class' members without an implicit dependency on the BizHawk.Common library, and without resorting to code duplication.
|
||||||
#else
|
#else
|
||||||
namespace BizHawk.Common
|
namespace BizHawk.Common
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
public sealed class OSTailoredCode
|
public static class OSTailoredCode
|
||||||
{
|
{
|
||||||
/// <remarks>macOS doesn't use PlatformID.MacOSX</remarks>
|
/// <remarks>macOS doesn't use <see cref="PlatformID.MacOSX">PlatformID.MacOSX</see></remarks>
|
||||||
public static readonly DistinctOS CurrentOS = Environment.OSVersion.Platform == PlatformID.Unix
|
public static readonly DistinctOS CurrentOS = Environment.OSVersion.Platform == PlatformID.Unix
|
||||||
? currentIsMacOS() ? DistinctOS.macOS : DistinctOS.Linux
|
? SimpleSubshell("uname", "-s", "Can't determine OS") == "Darwin" ? DistinctOS.macOS : DistinctOS.Linux
|
||||||
: DistinctOS.Windows;
|
: DistinctOS.Windows;
|
||||||
|
|
||||||
private static readonly Lazy<ILinkedLibManager> lazy = new Lazy<ILinkedLibManager>(() =>
|
private static readonly Lazy<ILinkedLibManager> _LinkedLibManager = new Lazy<ILinkedLibManager>(() =>
|
||||||
{
|
{
|
||||||
switch (CurrentOS)
|
switch (CurrentOS)
|
||||||
{
|
{
|
||||||
|
@ -31,11 +29,7 @@ namespace BizHawk.Common
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
public static ILinkedLibManager LinkedLibManager => lazy.Value;
|
public static ILinkedLibManager LinkedLibManager => _LinkedLibManager.Value;
|
||||||
|
|
||||||
private static bool currentIsMacOS() => SimpleSubshell("uname", "-s", "Can't determine OS") == "Darwin";
|
|
||||||
|
|
||||||
private OSTailoredCode() {}
|
|
||||||
|
|
||||||
public interface ILinkedLibManager
|
public interface ILinkedLibManager
|
||||||
{
|
{
|
||||||
|
@ -56,10 +50,8 @@ namespace BizHawk.Common
|
||||||
private static extern IntPtr dlsym(IntPtr handle, string symbol);
|
private static extern IntPtr dlsym(IntPtr handle, string symbol);
|
||||||
[DllImport("libdl.so.2")]
|
[DllImport("libdl.so.2")]
|
||||||
private static extern int dlclose(IntPtr handle);
|
private static extern int dlclose(IntPtr handle);
|
||||||
public IntPtr LoadPlatformSpecific(string dllToLoad)
|
|
||||||
{
|
public IntPtr LoadPlatformSpecific(string dllToLoad) => dlopen(dllToLoad, RTLD_NOW);
|
||||||
return dlopen(dllToLoad, RTLD_NOW);
|
|
||||||
}
|
|
||||||
public IntPtr GetProcAddr(IntPtr hModule, string procName)
|
public IntPtr GetProcAddr(IntPtr hModule, string procName)
|
||||||
{
|
{
|
||||||
dlerror();
|
dlerror();
|
||||||
|
@ -68,41 +60,29 @@ namespace BizHawk.Common
|
||||||
if (errPtr != IntPtr.Zero) throw new InvalidOperationException($"error in dlsym: {Marshal.PtrToStringAnsi(errPtr)}");
|
if (errPtr != IntPtr.Zero) throw new InvalidOperationException($"error in dlsym: {Marshal.PtrToStringAnsi(errPtr)}");
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
public int FreePlatformSpecific(IntPtr hModule)
|
public int FreePlatformSpecific(IntPtr hModule) => dlclose(hModule);
|
||||||
{
|
|
||||||
return dlclose(hModule);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class WindowsLLManager : ILinkedLibManager
|
private class WindowsLLManager : ILinkedLibManager
|
||||||
{
|
{
|
||||||
|
// comments reference extern functions removed from SevenZip.NativeMethods
|
||||||
[DllImport("kernel32.dll")]
|
[DllImport("kernel32.dll")]
|
||||||
private static extern uint GetLastError();
|
private static extern uint GetLastError();
|
||||||
// was annotated `[DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]` in SevenZip.NativeMethods
|
[DllImport("kernel32.dll")] // had BestFitMapping = false, ThrowOnUnmappableChar = true
|
||||||
// param dllToLoad was annotated `[MarshalAs(UnmanagedType.LPStr)]` in SevenZip.NativeMethods
|
private static extern IntPtr LoadLibrary(string dllToLoad); // param dllToLoad was annotated `[MarshalAs(UnmanagedType.LPStr)]`
|
||||||
|
[DllImport("kernel32.dll")] // had BestFitMapping = false, ThrowOnUnmappableChar = true
|
||||||
|
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName); // param procName was annotated `[MarshalAs(UnmanagedType.LPStr)]`
|
||||||
[DllImport("kernel32.dll")]
|
[DllImport("kernel32.dll")]
|
||||||
private static extern IntPtr LoadLibrary(string dllToLoad);
|
private static extern bool FreeLibrary(IntPtr hModule); // return type was annotated MarshalAs(UnmanagedType.Bool)
|
||||||
// was annotated `[DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]` in SevenZip.NativeMethods
|
|
||||||
// param procName was annotated `[MarshalAs(UnmanagedType.LPStr)]` in SevenZip.NativeMethods
|
|
||||||
[DllImport("kernel32.dll")]
|
|
||||||
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
|
|
||||||
// was annotated `[return: MarshalAs(UnmanagedType.Bool)]` in SevenZip.NativeMethods
|
|
||||||
[DllImport("kernel32.dll")]
|
|
||||||
private static extern bool FreeLibrary(IntPtr hModule);
|
|
||||||
public IntPtr LoadPlatformSpecific(string dllToLoad)
|
public IntPtr LoadPlatformSpecific(string dllToLoad)
|
||||||
{
|
{
|
||||||
var p = LoadLibrary(dllToLoad);
|
var p = LoadLibrary(dllToLoad);
|
||||||
if (p == IntPtr.Zero) throw new InvalidOperationException($"got null pointer, error code {GetLastError()}");
|
if (p == IntPtr.Zero) throw new InvalidOperationException($"got null pointer, error code {GetLastError()}");
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
public IntPtr GetProcAddr(IntPtr hModule, string procName)
|
public IntPtr GetProcAddr(IntPtr hModule, string procName) => GetProcAddress(hModule, procName);
|
||||||
{
|
public int FreePlatformSpecific(IntPtr hModule) => FreeLibrary(hModule) ? 1 : 0;
|
||||||
return GetProcAddress(hModule, procName);
|
|
||||||
}
|
|
||||||
public int FreePlatformSpecific(IntPtr hModule)
|
|
||||||
{
|
|
||||||
return FreeLibrary(hModule) ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum DistinctOS : byte
|
public enum DistinctOS : byte
|
||||||
|
|
Loading…
Reference in New Issue