Use SharpZipLib for lsmv import instead of hawkfile (#2589)

Use the same library and ideas that savestate and movie loading use.
This commit is contained in:
nattthebear 2021-01-21 07:50:50 -05:00 committed by GitHub
parent 99d027cc63
commit dbe0229810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 53 deletions

View File

@ -6,6 +6,7 @@ using BizHawk.Common.IOExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores; using BizHawk.Emulation.Cores;
using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Emulation.Cores.Nintendo.SNES;
using ICSharpCode.SharpZipLib.Zip;
namespace BizHawk.Client.Common.movie.import namespace BizHawk.Client.Common.movie.import
{ {
@ -14,20 +15,26 @@ namespace BizHawk.Client.Common.movie.import
[ImporterFor("LSNES", ".lsmv")] [ImporterFor("LSNES", ".lsmv")]
internal class LsmvImport : MovieImporter internal class LsmvImport : MovieImporter
{ {
private static readonly byte[] Zipheader = { 0x50, 0x4b, 0x03, 0x04 };
private LibsnesControllerDeck _deck; private LibsnesControllerDeck _deck;
protected override void RunImport() protected override void RunImport()
{ {
Result.Movie.HeaderEntries[HeaderKeys.Core] = CoreNames.Bsnes; Result.Movie.HeaderEntries[HeaderKeys.Core] = CoreNames.Bsnes;
var hf = new HawkFile(SourceFile.FullName);
// .LSMV movies are .zip files containing data files. // .LSMV movies are .zip files containing data files.
if (!hf.IsArchive) using (var fs = new FileStream(SourceFile.FullName, FileMode.Open, FileAccess.Read))
{ {
Result.Errors.Add("This is not an archive."); byte[] data = new byte[4];
return; fs.Read(data, 0, 4);
if (!data.SequenceEqual(Zipheader))
{
Result.Errors.Add("This is not a zip file.");
return;
}
} }
using var zip = new ZipFile(SourceFile.FullName);
var ss = new LibsnesCore.SnesSyncSettings var ss = new LibsnesCore.SnesSyncSettings
{ {
LeftPort = LibsnesControllerDeck.ControllerType.Gamepad, LeftPort = LibsnesControllerDeck.ControllerType.Gamepad,
@ -37,12 +44,11 @@ namespace BizHawk.Client.Common.movie.import
string platform = "SNES"; string platform = "SNES";
foreach (var item in hf.ArchiveItems) foreach (ZipEntry item in zip)
{ {
if (item.Name == "authors") if (item.Name == "authors")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string authors = Encoding.UTF8.GetString(stream.ReadAllBytes()); string authors = Encoding.UTF8.GetString(stream.ReadAllBytes());
string authorList = ""; string authorList = "";
string authorLast = ""; string authorLast = "";
@ -77,28 +83,22 @@ namespace BizHawk.Client.Common.movie.import
} }
Result.Movie.HeaderEntries[HeaderKeys.Author] = authorList; Result.Movie.HeaderEntries[HeaderKeys.Author] = authorList;
hf.Unbind();
} }
else if (item.Name == "coreversion") else if (item.Name == "coreversion")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string coreVersion = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string coreVersion = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.Comments.Add($"CoreOrigin {coreVersion}"); Result.Movie.Comments.Add($"CoreOrigin {coreVersion}");
hf.Unbind();
} }
else if (item.Name == "gamename") else if (item.Name == "gamename")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string gameName = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string gameName = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.HeaderEntries[HeaderKeys.GameName] = gameName; Result.Movie.HeaderEntries[HeaderKeys.GameName] = gameName;
hf.Unbind();
} }
else if (item.Name == "gametype") else if (item.Name == "gametype")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string gametype = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string gametype = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
// TODO: Handle the other types. // TODO: Handle the other types.
@ -120,12 +120,10 @@ namespace BizHawk.Client.Common.movie.import
bool pal = gametype == "snes_pal" || gametype == "sgb_pal"; bool pal = gametype == "snes_pal" || gametype == "sgb_pal";
Result.Movie.HeaderEntries[HeaderKeys.Pal] = pal.ToString(); Result.Movie.HeaderEntries[HeaderKeys.Pal] = pal.ToString();
hf.Unbind();
} }
else if (item.Name == "input") else if (item.Name == "input")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string input = Encoding.UTF8.GetString(stream.ReadAllBytes()); string input = Encoding.UTF8.GetString(stream.ReadAllBytes());
int lineNum = 0; int lineNum = 0;
@ -156,53 +154,43 @@ namespace BizHawk.Client.Common.movie.import
ImportTextFrame(line, platform); ImportTextFrame(line, platform);
} }
} }
hf.Unbind();
} }
else if (item.Name.StartsWith("moviesram.")) else if (item.Name.StartsWith("moviesram."))
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
byte[] movieSram = stream.ReadAllBytes(); byte[] movieSram = stream.ReadAllBytes();
if (movieSram.Length != 0) if (movieSram.Length != 0)
{ {
// TODO: Why don't we support this?
Result.Errors.Add("Movies that begin with SRAM are not supported."); Result.Errors.Add("Movies that begin with SRAM are not supported.");
hf.Unbind();
return; return;
} }
} }
else if (item.Name == "port1") else if (item.Name == "port1")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string port1 = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string port1 = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.HeaderEntries["port1"] = port1; Result.Movie.HeaderEntries["port1"] = port1;
ss.LeftPort = LibsnesControllerDeck.ControllerType.Gamepad; ss.LeftPort = LibsnesControllerDeck.ControllerType.Gamepad;
_deck = new LibsnesControllerDeck(ss); _deck = new LibsnesControllerDeck(ss);
hf.Unbind();
} }
else if (item.Name == "port2") else if (item.Name == "port2")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string port2 = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string port2 = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.HeaderEntries["port2"] = port2; Result.Movie.HeaderEntries["port2"] = port2;
ss.RightPort = LibsnesControllerDeck.ControllerType.Gamepad; ss.RightPort = LibsnesControllerDeck.ControllerType.Gamepad;
_deck = new LibsnesControllerDeck(ss); _deck = new LibsnesControllerDeck(ss);
hf.Unbind();
} }
else if (item.Name == "projectid") else if (item.Name == "projectid")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string projectId = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string projectId = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.HeaderEntries["ProjectID"] = projectId; Result.Movie.HeaderEntries["ProjectID"] = projectId;
hf.Unbind();
} }
else if (item.Name == "rerecords") else if (item.Name == "rerecords")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string rerecords = Encoding.UTF8.GetString(stream.ReadAllBytes()); string rerecords = Encoding.UTF8.GetString(stream.ReadAllBytes());
int rerecordCount; int rerecordCount;
@ -217,17 +205,14 @@ namespace BizHawk.Client.Common.movie.import
} }
Result.Movie.Rerecords = (ulong)rerecordCount; Result.Movie.Rerecords = (ulong)rerecordCount;
hf.Unbind();
} }
else if (item.Name.EndsWith(".sha256")) else if (item.Name.EndsWith(".sha256"))
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string rom = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string rom = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
int pos = item.Name.LastIndexOf(".sha256"); int pos = item.Name.LastIndexOf(".sha256");
string name = item.Name.Substring(0, pos); string name = item.Name.Substring(0, pos);
Result.Movie.HeaderEntries[$"SHA256_{name}"] = rom; Result.Movie.HeaderEntries[$"SHA256_{name}"] = rom;
hf.Unbind();
} }
else if (item.Name == "savestate") else if (item.Name == "savestate")
{ {
@ -236,8 +221,7 @@ namespace BizHawk.Client.Common.movie.import
} }
else if (item.Name == "subtitles") else if (item.Name == "subtitles")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string subtitles = Encoding.UTF8.GetString(stream.ReadAllBytes()); string subtitles = Encoding.UTF8.GetString(stream.ReadAllBytes());
using (var reader = new StringReader(subtitles)) using (var reader = new StringReader(subtitles))
{ {
@ -251,32 +235,24 @@ namespace BizHawk.Client.Common.movie.import
} }
} }
} }
hf.Unbind();
} }
else if (item.Name == "starttime.second") else if (item.Name == "starttime.second")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string startSecond = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string startSecond = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.HeaderEntries["StartSecond"] = startSecond; Result.Movie.HeaderEntries["StartSecond"] = startSecond;
hf.Unbind();
} }
else if (item.Name == "starttime.subsecond") else if (item.Name == "starttime.subsecond")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string startSubSecond = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string startSubSecond = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.HeaderEntries["StartSubSecond"] = startSubSecond; Result.Movie.HeaderEntries["StartSubSecond"] = startSubSecond;
hf.Unbind();
} }
else if (item.Name == "systemid") else if (item.Name == "systemid")
{ {
hf.BindArchiveMember(item.Index); using var stream = zip.GetInputStream(item);
var stream = hf.GetStream();
string systemId = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); string systemId = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim();
Result.Movie.Comments.Add($"{EmulationOrigin} {systemId}"); Result.Movie.Comments.Add($"{EmulationOrigin} {systemId}");
hf.Unbind();
} }
} }