BizHawk/BizHawk.Client.Common/rewind/Rewinder.cs

376 lines
8.8 KiB
C#
Raw Normal View History

using System;
using System.IO;
2015-01-17 06:20:00 +00:00
using BizHawk.Common;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
2013-12-27 01:14:17 +00:00
namespace BizHawk.Client.Common
{
public class Rewinder
{
private StreamBlobDatabase _rewindBuffer;
private RewindThreader _rewindThread;
private byte[] _lastState;
private bool _rewindImpossible;
private int _rewindFrequency = 1;
private bool _rewindDeltaEnable;
private byte[] _rewindFellationBuf;
private byte[] _tempBuf = new byte[0];
public Rewinder()
{
RewindActive = true;
}
public Action<string> MessageCallback { get; set; }
public bool RewindActive { get; set; }
2013-12-27 01:14:17 +00:00
// TODO: make RewindBuf never be null
public float FullnessRatio
{
get { return _rewindBuffer.FullnessRatio; }
}
public int Count
{
get { return _rewindBuffer != null ? _rewindBuffer.Count : 0; }
}
public long Size
{
get { return _rewindBuffer != null ? _rewindBuffer.Size : 0; }
}
public int BufferCount
{
get { return _rewindBuffer != null ? _rewindBuffer.Count : 0; }
}
public bool HasBuffer
{
get { return _rewindBuffer != null; }
}
public int RewindFrequency
{
get { return _rewindFrequency; }
}
// TOOD: this should not be parameterless?! It is only possible due to passing a static context in
public void CaptureRewindState()
{
if (Global.Emulator.HasSavestates())
{
if (_rewindImpossible)
{
return;
}
if (_lastState == null)
{
DoRewindSettings();
}
// log a frame
if (_lastState != null && Global.Emulator.Frame % _rewindFrequency == 0)
{
_rewindThread.Capture(Global.Emulator.AsStatable().SaveStateBinary());
}
}
}
public void DoRewindSettings()
{
if (Global.Emulator.HasSavestates())
{
// This is the first frame. Capture the state, and put it in LastState for future deltas to be compared against.
_lastState = (byte[])Global.Emulator.AsStatable().SaveStateBinary().Clone();
int state_size;
if (_lastState.Length >= Global.Config.Rewind_LargeStateSize)
{
SetRewindParams(Global.Config.RewindEnabledLarge, Global.Config.RewindFrequencyLarge);
state_size = 3;
}
else if (_lastState.Length >= Global.Config.Rewind_MediumStateSize)
{
SetRewindParams(Global.Config.RewindEnabledMedium, Global.Config.RewindFrequencyMedium);
state_size = 2;
}
else
{
SetRewindParams(Global.Config.RewindEnabledSmall, Global.Config.RewindFrequencySmall);
state_size = 1;
}
var rewind_enabled = false;
if (state_size == 1)
{
rewind_enabled = Global.Config.RewindEnabledSmall;
}
else if (state_size == 2)
2013-12-27 01:14:17 +00:00
{
rewind_enabled = Global.Config.RewindEnabledMedium;
}
else if (state_size == 3)
{
rewind_enabled = Global.Config.RewindEnabledLarge;
2013-12-27 01:14:17 +00:00
}
_rewindDeltaEnable = Global.Config.Rewind_UseDelta;
if (rewind_enabled)
2013-12-27 01:14:17 +00:00
{
var cap = Global.Config.Rewind_BufferSize * (long)1024 * 1024;
if (_rewindBuffer != null)
{
_rewindBuffer.Dispose();
}
_rewindBuffer = new StreamBlobDatabase(Global.Config.Rewind_OnDisk, cap, BufferManage);
2013-12-27 01:14:17 +00:00
if (_rewindThread != null)
{
_rewindThread.Dispose();
}
_rewindThread = new RewindThreader(this, Global.Config.Rewind_IsThreaded);
}
}
}
public void Rewind(int frames)
{
if (Global.Emulator.HasSavestates())
{
_rewindThread.Rewind(frames);
}
}
// TODO remove me
public void _RunRewind(int frames)
{
for (int i = 0; i < frames; i++)
{
if (_rewindBuffer.Count == 0 || (Global.MovieSession.Movie.IsActive && Global.MovieSession.Movie.InputLogLength == 0))
{
return;
}
if (_lastState.Length < 0x10000)
{
Rewind64K();
}
else
{
RewindLarge();
}
}
}
// TODO: only run by RewindThreader, refactor
public void RunCapture(byte[] coreSavestate)
{
if (_rewindDeltaEnable)
{
2015-01-17 19:22:21 +00:00
CaptureRewindStateDelta(coreSavestate);
}
else
{
CaptureRewindStateNonDelta(coreSavestate);
}
}
public void ResetRewindBuffer()
{
if (_rewindBuffer != null)
{
_rewindBuffer.Clear();
}
_rewindImpossible = false;
_lastState = null;
}
2013-12-27 01:14:17 +00:00
private void DoMessage(string message)
{
if (MessageCallback != null)
{
MessageCallback(message);
}
}
private void SetRewindParams(bool enabled, int frequency)
{
if (RewindActive != enabled)
{
2013-12-27 01:14:17 +00:00
DoMessage("Rewind " + (enabled ? "Enabled" : "Disabled"));
}
if (_rewindFrequency != frequency && enabled)
{
2013-12-27 01:14:17 +00:00
DoMessage("Rewind frequency set to " + frequency);
}
RewindActive = enabled;
_rewindFrequency = frequency;
if (!RewindActive)
{
_lastState = null;
}
}
private byte[] BufferManage(byte[] inbuf, long size, bool allocate)
{
if (allocate)
{
// if we have an appropriate buffer free, return it
if (_rewindFellationBuf != null && _rewindFellationBuf.LongLength == size)
{
var ret = _rewindFellationBuf;
_rewindFellationBuf = null;
return ret;
}
// otherwise, allocate it
return new byte[size];
}
_rewindFellationBuf = inbuf;
return null;
}
private void CaptureRewindStateNonDelta(byte[] currentState)
{
long offset = _rewindBuffer.Enqueue(0, currentState.Length + 1);
var stream = _rewindBuffer.Stream;
stream.Position = offset;
// write the header for a non-delta frame
stream.WriteByte(1); // i.e. true
stream.Write(currentState, 0, currentState.Length);
}
2015-01-17 19:22:21 +00:00
private unsafe void CaptureRewindStateDelta(byte[] currentState)
{
// in case the state sizes mismatch, capture a full state rather than trying to do anything clever
if (currentState.Length != _lastState.Length)
{
CaptureRewindStateNonDelta(currentState);
return;
}
int index = 0;
int stateLength = Math.Min(currentState.Length, _lastState.Length);
bool inChangeSequence = false;
int changeSequenceStartOffset = 0;
int lastChangeSequenceStartOffset = 0;
if (_tempBuf.Length < stateLength)
{
_tempBuf = new byte[stateLength];
}
_tempBuf[index++] = 0; // Full state (false = delta)
2015-01-17 06:19:13 +00:00
fixed (byte* pCurrentState = &currentState[0])
fixed (byte* pLastState = &_lastState[0])
for (int i = 0; i < stateLength; i++)
{
2015-01-17 06:19:13 +00:00
bool thisByteMatches = *(pCurrentState + i) == *(pLastState + i);
2015-01-17 03:55:41 +00:00
2015-01-17 06:19:13 +00:00
if (inChangeSequence == false)
{
if (thisByteMatches)
{
2015-01-17 06:19:13 +00:00
continue;
}
inChangeSequence = true;
changeSequenceStartOffset = i;
2015-01-17 06:19:13 +00:00
}
if (thisByteMatches || i == stateLength - 1)
2015-01-17 06:19:13 +00:00
{
const int maxHeaderSize = 10;
int length = i - changeSequenceStartOffset + (thisByteMatches ? 0 : 1);
2015-01-17 06:19:13 +00:00
if (index + length + maxHeaderSize >= stateLength)
2015-01-17 06:19:13 +00:00
{
// If the delta ends up being larger than the full state, capture the full state instead
CaptureRewindStateNonDelta(currentState);
return;
}
2015-01-17 19:22:21 +00:00
// Offset Delta
VLInteger.WriteUnsigned((uint)(changeSequenceStartOffset - lastChangeSequenceStartOffset), _tempBuf, ref index);
2015-01-17 06:19:13 +00:00
// Length
VLInteger.WriteUnsigned((uint)length, _tempBuf, ref index);
2015-01-17 06:19:13 +00:00
// Data
Buffer.BlockCopy(_lastState, changeSequenceStartOffset, _tempBuf, index, length);
index += length;
2015-01-17 06:19:13 +00:00
inChangeSequence = false;
lastChangeSequenceStartOffset = changeSequenceStartOffset;
}
}
Buffer.BlockCopy(currentState, 0, _lastState, 0, _lastState.Length);
2015-01-17 06:19:13 +00:00
_rewindBuffer.Push(new ArraySegment<byte>(_tempBuf, 0, index));
}
private void RewindLarge()
{
RewindDelta(false);
}
private void Rewind64K()
{
RewindDelta(true);
}
private void RewindDelta(bool isSmall)
{
if (Global.Emulator.HasSavestates())
{
var ms = _rewindBuffer.PopMemoryStream();
var reader = new BinaryReader(ms);
var fullstate = reader.ReadBoolean();
if (fullstate)
{
Global.Emulator.AsStatable().LoadStateBinary(reader);
}
else
{
byte[] buf = ms.GetBuffer();
var output = new MemoryStream(_lastState);
int index = 1;
int offset = 0;
while (index < buf.Length)
{
2015-01-17 19:22:21 +00:00
int offsetDelta = (int)VLInteger.ReadUnsigned(buf, ref index);
int length = (int)VLInteger.ReadUnsigned(buf, ref index);
2015-01-17 19:22:21 +00:00
offset += offsetDelta;
output.Position = offset;
output.Write(buf, index, length);
index += length;
}
reader.Close();
output.Position = 0;
Global.Emulator.AsStatable().LoadStateBinary(new BinaryReader(output));
}
}
}
}
}