2015-09-10 21:36:20 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using LuaInterface ;
2016-12-04 18:08:21 +00:00
using BizHawk.Emulation.Common ;
using BizHawk.Emulation.Common.IEmulatorExtensions ;
2015-09-10 21:36:20 +00:00
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 { get { return "memorysavestate" ; } }
private readonly Dictionary < Guid , byte [ ] > MemorySavestates = new Dictionary < Guid , byte [ ] > ( ) ;
2016-12-04 18:08:21 +00:00
[RequiredService]
private IStatable _statableCore { get ; set ; }
2015-09-10 21:36:20 +00:00
[ LuaMethodAttributes (
"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"
) ]
2016-02-08 07:28:26 +00:00
public string SaveCoreStateToMemory ( )
2015-09-10 21:36:20 +00:00
{
2016-12-04 18:08:21 +00:00
var guid = Guid . NewGuid ( ) ;
var bytes = ( byte [ ] ) _statableCore . SaveStateBinary ( ) . Clone ( ) ;
2015-09-10 21:36:20 +00:00
2016-12-04 18:08:21 +00:00
MemorySavestates . Add ( guid , bytes ) ;
2016-02-08 07:28:26 +00:00
2016-12-04 18:08:21 +00:00
return guid . ToString ( ) ;
2015-09-10 21:36:20 +00:00
}
[ LuaMethodAttributes (
"loadcorestate" ,
"loads an in memory state with the given identifier"
) ]
public void LoadCoreStateFromMemory ( string identifier )
{
var guid = new Guid ( identifier ) ;
2016-12-04 18:08:21 +00:00
try
2015-09-10 21:36:20 +00:00
{
2016-12-04 18:08:21 +00:00
var state = MemorySavestates [ guid ] ;
2015-09-10 21:36:20 +00:00
2016-12-04 18:08:21 +00:00
using ( MemoryStream ms = new MemoryStream ( state ) )
using ( BinaryReader br = new BinaryReader ( ms ) )
2015-09-10 21:36:20 +00:00
{
2016-12-04 18:08:21 +00:00
_statableCore . LoadStateBinary ( br ) ;
2015-09-10 21:36:20 +00:00
}
}
2016-12-04 18:08:21 +00:00
catch
2015-09-10 21:36:20 +00:00
{
2016-12-04 18:08:21 +00:00
Log ( "Unable to find the given savestate in memory" ) ;
2015-09-10 21:36:20 +00:00
}
}
[ LuaMethodAttributes (
"removestate" ,
"removes the savestate with the given identifier from memory"
) ]
public void DeleteState ( string identifier )
{
var guid = new Guid ( identifier ) ;
MemorySavestates . Remove ( guid ) ;
}
[ LuaMethodAttributes (
"clearstatesfrommemory" ,
"clears all savestates stored in memory"
) ]
public void ClearInMemoryStates ( )
{
MemorySavestates . Clear ( ) ;
}
}
}