2017-04-14 19:59:01 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2017-07-10 04:51:02 +00:00
using NLua ;
2017-04-14 19:59:01 +00:00
using BizHawk.Emulation.Common ;
namespace BizHawk.Client.Common
{
public sealed class MemorySavestateEmuLuaLibrary : LuaLibraryBase
{
public MemorySavestateEmuLuaLibrary ( Lua lua )
: base ( lua ) { }
public MemorySavestateEmuLuaLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
public override string Name = > "memorysavestate" ;
[RequiredService]
2017-05-19 18:17:07 +00:00
private IStatable StatableCore { get ; set ; }
private readonly Dictionary < Guid , byte [ ] > _memorySavestates = new Dictionary < Guid , byte [ ] > ( ) ;
2017-04-14 19:59:01 +00:00
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local mmsvstsvcst = memorysavestate.savecorestate( );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("savecorestate", "creates a core savestate and stores it in memory. Note: a core savestate is only the raw data from the core, and not extras such as movie input logs, or framebuffers. Returns a unique identifer for the savestate")]
2017-04-14 19:59:01 +00:00
public string SaveCoreStateToMemory ( )
{
var guid = Guid . NewGuid ( ) ;
2017-05-19 18:17:07 +00:00
var bytes = ( byte [ ] ) StatableCore . SaveStateBinary ( ) . Clone ( ) ;
2017-04-14 19:59:01 +00:00
2017-05-19 18:17:07 +00:00
_memorySavestates . Add ( guid , bytes ) ;
2016-02-08 07:28:26 +00:00
2017-04-14 19:59:01 +00:00
return guid . ToString ( ) ;
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("memorysavestate.loadcorestate( \"3fcf120f-0778-43fd-b2c5-460fb7d34184\" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("loadcorestate", "loads an in memory state with the given identifier")]
2017-04-14 19:59:01 +00:00
public void LoadCoreStateFromMemory ( string identifier )
{
var guid = new Guid ( identifier ) ;
try
{
2017-05-19 18:17:07 +00:00
var state = _memorySavestates [ guid ] ;
2017-04-14 19:59:01 +00:00
using ( var ms = new MemoryStream ( state ) )
using ( var br = new BinaryReader ( ms ) )
{
2017-05-19 18:17:07 +00:00
StatableCore . LoadStateBinary ( br ) ;
2017-04-14 19:59:01 +00:00
}
}
catch
{
Log ( "Unable to find the given savestate in memory" ) ;
}
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("memorysavestate.removestate( \"3fcf120f-0778-43fd-b2c5-460fb7d34184\" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("removestate", "removes the savestate with the given identifier from memory")]
2017-04-14 19:59:01 +00:00
public void DeleteState ( string identifier )
{
var guid = new Guid ( identifier ) ;
2017-05-19 18:17:07 +00:00
_memorySavestates . Remove ( guid ) ;
2017-04-14 19:59:01 +00:00
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("memorysavestate.clearstatesfrommemory( );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("clearstatesfrommemory", "clears all savestates stored in memory")]
2017-04-14 19:59:01 +00:00
public void ClearInMemoryStates ( )
{
2017-05-19 18:17:07 +00:00
_memorySavestates . Clear ( ) ;
2017-04-14 19:59:01 +00:00
}
}
}