Use ranges in ApiHawk and Lua

This commit is contained in:
YoshiRulz 2020-01-08 01:25:57 +10:00
parent 9a88a94ddc
commit 0f1fa1531f
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
7 changed files with 33 additions and 31 deletions

View File

@ -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
/// <exception cref="IndexOutOfRangeException">Raised when you specify a player less than 1 or greater than maximum allows (see SystemInfo class to get this information)</exception>
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
/// <remarks>Still have some strange behaviour with multiple inputs; so this feature is still in beta</remarks>
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});
}

View File

@ -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
/// <exception cref="IndexOutOfRangeException"><paramref name="player"/> not in range <c>1..max</c> where <c>max</c> is <paramref name="system"/>.<see cref="SystemInfo.MaxControllers"/></exception>
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}");
}

View File

@ -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);

View File

@ -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;
/// <remarks><c>-0x1FFFFFFFFFFFFF..0x1FFFFFFFFFFFFF</c></remarks>
private static readonly Range<double> PrecisionLimits = (-9007199254740991.0).RangeTo(9007199254740991.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 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<T>(this IEnumerable<T> list, Lua lua) => list.ToList().ToLuaTable(lua);

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -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
});
}
}