move SaveStateManager to savestates folder

This commit is contained in:
adelikat 2020-05-31 10:28:57 -05:00
parent a0680e4b94
commit 3b27eb5e91
1 changed files with 204 additions and 204 deletions

View File

@ -1,204 +1,204 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public static class SavestateManager public static class SavestateManager
{ {
public static void SaveStateFile(IEmulator emulator, string filename) public static void SaveStateFile(IEmulator emulator, string filename)
{ {
var core = emulator.AsStatable(); var core = emulator.AsStatable();
// the old method of text savestate save is now gone. // the old method of text savestate save is now gone.
// a text savestate is just like a binary savestate, but with a different core lump // a text savestate is just like a binary savestate, but with a different core lump
using var bs = new ZipStateSaver(filename, Global.Config.SaveStateCompressionLevelNormal); using var bs = new ZipStateSaver(filename, Global.Config.SaveStateCompressionLevelNormal);
bs.PutVersionLumps(); bs.PutVersionLumps();
if (Global.Config.SaveStateType == SaveStateTypeE.Text) if (Global.Config.SaveStateType == SaveStateTypeE.Text)
{ {
// text savestate format // text savestate format
using (new SimpleTime("Save Core")) using (new SimpleTime("Save Core"))
{ {
bs.PutLump(BinaryStateLump.CorestateText, tw => core.SaveStateText(tw)); bs.PutLump(BinaryStateLump.CorestateText, tw => core.SaveStateText(tw));
} }
} }
else else
{ {
// binary core lump format // binary core lump format
using (new SimpleTime("Save Core")) using (new SimpleTime("Save Core"))
{ {
bs.PutLump(BinaryStateLump.Corestate, bw => core.SaveStateBinary(bw)); bs.PutLump(BinaryStateLump.Corestate, bw => core.SaveStateBinary(bw));
} }
} }
if (Global.Config.SaveScreenshotWithStates && emulator.HasVideoProvider()) if (Global.Config.SaveScreenshotWithStates && emulator.HasVideoProvider())
{ {
var vp = emulator.AsVideoProvider(); var vp = emulator.AsVideoProvider();
var buff = vp.GetVideoBuffer(); var buff = vp.GetVideoBuffer();
if (buff.Length == 1) if (buff.Length == 1)
{ {
// is a hacky opengl texture ID. can't handle this now! // is a hacky opengl texture ID. can't handle this now!
// need to discuss options // need to discuss options
// 1. cores must be able to provide a pixels VideoProvider in addition to a texture ID, on command (not very hard overall but interface changing and work per core) // 1. cores must be able to provide a pixels VideoProvider in addition to a texture ID, on command (not very hard overall but interface changing and work per core)
// 2. SavestateManager must be setup with a mechanism for resolving texture IDs (even less work, but sloppy) // 2. SavestateManager must be setup with a mechanism for resolving texture IDs (even less work, but sloppy)
// There are additional problems with AVWriting. They depend on VideoProvider providing pixels. // There are additional problems with AVWriting. They depend on VideoProvider providing pixels.
} }
else else
{ {
int outWidth = vp.BufferWidth; int outWidth = vp.BufferWidth;
int outHeight = vp.BufferHeight; int outHeight = vp.BufferHeight;
// if buffer is too big, scale down screenshot // if buffer is too big, scale down screenshot
if (!Global.Config.NoLowResLargeScreenshotWithStates && buff.Length >= Global.Config.BigScreenshotSize) if (!Global.Config.NoLowResLargeScreenshotWithStates && buff.Length >= Global.Config.BigScreenshotSize)
{ {
outWidth /= 2; outWidth /= 2;
outHeight /= 2; outHeight /= 2;
} }
using (new SimpleTime("Save Framebuffer")) using (new SimpleTime("Save Framebuffer"))
{ {
bs.PutLump(BinaryStateLump.Framebuffer, s => QuickBmpFile.Save(emulator.AsVideoProvider(), s, outWidth, outHeight)); bs.PutLump(BinaryStateLump.Framebuffer, s => QuickBmpFile.Save(emulator.AsVideoProvider(), s, outWidth, outHeight));
} }
} }
} }
if (Global.MovieSession.Movie.IsActive()) if (Global.MovieSession.Movie.IsActive())
{ {
bs.PutLump(BinaryStateLump.Input, bs.PutLump(BinaryStateLump.Input,
delegate(TextWriter tw) delegate(TextWriter tw)
{ {
// this never should have been a core's responsibility // this never should have been a core's responsibility
tw.WriteLine("Frame {0}", emulator.Frame); tw.WriteLine("Frame {0}", emulator.Frame);
Global.MovieSession.HandleSaveState(tw); Global.MovieSession.HandleSaveState(tw);
}); });
} }
if (Global.UserBag.Any()) if (Global.UserBag.Any())
{ {
bs.PutLump(BinaryStateLump.UserData, bs.PutLump(BinaryStateLump.UserData,
delegate(TextWriter tw) delegate(TextWriter tw)
{ {
var data = ConfigService.SaveWithType(Global.UserBag); var data = ConfigService.SaveWithType(Global.UserBag);
tw.WriteLine(data); tw.WriteLine(data);
}); });
} }
if (Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie is ITasMovie) if (Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie is ITasMovie)
{ {
bs.PutLump(BinaryStateLump.LagLog, bs.PutLump(BinaryStateLump.LagLog,
delegate(TextWriter tw) delegate(TextWriter tw)
{ {
((ITasMovie)Global.MovieSession.Movie).LagLog.Save(tw); ((ITasMovie)Global.MovieSession.Movie).LagLog.Save(tw);
}); });
} }
} }
public static bool LoadStateFile(IEmulator emulator, string path) public static bool LoadStateFile(IEmulator emulator, string path)
{ {
var core = emulator.AsStatable(); var core = emulator.AsStatable();
// try to detect binary first // try to detect binary first
var bl = ZipStateLoader.LoadAndDetect(path); var bl = ZipStateLoader.LoadAndDetect(path);
if (bl != null) if (bl != null)
{ {
try try
{ {
var succeed = false; var succeed = false;
// Movie timeline check must happen before the core state is loaded // Movie timeline check must happen before the core state is loaded
if (Global.MovieSession.Movie.IsActive()) if (Global.MovieSession.Movie.IsActive())
{ {
bl.GetLump(BinaryStateLump.Input, true, tr => succeed = Global.MovieSession.CheckSavestateTimeline(tr)); bl.GetLump(BinaryStateLump.Input, true, tr => succeed = Global.MovieSession.CheckSavestateTimeline(tr));
if (!succeed) if (!succeed)
{ {
return false; return false;
} }
} }
using (new SimpleTime("Load Core")) using (new SimpleTime("Load Core"))
{ {
bl.GetCoreState(br => core.LoadStateBinary(br), tr => core.LoadStateText(tr)); bl.GetCoreState(br => core.LoadStateBinary(br), tr => core.LoadStateText(tr));
} }
// We must handle movie input AFTER the core is loaded to properly handle mode changes, and input latching // We must handle movie input AFTER the core is loaded to properly handle mode changes, and input latching
if (Global.MovieSession.Movie.IsActive()) if (Global.MovieSession.Movie.IsActive())
{ {
bl.GetLump(BinaryStateLump.Input, true, tr => succeed = Global.MovieSession.HandleLoadState(tr)); bl.GetLump(BinaryStateLump.Input, true, tr => succeed = Global.MovieSession.HandleLoadState(tr));
if (!succeed) if (!succeed)
{ {
return false; return false;
} }
} }
if (emulator.HasVideoProvider()) if (emulator.HasVideoProvider())
{ {
bl.GetLump(BinaryStateLump.Framebuffer, false, br => PopulateFramebuffer(br, emulator.AsVideoProvider())); bl.GetLump(BinaryStateLump.Framebuffer, false, br => PopulateFramebuffer(br, emulator.AsVideoProvider()));
} }
string userData = ""; string userData = "";
bl.GetLump(BinaryStateLump.UserData, false, delegate(TextReader tr) bl.GetLump(BinaryStateLump.UserData, false, delegate(TextReader tr)
{ {
string line; string line;
while ((line = tr.ReadLine()) != null) while ((line = tr.ReadLine()) != null)
{ {
if (!string.IsNullOrWhiteSpace(line)) if (!string.IsNullOrWhiteSpace(line))
{ {
userData = line; userData = line;
} }
} }
}); });
if (!string.IsNullOrWhiteSpace(userData)) if (!string.IsNullOrWhiteSpace(userData))
{ {
Global.UserBag = (Dictionary<string, object>)ConfigService.LoadWithType(userData); Global.UserBag = (Dictionary<string, object>)ConfigService.LoadWithType(userData);
} }
if (Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie is ITasMovie) if (Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie is ITasMovie)
{ {
bl.GetLump(BinaryStateLump.LagLog, false, delegate(TextReader tr) bl.GetLump(BinaryStateLump.LagLog, false, delegate(TextReader tr)
{ {
((ITasMovie)Global.MovieSession.Movie).LagLog.Load(tr); ((ITasMovie)Global.MovieSession.Movie).LagLog.Load(tr);
}); });
} }
} }
finally finally
{ {
bl.Dispose(); bl.Dispose();
} }
return true; return true;
} }
return false; return false;
} }
private static void PopulateFramebuffer(BinaryReader br, IVideoProvider videoProvider) private static void PopulateFramebuffer(BinaryReader br, IVideoProvider videoProvider)
{ {
try try
{ {
using (new SimpleTime("Load Framebuffer")) using (new SimpleTime("Load Framebuffer"))
{ {
QuickBmpFile.Load(videoProvider, br.BaseStream); QuickBmpFile.Load(videoProvider, br.BaseStream);
} }
} }
catch catch
{ {
var buff = videoProvider.GetVideoBuffer(); var buff = videoProvider.GetVideoBuffer();
try try
{ {
for (int i = 0; i < buff.Length; i++) for (int i = 0; i < buff.Length; i++)
{ {
int j = br.ReadInt32(); int j = br.ReadInt32();
buff[i] = j; buff[i] = j;
} }
} }
catch (EndOfStreamException) catch (EndOfStreamException)
{ {
} }
} }
} }
} }
} }