Skip to content

Commit 246d29c

Browse files
committed
Block more stuffs during invisible emulation, especially to make it work better with TAStudio.
1 parent 28fddb7 commit 246d29c

File tree

8 files changed

+47
-33
lines changed

8 files changed

+47
-33
lines changed

Assets/Lua/GBA/SonicAdvance_CamHack.lua

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,6 @@ local addr_offY = 0x5B98
66
local addr_camX = 0x59D0
77
local addr_camY = 0x59D2
88

9-
-- Seek forward to a given frame.
10-
-- This will unpause emulation, and restore the users' desired pause state when seeking is completed.
11-
local function seek_frame(frame)
12-
local pause = client.unpause()
13-
while emu.framecount() < frame do
14-
-- The user may pause mid-seek, perhaps even by accident.
15-
-- In this case, we will unpause but remember that the user wants to pause at the end.
16-
if client.ispaused() then
17-
pause = true
18-
client.unpause()
19-
end
20-
-- Yield, not frameadvance. With frameadvance we cannot detect pauses, since frameadvance would not return.
21-
-- This is true even if we have just called client.unpause.
22-
emu.yield()
23-
end
24-
25-
if pause then client.pause() end
26-
end
27-
289
event.onexit(function() client.invisibleemulation(false) end)
2910

3011
while true do
@@ -42,9 +23,9 @@ while true do
4223
mainmemory.write_u16_le(addr_camX, Xval)
4324
mainmemory.write_u16_le(addr_camY, Yval)
4425

45-
seek_frame(emu.framecount()+1)
26+
emu.frameadvance()
4627
client.invisibleemulation(false)
47-
seek_frame(emu.framecount()+1)
28+
emu.frameadvance()
4829
client.invisibleemulation(true)
4930
memorysavestate.loadcorestate(memorystate)
5031
memorysavestate.removestate(memorystate)

Assets/Lua/seek.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Seek forward to a given frame.
2+
-- This will unpause emulation, and restore the users' desired pause state when seeking is completed.
3+
-- Note that this may interfere with TAStudio's seeking behavior.
4+
local function force_seek_frame(frame)
5+
local pause = client.unpause()
6+
while emu.framecount() < frame do
7+
-- The user may pause mid-seek, perhaps even by accident.
8+
-- In this case, we will unpause but remember that the user wants to pause at the end.
9+
if client.ispaused() then
10+
pause = true
11+
client.unpause()
12+
end
13+
-- Yield, not frameadvance. With frameadvance we cannot detect pauses, since frameadvance would not return.
14+
-- This is true even if we have just called client.unpause.
15+
emu.yield()
16+
end
17+
18+
if pause then client.pause() end
19+
end
20+
21+
-- Seek but without touching the pause state. Function will not return if the given frame is never reached due to the user manaully pausing/rewinding.
22+
local function seek_frame(frame)
23+
while emu.framecount() < frame do
24+
emu.frameadvance()
25+
end
26+
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void FrameSkip(int numFrames)
135135
public int GetWindowSize()
136136
=> _config.GetWindowScaleFor(Emulator.SystemId);
137137

138-
public void InvisibleEmulation(bool invisible) => _mainForm.InvisibleEmulateNextFrame = invisible;
138+
public void InvisibleEmulation(bool invisible) => _mainForm.InvisibleEmulation = invisible;
139139

140140
public bool IsPaused() => _mainForm.EmulatorPaused;
141141

src/BizHawk.Client.Common/IMainFormForApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IMainFormForApi
1717
bool EmulatorPaused { get; }
1818

1919
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
20-
bool InvisibleEmulateNextFrame { get; set; }
20+
bool InvisibleEmulation { get; set; }
2121

2222
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
2323
bool IsSeeking { get; }

src/BizHawk.Client.EmuHawk/IMainFormForTools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IMainFormForTools : IDialogController
2525
bool HoldFrameAdvance { get; set; }
2626

2727
/// <remarks>only referenced from <see cref="BasicBot"/></remarks>
28-
bool InvisibleEmulateNextFrame { get; set; }
28+
bool InvisibleEmulation { get; set; }
2929

3030
/// <remarks>only referenced from <see cref="TAStudio"/></remarks>
3131
bool IsFastForwarding { get; }

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,9 +1149,11 @@ private void OnPauseToggle(bool newPauseState)
11491149
/// <item><description><see cref="ClientLuaLibrary.InvisibleEmulation(bool)"/></description></item>
11501150
/// </list>
11511151
/// </summary>
1152-
public bool InvisibleEmulateNextFrame { get; set; }
1152+
public bool InvisibleEmulation { get => _invisibleUpdate; set => _invisibleEmulateNextFrame = value; }
11531153

1154+
private bool _invisibleEmulateNextFrame;
11541155
private bool _invisibleEmulation;
1156+
private bool _invisibleUpdate;
11551157

11561158
private long MouseWheelTracker;
11571159

@@ -1176,7 +1178,7 @@ private void OnPauseToggle(bool newPauseState)
11761178

11771179
public bool IsSeeking => PauseOnFrame.HasValue;
11781180
private bool IsTurboSeeking => PauseOnFrame.HasValue && Config.TurboSeek;
1179-
public bool IsTurboing => InputManager.ClientControls["Turbo"] || IsTurboSeeking || _invisibleEmulation;
1181+
public bool IsTurboing => InputManager.ClientControls["Turbo"] || IsTurboSeeking || _invisibleUpdate;
11801182
public bool IsFastForwarding => InputManager.ClientControls["Fast Forward"] || IsTurboing;
11811183
public bool IsRewinding { get; private set; }
11821184

@@ -3000,7 +3002,7 @@ private void StepRunLoop_Core(bool force = false)
30003002
// BlockFrameAdvance (true when input it being editted in TAStudio) supercedes all other frame advance conditions
30013003
if ((runFrame || force) && !BlockFrameAdvance)
30023004
{
3003-
_invisibleEmulation = InvisibleEmulateNextFrame;
3005+
_invisibleEmulation = _invisibleEmulateNextFrame;
30043006

30053007
var isFastForwarding = IsFastForwarding;
30063008
var isFastForwardingOrRewinding = isFastForwarding || isRewinding || Config.Unthrottled;
@@ -3066,7 +3068,7 @@ private void StepRunLoop_Core(bool force = false)
30663068

30673069
RA?.OnFrameAdvance();
30683070

3069-
if (Config.AutosaveSaveRAM)
3071+
if (Config.AutosaveSaveRAM && !_invisibleEmulation)
30703072
{
30713073
AutoFlushSaveRamIn--;
30723074
if (AutoFlushSaveRamIn <= 0)
@@ -3087,7 +3089,7 @@ private void StepRunLoop_Core(bool force = false)
30873089
bool render = !_invisibleEmulation && (!_throttle.skipNextFrame || _currAviWriter?.UsesVideo is true || atTurboSeekEnd);
30883090
bool newFrame = Emulator.FrameAdvance(InputManager.ControllerOutput, render, renderSound);
30893091

3090-
MovieSession.HandleFrameAfter(ToolBypassingMovieEndAction is not null);
3092+
if (!_invisibleUpdate) MovieSession.HandleFrameAfter(ToolBypassingMovieEndAction is not null);
30913093

30923094
if (returnToRecording)
30933095
{
@@ -3124,6 +3126,7 @@ private void StepRunLoop_Core(bool force = false)
31243126
UpdateToolsAfter();
31253127
}
31263128
}
3129+
_invisibleUpdate = _invisibleEmulation;
31273130

31283131
if (!PauseAvi && newFrame && !_invisibleEmulation)
31293132
{
@@ -4411,6 +4414,8 @@ private bool Rewind(ref bool runFrame, long currentTimestamp, out bool returnToR
44114414

44124415
returnToRecording = false;
44134416

4417+
if (_invisibleEmulation) return false;
4418+
44144419
if (ToolControllingRewind is { } rewindTool)
44154420
{
44164421
if (InputManager.ClientControls["Rewind"] || PressRewind)

src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,8 @@ private void StartBot()
10471047

10481048
if (InvisibleEmulationCheckBox.Checked)
10491049
{
1050-
_previousInvisibleEmulation = MainForm.InvisibleEmulateNextFrame;
1051-
MainForm.InvisibleEmulateNextFrame = true;
1050+
_previousInvisibleEmulation = MainForm.InvisibleEmulation;
1051+
MainForm.InvisibleEmulation = true;
10521052
}
10531053

10541054
UpdateBotStatusIcon();
@@ -1103,7 +1103,7 @@ private void StopBot()
11031103
private void RestoreConfigFlags()
11041104
{
11051105
Config.DisplayMessages = _previousDisplayMessage;
1106-
MainForm.InvisibleEmulateNextFrame = _previousInvisibleEmulation;
1106+
MainForm.InvisibleEmulation = _previousInvisibleEmulation;
11071107
var movie = MovieSession.Movie;
11081108
if (movie.IsRecording()) movie.IsCountingRerecords = _oldCountingSetting;
11091109
}

src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected override void GeneralUpdate()
7171

7272
protected override void UpdateAfter()
7373
{
74-
if (!IsHandleCreated || IsDisposed || CurrentTasMovie == null)
74+
if (!IsHandleCreated || IsDisposed || CurrentTasMovie == null || MainForm.InvisibleEmulation)
7575
{
7676
return;
7777
}
@@ -109,6 +109,8 @@ protected override void UpdateAfter()
109109

110110
protected override void FastUpdateAfter()
111111
{
112+
if (MainForm.InvisibleEmulation) return;
113+
112114
if (_seekingTo != -1 && Emulator.Frame >= _seekingTo)
113115
{
114116
bool smga = _shouldMoveGreenArrow;

0 commit comments

Comments
 (0)