don't throw exceptions when passing a BufferSize or TargetFrameLength of 0 to a ZwinderBuffer

size 0 means 0 states will be captured; TargetFrameLength 0 means no spacing of states (result behavior is identical to TargetFrameLength of 1; capture every frame)
This commit is contained in:
SuuperW 2020-08-24 18:53:12 -05:00
parent 14653fac0e
commit 46de6aca94
1 changed files with 13 additions and 11 deletions

View File

@ -20,23 +20,22 @@ namespace BizHawk.Client.Common
public ZwinderBuffer(IRewindSettings settings)
{
long targetSize = settings.BufferSize * 1024 * 1024;
if (settings.TargetFrameLength < 1)
{
throw new ArgumentOutOfRangeException(nameof(settings.TargetFrameLength));
}
Size = 1L << (int)Math.Floor(Math.Log(targetSize, 2));
_sizeMask = Size - 1;
_buffer = new MemoryBlock((ulong)Size);
_buffer.Protect(_buffer.Start, _buffer.Size, MemoryBlock.Protection.RW);
_targetFrameLength = settings.TargetFrameLength;
_states = new StateInfo[STATEMASK + 1];
_useCompression = settings.UseCompression;
if (Size > 1)
{
_buffer = new MemoryBlock((ulong)Size);
_buffer.Protect(_buffer.Start, _buffer.Size, MemoryBlock.Protection.RW);
_states = new StateInfo[STATEMASK + 1];
}
}
public void Dispose()
{
_buffer.Dispose();
_buffer?.Dispose();
}
@ -142,7 +141,7 @@ namespace BizHawk.Client.Common
/// </param>
public void Capture(int frame, Action<Stream> callback, Action<int> indexInvalidated = null, bool force = false)
{
if (!force && !ShouldCapture(frame))
if ((!force && !ShouldCapture(frame)) || _buffer == null)
return;
if (Count == STATEMASK)
@ -286,8 +285,11 @@ namespace BizHawk.Client.Common
nextByte += _states[i].Size;
}
// TODO: Use spans to avoid this extra copy in .net core
var dest = _buffer.GetStream(_buffer.Start, (ulong)nextByte, true);
WaterboxUtils.CopySome(reader.BaseStream, dest, nextByte);
if (nextByte > 0)
{
var dest = _buffer.GetStream(_buffer.Start, (ulong)nextByte, true);
WaterboxUtils.CopySome(reader.BaseStream, dest, nextByte);
}
}
public static ZwinderBuffer Create(BinaryReader reader)