Move Lua hacks to Lua libs

Changed parameter of IInputMovie.SetRerecordCount to ulong to match internal
This commit is contained in:
YoshiRulz 2019-12-16 15:04:35 +10:00
parent 861f238cc1
commit 8f93a954e1
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
12 changed files with 66 additions and 73 deletions

View File

@ -352,29 +352,29 @@ namespace BizHawk.Client.Common
return false; return false;
} }
public void SetRenderPlanes(params bool[] luaParam) public void SetRenderPlanes(params bool[] args)
{ {
if (Emulator is GPGX gpgx) if (Emulator is GPGX gpgx)
{ {
var s = gpgx.GetSettings(); var s = gpgx.GetSettings();
s.DrawBGA = luaParam[0]; s.DrawBGA = args[0];
s.DrawBGB = luaParam[1]; s.DrawBGB = args[1];
s.DrawBGW = luaParam[2]; s.DrawBGW = args[2];
s.DrawObj = luaParam[3]; s.DrawObj = args[3];
gpgx.PutSettings(s); gpgx.PutSettings(s);
} }
else if (Emulator is LibsnesCore snes) else if (Emulator is LibsnesCore snes)
{ {
var s = snes.GetSettings(); var s = snes.GetSettings();
s.ShowBG1_0 = s.ShowBG1_1 = luaParam[0]; s.ShowBG1_0 = s.ShowBG1_1 = args[0];
s.ShowBG2_0 = s.ShowBG2_1 = luaParam[1]; s.ShowBG2_0 = s.ShowBG2_1 = args[1];
s.ShowBG3_0 = s.ShowBG3_1 = luaParam[2]; s.ShowBG3_0 = s.ShowBG3_1 = args[2];
s.ShowBG4_0 = s.ShowBG4_1 = luaParam[3]; s.ShowBG4_0 = s.ShowBG4_1 = args[3];
s.ShowOBJ_0 = luaParam[4]; s.ShowOBJ_0 = args[4];
s.ShowOBJ_1 = luaParam[5]; s.ShowOBJ_1 = args[5];
s.ShowOBJ_2 = luaParam[6]; s.ShowOBJ_2 = args[6];
s.ShowOBJ_3 = luaParam[7]; s.ShowOBJ_3 = args[7];
snes.PutSettings(s); snes.PutSettings(s);
} }
else if (Emulator is NES nes) else if (Emulator is NES nes)
@ -382,8 +382,8 @@ namespace BizHawk.Client.Common
// in the future, we could do something more arbitrary here. // in the future, we could do something more arbitrary here.
// but this isn't any worse than the old system // but this isn't any worse than the old system
var s = nes.GetSettings(); var s = nes.GetSettings();
s.DispSprites = luaParam[0]; s.DispSprites = args[0];
s.DispBackground = luaParam[1]; s.DispBackground = args[1];
nes.PutSettings(s); nes.PutSettings(s);
} }
else if (Emulator is QuickNES quicknes) else if (Emulator is QuickNES quicknes)
@ -391,7 +391,7 @@ namespace BizHawk.Client.Common
var s = quicknes.GetSettings(); var s = quicknes.GetSettings();
// this core doesn't support disabling BG // this core doesn't support disabling BG
bool showSp = GetSetting(0, luaParam); bool showSp = GetSetting(0, args);
if (showSp && s.NumSprites == 0) if (showSp && s.NumSprites == 0)
{ {
s.NumSprites = 8; s.NumSprites = 8;
@ -406,12 +406,12 @@ namespace BizHawk.Client.Common
else if (Emulator is PCEngine pce) else if (Emulator is PCEngine pce)
{ {
var s = pce.GetSettings(); var s = pce.GetSettings();
s.ShowOBJ1 = GetSetting(0, luaParam); s.ShowOBJ1 = GetSetting(0, args);
s.ShowBG1 = GetSetting(1, luaParam); s.ShowBG1 = GetSetting(1, args);
if (luaParam.Length > 2) if (args.Length > 2)
{ {
s.ShowOBJ2 = GetSetting(2, luaParam); s.ShowOBJ2 = GetSetting(2, args);
s.ShowBG2 = GetSetting(3, luaParam); s.ShowBG2 = GetSetting(3, args);
} }
pce.PutSettings(s); pce.PutSettings(s);
@ -419,16 +419,16 @@ namespace BizHawk.Client.Common
else if (Emulator is SMS sms) else if (Emulator is SMS sms)
{ {
var s = sms.GetSettings(); var s = sms.GetSettings();
s.DispOBJ = GetSetting(0, luaParam); s.DispOBJ = GetSetting(0, args);
s.DispBG = GetSetting(1, luaParam); s.DispBG = GetSetting(1, args);
sms.PutSettings(s); sms.PutSettings(s);
} }
else if (Emulator is WonderSwan ws) else if (Emulator is WonderSwan ws)
{ {
var s = ws.GetSettings(); var s = ws.GetSettings();
s.EnableSprites = GetSetting(0, luaParam); s.EnableSprites = GetSetting(0, args);
s.EnableFG = GetSetting(1, luaParam); s.EnableFG = GetSetting(1, args);
s.EnableBG = GetSetting(2, luaParam); s.EnableBG = GetSetting(2, args);
ws.PutSettings(s); ws.PutSettings(s);
} }
} }

View File

@ -44,10 +44,6 @@ namespace BizHawk.Client.Common
} }
} }
buttons["clear"] = null;
buttons["getluafunctionslist"] = null;
buttons["output"] = null;
return buttons; return buttons;
} }
@ -77,12 +73,12 @@ namespace BizHawk.Client.Common
foreach (var button in lg.Definition.BoolButtons) 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) foreach (var floatButton in lg.Definition.FloatControls)
{ {
Global.LuaAndAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton)); Global.ButtonOverrideAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton));
} }
} }
catch (Exception) catch (Exception)
@ -132,19 +128,19 @@ namespace BizHawk.Client.Common
{ {
if (theValue.HasValue) // Force if (theValue.HasValue) // Force
{ {
Global.LuaAndAdaptor.SetButton(toPress, theValue.Value); Global.ButtonOverrideAdaptor.SetButton(toPress, theValue.Value);
Global.ActiveController.Overrides(Global.LuaAndAdaptor); Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor);
} }
else // Unset else // Unset
{ {
Global.LuaAndAdaptor.UnSet(toPress); Global.ButtonOverrideAdaptor.UnSet(toPress);
Global.ActiveController.Overrides(Global.LuaAndAdaptor); Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor);
} }
} }
else // Inverse else // Inverse
{ {
Global.LuaAndAdaptor.SetInverse(toPress); Global.ButtonOverrideAdaptor.SetInverse(toPress);
Global.ActiveController.Overrides(Global.LuaAndAdaptor); Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor);
} }
} }
} }
@ -163,10 +159,10 @@ namespace BizHawk.Client.Common
toPress = $"P{controller} {button}"; toPress = $"P{controller} {button}";
} }
if (state.HasValue) if (state.HasValue)
Global.LuaAndAdaptor.SetButton(toPress, state.Value); Global.ButtonOverrideAdaptor.SetButton(toPress, state.Value);
else else
Global.LuaAndAdaptor.UnSet(toPress); Global.ButtonOverrideAdaptor.UnSet(toPress);
Global.ActiveController.Overrides(Global.LuaAndAdaptor); Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor);
} }
catch catch
{ {

View File

@ -74,20 +74,6 @@ namespace BizHawk.Client.Common
Global.MovieSession.ReadOnly = readOnly; 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) public static void SetRerecordCounting(bool counting)
{ {
Global.MovieSession.Movie.IsCountingRerecords = counting; Global.MovieSession.Movie.IsCountingRerecords = counting;
@ -272,10 +258,7 @@ namespace BizHawk.Client.Common
MoviePluginStatic.SetReadOnly(readOnly); MoviePluginStatic.SetReadOnly(readOnly);
} }
public void SetRerecordCount(double count) public void SetRerecordCount(ulong count) => Global.MovieSession.Movie.Rerecords = count;
{
MoviePluginStatic.SetRerecordCount(count);
}
public void SetRerecordCounting(bool counting) public void SetRerecordCounting(bool counting)
{ {

View File

@ -27,6 +27,6 @@ namespace BizHawk.Client.Common
string GetBoardName(); string GetBoardName();
object GetSettings(); object GetSettings();
bool PutSettings(object settings); bool PutSettings(object settings);
void SetRenderPlanes(params bool[] param); void SetRenderPlanes(params bool[] args);
} }
} }

View File

@ -16,7 +16,7 @@ namespace BizHawk.Client.Common
string Mode(); string Mode();
void Save(string filename = ""); void Save(string filename = "");
void SetReadOnly(bool readOnly); void SetReadOnly(bool readOnly);
void SetRerecordCount(double count); void SetRerecordCount(ulong count);
void SetRerecordCounting(bool counting); void SetRerecordCounting(bool counting);
void Stop(); void Stop();
double GetFps(); double GetFps();

View File

@ -58,9 +58,9 @@ namespace BizHawk.Client.Common
public static readonly StickyXorAdapter StickyXORAdapter = new StickyXorAdapter(); public static readonly StickyXorAdapter StickyXORAdapter = new StickyXorAdapter();
/// <summary> /// <summary>
/// Used to AND to another controller, used for Joypad.Set() /// Used to AND to another controller, used for <see cref="JoypadApi.Set(System.Collections.Generic.Dictionary{string,bool},System.Nullable{int})">JoypadApi.Set</see>
/// </summary> /// </summary>
public static readonly OverrideAdaptor LuaAndAdaptor = new OverrideAdaptor(); public static readonly OverrideAdaptor ButtonOverrideAdaptor = new OverrideAdaptor();
/// <summary> /// <summary>
/// fire off one-frame logical button clicks here. useful for things like ti-83 virtual pad and reset buttons /// fire off one-frame logical button clicks here. useful for things like ti-83 virtual pad and reset buttons

View File

@ -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")] [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) public LuaTable Get(int? controller = null)
{ {
return APIs.Joypad var table = APIs.Joypad.Get(controller).ToLuaTable(Lua);
.Get(controller) table["clear"] = null;
.ToLuaTable(Lua); table["getluafunctionslist"] = null;
table["output"] = null;
return table;
} }
[LuaMethodExample("local nljoyget = joypad.getimmediate( );")] [LuaMethodExample("local nljoyget = joypad.getimmediate( );")]

View File

@ -74,7 +74,7 @@ namespace BizHawk.Client.Common
[LuaMethodExample("movie.setrerecordcount( 20.0 );")] [LuaMethodExample("movie.setrerecordcount( 20.0 );")]
[LuaMethod("setrerecordcount", "Sets the rerecord count of the current movie.")] [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 );")] [LuaMethodExample("movie.setrerecordcounting( true );")]
[LuaMethod("setrerecordcounting", "Sets whether or not the current movie will increment the rerecord counter on loadstate")] [LuaMethod("setrerecordcounting", "Sets whether or not the current movie will increment the rerecord counter on loadstate")]

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLua; using NLua;
@ -7,6 +8,17 @@ namespace BizHawk.Client.Common
{ {
public static class LuaExtensions public static class LuaExtensions
{ {
private const double PrecisionLimit = 0x20000000000000 + 0.0;
/// <remarks>
/// 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.
/// </remarks>
/// <exception cref="ArithmeticException"><paramref name="d"/> &ge; 2^53 or <paramref name="d"/> &le; -2^53</exception>
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<T>(this IEnumerable<T> list, Lua lua) => list.ToList().ToLuaTable(lua); public static LuaTable EnumerateToLuaTable<T>(this IEnumerable<T> list, Lua lua) => list.ToList().ToLuaTable(lua);
public static LuaTable ToLuaTable<T>(this IList<T> list, Lua lua, int indexFrom = 0) public static LuaTable ToLuaTable<T>(this IList<T> list, Lua lua, int indexFrom = 0)

View File

@ -571,7 +571,7 @@ namespace BizHawk.Client.EmuHawk
} }
// autohold/autofire must not be affected by the following inputs // autohold/autofire must not be affected by the following inputs
Global.ActiveController.Overrides(Global.LuaAndAdaptor); Global.ActiveController.Overrides(Global.ButtonOverrideAdaptor);
if (GlobalWin.Tools.Has<LuaConsole>() && !SuppressLua) if (GlobalWin.Tools.Has<LuaConsole>() && !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 // 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.ClickyVirtualPadController.FrameTick();
Global.LuaAndAdaptor.FrameTick(); Global.ButtonOverrideAdaptor.FrameTick();
if (GlobalWin.Tools.Has<LuaConsole>() && !SuppressLua) if (GlobalWin.Tools.Has<LuaConsole>() && !SuppressLua)
{ {

View File

@ -935,7 +935,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var button in lg.Definition.BoolButtons) foreach (var button in lg.Definition.BoolButtons)
{ {
// TODO: make an input adapter specifically for the bot? // TODO: make an input adapter specifically for the bot?
Global.LuaAndAdaptor.SetButton(button, lg.IsPressed(button)); Global.ButtonOverrideAdaptor.SetButton(button, lg.IsPressed(button));
} }
} }
else else

View File

@ -32,12 +32,12 @@ namespace BizHawk.Client.EmuHawk
foreach (var button in lg.Definition.BoolButtons) 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) foreach (var floatButton in lg.Definition.FloatControls)
{ {
Global.LuaAndAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton)); Global.ButtonOverrideAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton));
} }
return lg; return lg;