diff --git a/BizHawk.Client.Common/BinarySaveStates.cs b/BizHawk.Client.Common/BinarySaveStates.cs index c6e62c7a3f..7131491f16 100644 --- a/BizHawk.Client.Common/BinarySaveStates.cs +++ b/BizHawk.Client.Common/BinarySaveStates.cs @@ -18,7 +18,11 @@ namespace BizHawk.Client.Common Movieheader, Comments, Subtitles, - SyncSettings + SyncSettings, + + // TasMovie + LagLog, + Greenzone } public class BinaryStateFileNames @@ -48,6 +52,10 @@ namespace BizHawk.Client.Common LumpNames[BinaryStateLump.Comments] = "Comments"; LumpNames[BinaryStateLump.Subtitles] = "Subtitles"; LumpNames[BinaryStateLump.SyncSettings] = "SyncSettings"; + + // TasMovie + LumpNames[BinaryStateLump.LagLog] = "LagLog"; + LumpNames[BinaryStateLump.Greenzone] = "GreenZone"; } public static string Get(BinaryStateLump lump) diff --git a/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs b/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs index 8e9d187b73..e98ad09b11 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs @@ -1,7 +1,10 @@ using System; using System.IO; +using System.Linq; using BizHawk.Common; +using BizHawk.Common.CollectionExtensions; +using BizHawk.Common.IOExtensions; namespace BizHawk.Client.Common { @@ -25,6 +28,8 @@ namespace BizHawk.Client.Common bs.PutLump(BinaryStateLump.Input, tw => tw.WriteLine(RawInputLog())); + bs.PutLump(BinaryStateLump.LagLog, (BinaryWriter bw) => bw.Write(LagLog.ToByteArray())); + if (StartsFromSavestate) { if (TextSavestate != null) @@ -118,6 +123,12 @@ namespace BizHawk.Client.Common ExtractInputLog(tr, out errorMessage); }); + bl.GetLump(BinaryStateLump.LagLog, true, delegate(BinaryReader br) + { + var bytes = br.BaseStream.ReadAllBytes(); + LagLog = bytes.ToBools().ToList(); + }); + if (StartsFromSavestate) { bl.GetCoreState( @@ -136,7 +147,13 @@ namespace BizHawk.Client.Common private void ClearTasprojExtrasBeforeLoad() { - // TODO + LagLog.Clear(); + StateManager.Clear(); + } + + private void RestoreLagLog(byte[] buffer) + { + } } } diff --git a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs index 7d4bbaeaa0..0e86f6832e 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs @@ -11,7 +11,7 @@ namespace BizHawk.Client.Common { public sealed partial class TasMovie : Bk2Movie { - private readonly List LagLog = new List(); + private List LagLog = new List(); private readonly TasStateManager StateManager = new TasStateManager(); public TasMovie(string path) : base(path) { } @@ -37,7 +37,7 @@ namespace BizHawk.Client.Common { State = StateManager[index], LogEntry = GetInput(index), - Lagged = LagLog[index] + Lagged = (index < LagLog.Count) ? LagLog[index] : false }; } } diff --git a/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs b/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs index 39b2f7bc1e..4924a9954f 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs @@ -63,5 +63,13 @@ namespace BizHawk.Client.Common States.Remove(f); } } + + /// + /// Clears all state information + /// + public void Clear() + { + States.Clear(); + } } } diff --git a/BizHawk.Common/Extensions/CollectionExtensions.cs b/BizHawk.Common/Extensions/CollectionExtensions.cs index 87b533b797..6d3b133f45 100644 --- a/BizHawk.Common/Extensions/CollectionExtensions.cs +++ b/BizHawk.Common/Extensions/CollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; @@ -94,5 +95,27 @@ namespace BizHawk.Common.CollectionExtensions throw new InvalidOperationException("Item not found"); } + + public static byte[] ToByteArray(this IEnumerable list) + { + var bits = new BitArray(list.ToArray()); + byte [] bytes = new byte[bits.Length / 8 + ( bits.Length % 8 == 0 ? 0 : 1 )]; + bits.CopyTo(bytes, 0); + return bytes; + } + + /// + /// Converts any byte array into a bit array represented as a list of bools + /// + public static IEnumerable ToBools(this byte[] bytes) + { + var bits = new BitArray(bytes); + var bools = new bool[bits.Length]; + bits.CopyTo(bools, 0); + + return bools; + } + + } }