Change loadstate methods to return a bool indicating success

This commit is contained in:
YoshiRulz 2023-01-03 06:00:18 +10:00
parent 250b839e2c
commit ec6fe5fcf1
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
16 changed files with 100 additions and 113 deletions

View File

@ -103,7 +103,11 @@ namespace BizHawk.Client.Common
public bool IsTurbo() => _mainForm.IsTurboing;
public void LoadState(string name) => _mainForm.LoadState(Path.Combine(_config.PathEntries.SaveStateAbsolutePath(Game.System), $"{name}.State"), name, suppressOSD: false);
public bool LoadState(string name)
=> _mainForm.LoadState(
path: Path.Combine(_config.PathEntries.SaveStateAbsolutePath(Game.System), $"{name}.State"),
userFriendlyStateName: name,
suppressOSD: false);
public void OnBeforeQuickLoad(object sender, string quickSaveSlotName, out bool eventHandled)
{

View File

@ -19,18 +19,17 @@ namespace BizHawk.Client.Common
_mainForm = mainForm;
}
public void Load(string path, bool suppressOSD)
public bool Load(string path, bool suppressOSD)
{
if (!File.Exists(path))
{
LogCallback($"could not find file: {path}");
return;
return false;
}
_mainForm.LoadState(path, Path.GetFileName(path), suppressOSD);
return _mainForm.LoadState(path: path, userFriendlyStateName: Path.GetFileName(path), suppressOSD);
}
public void LoadSlot(int slotNum, bool suppressOSD)
public bool LoadSlot(int slotNum, bool suppressOSD)
{
if (slotNum is < 0 or > 10) throw new ArgumentOutOfRangeException(paramName: nameof(slotNum), message: ERR_MSG_NOT_A_SLOT);
if (slotNum is 0)
@ -38,7 +37,7 @@ namespace BizHawk.Client.Common
LogCallback(ERR_MSG_USE_SLOT_10);
slotNum = 10;
}
_mainForm.LoadQuickSave(slotNum, suppressOSD: suppressOSD);
return _mainForm.LoadQuickSave(slotNum, suppressOSD: suppressOSD);
}
public void Save(string path, bool suppressOSD) => _mainForm.SaveState(path, path, true, suppressOSD);

View File

@ -88,7 +88,7 @@ namespace BizHawk.Client.Common
/// Load a savestate specified by its name
/// </summary>
/// <param name="name">Savestate friendly name</param>
void LoadState(string name);
bool LoadState(string name);
/// <summary>
/// Raised before a quickload is done (just after pressing shortcut button)

View File

@ -2,8 +2,10 @@
{
public interface ISaveStateApi : IExternalApi
{
void Load(string path, bool suppressOSD = false);
void LoadSlot(int slotNum, bool suppressOSD = false);
bool Load(string path, bool suppressOSD = false);
bool LoadSlot(int slotNum, bool suppressOSD = false);
void Save(string path, bool suppressOSD = false);
void SaveSlot(int slotNum, bool suppressOSD = false);
}

View File

@ -69,12 +69,12 @@ namespace BizHawk.Client.Common
bool LoadMovie(string filename, string archive = null);
/// <remarks>only referenced from <see cref="SaveStateApi"/></remarks>
void LoadQuickSave(int slot, bool suppressOSD = false);
bool LoadQuickSave(int slot, bool suppressOSD = false);
/// <remarks>only referenced from <c>EmuClientApi</c></remarks>
bool LoadRom(string path, LoadRomArgs args);
void LoadState(string combine, string name, bool suppressOSD = false);
bool LoadState(string path, string userFriendlyStateName, bool suppressOSD = false);
/// <remarks>only referenced from <c>EmuClientApi</c></remarks>
void PauseEmulator();

View File

@ -11,24 +11,22 @@ namespace BizHawk.Client.Common
[LuaMethodExample("savestate.load( \"C:\\state.bin\" );")]
[LuaMethod("load", "Loads a savestate with the given path. If EmuHawk is deferring quicksaves, to TAStudio for example, that form will do what it likes (and the path is ignored).")]
public void Load(string path, bool suppressOSD = false)
public bool Load(string path, bool suppressOSD = false)
{
_luaLibsImpl.IsUpdateSupressed = true;
APIs.SaveState.Load(path, suppressOSD);
var success = APIs.SaveState.Load(path, suppressOSD);
_luaLibsImpl.IsUpdateSupressed = false;
return success;
}
[LuaMethodExample("savestate.loadslot( 7 );")]
[LuaMethod("loadslot", "Loads the savestate at the given slot number (must be an integer between 0 and 9). If EmuHawk is deferring quicksaves, to TAStudio for example, that form will do what it likes with the slot number.")]
public void LoadSlot(int slotNum, bool suppressOSD = false)
public bool LoadSlot(int slotNum, bool suppressOSD = false)
{
_luaLibsImpl.IsUpdateSupressed = true;
APIs.SaveState.LoadSlot(slotNum, suppressOSD);
var success = APIs.SaveState.LoadSlot(slotNum, suppressOSD: suppressOSD);
_luaLibsImpl.IsUpdateSupressed = false;
return success;
}
[LuaMethodExample("savestate.save( \"C:\\state.bin\" );")]

View File

@ -8,11 +8,16 @@
bool WantsToControlSavestates { get; }
void SaveState();
void LoadState();
bool LoadState();
void SaveStateAs();
void LoadStateAs();
bool LoadStateAs();
void SaveQuickSave(int slot);
void LoadQuickSave(int slot);
bool LoadQuickSave(int slot);
/// <summary>
/// Overrides the select slot method

View File

@ -65,7 +65,7 @@ namespace BizHawk.Client.EmuHawk
void FrameBufferResized();
/// <remarks>only referenced from <see cref="BasicBot"/></remarks>
void LoadQuickSave(int slot, bool suppressOSD = false);
bool LoadQuickSave(int slot, bool suppressOSD = false);
/// <remarks>only referenced from <see cref="MultiDiskBundler"/></remarks>
bool LoadRom(string path, LoadRomArgs args);

View File

@ -406,7 +406,7 @@ namespace BizHawk.Client.EmuHawk
private void LoadCurrentSlotMenuItem_Click(object sender, EventArgs e)
=> LoadstateCurrentSlot();
private void LoadstateCurrentSlot()
private bool LoadstateCurrentSlot()
=> LoadQuickSave(Config.SaveSlot);
private void FlushSaveRAMMenuItem_Click(object sender, EventArgs e)
@ -2522,7 +2522,7 @@ namespace BizHawk.Client.EmuHawk
if (sender == Slot0StatusButton) slot = 10;
if (e.Button is MouseButtons.Right) SaveQuickSave(slot);
else if (e.Button is MouseButtons.Left && HasSlot(slot)) LoadQuickSave(slot);
else if (e.Button is MouseButtons.Left && HasSlot(slot)) _ = LoadQuickSave(slot);
}
private void KeyPriorityStatusLabel_Click(object sender, EventArgs e)

View File

@ -108,10 +108,8 @@ namespace BizHawk.Client.EmuHawk
LoadRom(filename, args);
}
private void LoadStateFile(string filename, string archive = null)
{
LoadState(filename, Path.GetFileName(filename));
}
private bool LoadStateFile(string filename, string archive = null)
=> LoadState(path: filename, userFriendlyStateName: Path.GetFileName(filename));
private void LoadWatch(string filename, string archive = null)
{
@ -267,7 +265,7 @@ namespace BizHawk.Client.EmuHawk
LoadRom(filename, fileInformation.ArchiveName);
break;
case LoadOrdering.State:
LoadStateFile(filename, fileInformation.ArchiveName);
_ = LoadStateFile(filename, fileInformation.ArchiveName);
break;
case LoadOrdering.Watch:
LoadWatch(filename, fileInformation.ArchiveName);

View File

@ -18,7 +18,7 @@ namespace BizHawk.Client.EmuHawk
}
void SelectAndLoadFromSlot(int slot)
{
LoadQuickSave(slot);
_ = LoadQuickSave(slot);
Config.SaveSlot = slot;
UpdateStatusSlots();
}
@ -63,7 +63,7 @@ namespace BizHawk.Client.EmuHawk
HardReset();
break;
case "Quick Load":
LoadstateCurrentSlot();
_ = LoadstateCurrentSlot();
break;
case "Quick Save":
SavestateCurrentSlot();
@ -255,7 +255,7 @@ namespace BizHawk.Client.EmuHawk
SaveStateAs();
break;
case "Load Named State":
LoadStateAs();
_ = LoadStateAs();
break;
case "Previous Slot":
PreviousSlot();

View File

@ -658,15 +658,17 @@ namespace BizHawk.Client.EmuHawk
{
if (_argParser.cmdLoadState != null)
{
LoadState(_argParser.cmdLoadState, Path.GetFileName(_argParser.cmdLoadState));
_ = LoadState(
path: _argParser.cmdLoadState,
userFriendlyStateName: Path.GetFileName(_argParser.cmdLoadState));
}
else if (_argParser.cmdLoadSlot != null)
{
LoadQuickSave(_argParser.cmdLoadSlot.Value);
_ = LoadQuickSave(_argParser.cmdLoadSlot.Value);
}
else if (Config.AutoLoadLastSaveSlot)
{
LoadstateCurrentSlot();
_ = LoadstateCurrentSlot();
}
}
@ -3958,7 +3960,7 @@ namespace BizHawk.Client.EmuHawk
Tools.UpdateCheatRelatedTools(null, null);
if (!MovieSession.NewMovieQueued && Config.AutoLoadLastSaveSlot && HasSlot(Config.SaveSlot))
{
LoadstateCurrentSlot();
_ = LoadstateCurrentSlot();
}
if (FirmwareManager.RecentlyServed.Count > 0)
@ -4201,23 +4203,15 @@ namespace BizHawk.Client.EmuHawk
return int.Parse(slot.Substring(slot.Length - 1, 1));
}
public void LoadState(string path, string userFriendlyStateName, bool suppressOSD = false) // Move to client.common
public bool LoadState(string path, string userFriendlyStateName, bool suppressOSD = false) // Move to client.common
{
if (!Emulator.HasSavestates())
{
return;
}
if (IsSavestateSlave)
{
Master.LoadState();
return;
}
if (!Emulator.HasSavestates()) return false;
if (IsSavestateSlave) return Master.LoadState();
if (!new SavestateFile(Emulator, MovieSession, QuickBmpFile, MovieSession.UserBag).Load(path, this))
{
AddOnScreenMessage("Loadstate error!");
return;
return false;
}
OSD.ClearGuiText();
@ -4246,36 +4240,27 @@ namespace BizHawk.Client.EmuHawk
{
AddOnScreenMessage($"Loaded state: {userFriendlyStateName}");
}
return true;
}
public void LoadQuickSave(int slot, bool suppressOSD = false)
public bool LoadQuickSave(int slot, bool suppressOSD = false)
{
if (!Emulator.HasSavestates())
{
return;
}
if (!Emulator.HasSavestates()) return false;
var quickSlotName = $"QuickSave{slot % 10}";
EmuClient.OnBeforeQuickLoad(this, quickSlotName, out var handled);
if (handled)
{
return;
}
if (handled) return true; // not sure
if (IsSavestateSlave)
{
Master.LoadQuickSave(SlotToInt(quickSlotName));
return;
}
if (IsSavestateSlave) return Master.LoadQuickSave(SlotToInt(quickSlotName));
var path = $"{SaveStatePrefix()}.{quickSlotName}.State";
if (!File.Exists(path))
{
AddOnScreenMessage($"Unable to load {quickSlotName}.State");
return;
return false;
}
LoadState(path, quickSlotName, suppressOSD);
return LoadState(path: path, userFriendlyStateName: quickSlotName, suppressOSD: suppressOSD);
}
public void SaveState(string path, string userFriendlyStateName, bool fromLua = false, bool suppressOSD = false)
@ -4434,25 +4419,17 @@ namespace BizHawk.Client.EmuHawk
}
}
private void LoadStateAs()
private bool LoadStateAs()
{
if (!Emulator.HasSavestates())
{
return;
}
if (IsSavestateSlave)
{
Master.LoadStateAs();
return;
}
if (!Emulator.HasSavestates()) return false;
if (IsSavestateSlave) return Master.LoadStateAs();
var result = this.ShowFileOpenDialog(
discardCWDChange: true,
filter: EmuHawkSaveStatesFSFilterSet,
initDir: Config.PathEntries.SaveStateAbsolutePath(Game.System));
if (result is null || !File.Exists(result)) return;
LoadState(result, Path.GetFileName(result));
if (result is null || !File.Exists(result)) return false;
return LoadState(path: result, userFriendlyStateName: Path.GetFileName(result));
}
private void SelectSlot(int slot)

View File

@ -453,7 +453,7 @@ namespace BizHawk.Client.EmuHawk
InputManager.SyncControls(Emulator, MovieSession, Config);
MainForm.LoadQuickSave(SelectedSlot, true); // Triggers an UpdateValues call
_ = MainForm.LoadQuickSave(SelectedSlot, true); // Triggers an UpdateValues call
_lastFrameAdvanced = Emulator.Frame;
_doNotUpdateValues = false;
_startFrame = Emulator.Frame;
@ -904,7 +904,7 @@ namespace BizHawk.Client.EmuHawk
reset_curent(Attempts);
_doNotUpdateValues = true;
PressButtons(true);
MainForm.LoadQuickSave(SelectedSlot, true);
_ = MainForm.LoadQuickSave(SelectedSlot, true);
_lastFrameAdvanced = Emulator.Frame;
_doNotUpdateValues = false;
return;
@ -1034,7 +1034,7 @@ namespace BizHawk.Client.EmuHawk
_doNotUpdateValues = true;
PressButtons(true);
MainForm.LoadQuickSave(SelectedSlot, true); // Triggers an UpdateValues call
_ = MainForm.LoadQuickSave(SelectedSlot, true); // Triggers an UpdateValues call
_lastFrameAdvanced = Emulator.Frame;
_doNotUpdateValues = false;
_startFrame = Emulator.Frame;

View File

@ -11,11 +11,20 @@
public bool WantsToControlSavestates => false;
public void SaveState() { }
public void LoadState() { }
public bool LoadState()
=> false;
public void SaveStateAs() { }
public void LoadStateAs() { }
public bool LoadStateAs()
=> false;
public void SaveQuickSave(int slot) { }
public void LoadQuickSave(int slot) { }
public bool LoadQuickSave(int slot)
=> false;
public bool SelectSlot(int slot) => false;
public bool PreviousSlot() => false;
public bool NextSlot() => false;

View File

@ -210,15 +210,14 @@ namespace BizHawk.Client.EmuHawk
Tastudio.RefreshDialog();
}
private void LoadSelectedBranch()
private bool LoadSelectedBranch()
{
if (SelectedBranch != null)
{
Branches.Current = BranchView.FirstSelectedRowIndex;
LoadBranch(SelectedBranch);
BranchView.Refresh();
Tastudio.MainForm.AddOnScreenMessage($"Loaded branch {Branches.Current + 1}");
}
if (SelectedBranch == null) return false;
Branches.Current = BranchView.FirstSelectedRowIndex;
LoadBranch(SelectedBranch);
BranchView.Refresh();
Tastudio.MainForm.AddOnScreenMessage($"Loaded branch {Branches.Current + 1}");
return true;
}
private void BranchesContextMenu_Opening(object sender, CancelEventArgs e)
@ -247,7 +246,7 @@ namespace BizHawk.Client.EmuHawk
Tastudio.MainForm.AddOnScreenMessage($"Added branch {Branches.Current + 1}");
}
private void PrepareHistoryAndLoadSelectedBranch()
private bool PrepareHistoryAndLoadSelectedBranch()
{
_backupBranch = CreateBranch();
@ -263,11 +262,11 @@ namespace BizHawk.Client.EmuHawk
toolTip1.SetToolTip(UndoBranchButton, "Undo Branch Load");
_branchUndo = BranchUndo.Load;
if (BranchView.AnyRowsSelected)
{
LoadSelectedBranch();
LoadedCallback?.Invoke(BranchView.FirstSelectedRowIndex);
}
if (!BranchView.AnyRowsSelected) return false; // why'd we do all that then
var success = LoadSelectedBranch();
LoadedCallback?.Invoke(BranchView.FirstSelectedRowIndex);
return success;
}
private void LoadBranchToolStripMenuItem_Click(object sender, EventArgs e)
@ -405,11 +404,11 @@ namespace BizHawk.Client.EmuHawk
AddBranchToolStripMenuItem_Click(null, null);
}
public void LoadBranchExternal(int slot = -1)
public bool LoadBranchExternal(int slot = -1)
{
if (Tastudio.AxisEditingMode)
{
return;
return false;
}
if (slot != -1)
@ -422,11 +421,11 @@ namespace BizHawk.Client.EmuHawk
else
{
NonExistentBranchMessage(slot);
return;
return false;
}
}
PrepareHistoryAndLoadSelectedBranch();
return PrepareHistoryAndLoadSelectedBranch();
}
public void UpdateBranchExternal(int slot = -1)

View File

@ -15,25 +15,21 @@ namespace BizHawk.Client.EmuHawk
BookMarkControl.UpdateBranchExternal();
}
public void LoadState()
{
BookMarkControl.LoadBranchExternal();
}
public bool LoadState()
=> BookMarkControl.LoadBranchExternal();
public void SaveStateAs()
{
// dummy
}
public void LoadStateAs()
{
// dummy
}
public bool LoadStateAs()
=> false;
public void SaveQuickSave(int slot)
=> BookMarkControl.UpdateBranchExternal(slot - 1);
public void LoadQuickSave(int slot)
public bool LoadQuickSave(int slot)
=> BookMarkControl.LoadBranchExternal(slot - 1);
public bool SelectSlot(int slot)