Skip to content

Commit

Permalink
Remove debug logic
Browse files Browse the repository at this point in the history
  • Loading branch information
saul committed Feb 24, 2024
1 parent 8d95cd1 commit 909c58a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 71 deletions.
14 changes: 0 additions & 14 deletions src/DemoFile/DemoParser.Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public partial class DemoParser
private readonly CEntityInstance?[] _entities = new CEntityInstance?[MaxEdicts];
private readonly CHandle<CCSTeam>[] _teamHandles = new CHandle<CCSTeam>[4];

private bool _fullSnapshotRead;
private CHandle<CCSGameRulesProxy> _gameRulesHandle;
private CHandle<CCSPlayerResource> _playerResourceHandle;

Expand Down Expand Up @@ -125,7 +124,6 @@ private void OnServerInfo(CSVCMsg_ServerInfo msg)
{
Debug.Assert(_playerInfos[msg.PlayerSlot] != null);
IsGotv = _playerInfos[msg.PlayerSlot]?.Ishltv ?? false;
_fullSnapshotRead = false;

MaxPlayers = msg.MaxClients;
_serverClassBits = (int)Math.Log2(msg.MaxClasses) + 1;
Expand Down Expand Up @@ -260,18 +258,6 @@ private void OnPacketEntities(CSVCMsg_PacketEntities msg)
// Clear out all entities - this is a full update.
if (!msg.LegacyIsDelta)
{
if (IsGotv)
{
// In GOTV recordings, we see a snapshot every 3840 ticks (60 seconds).
// After we've read the first one, we can ignore the rest.
if (_fullSnapshotRead && CurrentDemoTick != _readFullPacketTick)
return;

Console.WriteLine($"Reading snapshot tick: {CurrentDemoTick}");

_fullSnapshotRead = true;
}

foreach (var entity in _entities)
{
if (entity == null)
Expand Down
86 changes: 32 additions & 54 deletions src/DemoFile/DemoParser.FullPacket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Buffers;
using System.Diagnostics;

namespace DemoFile;
Expand All @@ -14,6 +13,7 @@ private readonly record struct FullPacketPosition(DemoTick Tick, long StreamPosi
/// Key ticks occur every 60 seconds
private const int FullPacketInterval = 64 * 60;

// todo: reset on StartReadingAsync
private readonly List<FullPacketPosition> _fullPacketPositions = new(64);
private DemoTick _readFullPacketTick = DemoTick.PreRecord;
private int _fullPacketTickOffset;
Expand All @@ -40,8 +40,6 @@ private bool TryFindFullPacketBefore(DemoTick demoTick, out FullPacketPosition f

public async ValueTask SeekToTickAsync(DemoTick tick, CancellationToken cancellationToken)
{
Console.WriteLine($">>> SeekToTick: {tick} (from {CurrentDemoTick})");

// todo: throw if currently in a MoveNextAsync

if (TickCount < DemoTick.Zero)
Expand All @@ -55,8 +53,6 @@ public async ValueTask SeekToTickAsync(DemoTick tick, CancellationToken cancella
}

var hasFullPacket = TryFindFullPacketBefore(tick, out var fullPacket);
Console.WriteLine($" Key tick: {(hasFullPacket ? $"{fullPacket.Tick}@{fullPacket.StreamPosition}" : "none")}");

if (tick < CurrentDemoTick)
{
if (!hasFullPacket)
Expand All @@ -66,7 +62,6 @@ public async ValueTask SeekToTickAsync(DemoTick tick, CancellationToken cancella

// Seeking backwards. Jump back to the key tick to read the snapshot
(CurrentDemoTick, _stream.Position) = fullPacket;
Console.WriteLine($" Seeking back from: {fullPacket.Tick}@{fullPacket.StreamPosition}");
}
else
{
Expand All @@ -76,26 +71,22 @@ public async ValueTask SeekToTickAsync(DemoTick tick, CancellationToken cancella
if (hasFullPacket && deltaTicks.Value >= FullPacketInterval)
{
(CurrentDemoTick, _stream.Position) = fullPacket;
Console.WriteLine($" Seeking forward from: {fullPacket.Tick}@{fullPacket.StreamPosition}");
}
}

// Keep reading commands until we reach the key tick
_readFullPacketTick = new DemoTick(tick.Value / FullPacketInterval * FullPacketInterval + _fullPacketTickOffset);
Console.WriteLine($" Closest full packet: {_readFullPacketTick}");
SkipToTick(_readFullPacketTick);

// Advance ticks until we get to the target tick
Console.WriteLine($" Advancing to: {tick}");
while (CurrentDemoTick < tick)
{
var startPosition = _stream.Position;
var cmd = ReadCommandHeader();

// We've arrived at the target tick
if (CurrentDemoTick == tick)
{
_stream.Position = startPosition;
_stream.Position = _commandStartPosition;
break;
}

Expand All @@ -104,53 +95,37 @@ public async ValueTask SeekToTickAsync(DemoTick tick, CancellationToken cancella
throw new EndOfStreamException($"Reached EOF at tick {CurrentDemoTick} while seeking to tick {tick}");
}
}

Console.WriteLine($" Arrived at: {CurrentDemoTick}/{tick}\n");
}

private void SkipToTick(DemoTick targetTick)
{
while (CurrentDemoTick < targetTick)
{
var startPosition = _stream.Position;
var cmd = ReadCommandHeader();
var actualCmd = cmd.Command & ~(uint) EDemoCommands.DemIsCompressed;

// If we're at the target tick, jump back to the start of the command.
if (CurrentDemoTick == targetTick && cmd.Command == (uint)EDemoCommands.DemStringTables)
// If we're at the target tick, jump back to the start of the command
if (CurrentDemoTick == targetTick && actualCmd == (uint)EDemoCommands.DemFullPacket)
{
_stream.Position = startPosition;
_stream.Position = _commandStartPosition;
break;
}

Console.WriteLine($" Skipping past {CurrentDemoTick}...");

// Always read string tables commands when seeking, as they contain
// key tick information that improves seeking performance.
if (cmd.Command == (uint) EDemoCommands.DemStringTables)
{
var rentedBuffer = ArrayPool<byte>.Shared.Rent((int)cmd.Size);
var buffer = rentedBuffer.AsSpan(0, (int)cmd.Size);
_stream.ReadExactly(buffer);
_demoEvents.DemoStringTables?.Invoke(CDemoStringTables.Parser.ParseFrom(buffer));
ArrayPool<byte>.Shared.Return(rentedBuffer);
}
else
// Record fullpackets even when seeking to improve seeking performance
if (actualCmd == (uint) EDemoCommands.DemFullPacket)
{
// Skip over the data and start reading the next command
_stream.Position += cmd.Size;
RecordFullPacket();
}

// Skip over the data and start reading the next command
_stream.Position += cmd.Size;
}
}

private void OnDemoFullPacket(CDemoFullPacket fullPacket)
private void RecordFullPacket()
{
if (CurrentDemoTick < DemoTick.Zero)
return;

// DemoStringTables and packet entity snapshots are recorded in demos
// every 3,840 ticks (60 secs). Keep track of where they are to allow
// for fast seeking through the demo.
// DemoStringTables are always before the packet entities snapshot.
// DemoFullPackets are recorded in demos every 3,840 ticks (60 secs).
// Keep track of where they are to allow for fast seeking through the demo.
var idx = _fullPacketPositions.BinarySearch(new FullPacketPosition(CurrentDemoTick, 0L));
if (idx < 0)
{
Expand All @@ -160,24 +135,27 @@ private void OnDemoFullPacket(CDemoFullPacket fullPacket)
// Some demos have fullpackets at tick 0, some at tick 1.
_fullPacketTickOffset = CurrentDemoTick.Value % FullPacketInterval;
Debug.Assert(_fullPacketTickOffset == 0 || _fullPacketTickOffset == 1, "Unexpected key tick offset");
}

Console.WriteLine($"DemoFullPacket - {CurrentDemoTick}");

// We only care about DemoStringTables if we're seeking to a key tick
if (CurrentDemoTick != _readFullPacketTick)
{
Console.WriteLine("\tSkipping!");
private void OnDemoFullPacket(CDemoFullPacket fullPacket)
{
if (CurrentDemoTick < DemoTick.Zero)
return;
}

var stringTables = fullPacket.StringTable;
for (var tableIdx = 0; tableIdx < stringTables.Tables.Count; tableIdx++)
RecordFullPacket();

// We only care about full packets if we're seeking
if (CurrentDemoTick == _readFullPacketTick)
{
var snapshot = stringTables.Tables[tableIdx];
var stringTable = _stringTables[snapshot.TableName];
stringTable.ReplaceWith(snapshot.Items);
}
var stringTables = fullPacket.StringTable;
for (var tableIdx = 0; tableIdx < stringTables.Tables.Count; tableIdx++)
{
var snapshot = stringTables.Tables[tableIdx];
var stringTable = _stringTables[snapshot.TableName];
stringTable.ReplaceWith(snapshot.Items);
}

OnDemoPacket(fullPacket.Packet);
OnDemoPacket(fullPacket.Packet);
}
}
}
3 changes: 0 additions & 3 deletions src/DemoFile/DemoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@ public async ValueTask ReadAllAsync(Stream stream, CancellationToken cancellatio

CurrentDemoTick = new DemoTick(tick);

//var actualCmd = command & ~(uint)EDemoCommands.DemIsCompressed;
//Console.WriteLine($" Header: Tick = {CurrentDemoTick}, Command = {(EDemoCommands)actualCmd}, Size = {size:N0} bytes");

return (Command: command, Size: size);
}

Expand Down

0 comments on commit 909c58a

Please sign in to comment.