From 8f93a954e15eed867435938f5dfd25d3efb14489 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Mon, 16 Dec 2019 15:04:35 +1000 Subject: [PATCH] Move Lua hacks to Lua libs Changed parameter of IInputMovie.SetRerecordCount to ulong to match internal --- BizHawk.Client.Common/Api/Classes/EmuApi.cs | 52 +++++++++---------- .../Api/Classes/JoypadApi.cs | 26 ++++------ BizHawk.Client.Common/Api/Classes/MovieApi.cs | 19 +------ BizHawk.Client.Common/Api/Interfaces/IEmu.cs | 2 +- .../Api/Interfaces/IInputMovie.cs | 2 +- BizHawk.Client.Common/Global.cs | 4 +- .../lua/EmuLuaLibrary.Joypad.cs | 8 +-- .../lua/EmuLuaLibrary.Movie.cs | 2 +- BizHawk.Client.Common/lua/LuaHelper.cs | 14 ++++- BizHawk.Client.EmuHawk/MainForm.cs | 4 +- .../tools/BasicBot/BasicBot.cs | 2 +- .../tools/TAStudio/TAStudioClipboard.cs | 4 +- 12 files changed, 66 insertions(+), 73 deletions(-) diff --git a/BizHawk.Client.Common/Api/Classes/EmuApi.cs b/BizHawk.Client.Common/Api/Classes/EmuApi.cs index 6c1cc14f5e..1f40a62691 100644 --- a/BizHawk.Client.Common/Api/Classes/EmuApi.cs +++ b/BizHawk.Client.Common/Api/Classes/EmuApi.cs @@ -352,29 +352,29 @@ namespace BizHawk.Client.Common return false; } - public void SetRenderPlanes(params bool[] luaParam) + public void SetRenderPlanes(params bool[] args) { if (Emulator is GPGX gpgx) { var s = gpgx.GetSettings(); - s.DrawBGA = luaParam[0]; - s.DrawBGB = luaParam[1]; - s.DrawBGW = luaParam[2]; - s.DrawObj = luaParam[3]; + s.DrawBGA = args[0]; + s.DrawBGB = args[1]; + s.DrawBGW = args[2]; + s.DrawObj = args[3]; gpgx.PutSettings(s); } else if (Emulator is LibsnesCore snes) { var s = snes.GetSettings(); - s.ShowBG1_0 = s.ShowBG1_1 = luaParam[0]; - s.ShowBG2_0 = s.ShowBG2_1 = luaParam[1]; - s.ShowBG3_0 = s.ShowBG3_1 = luaParam[2]; - s.ShowBG4_0 = s.ShowBG4_1 = luaParam[3]; - s.ShowOBJ_0 = luaParam[4]; - s.ShowOBJ_1 = luaParam[5]; - s.ShowOBJ_2 = luaParam[6]; - s.ShowOBJ_3 = luaParam[7]; + s.ShowBG1_0 = s.ShowBG1_1 = args[0]; + s.ShowBG2_0 = s.ShowBG2_1 = args[1]; + s.ShowBG3_0 = s.ShowBG3_1 = args[2]; + s.ShowBG4_0 = s.ShowBG4_1 = args[3]; + s.ShowOBJ_0 = args[4]; + s.ShowOBJ_1 = args[5]; + s.ShowOBJ_2 = args[6]; + s.ShowOBJ_3 = args[7]; snes.PutSettings(s); } else if (Emulator is NES nes) @@ -382,8 +382,8 @@ namespace BizHawk.Client.Common // in the future, we could do something more arbitrary here. // but this isn't any worse than the old system var s = nes.GetSettings(); - s.DispSprites = luaParam[0]; - s.DispBackground = luaParam[1]; + s.DispSprites = args[0]; + s.DispBackground = args[1]; nes.PutSettings(s); } else if (Emulator is QuickNES quicknes) @@ -391,7 +391,7 @@ namespace BizHawk.Client.Common var s = quicknes.GetSettings(); // this core doesn't support disabling BG - bool showSp = GetSetting(0, luaParam); + bool showSp = GetSetting(0, args); if (showSp && s.NumSprites == 0) { s.NumSprites = 8; @@ -406,12 +406,12 @@ namespace BizHawk.Client.Common else if (Emulator is PCEngine pce) { var s = pce.GetSettings(); - s.ShowOBJ1 = GetSetting(0, luaParam); - s.ShowBG1 = GetSetting(1, luaParam); - if (luaParam.Length > 2) + s.ShowOBJ1 = GetSetting(0, args); + s.ShowBG1 = GetSetting(1, args); + if (args.Length > 2) { - s.ShowOBJ2 = GetSetting(2, luaParam); - s.ShowBG2 = GetSetting(3, luaParam); + s.ShowOBJ2 = GetSetting(2, args); + s.ShowBG2 = GetSetting(3, args); } pce.PutSettings(s); @@ -419,16 +419,16 @@ namespace BizHawk.Client.Common else if (Emulator is SMS sms) { var s = sms.GetSettings(); - s.DispOBJ = GetSetting(0, luaParam); - s.DispBG = GetSetting(1, luaParam); + s.DispOBJ = GetSetting(0, args); + s.DispBG = GetSetting(1, args); sms.PutSettings(s); } else if (Emulator is WonderSwan ws) { var s = ws.GetSettings(); - s.EnableSprites = GetSetting(0, luaParam); - s.EnableFG = GetSetting(1, luaParam); - s.EnableBG = GetSetting(2, luaParam); + s.EnableSprites = GetSetting(0, args); + s.EnableFG = GetSetting(1, args); + s.EnableBG = GetSetting(2, args); ws.PutSettings(s); } } diff --git a/BizHawk.Client.Common/Api/Classes/JoypadApi.cs b/BizHawk.Client.Common/Api/Classes/JoypadApi.cs index e1bd636566..d2ca5ec8db 100644 --- a/BizHawk.Client.Common/Api/Classes/JoypadApi.cs +++ b/BizHawk.Client.Common/Api/Classes/JoypadApi.cs @@ -44,10 +44,6 @@ namespace BizHawk.Client.Common } } - buttons["clear"] = null; - buttons["getluafunctionslist"] = null; - buttons["output"] = null; - return buttons; } @@ -77,12 +73,12 @@ namespace BizHawk.Client.Common foreach (var button in lg.Definition.BoolButtons) { - Global.LuaAndAdaptor.SetButton(button, lg.IsPressed(button)); + Global.ButtonOverrideAdaptor.SetButton(button, lg.IsPressed(button)); } foreach (var floatButton in lg.Definition.FloatControls) { - Global.LuaAndAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton)); + Global.ButtonOverrideAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton)); } } catch (Exception) @@ -132,19 +128,19 @@ namespace BizHawk.Client.Common { if (theValue.HasValue) // Force { - Global.LuaAndAdaptor.SetButton(toPress, theValue.Value); - Global.ActiveController.Overrides(Global.LuaAndAdaptor); + Global.ButtonOverrideAdaptor.SetButton(toPress, theValue.Value); + Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor); } else // Unset { - Global.LuaAndAdaptor.UnSet(toPress); - Global.ActiveController.Overrides(Global.LuaAndAdaptor); + Global.ButtonOverrideAdaptor.UnSet(toPress); + Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor); } } else // Inverse { - Global.LuaAndAdaptor.SetInverse(toPress); - Global.ActiveController.Overrides(Global.LuaAndAdaptor); + Global.ButtonOverrideAdaptor.SetInverse(toPress); + Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor); } } } @@ -163,10 +159,10 @@ namespace BizHawk.Client.Common toPress = $"P{controller} {button}"; } if (state.HasValue) - Global.LuaAndAdaptor.SetButton(toPress, state.Value); + Global.ButtonOverrideAdaptor.SetButton(toPress, state.Value); else - Global.LuaAndAdaptor.UnSet(toPress); - Global.ActiveController.Overrides(Global.LuaAndAdaptor); + Global.ButtonOverrideAdaptor.UnSet(toPress); + Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor); } catch { diff --git a/BizHawk.Client.Common/Api/Classes/MovieApi.cs b/BizHawk.Client.Common/Api/Classes/MovieApi.cs index b7336b1d6e..40cbd87ffe 100644 --- a/BizHawk.Client.Common/Api/Classes/MovieApi.cs +++ b/BizHawk.Client.Common/Api/Classes/MovieApi.cs @@ -74,20 +74,6 @@ namespace BizHawk.Client.Common Global.MovieSession.ReadOnly = readOnly; } - public static void SetRerecordCount(double count) - { - // Lua numbers are always double, integer precision holds up - // to 53 bits, so throw an error if it's bigger than that. - const double PrecisionLimit = 9007199254740992d; - - if (count > PrecisionLimit) - { - throw new Exception("Rerecord count exceeds Lua integer precision."); - } - - Global.MovieSession.Movie.Rerecords = (ulong)count; - } - public static void SetRerecordCounting(bool counting) { Global.MovieSession.Movie.IsCountingRerecords = counting; @@ -272,10 +258,7 @@ namespace BizHawk.Client.Common MoviePluginStatic.SetReadOnly(readOnly); } - public void SetRerecordCount(double count) - { - MoviePluginStatic.SetRerecordCount(count); - } + public void SetRerecordCount(ulong count) => Global.MovieSession.Movie.Rerecords = count; public void SetRerecordCounting(bool counting) { diff --git a/BizHawk.Client.Common/Api/Interfaces/IEmu.cs b/BizHawk.Client.Common/Api/Interfaces/IEmu.cs index f14d0cd9cc..7f84f92e28 100644 --- a/BizHawk.Client.Common/Api/Interfaces/IEmu.cs +++ b/BizHawk.Client.Common/Api/Interfaces/IEmu.cs @@ -27,6 +27,6 @@ namespace BizHawk.Client.Common string GetBoardName(); object GetSettings(); bool PutSettings(object settings); - void SetRenderPlanes(params bool[] param); + void SetRenderPlanes(params bool[] args); } } diff --git a/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs b/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs index 8bb1f206d3..9ff1b17776 100644 --- a/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs +++ b/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs @@ -16,7 +16,7 @@ namespace BizHawk.Client.Common string Mode(); void Save(string filename = ""); void SetReadOnly(bool readOnly); - void SetRerecordCount(double count); + void SetRerecordCount(ulong count); void SetRerecordCounting(bool counting); void Stop(); double GetFps(); diff --git a/BizHawk.Client.Common/Global.cs b/BizHawk.Client.Common/Global.cs index 4b8c82a35b..d7527233d2 100644 --- a/BizHawk.Client.Common/Global.cs +++ b/BizHawk.Client.Common/Global.cs @@ -58,9 +58,9 @@ namespace BizHawk.Client.Common public static readonly StickyXorAdapter StickyXORAdapter = new StickyXorAdapter(); /// - /// Used to AND to another controller, used for Joypad.Set() + /// Used to AND to another controller, used for JoypadApi.Set /// - public static readonly OverrideAdaptor LuaAndAdaptor = new OverrideAdaptor(); + public static readonly OverrideAdaptor ButtonOverrideAdaptor = new OverrideAdaptor(); /// /// fire off one-frame logical button clicks here. useful for things like ti-83 virtual pad and reset buttons diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs index 2cb24c412f..b84768c54d 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs @@ -20,9 +20,11 @@ namespace BizHawk.Client.Common [LuaMethod("get", "returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller")] public LuaTable Get(int? controller = null) { - return APIs.Joypad - .Get(controller) - .ToLuaTable(Lua); + var table = APIs.Joypad.Get(controller).ToLuaTable(Lua); + table["clear"] = null; + table["getluafunctionslist"] = null; + table["output"] = null; + return table; } [LuaMethodExample("local nljoyget = joypad.getimmediate( );")] diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs index 4483c9cf94..ba07926f20 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs @@ -74,7 +74,7 @@ namespace BizHawk.Client.Common [LuaMethodExample("movie.setrerecordcount( 20.0 );")] [LuaMethod("setrerecordcount", "Sets the rerecord count of the current movie.")] - public void SetRerecordCount(double count) => APIs.Movie.SetRerecordCount(count); + public void SetRerecordCount(double count) => APIs.Movie.SetRerecordCount((ulong) count.AsInteger()); [LuaMethodExample("movie.setrerecordcounting( true );")] [LuaMethod("setrerecordcounting", "Sets whether or not the current movie will increment the rerecord counter on loadstate")] diff --git a/BizHawk.Client.Common/lua/LuaHelper.cs b/BizHawk.Client.Common/lua/LuaHelper.cs index 918fd02aa6..4f8283c9d7 100644 --- a/BizHawk.Client.Common/lua/LuaHelper.cs +++ b/BizHawk.Client.Common/lua/LuaHelper.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using NLua; @@ -7,6 +8,17 @@ namespace BizHawk.Client.Common { public static class LuaExtensions { + private const double PrecisionLimit = 0x20000000000000 + 0.0; + + /// + /// Lua numbers are always double-length floats, so integers whose magnitude is at least 2^53 may not fit in the 53-bit mantissa (the sign is stored separately). + /// These extremely large values aren't that useful, so we'll just assume they're erroneous and give the script author an error. + /// + /// ≥ 2^53 or ≤ -2^53 + public static long AsInteger(this double d) => -PrecisionLimit < d && d < PrecisionLimit + ? (long) d + : throw new ArithmeticException("integer value exceeds the precision of Lua's integer-as-double"); + public static LuaTable EnumerateToLuaTable(this IEnumerable list, Lua lua) => list.ToList().ToLuaTable(lua); public static LuaTable ToLuaTable(this IList list, Lua lua, int indexFrom = 0) diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index a8a63e79e2..a585f33e51 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -571,7 +571,7 @@ namespace BizHawk.Client.EmuHawk } // autohold/autofire must not be affected by the following inputs - Global.ActiveController.Overrides(Global.LuaAndAdaptor); + Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor); if (GlobalWin.Tools.Has() && !SuppressLua) { @@ -2971,7 +2971,7 @@ namespace BizHawk.Client.EmuHawk // zero 03-may-2014 - moved this before call to UpdateToolsBefore(), since it seems to clear the state which a lua event.framestart is going to want to alter Global.ClickyVirtualPadController.FrameTick(); - Global.LuaAndAdaptor.FrameTick(); + Global.ButtonOverrideAdaptor.FrameTick(); if (GlobalWin.Tools.Has() && !SuppressLua) { diff --git a/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs b/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs index e4a1d99ca2..6df7321e47 100644 --- a/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs +++ b/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs @@ -935,7 +935,7 @@ namespace BizHawk.Client.EmuHawk foreach (var button in lg.Definition.BoolButtons) { // TODO: make an input adapter specifically for the bot? - Global.LuaAndAdaptor.SetButton(button, lg.IsPressed(button)); + Global.ButtonOverrideAdaptor.SetButton(button, lg.IsPressed(button)); } } else diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudioClipboard.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudioClipboard.cs index 57b9214e50..1e442824d2 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudioClipboard.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudioClipboard.cs @@ -32,12 +32,12 @@ namespace BizHawk.Client.EmuHawk foreach (var button in lg.Definition.BoolButtons) { - Global.LuaAndAdaptor.SetButton(button, lg.IsPressed(button)); + Global.ButtonOverrideAdaptor.SetButton(button, lg.IsPressed(button)); } foreach (var floatButton in lg.Definition.FloatControls) { - Global.LuaAndAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton)); + Global.ButtonOverrideAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton)); } return lg;