Skip to content

Commit

Permalink
Make Lua savestate.* functions return false when C# call throws
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Feb 28, 2025
1 parent 6b4d0fa commit f409cc4
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions src/BizHawk.Client.Common/lua/CommonLibs/SaveStateLuaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ public bool Load(string path, bool suppressOSD = false)
}

_luaLibsImpl.IsUpdateSupressed = true;
var success = APIs.SaveState.Load(path, suppressOSD);
_luaLibsImpl.IsUpdateSupressed = false;
return success;
try
{
return APIs.SaveState.Load(path, suppressOSD);
}
catch
{
return false;
}
finally
{
_luaLibsImpl.IsUpdateSupressed = false;
}
}

[LuaMethodExample("savestate.loadslot( 7 );")]
Expand All @@ -32,33 +41,56 @@ public bool LoadSlot(int slotNum, bool suppressOSD = false)
}

_luaLibsImpl.IsUpdateSupressed = true;
var success = APIs.SaveState.LoadSlot(slotNum, suppressOSD: suppressOSD);
_luaLibsImpl.IsUpdateSupressed = false;
return success;
try
{
return APIs.SaveState.LoadSlot(slotNum, suppressOSD: suppressOSD);
}
catch
{
return false;
}
finally
{
_luaLibsImpl.IsUpdateSupressed = false;
}
}

[LuaMethodExample("savestate.save( \"C:\\state.bin\" );")]
[LuaMethod("save", "Saves a state at 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 Save(string path, bool suppressOSD = false)
public bool Save(string path, bool suppressOSD = false)
{
if (_luaLibsImpl.IsInInputOrMemoryCallback)
{
throw new InvalidOperationException("savestate.save() is not allowed during input/memory callbacks");
}

APIs.SaveState.Save(path, suppressOSD);
try
{
APIs.SaveState.Save(path, suppressOSD);
return true;
}
catch
{
return false;
}
}

[LuaMethodExample("savestate.saveslot( 7 );")]
[LuaMethod("saveslot", "Saves a state at the given save slot (must be an integer between 1 and 10). If EmuHawk is deferring quicksaves, to TAStudio for example, that form will do what it likes with the slot number.")]
public void SaveSlot(int slotNum, bool suppressOSD = false)
public bool SaveSlot(int slotNum, bool suppressOSD = false)
{
if (_luaLibsImpl.IsInInputOrMemoryCallback)
{
throw new InvalidOperationException("savestate.saveslot() is not allowed during input/memory callbacks");
}

APIs.SaveState.SaveSlot(slotNum, suppressOSD);
try
{
APIs.SaveState.SaveSlot(slotNum, suppressOSD);
return true;
}
catch
{
return false;
}
}
}
}

0 comments on commit f409cc4

Please sign in to comment.