diff --git a/src/BizHawk.Client.Common/rewind/Rewinder.cs b/src/BizHawk.Client.Common/rewind/Rewinder.cs index 54257a04d0..3a9363acf3 100644 --- a/src/BizHawk.Client.Common/rewind/Rewinder.cs +++ b/src/BizHawk.Client.Common/rewind/Rewinder.cs @@ -6,6 +6,8 @@ namespace BizHawk.Client.Common { public class Rewinder { + private IStatable _statableCore; + private const int MaxByteArraySize = 0x7FFFFFC7; // .NET won't let us allocate more than this in one array private StreamBlobDatabase _rewindBuffer; @@ -35,43 +37,41 @@ namespace BizHawk.Client.Common public int RewindFrequency { get; private set; } - public void Initialize() + public void Initialize(IStatable statableCore, RewindConfig rewindConfig) { Uninitialize(); - if (Global.Emulator.HasSavestates()) - { - int stateSize = Global.Emulator.AsStatable().SaveStateBinary().Length; + _statableCore = statableCore; - if (stateSize >= Global.Config.Rewind.LargeStateSize) - { - RewindEnabled = Global.Config.Rewind.EnabledLarge; - RewindFrequency = Global.Config.Rewind.FrequencyLarge; - } - else if (stateSize >= Global.Config.Rewind.MediumStateSize) - { - RewindEnabled = Global.Config.Rewind.EnabledMedium; - RewindFrequency = Global.Config.Rewind.FrequencyMedium; - } - else - { - RewindEnabled = Global.Config.Rewind.EnabledSmall; - RewindFrequency = Global.Config.Rewind.FrequencySmall; - } + int stateSize = _statableCore.CloneSavestate().Length; + + if (stateSize >= rewindConfig.LargeStateSize) + { + RewindEnabled = rewindConfig.EnabledLarge; + RewindFrequency = rewindConfig.FrequencyLarge; + } + else if (stateSize >= rewindConfig.MediumStateSize) + { + RewindEnabled = rewindConfig.EnabledMedium; + RewindFrequency = rewindConfig.FrequencyMedium; + } + else + { + RewindEnabled = rewindConfig.EnabledSmall; + RewindFrequency = rewindConfig.FrequencySmall; } - DoMessage(RewindEnabled ? + MessageCallback?.Invoke(RewindEnabled ? $"Rewind enabled, frequency: {RewindFrequency}" : "Rewind disabled"); - _rewindDeltaEnable = Global.Config.Rewind.UseDelta; + _rewindDeltaEnable = rewindConfig.UseDelta; if (RewindActive) { - var capacity = Global.Config.Rewind.BufferSize * 1024L * 1024L; - _rewindBuffer = new StreamBlobDatabase(Global.Config.Rewind.OnDisk, capacity, BufferManage); - - _rewindThread = new RewindThreader(CaptureInternal, RewindInternal, Global.Config.Rewind.IsThreaded); + var capacity = rewindConfig.BufferSize * 1024L * 1024L; + _rewindBuffer = new StreamBlobDatabase(rewindConfig.OnDisk, capacity, BufferManage); + _rewindThread = new RewindThreader(CaptureInternal, RewindInternal, rewindConfig.IsThreaded); } } @@ -101,11 +101,6 @@ namespace BizHawk.Client.Common _lastState = new byte[0]; } - private void DoMessage(string message) - { - MessageCallback?.Invoke(message); - } - private byte[] BufferManage(byte[] inbuf, ref long size, bool allocate) { if (!allocate) @@ -141,24 +136,19 @@ namespace BizHawk.Client.Common throw new OutOfMemoryException(); } - public void Capture() + public void Capture(int frame) { if (!RewindActive) { return; } - if (_rewindThread == null) - { - Initialize(); - } - - if (_rewindThread == null || Global.Emulator.Frame % RewindFrequency != 0) + if (_rewindThread == null || frame % RewindFrequency != 0) { return; } - _rewindThread.Capture(Global.Emulator.AsStatable().SaveStateBinary()); + _rewindThread.Capture(_statableCore.SaveStateBinary()); } private void CaptureInternal(byte[] coreSavestate) @@ -371,7 +361,7 @@ namespace BizHawk.Client.Common } } - Global.Emulator.AsStatable().LoadStateBinary(_lastState); + _statableCore.LoadStateBinary(_lastState); } else { @@ -380,7 +370,7 @@ namespace BizHawk.Client.Common throw new InvalidOperationException(); } - Global.Emulator.AsStatable().LoadStateBinary(reader); + _statableCore.LoadStateBinary(reader); } } } diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index d19ad42652..81a23305ad 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -3775,7 +3775,10 @@ namespace BizHawk.Client.EmuHawk OnRomChanged(); DisplayManager.Blank(); - Rewinder.Initialize(); + if (Emulator.HasSavestates()) + { + Rewinder.Initialize(Emulator.AsStatable(), Config.Rewind); + } Global.InputManager.StickyXorAdapter.ClearStickies(); Global.InputManager.StickyXorAdapter.ClearStickyAxes(); @@ -4313,7 +4316,7 @@ namespace BizHawk.Client.EmuHawk } else if (!suppressCaptureRewind && Rewinder.RewindActive) { - Rewinder.Capture(); + Rewinder.Capture(Emulator.Frame); } } diff --git a/src/BizHawk.Client.EmuHawk/config/RewindConfig.cs b/src/BizHawk.Client.EmuHawk/config/RewindConfig.cs index 953a62e74b..339e49fda2 100644 --- a/src/BizHawk.Client.EmuHawk/config/RewindConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/RewindConfig.cs @@ -175,7 +175,7 @@ namespace BizHawk.Client.EmuHawk if (TriggerRewindSettingsReload) { - _rewinder.Initialize(); + _rewinder.Initialize(_statableCore, _config.Rewind); } // These settings are not used by DoRewindSettings