some ApiHawk cleanups

This commit is contained in:
adelikat 2020-01-03 16:37:33 -06:00
parent 733bdd09b3
commit b875f46a95
2 changed files with 80 additions and 90 deletions

View File

@ -11,7 +11,9 @@ using BizHawk.Emulation.Cores.PCEngine;
using BizHawk.Emulation.Cores.Sega.MasterSystem; using BizHawk.Emulation.Cores.Sega.MasterSystem;
using BizHawk.Client.ApiHawk.Classes.Events; using BizHawk.Client.ApiHawk.Classes.Events;
using System.IO; using System.IO;
using BizHawk.Emulation.Common.IEmulatorExtensions;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.ApiHawk namespace BizHawk.Client.ApiHawk
{ {
/// <summary> /// <summary>
@ -22,18 +24,18 @@ namespace BizHawk.Client.ApiHawk
{ {
#region Fields #region Fields
private static IEmulator Emulator; private static IEmulator Emulator { get; set; }
private static IVideoProvider VideoProvider; private static IVideoProvider VideoProvider { get; set; }
private static readonly Assembly clientAssembly; private static readonly Assembly ClientAssembly;
private static readonly object clientMainFormInstance; private static readonly object ClientMainFormInstance;
private static readonly Type mainFormClass; private static readonly Type MainFormClass;
private static readonly Array joypadButtonsArray = Enum.GetValues(typeof(JoypadButton)); private static readonly Array JoypadButtonsArray = Enum.GetValues(typeof(JoypadButton));
internal static readonly BizHawkSystemIdToEnumConverter SystemIdConverter = new BizHawkSystemIdToEnumConverter(); internal static readonly BizHawkSystemIdToEnumConverter SystemIdConverter = new BizHawkSystemIdToEnumConverter();
internal static readonly JoypadStringToEnumConverter JoypadConverter = new JoypadStringToEnumConverter(); internal static readonly JoypadStringToEnumConverter JoypadConverter = new JoypadStringToEnumConverter();
private static List<Joypad> allJoypads; private static List<Joypad> _allJoyPads;
/// <summary> /// <summary>
/// Occurs before a quickload is done (just after user has pressed the shortcut button /// Occurs before a quickload is done (just after user has pressed the shortcut button
@ -67,15 +69,15 @@ namespace BizHawk.Client.ApiHawk
/// </summary> /// </summary>
static ClientApi() static ClientApi()
{ {
clientAssembly = Assembly.GetEntryAssembly(); ClientAssembly = Assembly.GetEntryAssembly();
clientMainFormInstance = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("MainForm").GetValue(null); ClientMainFormInstance = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("MainForm").GetValue(null);
mainFormClass = clientAssembly.GetType("BizHawk.Client.EmuHawk.MainForm"); MainFormClass = ClientAssembly.GetType("BizHawk.Client.EmuHawk.MainForm");
} }
public static void UpdateEmulatorAndVP(IEmulator emu = null) public static void UpdateEmulatorAndVP(IEmulator emu = null)
{ {
Emulator = emu; Emulator = emu;
VideoProvider = Emulation.Common.IEmulatorExtensions.Extensions.AsVideoProviderOrDefault(emu); VideoProvider = emu.AsVideoProviderOrDefault();
} }
#endregion #endregion
@ -94,22 +96,22 @@ namespace BizHawk.Client.ApiHawk
{ {
typeList.Add(obj.GetType()); typeList.Add(obj.GetType());
} }
method = mainFormClass.GetMethod(name, typeList.ToArray()); method = MainFormClass.GetMethod(name, typeList.ToArray());
} }
else method = mainFormClass.GetMethod(name); else method = MainFormClass.GetMethod(name);
if(method != null) if(method != null)
method.Invoke(clientMainFormInstance, paramList); method.Invoke(ClientMainFormInstance, paramList);
} }
private static object GetMainFormField(string name) private static object GetMainFormField(string name)
{ {
return mainFormClass.GetField(name); return MainFormClass.GetField(name);
} }
private static void SetMainFormField(string name, object value) private static void SetMainFormField(string name, object value)
{ {
mainFormClass.GetField(name).SetValue(clientMainFormInstance, value); MainFormClass.GetField(name).SetValue(ClientMainFormInstance, value);
} }
#endregion #endregion
@ -120,11 +122,11 @@ namespace BizHawk.Client.ApiHawk
/// </summary> /// </summary>
public static void DoFrameAdvance() public static void DoFrameAdvance()
{ {
InvokeMainFormMethod("FrameAdvance", null); InvokeMainFormMethod("FrameAdvance");
InvokeMainFormMethod("StepRunLoop_Throttle", null); InvokeMainFormMethod("StepRunLoop_Throttle");
InvokeMainFormMethod("Render", null); InvokeMainFormMethod("Render");
} }
/// <summary> /// <summary>
@ -149,18 +151,16 @@ namespace BizHawk.Client.ApiHawk
{ {
throw new IndexOutOfRangeException($"{RunningSystem.DisplayName} does not support {player} controller(s)"); throw new IndexOutOfRangeException($"{RunningSystem.DisplayName} does not support {player} controller(s)");
} }
else
{ GetAllInputs();
GetAllInputs(); return _allJoyPads[player - 1];
return allJoypads[player - 1];
}
} }
/// <summary> /// <summary>
/// Load a savestate specified by its name /// Load a savestate specified by its name
/// </summary> /// </summary>
/// <param name="name">Savetate friendly name</param> /// <param name="name">Savestate friendly name</param>
public static void LoadState(string name) public static void LoadState(string name)
{ {
InvokeMainFormMethod("LoadState", new object[] { Path.Combine(PathManager.GetSaveStatePath(Global.Game), $"{name}.State"), name, false, false }); InvokeMainFormMethod("LoadState", new object[] { Path.Combine(PathManager.GetSaveStatePath(Global.Game), $"{name}.State"), name, false, false });
@ -178,7 +178,7 @@ namespace BizHawk.Client.ApiHawk
eventHandled = false; eventHandled = false;
if (BeforeQuickLoad != null) if (BeforeQuickLoad != null)
{ {
BeforeQuickLoadEventArgs e = new BeforeQuickLoadEventArgs(quickSaveSlotName); var e = new BeforeQuickLoadEventArgs(quickSaveSlotName);
BeforeQuickLoad(sender, e); BeforeQuickLoad(sender, e);
eventHandled = e.Handled; eventHandled = e.Handled;
} }
@ -196,7 +196,7 @@ namespace BizHawk.Client.ApiHawk
eventHandled = false; eventHandled = false;
if (BeforeQuickSave != null) if (BeforeQuickSave != null)
{ {
BeforeQuickSaveEventArgs e = new BeforeQuickSaveEventArgs(quickSaveSlotName); var e = new BeforeQuickSaveEventArgs(quickSaveSlotName);
BeforeQuickSave(sender, e); BeforeQuickSave(sender, e);
eventHandled = e.Handled; eventHandled = e.Handled;
} }
@ -229,13 +229,13 @@ namespace BizHawk.Client.ApiHawk
public static void OnRomLoaded(IEmulator emu) public static void OnRomLoaded(IEmulator emu)
{ {
Emulator = emu; Emulator = emu;
VideoProvider = Emulation.Common.IEmulatorExtensions.Extensions.AsVideoProviderOrDefault(emu); VideoProvider = emu.AsVideoProviderOrDefault();
RomLoaded?.Invoke(null, EventArgs.Empty); RomLoaded?.Invoke(null, EventArgs.Empty);
allJoypads = new List<Joypad>(RunningSystem.MaxControllers); _allJoyPads = new List<Joypad>(RunningSystem.MaxControllers);
for (int i = 1; i <= RunningSystem.MaxControllers; i++) for (int i = 1; i <= RunningSystem.MaxControllers; i++)
{ {
allJoypads.Add(new Joypad(RunningSystem, i)); _allJoyPads.Add(new Joypad(RunningSystem, i));
} }
} }
@ -258,7 +258,7 @@ namespace BizHawk.Client.ApiHawk
/// <param name="bottom">Bottom padding</param> /// <param name="bottom">Bottom padding</param>
public static void SetGameExtraPadding(int left, int top, int right, int bottom) public static void SetGameExtraPadding(int left, int top, int right, int bottom)
{ {
FieldInfo f = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("DisplayManager"); FieldInfo f = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("DisplayManager");
object displayManager = f.GetValue(null); object displayManager = f.GetValue(null);
f = f.FieldType.GetField("GameExtraPadding"); f = f.FieldType.GetField("GameExtraPadding");
f.SetValue(displayManager, new Padding(left, top, right, bottom)); f.SetValue(displayManager, new Padding(left, top, right, bottom));
@ -305,7 +305,7 @@ namespace BizHawk.Client.ApiHawk
/// <param name="bottom">Bottom padding</param> /// <param name="bottom">Bottom padding</param>
public static void SetExtraPadding(int left, int top, int right, int bottom) public static void SetExtraPadding(int left, int top, int right, int bottom)
{ {
FieldInfo f = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("DisplayManager"); FieldInfo f = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("DisplayManager");
object displayManager = f.GetValue(null); object displayManager = f.GetValue(null);
f = f.FieldType.GetField("ClientExtraPadding"); f = f.FieldType.GetField("ClientExtraPadding");
f.SetValue(displayManager, new Padding(left, top, right, bottom)); f.SetValue(displayManager, new Padding(left, top, right, bottom));
@ -360,24 +360,20 @@ namespace BizHawk.Client.ApiHawk
{ {
if (joypad.Inputs == 0) if (joypad.Inputs == 0)
{ {
AutoFireStickyXorAdapter joypadAdaptor = Global.AutofireStickyXORAdapter; AutoFireStickyXorAdapter joypadAdapter = Global.AutofireStickyXORAdapter;
joypadAdaptor.ClearStickies(); joypadAdapter.ClearStickies();
} }
else else
{ {
foreach (JoypadButton button in joypadButtonsArray) foreach (JoypadButton button in JoypadButtonsArray)
{ {
if (joypad.Inputs.HasFlag(button)) if (joypad.Inputs.HasFlag(button))
{ {
AutoFireStickyXorAdapter joypadAdaptor = Global.AutofireStickyXORAdapter; AutoFireStickyXorAdapter joypadAdapter = Global.AutofireStickyXORAdapter;
if (RunningSystem == SystemInfo.GB) joypadAdapter.SetSticky(
{ RunningSystem == SystemInfo.GB
joypadAdaptor.SetSticky($"{JoypadConverter.ConvertBack(button, RunningSystem)}", true); ? $"{JoypadConverter.ConvertBack(button, RunningSystem)}"
} : $"P{player} {JoypadConverter.ConvertBack(button, RunningSystem)}", true);
else
{
joypadAdaptor.SetSticky($"P{player} {JoypadConverter.ConvertBack(button, RunningSystem)}", true);
}
} }
} }
} }
@ -401,7 +397,7 @@ namespace BizHawk.Client.ApiHawk
/// </summary> /// </summary>
public static void UnpauseEmulation() public static void UnpauseEmulation()
{ {
InvokeMainFormMethod("UnpauseEmulator", null); InvokeMainFormMethod("UnpauseEmulator");
} }
#endregion Public #endregion Public
@ -411,29 +407,27 @@ namespace BizHawk.Client.ApiHawk
/// </summary> /// </summary>
private static void GetAllInputs() private static void GetAllInputs()
{ {
AutoFireStickyXorAdapter joypadAdapter = Global.AutofireStickyXORAdapter; var joypadAdapter = Global.AutofireStickyXORAdapter;
IEnumerable<string> pressedButtons = from button in joypadAdapter.Definition.BoolButtons var pressedButtons = joypadAdapter.Definition.BoolButtons
where joypadAdapter.IsPressed(button) .Where(b => joypadAdapter.IsPressed(b));
select button;
foreach (Joypad j in allJoypads) foreach (Joypad j in _allJoyPads)
{ {
j.ClearInputs(); j.ClearInputs();
} }
Parallel.ForEach<string>(pressedButtons, button => Parallel.ForEach(pressedButtons, button =>
{ {
int player;
if (RunningSystem == SystemInfo.GB) if (RunningSystem == SystemInfo.GB)
{ {
allJoypads[0].AddInput(JoypadConverter.Convert(button)); _allJoyPads[0].AddInput(JoypadConverter.Convert(button));
} }
else else
{ {
if (int.TryParse(button.Substring(1, 2), out player)) if (int.TryParse(button.Substring(1, 2), out var player))
{ {
allJoypads[player - 1].AddInput(JoypadConverter.Convert(button.Substring(3))); _allJoyPads[player - 1].AddInput(JoypadConverter.Convert(button.Substring(3)));
} }
} }
}); });
@ -442,8 +436,8 @@ namespace BizHawk.Client.ApiHawk
{ {
for (int i = 1; i <= RunningSystem.MaxControllers; i++) for (int i = 1; i <= RunningSystem.MaxControllers; i++)
{ {
allJoypads[i - 1].AnalogX = joypadAdapter.GetFloat($"P{i} X Axis"); _allJoyPads[i - 1].AnalogX = joypadAdapter.GetFloat($"P{i} X Axis");
allJoypads[i - 1].AnalogY = joypadAdapter.GetFloat($"P{i} Y Axis"); _allJoyPads[i - 1].AnalogY = joypadAdapter.GetFloat($"P{i} Y Axis");
} }
} }
} }
@ -461,7 +455,7 @@ namespace BizHawk.Client.ApiHawk
public static int BorderHeight() public static int BorderHeight()
{ {
var point = new System.Drawing.Point(0, 0); var point = new System.Drawing.Point(0, 0);
Type t = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin"); Type t = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin");
FieldInfo f = t.GetField("DisplayManager"); FieldInfo f = t.GetField("DisplayManager");
object displayManager = f.GetValue(null); object displayManager = f.GetValue(null);
MethodInfo m = t.GetMethod("TransFormPoint"); MethodInfo m = t.GetMethod("TransFormPoint");
@ -472,7 +466,7 @@ namespace BizHawk.Client.ApiHawk
public static int BorderWidth() public static int BorderWidth()
{ {
var point = new System.Drawing.Point(0, 0); var point = new System.Drawing.Point(0, 0);
Type t = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin"); Type t = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin");
FieldInfo f = t.GetField("DisplayManager"); FieldInfo f = t.GetField("DisplayManager");
object displayManager = f.GetValue(null); object displayManager = f.GetValue(null);
MethodInfo m = t.GetMethod("TransFormPoint"); MethodInfo m = t.GetMethod("TransFormPoint");
@ -561,11 +555,11 @@ namespace BizHawk.Client.ApiHawk
public static void OpenRom(string path) public static void OpenRom(string path)
{ {
var ioa = OpenAdvancedSerializer.ParseWithLegacy(path); var ioa = OpenAdvancedSerializer.ParseWithLegacy(path);
Type t = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin.MainForm.LoadRomArgs"); Type t = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin.MainForm.LoadRomArgs");
object o = Activator.CreateInstance(t); object o = Activator.CreateInstance(t);
t.GetField("OpenAdvanced").SetValue(o, ioa); t.GetField("OpenAdvanced").SetValue(o, ioa);
InvokeMainFormMethod("LoadRom", new object[] {path, o}); InvokeMainFormMethod("LoadRom", new[] {path, o});
} }
public static void Pause() public static void Pause()
@ -641,7 +635,7 @@ namespace BizHawk.Client.ApiHawk
{ {
Global.Config.TargetZoomFactors[Emulator.SystemId] = size; Global.Config.TargetZoomFactors[Emulator.SystemId] = size;
InvokeMainFormMethod("FrameBufferResized"); InvokeMainFormMethod("FrameBufferResized");
Type t = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin"); Type t = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin");
FieldInfo f = t.GetField("OSD"); FieldInfo f = t.GetField("OSD");
object osd = f.GetValue(null); object osd = f.GetValue(null);
t = f.GetType(); t = f.GetType();
@ -674,7 +668,7 @@ namespace BizHawk.Client.ApiHawk
public static int TransformPointX(int x) public static int TransformPointX(int x)
{ {
var point = new System.Drawing.Point(x, 0); var point = new System.Drawing.Point(x, 0);
Type t = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin"); Type t = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin");
FieldInfo f = t.GetField("DisplayManager"); FieldInfo f = t.GetField("DisplayManager");
object displayManager = f.GetValue(null); object displayManager = f.GetValue(null);
MethodInfo m = t.GetMethod("TransFormPoint"); MethodInfo m = t.GetMethod("TransFormPoint");
@ -685,7 +679,7 @@ namespace BizHawk.Client.ApiHawk
public static int TransformPointY(int y) public static int TransformPointY(int y)
{ {
var point = new System.Drawing.Point(0, y); var point = new System.Drawing.Point(0, y);
Type t = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin"); Type t = ClientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin");
FieldInfo f = t.GetField("DisplayManager"); FieldInfo f = t.GetField("DisplayManager");
object displayManager = f.GetValue(null); object displayManager = f.GetValue(null);
MethodInfo m = t.GetMethod("TransFormPoint"); MethodInfo m = t.GetMethod("TransFormPoint");
@ -706,14 +700,14 @@ namespace BizHawk.Client.ApiHawk
public static int Xpos() public static int Xpos()
{ {
object o = GetMainFormField("DesktopLocation"); object o = GetMainFormField("DesktopLocation");
Type t = mainFormClass.GetField("DesktopLocation").GetType(); Type t = MainFormClass.GetField("DesktopLocation").GetType();
return (int)t.GetField("X").GetValue(o); return (int)t.GetField("X").GetValue(o);
} }
public static int Ypos() public static int Ypos()
{ {
object o = GetMainFormField("DesktopLocation"); object o = GetMainFormField("DesktopLocation");
Type t = mainFormClass.GetField("DesktopLocation").GetType(); Type t = MainFormClass.GetField("DesktopLocation").GetType();
return (int)t.GetField("Y").GetValue(o); return (int)t.GetField("Y").GetValue(o);
} }

View File

@ -10,11 +10,10 @@ namespace BizHawk.Client.ApiHawk
{ {
#region Fields #region Fields
private SystemInfo _System; private JoypadButton _pressedButtons;
private JoypadButton _PressedButtons; private float _analogX;
private float _AnalogX; private float _analogY;
private float _AnalogY; private int _player;
private int _Player;
#endregion #endregion
@ -33,8 +32,8 @@ namespace BizHawk.Client.ApiHawk
throw new InvalidOperationException($"{player} is invalid for {system.DisplayName}"); throw new InvalidOperationException($"{player} is invalid for {system.DisplayName}");
} }
_System = system; System = system;
_Player = player; _player = player;
} }
#endregion #endregion
@ -47,52 +46,49 @@ namespace BizHawk.Client.ApiHawk
/// <param name="input">Input to add</param> /// <param name="input">Input to add</param>
public void AddInput(JoypadButton input) public void AddInput(JoypadButton input)
{ {
input &= _System.AvailableButtons; input &= System.AvailableButtons;
_PressedButtons |= input; _pressedButtons |= input;
} }
/// <summary> /// <summary>
/// Clear inputs /// Clear inputs
/// </summary> /// </summary>
public void ClearInputs() public void ClearInputs()
{ {
_PressedButtons = 0; _pressedButtons = 0;
} }
/// <summary> /// <summary>
/// Remove specified input to current ones /// Remove specified input to current ones
/// </summary> /// </summary>
/// <param name="input">Input to remove</param> /// <param name="input">Input to remove</param>
public void RemoveInput(JoypadButton input) public void RemoveInput(JoypadButton input)
{ {
_PressedButtons ^= input; _pressedButtons ^= input;
} }
#endregion #endregion
#region Properties #region Properties
/// <summary> /// <summary>
/// Gets or sets X value for Analog stick /// Gets or sets X value for Analog stick
/// </summary> /// </summary>
/// <remarks>The value you get will aways be rounded to 0 decimal</remarks> /// <remarks>The value you get will always be rounded to 0 decimal</remarks>
public float AnalogX public float AnalogX
{ {
get => (float)Math.Round(_AnalogX, 0); get => (float)Math.Round(_analogX, 0);
set => _AnalogX = value; set => _analogX = value;
} }
/// <summary> /// <summary>
/// Gets or sets Y value for Analog stick /// Gets or sets Y value for Analog stick
/// </summary> /// </summary>
/// <remarks>The value you get will aways be rounded to 0 decimal</remarks> /// <remarks>The value you get will always be rounded to 0 decimal</remarks>
public float AnalogY public float AnalogY
{ {
get => (float)Math.Round(_AnalogY, 0); get => (float)Math.Round(_analogY, 0);
set => _AnalogY = value; set => _analogY = value;
} }
/// <summary> /// <summary>
@ -102,18 +98,18 @@ namespace BizHawk.Client.ApiHawk
/// <remarks>It overrides all existing inputs</remarks> /// <remarks>It overrides all existing inputs</remarks>
public JoypadButton Inputs public JoypadButton Inputs
{ {
get => _PressedButtons; get => _pressedButtons;
set set
{ {
value &= _System.AvailableButtons; value &= System.AvailableButtons;
_PressedButtons = value; _pressedButtons = value;
} }
} }
/// <summary> /// <summary>
/// Gets <see cref="SystemInfo"/> for current <see cref="Joypad"/> /// Gets <see cref="SystemInfo"/> for current <see cref="Joypad"/>
/// </summary> /// </summary>
public SystemInfo System => _System; public SystemInfo System { get; }
#endregion #endregion
} }