Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/BizHawk.Client.EmuHawk/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3942,6 +3942,8 @@ private bool CloseGame(bool clearSram = false)
if (saveMovieResult == TryAgainResult.Canceled) return false;
}

if (Tools.Has<LuaConsole>()) Tools.LuaConsole.CallScriptExitCallbacks(); // important to do this here with the stale `IEmulator`, see https://github.com/TASEmulators/BizHawk/issues/4629
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emulator isn't stale yet, it is still current.


if (clearSram)
{
var path = Config.PathEntries.SaveRamAbsolutePath(Game, MovieSession.Movie);
Expand Down
5 changes: 5 additions & 0 deletions src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ public void CallStateLoadCallbacks(string userFriendlyStateName)
public void CallStateSaveCallbacks(string userFriendlyStateName)
=> LuaImp.CallSaveStateEvent(userFriendlyStateName);

public void CallScriptExitCallbacks()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the purpose of this needs to be documented. Calling the exit event without stopping the script is a weird thing to do, and that should be noted along with why it's done here.

Alternatively, you could do what I did and actually stop the scripts (renaming the method accordingly). Just keep a list of which ones were running so they can be started again in restart.

{
foreach (var lf in LuaImp.ScriptList) LuaImp.CallExitEvent(lf, alsoUnregister: true);
}

protected override void UpdateBefore()
{
if (LuaImp.IsUpdateSupressed)
Expand Down
7 changes: 6 additions & 1 deletion src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,19 @@ public void CallFrameAfterEvent()
}
}

public void CallExitEvent(LuaFile lf)
/// <param name="alsoUnregister">
/// <c>LuaConsole.CallScriptExitCallbacks</c> passes <see langword="true" /> for this so that the subsequent call to <c>LuaConsole.Restart</c>
/// doesn't cause those callbacks to run at a point where the core has already been disposed
/// </param>
public void CallExitEvent(LuaFile lf, bool alsoUnregister = false)
{
foreach (var exitCallback in RegisteredFunctions
.Where(l => l.Event == NamedLuaFunction.EVENT_TYPE_ENGINESTOP
&& (l.LuaFile.Path == lf.Path || ReferenceEquals(l.LuaFile.Thread, lf.Thread)))
.ToList())
{
exitCallback.Call();
if (alsoUnregister) RegisteredFunctions.Remove(exitCallback);
}
}

Expand Down