From 799742c3ff299f801ea00a243a28bfe8d1795926 Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 8 Jun 2020 11:29:08 -0500 Subject: [PATCH] IRewinder - dress up the api a bit, fix a potential NRE --- src/BizHawk.Client.Common/rewind/Rewinder.cs | 18 +++++++++++++++--- src/BizHawk.Client.EmuHawk/MainForm.cs | 16 +++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/BizHawk.Client.Common/rewind/Rewinder.cs b/src/BizHawk.Client.Common/rewind/Rewinder.cs index 4bc200c8e2..a7fa0634d4 100644 --- a/src/BizHawk.Client.Common/rewind/Rewinder.cs +++ b/src/BizHawk.Client.Common/rewind/Rewinder.cs @@ -12,10 +12,12 @@ namespace BizHawk.Client.Common int RewindFrequency { get; } bool Active { get; } - bool Suspend { get; set; } void Capture(int frame); bool Rewind(int frames); + + void Suspend(); + void Resume(); } public class Rewinder : IRewinder @@ -33,11 +35,11 @@ namespace BizHawk.Client.Common private bool _lastRewindLoadedState; private byte[] _deltaBuffer = new byte[0]; - public bool Active => RewindEnabled && !Suspend; + public bool Active => RewindEnabled && !_suspend; private bool RewindEnabled { get; } - public bool Suspend { get; set; } + private bool _suspend; public float FullnessRatio => _rewindBuffer?.FullnessRatio ?? 0; @@ -79,6 +81,16 @@ namespace BizHawk.Client.Common } } + public void Suspend() + { + _suspend = true; + } + + public void Resume() + { + _suspend = false; + } + private void Clear() { _rewindBuffer?.Clear(); diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index f2ec4af8bd..8a8bd4c108 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -4009,12 +4009,22 @@ namespace BizHawk.Client.EmuHawk public void EnableRewind(bool enabled) { - if (Rewinder == null && Emulator.HasSavestates()) + if (!Emulator.HasSavestates()) { - Rewinder = new Rewinder(Emulator.AsStatable(), Config.Rewind); + return; + } + + Rewinder ??= new Rewinder(Emulator.AsStatable(), Config.Rewind); + + if (enabled) + { + Rewinder.Resume(); + } + else + { + Rewinder.Suspend(); } - Rewinder.Suspend = !enabled; AddOnScreenMessage($"Rewind {(enabled ? "enabled" : "suspended")}"); }