Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ZwinderBuffer invalidation logic into the class #4066

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions src/BizHawk.Client.Common/movie/tasproj/ZwinderStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private void CaptureGap(int frame, IStatable source)
else
StateCache.Remove(lastGap.Frame);

_gapFiller.InvalidateEnd(i);
_gapFiller.InvalidateLast();
}

_gapFiller.Capture(
Expand All @@ -461,9 +461,9 @@ private void CaptureGap(int frame, IStatable source)

public void Clear()
{
_current.InvalidateEnd(0);
_recent.InvalidateEnd(0);
_gapFiller.InvalidateEnd(0);
_current.InvalidateAfter(-1);
_recent.InvalidateAfter(-1);
_gapFiller.InvalidateAfter(-1);
StateCache.Clear();
AddStateCache(0);
_reserved = _reserved.Where(static kvp => kvp.Key is 0).ToDictionary(); //TODO clone needed?
Expand All @@ -485,39 +485,18 @@ public bool HasState(int frame)

private bool InvalidateGaps(int frame)
{
for (var i = 0; i < _gapFiller.Count; i++)
{
var state = _gapFiller.GetState(i);
if (state.Frame > frame)
{
_gapFiller.InvalidateEnd(i);
return true;
}
}
return false;
return _gapFiller.InvalidateAfter(frame);
}

private bool InvalidateNormal(int frame)
{
for (var i = 0; i < _recent.Count; i++)
if (_recent.InvalidateAfter(frame))
{
if (_recent.GetState(i).Frame > frame)
{
_recent.InvalidateEnd(i);
_current.InvalidateEnd(0);
return true;
}
_current.InvalidateAfter(-1);
return true;
}

for (var i = 0; i < _current.Count; i++)
{
if (_current.GetState(i).Frame > frame)
{
_current.InvalidateEnd(i);
return true;
}
}
return false;
return _current.InvalidateAfter(frame);
}

private bool InvalidateReserved(int frame)
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Client.Common/rewind/ZeldaWinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void Dispose()
public void Clear()
{
Sync();
_buffer.InvalidateEnd(0);
_buffer.InvalidateAfter(-1);
_count = 0;
_masterFrame = -1;
}
Expand Down Expand Up @@ -236,7 +236,7 @@ public bool Rewind(int frameToAvoid)
{
var index = _buffer.Count - 1;
RefillMaster(_buffer.GetState(index));
_buffer.InvalidateEnd(index);
_buffer.InvalidateLast();
_stateSource.LoadStateBinary(new BinaryReader(new MemoryStream(_master, 0, _masterLength, false)));
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Client.Common/rewind/Zwinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public bool Rewind(int frameToAvoid)
}
using var br = new BinaryReader(state.GetReadStream());
_stateSource.LoadStateBinary(br);
_buffer.InvalidateEnd(index);
_buffer.InvalidateLast();
}
else
{
Expand Down Expand Up @@ -103,7 +103,7 @@ public void Dispose()

public void Clear()
{
_buffer.InvalidateEnd(0);
_buffer.InvalidateAfter(-1);
}
}
}
25 changes: 20 additions & 5 deletions src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,33 @@ public StateInformation GetState(int index)
}

/// <summary>
/// Invalidate states from GetState(index) on to the end of the buffer, so that Count == index afterwards
/// Invalidate all states with frame number > frame.
/// <returns>True iff any state was invalidated, else false</returns>
/// </summary>
public void InvalidateEnd(int index)
public bool InvalidateAfter(int frame)
{
if ((uint) index > (uint) Count) // intentionally allows index == Count (e.g. clearing an empty buffer)
for (int i = _firstStateIndex; i != _nextStateIndex; i = (i + 1) & STATEMASK)
{
throw new ArgumentOutOfRangeException(paramName: nameof(index), index, message: "index out of range");
if (_states[i].Frame > frame)
{
_nextStateIndex = (i + _firstStateIndex) & STATEMASK;
return true;
}
}
_nextStateIndex = (index + _firstStateIndex) & STATEMASK;

return false;
//Util.DebugWriteLine($"Size: {Size >> 20}MiB, Used: {Used >> 20}MiB, States: {Count}");
}

/// <summary>
/// Invalidates the last state in the buffer
/// </summary>
public void InvalidateLast()
{
if (Count != 0)
_nextStateIndex = (_nextStateIndex - 1) & STATEMASK;
}

public void SaveStateBinary(BinaryWriter writer)
{
// version number
Expand Down