Skip to content

Commit 6223c9d

Browse files
committed
delegate type for ShowFuture
1 parent 47adeb5 commit 6223c9d

7 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,14 @@ public void SetWindowSize(int size)
219219
}
220220
}
221221

222-
public void ShowFuture(Func<int, bool> preFrameCallback, int maxFrames)
222+
public void ShowFuture(ShowFutureCallback/*?*/ preFrameCallback, short maxFrames)
223223
{
224+
if (maxFrames < 1 && preFrameCallback != null)
225+
{
226+
_logCallback($"Invalid number of future frames ({maxFrames}); number must be positive.");
227+
return;
228+
}
229+
224230
_mainForm.PreFutureFrameCallback = preFrameCallback;
225231
_mainForm.MaxFutureFrames = maxFrames;
226232
}

src/BizHawk.Client.Common/Api/Interfaces/IEmuClientApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public interface IEmuClientApi : IDisposable, IExternalApi
145145
/// When the callback returns true, emulation will rewind to the real current frame and the just-run future frame will be displayed.
146146
/// <br/>Pass null to disable future frame display.</param>
147147
/// <param name="maxFrames">The maximum number of future frames to emulate. Useful to avoid freezing the client UI in case of accidentally never returning true from the callback.</param>
148-
void ShowFuture(Func<int, bool> preFrameCallback, int maxFrames);
148+
void ShowFuture(ShowFutureCallback/*?*/ preFrameCallback, short maxFrames);
149149

150150
void SpeedMode(int percent);
151151

src/BizHawk.Client.Common/EventTypes.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,11 @@ public StateSavedEventArgs(string stateName)
122122
/// <param name="sender">Object that raised the event</param>
123123
/// <param name="e">Event arguments</param>
124124
public delegate void StateSavedEventHandler(object sender, StateSavedEventArgs e);
125+
126+
/// <summary>
127+
/// Represent a method that will control future emulation.
128+
/// </summary>
129+
/// <param name="futureFramesEmulated">The number of frames into the future that have been emulated. This is a short purely to help avoid accidentally passing in absurdly large numbers.</param>
130+
/// <returns>True to display the current frame, false to continue emulating into the future.</returns>
131+
public delegate bool ShowFutureCallback(short futureFramesEmulated);
125132
}

src/BizHawk.Client.Common/IMainFormForApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface IMainFormForApi
3535
bool PauseAvi { get; set; }
3636

3737
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
38-
public Func<int, bool>/*?*/ PreFutureFrameCallback { get; set; }
38+
public ShowFutureCallback/*?*/ PreFutureFrameCallback { get; set; }
3939

4040
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
4141
void ClearHolds();

src/BizHawk.Client.Common/lua/CommonLibs/ClientLuaLibrary.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,17 @@ public void SetWindowSize(int size)
286286
"Unregister the callback with event.unregister____ to disable future frame display. " +
287287
"No more than `maxFrames` future frames will be emulated. Useful to avoid freezing " +
288288
"the client UI in case of accidentally never returning true from the callback.")]
289-
public string ShowFuture(LuaFunction luaf, int maxFrames, string name = null)
289+
public string ShowFuture(LuaFunction luaf, long maxFrames, string name = null)
290290
{
291+
if (maxFrames < 1 || maxFrames > short.MaxValue)
292+
{
293+
Log($"Invalid number of future frames ({maxFrames}); number must be positive and less than 2^15.");
294+
return "";
295+
}
296+
291297
INamedLuaFunction nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_FUTURE, prohibitedApis: ApiGroup.PROHIBITED_MID_FRAME, name: name);
292-
Func<int, bool> FutureCallback = (f) => nlf.Call(f) is [ bool r ] ? r : false;
293-
APIs.EmuClient.ShowFuture(FutureCallback, maxFrames);
298+
ShowFutureCallback FutureCallback = (f) => nlf.Call(f) is [ bool r ] ? r : false;
299+
APIs.EmuClient.ShowFuture(FutureCallback, (short)maxFrames);
294300
nlf.OnRemove += () => APIs.EmuClient.ShowFuture(null, 0);
295301
return nlf.GuidStr;
296302
}

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ private set
11881188

11891189
public event StateSavedEventHandler SavestateSaved;
11901190

1191-
public Func<int, bool>/*?*/ PreFutureFrameCallback { get; set; }
1191+
public ShowFutureCallback/*?*/ PreFutureFrameCallback { get; set; }
11921192

11931193
public int MaxFutureFrames { get; set; }
11941194

@@ -3081,7 +3081,7 @@ private void StepRunLoop_Core(bool force = false)
30813081
statable.SaveStateBinary(new(state));
30823082

30833083
int frameCount = 0;
3084-
while (!PreFutureFrameCallback(frameCount) && frameCount < MaxFutureFrames)
3084+
while (!PreFutureFrameCallback((short)frameCount) && frameCount < MaxFutureFrames)
30853085
{
30863086
frameCount++;
30873087
MovieSession.HandleFrameBefore();

src/BizHawk.Tests.Client.Common/FakeMainFormForApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class FakeMainFormForApi : IMainFormForApi
2727

2828
public bool PauseAvi { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
2929
public int MaxFutureFrames { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
30-
public Func<int, bool> PreFutureFrameCallback { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
30+
public ShowFutureCallback PreFutureFrameCallback { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
3131

3232
#pragma warning disable CS0067 // Events are never used
3333
public event BeforeQuickLoadEventHandler? QuicksaveLoad;

0 commit comments

Comments
 (0)