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
6 changes: 6 additions & 0 deletions src/BizHawk.Client.Common/tools/Interfaces/IToolForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public interface IToolForm
/// </summary>
void UpdateValues(ToolFormUpdateType type);

/// <summary>
/// Stop the tool, in preparation for a game or core change.
/// A tool should expect that either <see cref="Restart"/> or <see cref="Close"/> will be called after this.
/// </summary>
void Stop();

/// <summary>
/// Will be called anytime the dialog needs to be restarted, such as when a new ROM is loaded
/// The tool implementing this needs to account for a Game and Core change
Expand Down
2 changes: 2 additions & 0 deletions src/BizHawk.Client.EmuHawk/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3936,6 +3936,8 @@ private bool CloseGame(bool clearSram = false)
if (saveMovieResult == TryAgainResult.Canceled) return false;
}

Tools.Stop();

if (clearSram)
{
var path = Config.PathEntries.SaveRamAbsolutePath(Game, MovieSession.Movie);
Expand Down
22 changes: 12 additions & 10 deletions src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ public string FromSlot
protected override void UpdateAfter() => Update(fast: false);
protected override void FastUpdateAfter() => Update(fast: true);

public override void Stop()
{
if (_isBotting)
{
StopBot();
}
else if (_replayMode)
{
FinishReplay();
}
}

public override void Restart()
{
_ = StatableCore!; // otherwise unused due to loadstating via MainForm; however this service is very much required so the property needs to be present
Expand All @@ -272,16 +284,6 @@ public override void Restart()
_dataSize = 1;
}

if (_isBotting)
{
StopBot();
}
else if (_replayMode)
{
FinishReplay();
}


if (_lastRom != MainForm.CurrentlyOpenRom)
{
_lastRom = MainForm.CurrentlyOpenRom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ private void FullUpdate()
BreakPointControl1.UpdateValues();
}

public override void Stop()
{
DisengageDebugger();
}

public override void Restart()
{
UpdateCapabilitiesProps();
DisengageDebugger();
EngageDebugger();
}

Expand Down
39 changes: 20 additions & 19 deletions src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public LuaConsole()
}

private LuaLibraries LuaImp;
private List<LuaFile> _scriptsToRestart = new();

private ConsoleLuaLibrary _consoleLib;

Expand Down Expand Up @@ -198,10 +199,22 @@ private void BranchesMarkersSplit_SplitterMoved(object sender, SplitterEventArgs
Settings.SplitDistance = splitContainer1.SplitterDistance;
}

public override void Restart()
public override void Stop()
{
List<LuaFile> runningScripts = new();
if (LuaImp is not null && !LuaImp.IsRebootingCore)
{
_scriptsToRestart = LuaImp.ScriptList.Where(lf => lf.Enabled).ToList();

// we don't use _scriptsToRestart here, in case something is somehow partially active
foreach (var file in LuaImp.ScriptList)
{
file.Stop();
}
}
}

public override void Restart()
{
ApiContainer apiContainer = ApiManager.RestartLua(
Emulator.ServiceProvider,
WriteToOutputWindow,
Expand All @@ -215,23 +228,11 @@ public override void Restart()
Game,
DialogController);

// Things we need to do with the existing LuaImp before we can make a new one
if (LuaImp is not null)
if (LuaImp is not null && LuaImp.IsRebootingCore)
{
if (LuaImp.IsRebootingCore)
{
// Even if the lua console is self-rebooting from client.reboot_core() we still want to re-inject dependencies
LuaImp.Restart(Emulator.ServiceProvider, Config, apiContainer);
return;
}

runningScripts = LuaImp.ScriptList.Where(lf => lf.Enabled).ToList();

// we don't use runningScripts here as the other scripts need to be stopped too
foreach (var file in LuaImp.ScriptList)
{
file.Stop();
}
// Even if the lua console is self-rebooting from client.reboot_core() we still want to re-inject dependencies
LuaImp.Restart(Emulator.ServiceProvider, Config, apiContainer);
return;
}

_openedFiles = new(_openedFiles, onChanged: SessionChangedCallback);
Expand All @@ -255,7 +256,7 @@ public override void Restart()
.Select(static f => $"{f.Library}.{f.Name}")
.ToArray());

foreach (var file in runningScripts)
foreach (var file in _scriptsToRestart)
{
EnableLuaFile(file);
}
Expand Down
2 changes: 2 additions & 0 deletions src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class ToolFormBase : FormBase, IToolForm, IDialogParent
public virtual bool IsActive => IsHandleCreated && !IsDisposed;
public virtual bool IsLoaded => IsActive;

public virtual void Stop() {}

public virtual void Restart() {}

public void SetToolFormBaseProps(
Expand Down
14 changes: 13 additions & 1 deletion src/BizHawk.Client.EmuHawk/tools/ToolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static ToolManager()
// For instance, add an IToolForm property called UsesCheats, so that a UpdateCheatRelatedTools() method can update all tools of this type
// Also a UsesRam, and similar method
private readonly List<IToolForm> _tools = new List<IToolForm>();
private readonly List<IToolForm> _toolsToRestart = new();

/// <summary>
/// Initializes a new instance of the <see cref="ToolManager"/> class.
Expand Down Expand Up @@ -582,6 +583,15 @@ public void GeneralUpdateActiveExtTools()
}
}

public void Stop()
{
foreach (IToolForm tool in _tools.Where(static t => t.IsActive))
{
_toolsToRestart.Add(tool);
tool.Stop();
}
}

public void Restart(Config config, IEmulator emulator, IGameInfo game)
{
_config = config;
Expand All @@ -597,7 +607,7 @@ public void Restart(Config config, IEmulator emulator, IGameInfo game)
if (ServiceInjector.UpdateServices(_emulator.ServiceProvider, tool)
&& (tool is not IExternalToolForm || ApiInjector.UpdateApis(GetOrInitApiProvider, tool)))
{
if (tool.IsActive) tool.Restart();
if (_toolsToRestart.Contains(tool)) tool.Restart();
}
else
{
Expand All @@ -611,6 +621,8 @@ public void Restart(Config config, IEmulator emulator, IGameInfo game)
tool.Close();
_tools.Remove(tool);
}

_toolsToRestart.Clear();
}

/// <summary>
Expand Down