diff --git a/BizHawk.Client.ApiHawk/Classes/ClientApi.cs b/BizHawk.Client.ApiHawk/Classes/ClientApi.cs index a2b39a3e62..cc2e778159 100644 --- a/BizHawk.Client.ApiHawk/Classes/ClientApi.cs +++ b/BizHawk.Client.ApiHawk/Classes/ClientApi.cs @@ -12,6 +12,7 @@ using BizHawk.Emulation.Cores.Sega.MasterSystem; using BizHawk.Client.ApiHawk.Classes.Events; using System.IO; using BizHawk.Emulation.Common.IEmulatorExtensions; +using BizHawk.Common; // ReSharper disable UnusedMember.Global namespace BizHawk.Client.ApiHawk @@ -147,7 +148,7 @@ namespace BizHawk.Client.ApiHawk /// Raised when you specify a player less than 1 or greater than maximum allows (see SystemInfo class to get this information) public static Joypad GetInput(int player) { - if (player < 1 || player > RunningSystem.MaxControllers) + if (!1.RangeTo(RunningSystem.MaxControllers).Contains(player)) { throw new IndexOutOfRangeException($"{RunningSystem.DisplayName} does not support {player} controller(s)"); } @@ -352,7 +353,7 @@ namespace BizHawk.Client.ApiHawk /// Still have some strange behaviour with multiple inputs; so this feature is still in beta public static void SetInput(int player, Joypad joypad) { - if (player < 1 || player > RunningSystem.MaxControllers) + if (!1.RangeTo(RunningSystem.MaxControllers).Contains(player)) { throw new IndexOutOfRangeException($"{RunningSystem.DisplayName} does not support {player} controller(s)"); } @@ -650,7 +651,7 @@ namespace BizHawk.Client.ApiHawk public static void SpeedMode(int percent) { - if (percent > 0 && percent < 6400) + if (percent.StrictlyBoundedBy(0.RangeTo(6400))) { InvokeMainFormMethod("ClickSpeedItem", new object[] {percent}); } diff --git a/BizHawk.Client.ApiHawk/Classes/Joypad.cs b/BizHawk.Client.ApiHawk/Classes/Joypad.cs index c8829995d4..0e413c92d3 100644 --- a/BizHawk.Client.ApiHawk/Classes/Joypad.cs +++ b/BizHawk.Client.ApiHawk/Classes/Joypad.cs @@ -1,5 +1,6 @@ using System; using BizHawk.Client.Common; +using BizHawk.Common; namespace BizHawk.Client.ApiHawk { @@ -27,7 +28,7 @@ namespace BizHawk.Client.ApiHawk /// not in range 1..max where max is . internal Joypad(SystemInfo system, int player) { - if (player < 1 || player > system.MaxControllers) + if (!1.RangeTo(system.MaxControllers).Contains(player)) { throw new InvalidOperationException($"{player} is invalid for {system.DisplayName}"); } diff --git a/BizHawk.Client.Common/Api/Classes/MemApi.cs b/BizHawk.Client.Common/Api/Classes/MemApi.cs index 9b09842187..491fb1ff15 100644 --- a/BizHawk.Client.Common/Api/Classes/MemApi.cs +++ b/BizHawk.Client.Common/Api/Classes/MemApi.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; +using BizHawk.Common; using BizHawk.Common.BufferExtensions; using BizHawk.Emulation.Common; using BizHawk.Emulation.Common.IEmulatorExtensions; @@ -194,7 +195,7 @@ namespace BizHawk.Client.Common public string HashRegion(long addr, int count, string domain = null) { var d = NamedDomainOrCurrent(domain); - if (addr < 0 || addr >= d.Size) + if (!0L.RangeToExclusive(d.Size).Contains(addr)) { var error = $"Address {addr} is outside the bounds of domain {d.Name}"; LogCallback(error); diff --git a/BizHawk.Client.Common/lua/LuaHelper.cs b/BizHawk.Client.Common/lua/LuaHelper.cs index 7d1938018c..375a158ab0 100644 --- a/BizHawk.Client.Common/lua/LuaHelper.cs +++ b/BizHawk.Client.Common/lua/LuaHelper.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; + +using BizHawk.Common; using BizHawk.Emulation.Common; using NLua; @@ -8,16 +10,15 @@ namespace BizHawk.Client.Common { public static class LuaExtensions { - private const double PrecisionLimit = 0x20000000000000 + 0.0; + /// -0x1FFFFFFFFFFFFF..0x1FFFFFFFFFFFFF + private static readonly Range PrecisionLimits = (-9007199254740991.0).RangeTo(9007199254740991.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 long AsInteger(this double d) => PrecisionLimits.Contains(d) ? (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); diff --git a/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs b/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs index 91ae98df75..723fdbc7f6 100644 --- a/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs +++ b/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs @@ -2,6 +2,7 @@ using System.IO; using BizHawk.Client.Common; +using BizHawk.Common; namespace BizHawk.Client.EmuHawk { @@ -28,14 +29,14 @@ namespace BizHawk.Client.EmuHawk public void LoadSlot(int slotNum, bool suppressOSD) { - if (0 <= slotNum && slotNum <= 9) GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", true, suppressOSD); + if (0.RangeTo(9).Contains(slotNum)) GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", true, suppressOSD); } public void Save(string path, bool suppressOSD) => GlobalWin.MainForm.SaveState(path, path, true, suppressOSD); public void SaveSlot(int slotNum, bool suppressOSD) { - if (0 <= slotNum && slotNum <= 9) GlobalWin.MainForm.SaveQuickSave($"QuickSave{slotNum}", true, suppressOSD); + if (0.RangeTo(9).Contains(slotNum)) GlobalWin.MainForm.SaveQuickSave($"QuickSave{slotNum}", true, suppressOSD); } } } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs index 15d6451f60..bdbaf875af 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs @@ -8,6 +8,8 @@ using BizHawk.Client.Common; using System.Threading; using System.Diagnostics; +using BizHawk.Common; + // ReSharper disable StringLiteralTypo // ReSharper disable UnusedMember.Global namespace BizHawk.Client.EmuHawk @@ -342,7 +344,7 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("speedmode", "Sets the speed of the emulator (in terms of percent)")] public void SpeedMode(int percent) { - if (percent > 0 && percent < 6400) + if (percent.StrictlyBoundedBy(0.RangeTo(6400))) { MainForm.ClickSpeedItem(percent); } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs index 20ba2565ad..b94febcffc 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs @@ -6,6 +6,7 @@ using System.Linq; using NLua; using BizHawk.Client.Common; +using BizHawk.Common; // ReSharper disable UnusedMember.Global // ReSharper disable StringLiteralTypo @@ -151,7 +152,7 @@ namespace BizHawk.Client.EmuHawk f = Tastudio.CurrentTasMovie.Markers[f].Frame; } - if (f < Tastudio.CurrentTasMovie.InputLogLength && f >= 0) + if (0.RangeToExclusive(Tastudio.CurrentTasMovie.InputLogLength).Contains(f)) { Tastudio.GoToFrame(f, true); } @@ -240,17 +241,14 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("submitinsertframes", "")] public void SubmitInsertFrames(int frame, int number) { - if (Engaged()) + if (Engaged() && 0.RangeToExclusive(Tastudio.CurrentTasMovie.InputLogLength).Contains(frame) && number > 0) { - if (frame >= 0 && frame < Tastudio.CurrentTasMovie.InputLogLength && number > 0) + _changeList.Add(new PendingChanges { - _changeList.Add(new PendingChanges - { - Type = LuaChangeTypes.InsertFrames, - Frame = frame, - Number = number - }); - } + Type = LuaChangeTypes.InsertFrames, + Frame = frame, + Number = number + }); } } @@ -258,17 +256,14 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("submitdeleteframes", "")] public void SubmitDeleteFrames(int frame, int number) { - if (Engaged()) + if (Engaged() && 0.RangeToExclusive(Tastudio.CurrentTasMovie.InputLogLength).Contains(frame) && number > 0) { - if (frame >= 0 && frame < Tastudio.CurrentTasMovie.InputLogLength && number > 0) + _changeList.Add(new PendingChanges { - _changeList.Add(new PendingChanges - { - Type = LuaChangeTypes.DeleteFrames, - Frame = frame, - Number = number - }); - } + Type = LuaChangeTypes.DeleteFrames, + Frame = frame, + Number = number + }); } }