From d76e1a8a8b7483b052574e8fa62e657ec80ebc4e Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sat, 18 May 2019 15:30:29 +1000 Subject: [PATCH] Refactor PlatformLinkedLibSingleton and replace RunningOnUnix with CurrentOS --- BizHawk.Client.EmuHawk/Input/Input.cs | 30 ++++---- BizHawk.Client.EmuHawk/Program.cs | 10 +-- BizHawk.Client.EmuHawk/ScreenSaver.cs | 6 +- BizHawk.Client.EmuHawk/Sound/Sound.cs | 2 +- BizHawk.Client.EmuHawk/Throttle.cs | 2 +- BizHawk.Client.EmuHawk/config/InputWidget.cs | 4 +- .../tools/Lua/LuaConsole.cs | 6 +- BizHawk.Client.EmuHawk/tools/ToolManager.cs | 2 +- BizHawk.Common/PlatformLinkedLibSingleton.cs | 70 ++++++++++++++----- 9 files changed, 85 insertions(+), 47 deletions(-) diff --git a/BizHawk.Client.EmuHawk/Input/Input.cs b/BizHawk.Client.EmuHawk/Input/Input.cs index 519594fe34..2f84152d7f 100644 --- a/BizHawk.Client.EmuHawk/Input/Input.cs +++ b/BizHawk.Client.EmuHawk/Input/Input.cs @@ -123,24 +123,24 @@ namespace BizHawk.Client.EmuHawk public static void Initialize() { - if (PlatformLinkedLibSingleton.RunningOnUnix) - { - OTK_Keyboard.Initialize(); -// OTK_Gamepad.Initialize(); - } - else + if (PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows) { KeyInput.Initialize(); IPCKeyInput.Initialize(); GamePad.Initialize(); GamePad360.Initialize(); } + else + { + OTK_Keyboard.Initialize(); +// OTK_Gamepad.Initialize(); + } Instance = new Input(); } public static void Cleanup() { - if (!PlatformLinkedLibSingleton.RunningOnUnix) + if (PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows) { KeyInput.Cleanup(); GamePad.Cleanup(); @@ -331,18 +331,18 @@ namespace BizHawk.Client.EmuHawk { while (true) { - var keyEvents = PlatformLinkedLibSingleton.RunningOnUnix - ? OTK_Keyboard.Update() - : KeyInput.Update().Concat(IPCKeyInput.Update()); - if (PlatformLinkedLibSingleton.RunningOnUnix) - { - //TODO - } - else + var keyEvents = PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows + ? KeyInput.Update().Concat(IPCKeyInput.Update()) + : OTK_Keyboard.Update(); + if (PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows) { GamePad.UpdateAll(); GamePad360.UpdateAll(); } + else + { + //TODO + } //this block is going to massively modify data structures that the binding method uses, so we have to lock it all lock (this) diff --git a/BizHawk.Client.EmuHawk/Program.cs b/BizHawk.Client.EmuHawk/Program.cs index b09c9b6a51..6993c8f3f8 100644 --- a/BizHawk.Client.EmuHawk/Program.cs +++ b/BizHawk.Client.EmuHawk/Program.cs @@ -22,7 +22,7 @@ namespace BizHawk.Client.EmuHawk Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - if (!EXE_PROJECT.PlatformLinkedLibSingleton.RunningOnUnix) + if (EXE_PROJECT.PlatformLinkedLibSingleton.CurrentOS == EXE_PROJECT.PlatformLinkedLibSingleton.DistinctOS.Windows) { var libLoader = EXE_PROJECT.PlatformLinkedLibSingleton.LinkedLibManager; @@ -141,8 +141,10 @@ namespace BizHawk.Client.EmuHawk GlobalWin.GLManager = GLManager.Instance; //now create the "GL" context for the display method. we can reuse the IGL_TK context if opengl display method is chosen - if (EXE_PROJECT.PlatformLinkedLibSingleton.RunningOnUnix) Global.Config.DispMethod = Config.EDispMethod.GdiPlus; - REDO_DISPMETHOD: + if (EXE_PROJECT.PlatformLinkedLibSingleton.CurrentOS != EXE_PROJECT.PlatformLinkedLibSingleton.DistinctOS.Windows) + Global.Config.DispMethod = Config.EDispMethod.GdiPlus; + +REDO_DISPMETHOD: if (Global.Config.DispMethod == Config.EDispMethod.GdiPlus) GlobalWin.GL = new Bizware.BizwareGL.Drivers.GdiPlus.IGL_GdiPlus(); else if (Global.Config.DispMethod == Config.EDispMethod.SlimDX9) @@ -187,7 +189,7 @@ namespace BizHawk.Client.EmuHawk goto REDO_DISPMETHOD; } - if (!EXE_PROJECT.PlatformLinkedLibSingleton.RunningOnUnix) + if (EXE_PROJECT.PlatformLinkedLibSingleton.CurrentOS == EXE_PROJECT.PlatformLinkedLibSingleton.DistinctOS.Windows) { //WHY do we have to do this? some intel graphics drivers (ig7icd64.dll 10.18.10.3304 on an unknown chip on win8.1) are calling SetDllDirectory() for the process, which ruins stuff. //The relevant initialization happened just before in "create IGL context". diff --git a/BizHawk.Client.EmuHawk/ScreenSaver.cs b/BizHawk.Client.EmuHawk/ScreenSaver.cs index d362b4b2c2..d09ea9b8a9 100644 --- a/BizHawk.Client.EmuHawk/ScreenSaver.cs +++ b/BizHawk.Client.EmuHawk/ScreenSaver.cs @@ -41,9 +41,9 @@ namespace BizHawk.Client.EmuHawk //TODO implement } } - private static PlatformSpecificScreenBlankInterface screenBlankInterface = PlatformLinkedLibSingleton.RunningOnUnix - ? (PlatformSpecificScreenBlankInterface) new MiscUnixScreenBlankInterface() - : (PlatformSpecificScreenBlankInterface) new WinScreenBlankInterface(); + private static PlatformSpecificScreenBlankInterface screenBlankInterface = PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows + ? (PlatformSpecificScreenBlankInterface) new WinScreenBlankInterface() + : (PlatformSpecificScreenBlankInterface) new MiscUnixScreenBlankInterface(); private const int SPI_GETSCREENSAVERTIMEOUT = 14; private const int SPI_SETSCREENSAVERTIMEOUT = 15; diff --git a/BizHawk.Client.EmuHawk/Sound/Sound.cs b/BizHawk.Client.EmuHawk/Sound/Sound.cs index 1edaaae699..cd6891bd9d 100644 --- a/BizHawk.Client.EmuHawk/Sound/Sound.cs +++ b/BizHawk.Client.EmuHawk/Sound/Sound.cs @@ -27,7 +27,7 @@ namespace BizHawk.Client.EmuHawk { if (Global.Config.SoundOutputMethod == Config.ESoundOutputMethod.OpenAL) _outputDevice = new OpenALSoundOutput(this); - if (!PlatformLinkedLibSingleton.RunningOnUnix) + if (PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows) { if (Global.Config.SoundOutputMethod == Config.ESoundOutputMethod.DirectSound) _outputDevice = new DirectSoundSoundOutput(this, mainWindowHandle); diff --git a/BizHawk.Client.EmuHawk/Throttle.cs b/BizHawk.Client.EmuHawk/Throttle.cs index af7334ef81..d703bce4ac 100644 --- a/BizHawk.Client.EmuHawk/Throttle.cs +++ b/BizHawk.Client.EmuHawk/Throttle.cs @@ -160,7 +160,7 @@ namespace BizHawk.Client.EmuHawk return timeBeginPeriod(ms); } } - static PlatformSpecificSysTimer sysTimer = PlatformLinkedLibSingleton.RunningOnUnix ? (PlatformSpecificSysTimer) new UnixMonoSysTimer() : (PlatformSpecificSysTimer) new WinSysTimer(); + static PlatformSpecificSysTimer sysTimer = PlatformLinkedLibSingleton.CurrentOS != PlatformLinkedLibSingleton.DistinctOS.Windows ? (PlatformSpecificSysTimer) new UnixMonoSysTimer() : (PlatformSpecificSysTimer) new WinSysTimer(); static uint TimeBeginPeriod(uint ms) { return sysTimer.TimeBeginPeriod(ms); diff --git a/BizHawk.Client.EmuHawk/config/InputWidget.cs b/BizHawk.Client.EmuHawk/config/InputWidget.cs index 79167ea11f..b6ffe91457 100644 --- a/BizHawk.Client.EmuHawk/config/InputWidget.cs +++ b/BizHawk.Client.EmuHawk/config/InputWidget.cs @@ -71,7 +71,7 @@ namespace BizHawk.Client.EmuHawk protected override void OnMouseClick(MouseEventArgs e) { - if (!PlatformLinkedLibSingleton.RunningOnUnix) HideCaret(Handle); + if (PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows) HideCaret(Handle); base.OnMouseClick(e); } @@ -264,7 +264,7 @@ namespace BizHawk.Client.EmuHawk protected override void OnGotFocus(EventArgs e) { - if (!PlatformLinkedLibSingleton.RunningOnUnix) HideCaret(Handle); + if (PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows) HideCaret(Handle); } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 17b646b63c..92f954e0c2 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -186,9 +186,9 @@ namespace BizHawk.Client.EmuHawk } var currentScripts = LuaImp?.ScriptList; // Temp fix for now - LuaImp = PlatformLinkedLibSingleton.RunningOnUnix - ? (PlatformEmuLuaLibrary) new NotReallyLuaLibrary() - : (PlatformEmuLuaLibrary) new EmuLuaLibrary(Emulator.ServiceProvider); + LuaImp = PlatformLinkedLibSingleton.CurrentOS == PlatformLinkedLibSingleton.DistinctOS.Windows + ? (PlatformEmuLuaLibrary) new EmuLuaLibrary(Emulator.ServiceProvider) + : (PlatformEmuLuaLibrary) new NotReallyLuaLibrary(); if (currentScripts != null) { LuaImp.ScriptList.AddRange(currentScripts); diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index 1d4f348f0f..5d381b2184 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -740,7 +740,7 @@ namespace BizHawk.Client.EmuHawk return false; } - if (t == typeof(LuaConsole) && PlatformLinkedLibSingleton.RunningOnUnix) return false; + if (t == typeof(LuaConsole) && PlatformLinkedLibSingleton.CurrentOS != PlatformLinkedLibSingleton.DistinctOS.Windows) return false; var tool = Assembly .GetExecutingAssembly() diff --git a/BizHawk.Common/PlatformLinkedLibSingleton.cs b/BizHawk.Common/PlatformLinkedLibSingleton.cs index 14ed9c29e6..afc8b46054 100644 --- a/BizHawk.Common/PlatformLinkedLibSingleton.cs +++ b/BizHawk.Common/PlatformLinkedLibSingleton.cs @@ -1,24 +1,53 @@ using System; -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 +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 #else namespace BizHawk.Common #endif -{ - -public sealed class PlatformLinkedLibSingleton +{ + public sealed class PlatformLinkedLibSingleton { - public static readonly bool RunningOnUnix = Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX; + /// macOS doesn't use PlatformID.MacOSX + public static readonly DistinctOS CurrentOS = Environment.OSVersion.Platform == PlatformID.Unix + ? currentIsMacOS() ? DistinctOS.macOS : DistinctOS.Linux + : DistinctOS.Windows; - private static readonly Lazy lazy = new Lazy(() => RunningOnUnix - ? (PlatformLinkedLibManager) new UnixMonoLinkedLibManager() - : (PlatformLinkedLibManager) new Win32LinkedLibManager()); + private static readonly Lazy lazy = new Lazy(() => + { + switch (CurrentOS) + { + case DistinctOS.Linux: + case DistinctOS.macOS: + return new UnixMonoLinkedLibManager(); + case DistinctOS.Windows: + return new Win32LinkedLibManager(); + default: + throw new ArgumentOutOfRangeException(); + } + }); - public static PlatformLinkedLibManager LinkedLibManager { get { return lazy.Value; } } + public static PlatformLinkedLibManager LinkedLibManager => lazy.Value; + + private static bool currentIsMacOS() + { + var proc = new Process { + StartInfo = new ProcessStartInfo { + Arguments = "-s", + CreateNoWindow = true, + FileName = "uname", + RedirectStandardOutput = true, + UseShellExecute = false + } + }; + proc.Start(); + if (proc.StandardOutput.EndOfStream) throw new Exception("Can't determine OS (uname wrote nothing to stdout)!"); + return proc.StandardOutput.ReadLine() == "Darwin"; + } private PlatformLinkedLibSingleton() {} @@ -29,16 +58,16 @@ public sealed class PlatformLinkedLibSingleton int FreePlatformSpecific(IntPtr hModule); } - public class UnixMonoLinkedLibManager : PlatformLinkedLibManager + private class UnixMonoLinkedLibManager : PlatformLinkedLibManager { // This class is copied from a tutorial, so don't git blame and then email me expecting insight. const int RTLD_NOW = 2; [DllImport("libdl.so.2")] - private static extern IntPtr dlopen(String fileName, int flags); + private static extern IntPtr dlopen(string fileName, int flags); [DllImport("libdl.so.2")] private static extern IntPtr dlerror(); [DllImport("libdl.so.2")] - private static extern IntPtr dlsym(IntPtr handle, String symbol); + 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) @@ -59,7 +88,7 @@ public sealed class PlatformLinkedLibSingleton } } - public class Win32LinkedLibManager : PlatformLinkedLibManager + private class Win32LinkedLibManager : PlatformLinkedLibManager { [DllImport("kernel32.dll")] private static extern UInt32 GetLastError(); @@ -89,5 +118,12 @@ public sealed class PlatformLinkedLibSingleton return FreeLibrary(hModule) ? 1 : 0; } } + + public enum DistinctOS : byte + { + Linux = 0, + macOS = 1, + Windows = 2 + } } -} +} \ No newline at end of file