From 67cbe9a5ee22c8bf83736dc9a2158ecd1032c3a1 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Mon, 12 Aug 2019 20:53:55 +1000 Subject: [PATCH] Cleanup OSTailoredCode --- BizHawk.Common/OSTailoredCode.cs | 56 ++++++++++---------------------- 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/BizHawk.Common/OSTailoredCode.cs b/BizHawk.Common/OSTailoredCode.cs index e11aeecc8b..9109daeeda 100644 --- a/BizHawk.Common/OSTailoredCode.cs +++ b/BizHawk.Common/OSTailoredCode.cs @@ -2,22 +2,20 @@ using System; using System.Diagnostics; 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 -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 namespace BizHawk.Common #endif { - public sealed class OSTailoredCode + public static class OSTailoredCode { - /// macOS doesn't use PlatformID.MacOSX + /// macOS doesn't use PlatformID.MacOSX 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; - private static readonly Lazy lazy = new Lazy(() => + private static readonly Lazy _LinkedLibManager = new Lazy(() => { switch (CurrentOS) { @@ -31,11 +29,7 @@ namespace BizHawk.Common } }); - public static ILinkedLibManager LinkedLibManager => lazy.Value; - - private static bool currentIsMacOS() => SimpleSubshell("uname", "-s", "Can't determine OS") == "Darwin"; - - private OSTailoredCode() {} + public static ILinkedLibManager LinkedLibManager => _LinkedLibManager.Value; public interface ILinkedLibManager { @@ -56,10 +50,8 @@ namespace BizHawk.Common private static extern IntPtr dlsym(IntPtr handle, string symbol); [DllImport("libdl.so.2")] private static extern int dlclose(IntPtr handle); - public IntPtr LoadPlatformSpecific(string dllToLoad) - { - return dlopen(dllToLoad, RTLD_NOW); - } + + public IntPtr LoadPlatformSpecific(string dllToLoad) => dlopen(dllToLoad, RTLD_NOW); public IntPtr GetProcAddr(IntPtr hModule, string procName) { dlerror(); @@ -68,41 +60,29 @@ namespace BizHawk.Common if (errPtr != IntPtr.Zero) throw new InvalidOperationException($"error in dlsym: {Marshal.PtrToStringAnsi(errPtr)}"); return res; } - public int FreePlatformSpecific(IntPtr hModule) - { - return dlclose(hModule); - } + public int FreePlatformSpecific(IntPtr hModule) => dlclose(hModule); } private class WindowsLLManager : ILinkedLibManager { + // comments reference extern functions removed from SevenZip.NativeMethods [DllImport("kernel32.dll")] private static extern uint GetLastError(); - // was annotated `[DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]` in SevenZip.NativeMethods - // param dllToLoad was annotated `[MarshalAs(UnmanagedType.LPStr)]` in SevenZip.NativeMethods + [DllImport("kernel32.dll")] // had BestFitMapping = false, ThrowOnUnmappableChar = true + 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")] - private static extern IntPtr LoadLibrary(string dllToLoad); - // 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); + private static extern bool FreeLibrary(IntPtr hModule); // return type was annotated MarshalAs(UnmanagedType.Bool) + public IntPtr LoadPlatformSpecific(string dllToLoad) { var p = LoadLibrary(dllToLoad); if (p == IntPtr.Zero) throw new InvalidOperationException($"got null pointer, error code {GetLastError()}"); return p; } - public IntPtr GetProcAddr(IntPtr hModule, string procName) - { - return GetProcAddress(hModule, procName); - } - public int FreePlatformSpecific(IntPtr hModule) - { - return FreeLibrary(hModule) ? 1 : 0; - } + public IntPtr GetProcAddr(IntPtr hModule, string procName) => GetProcAddress(hModule, procName); + public int FreePlatformSpecific(IntPtr hModule) => FreeLibrary(hModule) ? 1 : 0; } public enum DistinctOS : byte