Refactor PlatformLinkedLibSingleton and replace RunningOnUnix with CurrentOS

This commit is contained in:
YoshiRulz 2019-05-18 15:30:29 +10:00
parent 436b6452fb
commit d76e1a8a8b
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
9 changed files with 85 additions and 47 deletions

View File

@ -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)

View File

@ -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".

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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()

View File

@ -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;
/// <remarks>macOS doesn't use PlatformID.MacOSX</remarks>
public static readonly DistinctOS CurrentOS = Environment.OSVersion.Platform == PlatformID.Unix
? currentIsMacOS() ? DistinctOS.macOS : DistinctOS.Linux
: DistinctOS.Windows;
private static readonly Lazy<PlatformLinkedLibManager> lazy = new Lazy<PlatformLinkedLibManager>(() => RunningOnUnix
? (PlatformLinkedLibManager) new UnixMonoLinkedLibManager()
: (PlatformLinkedLibManager) new Win32LinkedLibManager());
private static readonly Lazy<PlatformLinkedLibManager> lazy = new Lazy<PlatformLinkedLibManager>(() =>
{
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
}
}
}
}