Skip to content

Commit

Permalink
Change loadstate methods to return a bool indicating success
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Jan 2, 2023
1 parent 250b839 commit ec6fe5f
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 113 deletions.
6 changes: 5 additions & 1 deletion src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ public void FrameSkip(int numFrames)

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)
{
Expand Down
11 changes: 5 additions & 6 deletions src/BizHawk.Client.Common/Api/Classes/SaveStateApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ public SaveStateApi(Action<string> logCallback, IMainFormForApi mainForm)
_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)
{
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);
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/Api/Interfaces/IEmuClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public interface IEmuClientApi : IExternalApi
/// 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)
Expand Down
6 changes: 4 additions & 2 deletions src/BizHawk.Client.Common/Api/Interfaces/ISaveStateApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Client.Common/IMainFormForApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public interface IMainFormForApi
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();
Expand Down
14 changes: 6 additions & 8 deletions src/BizHawk.Client.Common/lua/CommonLibs/SaveStateLuaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ public SaveStateLuaLibrary(ILuaLibraries luaLibsImpl, ApiContainer apiContainer,

[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\" );")]
Expand Down
11 changes: 8 additions & 3 deletions src/BizHawk.Client.EmuHawk/IControlMainform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ public interface IControlMainform
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
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.EmuHawk/IMainFormForTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public interface IMainFormForTools : IDialogController
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);
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Client.EmuHawk/MainForm.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ private void SavestateCurrentSlot()
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)
Expand Down Expand Up @@ -2522,7 +2522,7 @@ private void SlotStatusButtons_MouseUp(object sender, MouseEventArgs e)
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)
Expand Down
8 changes: 3 additions & 5 deletions src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ private void LoadRom(string filename, string archive = null)
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)
{
Expand Down Expand Up @@ -267,7 +265,7 @@ private void FormDragDrop_internal()
LoadRom(filename, fileInformation.ArchiveName);
break;
case LoadOrdering.State:
LoadStateFile(filename, fileInformation.ArchiveName);
_ = LoadStateFile(filename, fileInformation.ArchiveName);
break;
case LoadOrdering.Watch:
LoadWatch(filename, fileInformation.ArchiveName);
Expand Down
6 changes: 3 additions & 3 deletions src/BizHawk.Client.EmuHawk/MainForm.Hotkey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void SelectAndSaveToSlot(int slot)
}
void SelectAndLoadFromSlot(int slot)
{
LoadQuickSave(slot);
_ = LoadQuickSave(slot);
Config.SaveSlot = slot;
UpdateStatusSlots();
}
Expand Down Expand Up @@ -63,7 +63,7 @@ void ToggleGambatteSyncSetting(
HardReset();
break;
case "Quick Load":
LoadstateCurrentSlot();
_ = LoadstateCurrentSlot();
break;
case "Quick Save":
SavestateCurrentSlot();
Expand Down Expand Up @@ -255,7 +255,7 @@ void ToggleGambatteSyncSetting(
SaveStateAs();
break;
case "Load Named State":
LoadStateAs();
_ = LoadStateAs();
break;
case "Previous Slot":
PreviousSlot();
Expand Down
69 changes: 23 additions & 46 deletions src/BizHawk.Client.EmuHawk/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -658,15 +658,17 @@ _argParser.SocketAddress is var (socketIP, socketPort)
{
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();
}
}

Expand Down Expand Up @@ -3958,7 +3960,7 @@ private bool LoadRomInternal(string path, LoadRomArgs args, out bool failureIsFr
Tools.UpdateCheatRelatedTools(null, null);
if (!MovieSession.NewMovieQueued && Config.AutoLoadLastSaveSlot && HasSlot(Config.SaveSlot))
{
LoadstateCurrentSlot();
_ = LoadstateCurrentSlot();
}

if (FirmwareManager.RecentlyServed.Count > 0)
Expand Down Expand Up @@ -4201,23 +4203,15 @@ private int SlotToInt(string slot)
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();
Expand Down Expand Up @@ -4246,36 +4240,27 @@ public void LoadState(string path, string userFriendlyStateName, bool suppressOS
{
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)
Expand Down Expand Up @@ -4434,25 +4419,17 @@ private void SaveStateAs()
}
}

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)
Expand Down
6 changes: 3 additions & 3 deletions src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private void PlayBestButton_Click(object sender, EventArgs e)

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;
Expand Down Expand Up @@ -904,7 +904,7 @@ private void Update(bool fast)
reset_curent(Attempts);
_doNotUpdateValues = true;
PressButtons(true);
MainForm.LoadQuickSave(SelectedSlot, true);
_ = MainForm.LoadQuickSave(SelectedSlot, true);
_lastFrameAdvanced = Emulator.Frame;
_doNotUpdateValues = false;
return;
Expand Down Expand Up @@ -1034,7 +1034,7 @@ private void StartBot()

_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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ public void RebootCore()
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;
Expand Down
Loading

0 comments on commit ec6fe5f

Please sign in to comment.