From 7173447e8ab58725c518c85f999090850486c703 Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 7 Jul 2014 18:00:25 +0000 Subject: [PATCH] TasMovie - some reorg in preparation for actual progress --- .../movie/tasproj/MovieRecord.cs | 42 +----- .../movie/tasproj/MovieRecordList.cs | 53 ------- .../movie/tasproj/TasMovie.IO.cs | 142 ++++++++++++++++++ .../movie/tasproj/TasMovie.cs | 26 ++-- 4 files changed, 156 insertions(+), 107 deletions(-) delete mode 100644 BizHawk.Client.Common/movie/tasproj/MovieRecordList.cs create mode 100644 BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs diff --git a/BizHawk.Client.Common/movie/tasproj/MovieRecord.cs b/BizHawk.Client.Common/movie/tasproj/MovieRecord.cs index 4b62c6327d..ea3e29808a 100644 --- a/BizHawk.Client.Common/movie/tasproj/MovieRecord.cs +++ b/BizHawk.Client.Common/movie/tasproj/MovieRecord.cs @@ -5,49 +5,13 @@ namespace BizHawk.Client.Common { public class MovieRecord { - private byte[] _state = new byte[0]; - - public MovieRecord(string serializedInput, bool captureState) - { - SerializedInput = serializedInput; - if (captureState) - { - CaptureSate(); - } - } - - public string SerializedInput { get; private set; } - public bool Lagged { get; private set; } - - public void ClearInput() - { - SerializedInput = string.Empty; - } - - public void SetInput(string input) - { - SerializedInput = input ?? string.Empty; - } - - public void CaptureSate() - { - Lagged = Global.Emulator.IsLagFrame; - _state = (byte[])Global.Emulator.SaveStateBinary().Clone(); - } - - public IEnumerable State - { - get { return _state; } - } + public byte[] State { get; set; } + public bool Lagged { get; set; } + public string LogEntry { get; set; } public bool HasState { get { return State.Any(); } } - - public void ClearState() - { - _state = new byte[0]; - } } } diff --git a/BizHawk.Client.Common/movie/tasproj/MovieRecordList.cs b/BizHawk.Client.Common/movie/tasproj/MovieRecordList.cs deleted file mode 100644 index 6530fcfd15..0000000000 --- a/BizHawk.Client.Common/movie/tasproj/MovieRecordList.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using System.Text; - -namespace BizHawk.Client.Common -{ - public class MovieRecordList : List - { - /// - /// DONT USE ME RIGHT NOW - /// - public void WriteToText(TextWriter tw) - { - tw.WriteLine("[Input]"); - tw.WriteLine("Frame {0}", Global.Emulator.Frame); - ForEach(record => tw.WriteLine(record.ToString())); - tw.WriteLine("[/Input]"); - } - - public override string ToString() - { - var sb = new StringBuilder(); - sb - .AppendLine("[Input]") - - .Append("Frame ") - .Append(Global.Emulator.Frame) - .AppendLine(); - - ForEach(record => sb.Append(record.ToString())); - - sb.AppendLine("[/Input]"); - - return sb.ToString(); - } - - public void Truncate(int index) - { - if (index < Count) - { - RemoveRange(index, Count - index); - } - } - - public void TruncateStates(int index) - { - for (int i = index; i < Count; i++) - { - this[i].ClearState(); - } - } - } -} diff --git a/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs b/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs new file mode 100644 index 0000000000..8e9d187b73 --- /dev/null +++ b/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs @@ -0,0 +1,142 @@ +using System; +using System.IO; + +using BizHawk.Common; + +namespace BizHawk.Client.Common +{ + public partial class TasMovie + { + protected override void Write(string fn) + { + var file = new FileInfo(fn); + if (!file.Directory.Exists) + { + Directory.CreateDirectory(file.Directory.ToString()); + } + + using (var fs = new FileStream(fn, FileMode.Create, FileAccess.Write)) + using (var bs = new BinaryStateSaver(fs, false)) + { + 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, tw => tw.WriteLine(RawInputLog())); + + if (StartsFromSavestate) + { + if (TextSavestate != null) + { + bs.PutLump(BinaryStateLump.CorestateText, (TextWriter tw) => tw.Write(TextSavestate)); + } + else + { + bs.PutLump(BinaryStateLump.Corestate, (BinaryWriter bw) => bw.Write(BinarySavestate)); + } + } + } + + Changes = false; + } + + public override bool Load() + { + var file = new FileInfo(Filename); + if (!file.Exists) + { + return false; + } + + using (var bl = BinaryStateLoader.LoadAndDetect(Filename, true)) + { + if (bl == null) + { + return false; + } + + ClearBeforeLoad(); + ClearTasprojExtrasBeforeLoad(); + + bl.GetLump(BinaryStateLump.Movieheader, true, delegate(TextReader tr) + { + string line; + while ((line = tr.ReadLine()) != null) + { + if (!string.IsNullOrWhiteSpace(line)) + { + var pair = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); + + if (pair.Length > 1) + { + Header.Add(pair[0], pair[1]); + } + } + } + }); + + bl.GetLump(BinaryStateLump.Comments, true, delegate(TextReader tr) + { + string line; + while ((line = tr.ReadLine()) != null) + { + if (!string.IsNullOrWhiteSpace(line)) + { + Comments.Add(line); + } + } + }); + + bl.GetLump(BinaryStateLump.Subtitles, true, delegate(TextReader tr) + { + string line; + while ((line = tr.ReadLine()) != null) + { + if (!string.IsNullOrWhiteSpace(line)) + { + Subtitles.AddFromString(line); + } + } + }); + + bl.GetLump(BinaryStateLump.SyncSettings, true, delegate(TextReader tr) + { + string line; + while ((line = tr.ReadLine()) != null) + { + if (!string.IsNullOrWhiteSpace(line)) + { + SyncSettingsJson = line; + } + } + }); + + bl.GetLump(BinaryStateLump.Input, true, delegate(TextReader tr) + { + var errorMessage = string.Empty; + ExtractInputLog(tr, out errorMessage); + }); + + if (StartsFromSavestate) + { + bl.GetCoreState( + delegate(BinaryReader br) + { + BinarySavestate = br.ReadBytes((int)br.BaseStream.Length); + }, + delegate(TextReader tr) + { + TextSavestate = tr.ReadToEnd(); + }); + } + } + return true; + } + + private void ClearTasprojExtrasBeforeLoad() + { + // TODO + } + } +} diff --git a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs index 1a800f26c2..87e1c5f313 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs @@ -9,13 +9,11 @@ using BizHawk.Emulation.Common; namespace BizHawk.Client.Common { - public class TasMovie : Bk2Movie + public sealed partial class TasMovie : Bk2Movie { - public TasMovie(string path) - : base(path) - { + private readonly List LagLog = new List(); - } + public TasMovie(string path) : base(path) { } public TasMovie() : base() @@ -25,10 +23,7 @@ namespace BizHawk.Client.Common public override string PreferredExtension { - get - { - return Extension; - } + get { return Extension; } } public new const string Extension = "tasproj"; @@ -37,14 +32,15 @@ namespace BizHawk.Client.Common { get { - // TODO - return new MovieRecord("", false); + return new MovieRecord // TODO + { + State = null, + LogEntry = "", + Lagged = false + }; } } - public void SetBoolButton(int index, string button, bool value) - { - // TODO - } + // _state = (byte[])Global.Emulator.SaveStateBinary().Clone(); } }