Skip to content

usable movie sram #4337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/BizHawk.Client.Common/savestates/BinaryStateLump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public class BinaryStateLump
public static BinaryStateLump SyncSettings { get; private set; }

// TasMovie
[Name("LagLog")]
[Name("LagLog", ".json")]
public static BinaryStateLump LagLog { get; private set; }
[Name("GreenZone")]
public static BinaryStateLump StateHistory { get; private set; }
[Name("GreenZoneSettings", "txt")]
[Name("GreenZoneSettings", "json")]
public static BinaryStateLump StateHistorySettings { get; private set; }
[Name("Markers", "txt")]
public static BinaryStateLump Markers { get; private set; }
Expand All @@ -46,7 +46,7 @@ public class BinaryStateLump
public static BinaryStateLump VerificationLog { get; private set; }
[Name("UserData", "txt")]
public static BinaryStateLump UserData { get; private set; }
[Name("Session", "txt")]
[Name("Session", "json")]
public static BinaryStateLump Session { get; private set; }

// branch stuff
Expand Down Expand Up @@ -82,8 +82,7 @@ public NameAttribute(string name, string ext)
}
}

public string ReadName => Name;
public string WriteName => Ext != null ? Name + '.' + Ext : Name;
public string FileName => Ext != null ? Name + '.' + Ext : Name;

public string Name { get; protected set; }
public string Ext { get; protected set; }
Expand Down
45 changes: 30 additions & 15 deletions src/BizHawk.Client.Common/savestates/ZipStateLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ private void PopulateEntries()
_entriesByName = new Dictionary<string, ZipArchiveEntry>();
foreach (var z in _zip.Entries)
{
string name = z.FullName;
int i;
if ((i = name.LastIndexOf('.')) != -1)
{
name = name.Substring(0, i);
}
string name = z.FullName.Replace('\\', '/');
int i = name.IndexOf('.');
if (i != -1) name = name.Substring(0, i);

// .zst compression is optional, but loader needs to know if it was used
if (z.FullName.EndsWith(".zst")) name += ".zst";

_entriesByName.Add(name.Replace('\\', '/'), z);
if (_entriesByName.ContainsKey(name)) throw new Exception($"Duplicate file found in zip archive: {name}. Please delete one.");
_entriesByName.Add(name, z);
}
}

Expand All @@ -91,14 +92,14 @@ public static ZipStateLoader LoadAndDetect(string filename, bool isMovieLoad = f
ret.PopulateEntries();
if (isMovieLoad)
{
if (!ret.GetLump(BinaryStateLump.ZipVersion, false, ret.ReadZipVersion, false))
if (!ret.GetLump(BinaryStateLump.ZipVersion, false, ret.ReadZipVersion))
{
// movies before 1.0.2 did not include the BizState 1.0 file, don't strictly error in this case
ret._ver = new Version(1, 0, 0);
Console.WriteLine("Read a zipstate of version {0}", ret._ver);
}
}
else if (!ret.GetLump(BinaryStateLump.ZipVersion, false, ret.ReadZipVersion, false))
else if (!ret.GetLump(BinaryStateLump.ZipVersion, false, ret.ReadZipVersion))
{
ret._zip.Dispose();
return null;
Expand All @@ -115,16 +116,30 @@ public static ZipStateLoader LoadAndDetect(string filename, bool isMovieLoad = f
/// <param name="lump">lump to retrieve</param>
/// <param name="abort">pass true to throw exception instead of returning false</param>
/// <param name="callback">function to call with the desired stream</param>
/// <param name="isZstdCompressed">lump is zstd compressed</param>
/// <returns>true iff stream was loaded</returns>
/// <exception cref="Exception">stream not found and <paramref name="abort"/> is <see langword="true"/></exception>
public bool GetLump(BinaryStateLump lump, bool abort, Action<Stream, long> callback, bool isZstdCompressed = true)
public bool GetLump(BinaryStateLump lump, bool abort, Action<Stream, long> callback)
{
if (_entriesByName.TryGetValue(lump.ReadName, out var e))
bool fileFound = _entriesByName.TryGetValue(lump.Name, out var e);
bool isZstdCompressed = false;
if (!fileFound)
{
isZstdCompressed = fileFound = _entriesByName.TryGetValue(lump.Name + ".zst", out e);
}

if (fileFound)
{
// In version 1.0.2, we did not use .zst to mark zstd compression (earlier versions had no zstd)
// These particular files/exensions were zstd compressed:
if (_ver == new Version(1, 0, 2) &&
(lump.Ext == "bin" || lump.Ext == "bmp" || lump.Name == "Greenzone"))
{
isZstdCompressed = true;
}

using var zs = e.Open();

if (isZstdCompressed && _ver.Build > 1)
if (isZstdCompressed)
{
using var z = _zstd.CreateZstdDecompressionStream(zs);
callback(z, e.Length);
Expand All @@ -139,7 +154,7 @@ public bool GetLump(BinaryStateLump lump, bool abort, Action<Stream, long> callb

if (abort)
{
throw new Exception($"Essential zip section not found: {lump.ReadName}");
throw new Exception($"Essential zip section not found: {lump.FileName}");
}

return false;
Expand All @@ -149,7 +164,7 @@ public bool GetLump(BinaryStateLump lump, bool abort, Action<BinaryReader> callb
=> GetLump(lump, abort, (s, _) => callback(new(s)));

public bool GetLump(BinaryStateLump lump, bool abort, Action<TextReader> callback)
=> GetLump(lump, abort, (s, _) => callback(new StreamReader(s)), false);
=> GetLump(lump, abort, (s, _) => callback(new StreamReader(s)));

/// <exception cref="Exception">couldn't find Binary or Text savestate</exception>
public void GetCoreState(Action<BinaryReader> callbackBinary, Action<TextReader> callbackText)
Expand Down
14 changes: 10 additions & 4 deletions src/BizHawk.Client.Common/savestates/ZipStateSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ZipStateSaver : IDisposable
private static void WriteZipVersion(Stream s)
{
using var sw = new StreamWriter(s);
sw.WriteLine("2"); // version 1.0.2
sw.WriteLine("3"); // version 1.0.3
}

private static void WriteEmuVersion(Stream s)
Expand All @@ -33,7 +33,14 @@ public ZipStateSaver(string path, int compressionLevel)

public void PutLump(BinaryStateLump lump, Action<Stream> callback, bool zstdCompress = true)
{
_zip.WriteItem(lump.WriteName, callback, zstdCompress);
if (zstdCompress)
{
_zip.WriteItem(lump.FileName + ".zst", callback, true);
}
else
{
_zip.WriteItem(lump.FileName, callback, false);
}
}

public void PutLump(BinaryStateLump lump, Action<BinaryWriter> callback)
Expand All @@ -48,13 +55,12 @@ public void PutLump(BinaryStateLump lump, Action<BinaryWriter> callback)

public void PutLump(BinaryStateLump lump, Action<TextWriter> callback)
{
// don't zstd compress text, as it's annoying for users
PutLump(lump, s =>
{
TextWriter tw = new StreamWriter(s);
callback(tw);
tw.Flush();
}, false);
});
}

public void Dispose()
Expand Down