From 3c27a332bbd2cdf2ab3478983bb911bc75a39da8 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 15 Jun 2019 14:31:06 -0500 Subject: [PATCH] create IStateManager interface and use that instead of TasStateManager --- .../BizHawk.Client.Common.csproj | 1 + .../movie/tasproj/IStateManager.cs | 62 +++++++++++++++++++ .../movie/tasproj/StateManagerDecay.cs | 4 +- .../movie/tasproj/StateManagerState.cs | 6 +- .../movie/tasproj/TasMovie.cs | 4 +- .../movie/tasproj/TasStateManager.cs | 4 +- 6 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 BizHawk.Client.Common/movie/tasproj/IStateManager.cs diff --git a/BizHawk.Client.Common/BizHawk.Client.Common.csproj b/BizHawk.Client.Common/BizHawk.Client.Common.csproj index ec805f3bfe..99e67e062f 100644 --- a/BizHawk.Client.Common/BizHawk.Client.Common.csproj +++ b/BizHawk.Client.Common/BizHawk.Client.Common.csproj @@ -186,6 +186,7 @@ + diff --git a/BizHawk.Client.Common/movie/tasproj/IStateManager.cs b/BizHawk.Client.Common/movie/tasproj/IStateManager.cs new file mode 100644 index 0000000000..b0bafb28d3 --- /dev/null +++ b/BizHawk.Client.Common/movie/tasproj/IStateManager.cs @@ -0,0 +1,62 @@ +using System; + +namespace BizHawk.Client.Common +{ + using System.Collections.Generic; + using System.IO; + + using BizHawk.Common; + + public interface IStateManager : IDisposable + { + // byte[] this[int frame] { get; } // TODO: I had it refactored to this back in the day + KeyValuePair this[int frame] { get; } + + TasStateManagerSettings Settings { get; set; } + + Action InvalidateCallback { set; } + + void Capture(bool force = false); + + bool HasState(int frame); + + bool Invalidate(int frame); + + // TODO: rename to Clear() + // TODO: consider it passing a bool if anything was cleared, and the .Any() could go away + void ClearStateHistory(); + + void Save(BinaryWriter bw); + + void Load(BinaryReader br); + + KeyValuePair GetStateClosestToFrame(int frame); + + bool Any(); + + // TODO: rename to Last + int LastStatedFrame { get; } + + // ********* Delete these ********** + void MountWriteAccess(); + + // TODO: delete me, I don't work + NDBDatabase NdbDatabase { get; } + + // *********** Reconsider these ************/ + void LimitStateCount(); + + void UpdateStateFrequency(); + + bool RemoveState(int frame); + + int LastEditedFrame { get; } + bool StateIsMarker(int frame); + + int StateCount { get; } + + int GetStateIndexByFrame(int frame); + + int GetStateFrameByIndex(int index); + } +} diff --git a/BizHawk.Client.Common/movie/tasproj/StateManagerDecay.cs b/BizHawk.Client.Common/movie/tasproj/StateManagerDecay.cs index f7b3130881..6f06f9f850 100644 --- a/BizHawk.Client.Common/movie/tasproj/StateManagerDecay.cs +++ b/BizHawk.Client.Common/movie/tasproj/StateManagerDecay.cs @@ -44,7 +44,7 @@ namespace BizHawk.Client.Common { internal class StateManagerDecay { - private TasStateManager _tsm; // access tsm methods to make life easier + private readonly IStateManager _tsm; // access tsm methods to make life easier private List _zeros; // amount of least significant zeros in bitwise view (also max pattern step) private int _bits; // size of _zeros is 2 raised to the power of _bits private int _mask; // for remainder calculation using bitwise instead of division @@ -53,7 +53,7 @@ namespace BizHawk.Client.Common private int _step; // initial memory state gap private bool _align; // extra care about fine alignment. TODO: do we want it? - public StateManagerDecay(TasStateManager tsm) + public StateManagerDecay(IStateManager tsm) { _tsm = tsm; _align = false; diff --git a/BizHawk.Client.Common/movie/tasproj/StateManagerState.cs b/BizHawk.Client.Common/movie/tasproj/StateManagerState.cs index ae5255c80c..f045aa957b 100644 --- a/BizHawk.Client.Common/movie/tasproj/StateManagerState.cs +++ b/BizHawk.Client.Common/movie/tasproj/StateManagerState.cs @@ -9,14 +9,14 @@ namespace BizHawk.Client.Common internal class StateManagerState : IDisposable { private static long _stateId; - private readonly TasStateManager _manager; + private readonly IStateManager _manager; private readonly long _id; private byte[] _state; public int Frame { get; } - public static StateManagerState Read(BinaryReader r, TasStateManager m) + public static StateManagerState Read(BinaryReader r, IStateManager m) { int frame = r.ReadInt32(); byte[] data = r.ReadBytes(r.ReadInt32()); @@ -59,7 +59,7 @@ namespace BizHawk.Client.Common public bool IsOnDisk => _state == null; - public StateManagerState(TasStateManager manager, byte[] state, int frame) + public StateManagerState(IStateManager manager, byte[] state, int frame) { _manager = manager; _state = state; diff --git a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs index 3814fcdb11..08e8023487 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs @@ -13,7 +13,7 @@ namespace BizHawk.Client.Common public sealed partial class TasMovie : Bk2Movie, INotifyPropertyChanged { private readonly Bk2MnemonicConstants _mnemonics = new Bk2MnemonicConstants(); - private readonly TasStateManager _stateManager; + private readonly IStateManager _stateManager; private readonly TasLagLog _lagLog = new TasLagLog(); private readonly Dictionary _inputStateCache = new Dictionary(); private BackgroundWorker _progressReportWorker; @@ -37,7 +37,7 @@ namespace BizHawk.Client.Common public int BranchCount => Branches.Count; public int LastStatedFrame => _stateManager.LastStatedFrame; public override string PreferredExtension => Extension; - public TasStateManager TasStateManager => _stateManager; + public IStateManager TasStateManager => _stateManager; public TasMovieRecord this[int index] => new TasMovieRecord { diff --git a/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs b/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs index 38b961c6c1..4ece710660 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasStateManager.cs @@ -15,7 +15,7 @@ namespace BizHawk.Client.Common /// Captures savestates and manages the logic of adding, retrieving, /// invalidating/clearing of states. Also does memory management and limiting of states /// - public class TasStateManager : IDisposable + public class TasStateManager : IStateManager { // TODO: pass this in, and find a solution to a stale reference (this is instantiated BEFORE a new core instance is made, making this one stale if it is simply set in the constructor private IStatable Core => Global.Emulator.AsStatable(); @@ -27,7 +27,7 @@ namespace BizHawk.Client.Common InvalidateCallback?.Invoke(index); } - internal NDBDatabase NdbDatabase { get; set; } + public NDBDatabase NdbDatabase { get; private set; } private Guid _guid = Guid.NewGuid(); private SortedList _states = new SortedList();