add an IStatable extension method that can load a state directly from a byte array, reduces the stream creation boiler plate in a number of palces

This commit is contained in:
adelikat 2020-04-18 10:26:08 -05:00
parent a460846ff8
commit 9494243eef
5 changed files with 14 additions and 9 deletions

View File

@ -34,9 +34,7 @@ namespace BizHawk.Client.Common
var guid = new Guid(identifier);
try
{
using var ms = new MemoryStream(_memorySavestates[guid]);
using var br = new BinaryReader(ms);
StatableCore.LoadStateBinary(br);
StatableCore.LoadStateBinary(_memorySavestates[guid]);
}
catch
{

View File

@ -371,8 +371,7 @@ namespace BizHawk.Client.Common
}
}
using var lastStateReader = new BinaryReader(new MemoryStream(_lastState));
Global.Emulator.AsStatable().LoadStateBinary(lastStateReader);
Global.Emulator.AsStatable().LoadStateBinary(_lastState);
}
else
{

View File

@ -39,7 +39,7 @@ namespace BizHawk.Client.EmuHawk
}
else
{
Emulator.AsStatable().LoadStateBinary(new BinaryReader(new MemoryStream(movie.BinarySavestate, false)));
Emulator.AsStatable().LoadStateBinary(movie.BinarySavestate);
}
if (movie.SavestateFramebuffer != null && Emulator.HasVideoProvider())

View File

@ -1070,9 +1070,7 @@ namespace BizHawk.Client.EmuHawk
public void LoadState(KeyValuePair<int, byte[]> state)
{
using var ms = new MemoryStream(state.Value);
using var br = new BinaryReader(ms);
StatableEmulator.LoadStateBinary(br);
StatableEmulator.LoadStateBinary(state.Value);
if (state.Key == 0 && CurrentTasMovie.StartsFromSavestate)
{

View File

@ -72,5 +72,15 @@ namespace BizHawk.Emulation.Common
core.LoadStateBinary(br);
}
}
/// <summary>
/// Loads a state directly from a byte array
/// </summary>
public static void LoadStateBinary(this IStatable core, byte[] state)
{
using var ms = new MemoryStream(state, false);
using var br = new BinaryReader(ms);
core.LoadStateBinary(br);
}
}
}