Allow ApiManager to pass some extra globals to the 5 EmuHawk APIs

This commit is contained in:
YoshiRulz 2020-06-12 06:14:32 +10:00
parent d2e20a7a1e
commit 52cc0050b8
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 34 additions and 17 deletions

View File

@ -12,7 +12,9 @@ namespace BizHawk.Client.EmuHawk
{
public static class ApiManager
{
private static readonly Type[] CtorParamTypes = { typeof(Action<string>) };
private static readonly Type[] CtorParamTypesA = { typeof(Action<string>), typeof(DisplayManager), typeof(InputManager), typeof(MainForm) };
private static readonly Type[] CtorParamTypesB = { typeof(Action<string>) };
/// <remarks>TODO do we need to keep references to these because of GC weirdness? --yoshi</remarks>
private static ApiContainer? _container;
@ -21,7 +23,6 @@ namespace BizHawk.Client.EmuHawk
private static ApiContainer Register(IEmulatorServiceProvider serviceProvider, Action<string> logCallback)
{
var ctorParamTypes = CtorParamTypes;
var libDict = new Dictionary<Type, IExternalApi>();
foreach (var api in Assembly.GetAssembly(typeof(ApiSubsetContainer)).GetTypes()
.Concat(Assembly.GetAssembly(typeof(ApiContainer)).GetTypes())
@ -29,7 +30,8 @@ namespace BizHawk.Client.EmuHawk
&& typeof(IExternalApi).IsAssignableFrom(t)
&& ServiceInjector.IsAvailable(serviceProvider, t)))
{
var instance = api.GetConstructor(ctorParamTypes)?.Invoke(new object[] { logCallback })
var instance = api.GetConstructor(CtorParamTypesA)?.Invoke(new object[] { logCallback, GlobalWin.DisplayManager, GlobalWin.InputManager, GlobalWin.MainForm })
?? api.GetConstructor(CtorParamTypesB)?.Invoke(new object[] { logCallback })
?? Activator.CreateInstance(api);
ServiceInjector.UpdateServices(serviceProvider, instance);
libDict.Add(

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
@ -8,10 +9,23 @@ namespace BizHawk.Client.EmuHawk
{
public sealed class InputApi : IInput
{
private readonly DisplayManager _displayManager;
private readonly InputManager _inputManager;
private readonly MainForm _mainForm;
public InputApi(Action<string> logCallback, DisplayManager displayManager, InputManager inputManager, MainForm mainForm)
{
_displayManager = displayManager;
_inputManager = inputManager;
_mainForm = mainForm;
}
public Dictionary<string, bool> Get()
{
var buttons = new Dictionary<string, bool>();
foreach (var kvp in GlobalWin.InputManager.ControllerInputCoalescer.BoolButtons().Where(kvp => kvp.Value)) buttons[kvp.Key] = true;
foreach (var kvp in _inputManager.ControllerInputCoalescer.BoolButtons().Where(kvp => kvp.Value)) buttons[kvp.Key] = true;
return buttons;
}
@ -19,7 +33,7 @@ namespace BizHawk.Client.EmuHawk
{
var buttons = new Dictionary<string, object>();
// TODO - need to specify whether in "emu" or "native" coordinate space.
var p = GlobalWin.DisplayManager.UntransformPoint(Control.MousePosition);
var p = _displayManager.UntransformPoint(Control.MousePosition);
buttons["X"] = p.X;
buttons["Y"] = p.Y;
buttons[MouseButtons.Left.ToString()] = (Control.MouseButtons & MouseButtons.Left) != 0;
@ -27,7 +41,7 @@ namespace BizHawk.Client.EmuHawk
buttons[MouseButtons.Right.ToString()] = (Control.MouseButtons & MouseButtons.Right) != 0;
buttons[MouseButtons.XButton1.ToString()] = (Control.MouseButtons & MouseButtons.XButton1) != 0;
buttons[MouseButtons.XButton2.ToString()] = (Control.MouseButtons & MouseButtons.XButton2) != 0;
buttons["Wheel"] = GlobalWin.MainForm.MouseWheelTracker;
buttons["Wheel"] = _mainForm.MouseWheelTracker;
return buttons;
}
}

View File

@ -8,15 +8,16 @@ namespace BizHawk.Client.EmuHawk
{
public sealed class SaveStateApi : ISaveState
{
public SaveStateApi(Action<string> logCallback)
{
LogCallback = logCallback;
}
public SaveStateApi() : this(Console.WriteLine) {}
private readonly MainForm _mainForm;
private readonly Action<string> LogCallback;
public SaveStateApi(Action<string> logCallback, DisplayManager displayManager, InputManager inputManager, MainForm mainForm)
{
LogCallback = logCallback;
_mainForm = mainForm;
}
public void Load(string path, bool suppressOSD)
{
if (!File.Exists(path))
@ -25,19 +26,19 @@ namespace BizHawk.Client.EmuHawk
return;
}
GlobalWin.MainForm.LoadState(path, Path.GetFileName(path), suppressOSD);
_mainForm.LoadState(path, Path.GetFileName(path), suppressOSD);
}
public void LoadSlot(int slotNum, bool suppressOSD)
{
if (0.RangeTo(9).Contains(slotNum)) GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", suppressOSD);
if (0.RangeTo(9).Contains(slotNum)) _mainForm.LoadQuickSave($"QuickSave{slotNum}", suppressOSD);
}
public void Save(string path, bool suppressOSD) => GlobalWin.MainForm.SaveState(path, path, true, suppressOSD);
public void Save(string path, bool suppressOSD) => _mainForm.SaveState(path, path, true, suppressOSD);
public void SaveSlot(int slotNum, bool suppressOSD)
{
if (0.RangeTo(9).Contains(slotNum)) GlobalWin.MainForm.SaveQuickSave($"QuickSave{slotNum}", true, suppressOSD);
if (0.RangeTo(9).Contains(slotNum)) _mainForm.SaveQuickSave($"QuickSave{slotNum}", true, suppressOSD);
}
}
}