From 032f8a9b2166bc3cf69b7a81077a77c867b31fac Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 13 Jun 2014 11:30:25 +0000 Subject: [PATCH] bk2 progress --- BizHawk.Client.Common/BinarySaveStates.cs | 10 ++- .../movie/bk2/Bk2Movie.HeaderApi.cs | 25 ++++---- .../movie/bk2/Bk2Movie.IO.cs | 61 ++++++++++++++++++- .../movie/bk2/Bk2Movie.InputLog.cs | 19 +++++- 4 files changed, 100 insertions(+), 15 deletions(-) diff --git a/BizHawk.Client.Common/BinarySaveStates.cs b/BizHawk.Client.Common/BinarySaveStates.cs index 61199e4958..f8d7b9498e 100644 --- a/BizHawk.Client.Common/BinarySaveStates.cs +++ b/BizHawk.Client.Common/BinarySaveStates.cs @@ -13,7 +13,11 @@ namespace BizHawk.Client.Common Framebuffer, Input, CorestateText, - Movieheader + + // Only for movies they probably shoudln't be leaching this stuff + Movieheader, + Comments, + Subtitles } public class BinaryStateFileNames @@ -38,6 +42,10 @@ namespace BizHawk.Client.Common LumpNames[BinaryStateLump.Input] = "Input Log"; LumpNames[BinaryStateLump.CorestateText] = "CoreText"; LumpNames[BinaryStateLump.Movieheader] = "Header"; + + // Only for movies they probably shoudln't be leaching this stuff + LumpNames[BinaryStateLump.Comments] = "Comments"; + LumpNames[BinaryStateLump.Subtitles] = "Subtitles"; } public static string Get(BinaryStateLump lump) diff --git a/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs b/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs index 24aa41c1ed..9d7cc454a1 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs @@ -14,17 +14,8 @@ namespace BizHawk.Client.Common get { return Header; } } - public SubtitleList Subtitles - { - get; - private set; - } - - public IList Comments - { - get; - private set; - } + public SubtitleList Subtitles { get; private set; } + public IList Comments { get; private set; } public string SyncSettingsJson { @@ -177,5 +168,17 @@ namespace BizHawk.Client.Common get { return Header[HeaderKeys.FIRMWARESHA1]; } set { Header[HeaderKeys.FIRMWARESHA1] = value; } } + + private string CommentsString() + { + StringBuilder sb = new StringBuilder(); + + foreach(var comment in Comments) + { + sb.AppendLine(comment); + } + + return sb.ToString(); + } } } diff --git a/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs b/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs index 2ac9b172a7..3bb1715331 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; @@ -11,16 +12,50 @@ namespace BizHawk.Client.Common { public void Save() { - throw new NotImplementedException(); + Write(Filename); } public void SaveBackup() { - throw new NotImplementedException(); + if (string.IsNullOrWhiteSpace(Filename)) + { + return; + } + + var backupName = Filename; + backupName = backupName.Insert(Filename.LastIndexOf("."), string.Format(".{0:yyyy-MM-dd HH.mm.ss}", DateTime.Now)); + backupName = Path.Combine(Global.Config.PathEntries["Global", "Movie backups"].Path, Path.GetFileName(backupName)); + + var directory_info = new FileInfo(backupName).Directory; + if (directory_info != null) + { + Directory.CreateDirectory(directory_info.FullName); + } + + Write(backupName); } public bool Load() { + var file = new FileInfo(Filename); + if (!file.Exists) + { + return false; + } + + using (BinaryStateLoader bl = BinaryStateLoader.LoadAndDetect(Filename)) + { + if (bl == null) + { + return false; + } + + Header.Clear(); + _log.Clear(); + Subtitles.Clear(); + Comments.Clear(); + } + throw new NotImplementedException(); } @@ -28,5 +63,27 @@ namespace BizHawk.Client.Common { throw new NotImplementedException(); } + + private void Write(string fn) + { + // Movies 2.0 TODO: Save and Load Movie version + // there's a lot of common code here with SavestateManager. refactor? + using (FileStream fs = new FileStream(Filename, FileMode.Create, FileAccess.Write)) + using (BinaryStateSaver bs = new BinaryStateSaver(fs)) + { + bs.PutLump(BinaryStateLump.Movieheader, (tw) => tw.WriteLine(Header.ToString())); + bs.PutLump(BinaryStateLump.Input, (tw) => tw.WriteLine(RawInputLog())); + + bs.PutLump(BinaryStateLump.Comments, (tw) => tw.WriteLine(CommentsString())); + bs.PutLump(BinaryStateLump.Subtitles, (tw) => tw.WriteLine(Subtitles.ToString())); + + if (StartsFromSavestate) + { + bs.PutLump(BinaryStateLump.CorestateText, (tw) => tw.WriteLine(SavestateBinaryBase64Blob)); + } + } + + Changes = false; + } } } diff --git a/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs b/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs index c85733f1f1..444bb79742 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs @@ -12,7 +12,13 @@ namespace BizHawk.Client.Common public string GetInputLog() { - throw new NotImplementedException(); + var sb = new StringBuilder(); + + sb.AppendLine("[Input]"); + sb.Append(RawInputLog()); + sb.AppendLine("[/Input]"); + + return sb.ToString(); } public bool ExtractInputLog(TextReader reader, out string errorMessage) @@ -24,5 +30,16 @@ namespace BizHawk.Client.Common { throw new NotImplementedException(); } + + private StringBuilder RawInputLog() + { + var sb = new StringBuilder(); + foreach (var record in _log) + { + sb.AppendLine(record); + } + + return sb; + } } }