meh, make less copy/pasta in Bk2/Tasproj Write() methods. This is a less overall code, potentially reads better

This commit is contained in:
adelikat 2020-06-01 20:38:50 -05:00
parent bac29fe187
commit ed9a8ec877
2 changed files with 33 additions and 51 deletions

View File

@ -25,12 +25,6 @@ namespace BizHawk.Client.Common
backupName = backupName.Insert(Filename.LastIndexOf("."), $".{DateTime.Now:yyyy-MM-dd HH.mm.ss}");
backupName = Path.Combine(backupDir, Path.GetFileName(backupName));
var directoryInfo = new FileInfo(backupName).Directory;
if (directoryInfo != null)
{
Directory.CreateDirectory(directoryInfo.FullName);
}
Write(backupName, isBackup: true);
}
@ -162,6 +156,20 @@ namespace BizHawk.Client.Common
}
protected virtual void Write(string fn, bool isBackup = false)
{
SetCycleValues(); // We are pretending these only need to be set on save
CreateDirectoryIfNotExists(fn);
using var bs = new ZipStateSaver(fn, Global.Config.Savestates.MovieCompressionLevel);
AddLumps(bs);
if (!isBackup)
{
Changes = false;
}
}
private void SetCycleValues()
{
if (Emulator is Emulation.Cores.Nintendo.SubNESHawk.SubNESHawk subNes)
{
@ -175,19 +183,28 @@ namespace BizHawk.Client.Common
{
Header[HeaderKeys.CycleCount] = subGb.CycleCount.ToString();
}
}
private void CreateDirectoryIfNotExists(string fn)
{
var file = new FileInfo(fn);
if (!file.Directory.Exists)
if (file.Directory != null && !file.Directory.Exists)
{
Directory.CreateDirectory(file.Directory.ToString());
}
}
using var bs = new ZipStateSaver(fn, Global.Config.Savestates.MovieCompressionLevel);
protected virtual void AddLumps(ZipStateSaver bs, bool isBackup = false)
{
AddBk2Lumps(bs);
}
protected void AddBk2Lumps(ZipStateSaver bs)
{
bs.PutLump(BinaryStateLump.Movieheader, tw => tw.WriteLine(Header.ToString()));
bs.PutLump(BinaryStateLump.Comments, tw => tw.WriteLine(CommentsString()));
bs.PutLump(BinaryStateLump.Subtitles, tw => tw.WriteLine(Subtitles.ToString()));
bs.PutLump(BinaryStateLump.SyncSettings, tw => tw.WriteLine(_syncSettingsJson));
bs.PutLump(BinaryStateLump.SyncSettings, tw => tw.WriteLine(SyncSettingsJson));
bs.PutLump(BinaryStateLump.Input, WriteInputLog);
if (StartsFromSavestate)
@ -210,11 +227,6 @@ namespace BizHawk.Client.Common
{
bs.PutLump(BinaryStateLump.MovieSaveRam, (BinaryWriter bw) => bw.Write(SaveRam));
}
if (!isBackup)
{
Changes = false;
}
}
protected void ClearBeforeLoad()

View File

@ -10,44 +10,19 @@ namespace BizHawk.Client.Common
public Func<string> ClientSettingsForSave { get; set; }
public Action<string> GetClientSettingsOnLoad { get; set; }
protected override void Write(string fn, bool isBackup = false)
protected override void AddLumps(ZipStateSaver bs, bool isBackup = false)
{
var file = new FileInfo(fn);
if (file.Directory != null && !file.Directory.Exists)
{
Directory.CreateDirectory(file.Directory.ToString());
}
AddBk2Lumps(bs);
AddTasProjLumps(bs, isBackup);
}
using var bs = new ZipStateSaver(fn, Global.Config.Savestates.MovieCompressionLevel);
bs.PutLump(BinaryStateLump.Movieheader, tw => tw.WriteLine(Header.ToString()));
bs.PutLump(BinaryStateLump.Comments, tw => tw.WriteLine(CommentsString()));
bs.PutLump(BinaryStateLump.Subtitles, tw => tw.WriteLine(Subtitles.ToString()));
bs.PutLump(BinaryStateLump.SyncSettings, tw => tw.WriteLine(SyncSettingsJson));
bs.PutLump(BinaryStateLump.Input, WriteInputLog);
// TasProj extras
private void AddTasProjLumps(ZipStateSaver bs, bool isBackup = false)
{
var settings = JsonConvert.SerializeObject(TasStateManager.Settings);
bs.PutLump(BinaryStateLump.StateHistorySettings, tw => tw.WriteLine(settings));
bs.PutLump(BinaryStateLump.LagLog, tw => LagLog.Save(tw));
bs.PutLump(BinaryStateLump.Markers, tw => tw.WriteLine(Markers.ToString()));
if (StartsFromSavestate)
{
if (TextSavestate != null)
{
bs.PutLump(BinaryStateLump.CorestateText, (TextWriter tw) => tw.Write(TextSavestate));
}
else
{
bs.PutLump(BinaryStateLump.Corestate, (BinaryWriter bw) => bw.Write(BinarySavestate));
}
}
else if (StartsFromSaveRam)
{
bs.PutLump(BinaryStateLump.MovieSaveRam, (BinaryWriter bw) => bw.Write(SaveRam));
}
if (ClientSettingsForSave != null)
{
var clientSettingsJson = ClientSettingsForSave();
@ -70,11 +45,6 @@ namespace BizHawk.Client.Common
{
bs.PutLump(BinaryStateLump.StateHistory, bw => TasStateManager.Save(bw));
}
if (!isBackup)
{
Changes = false;
}
}
public override bool Load(bool preload)