2017-04-14 19:59:01 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using LuaInterface ;
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
[ LuaMethodAttributes (
2017-05-09 18:19:55 +00:00
"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 ( ) ;
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("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" ) ;
}
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("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
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("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
}
}
}