Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.

Commit 730b797

Browse files
committed
TickTime and TickRate fixed for corrupted headers. HeaderCorrupted event and IsCorruptedHeader bool added.
1 parent 0e178dc commit 730b797

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

DemoInfo/DemoParser.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ public string Map {
264264
/// <value>The header.</value>
265265
public DemoHeader Header { get; private set; }
266266

267+
/// <summary>
268+
/// True when header is corrupted
269+
/// </summary>
270+
public bool IsHeaderCorrupted { get; private set; }
271+
272+
/// <summary>
273+
/// Used to sample gap in ticks when header is corrupted
274+
/// </summary>
275+
internal List<int> TickGaps = new List<int>();
276+
267277
/// <summary>
268278
/// Gets the participants of this game
269279
/// </summary>
@@ -453,15 +463,16 @@ public string TFlag
453463
/// </summary>
454464
/// <value>The tick rate.</value>
455465
public float TickRate {
456-
get { return this.Header.PlaybackFrames / this.Header.PlaybackTime; }
466+
get { return IsHeaderCorrupted ? 1/_ticktime : this.Header.PlaybackFrames / this.Header.PlaybackTime; }
457467
}
458468

459469
/// <summary>
460470
/// How long a tick of the demo is in s^-1
461471
/// </summary>
462472
/// <value>The tick time.</value>
473+
private float _ticktime;
463474
public float TickTime {
464-
get { return this.Header.PlaybackTime / this.Header.PlaybackFrames; }
475+
get { return IsHeaderCorrupted ? _ticktime : this.Header.PlaybackTime / this.Header.PlaybackFrames; }
465476
}
466477

467478
/// <summary>
@@ -535,10 +546,11 @@ public void ParseHeader()
535546
throw new InvalidDataException("Invalid Demo-Protocol");
536547

537548
Header = header;
549+
IsHeaderCorrupted = (header.PlaybackTime == 0);
538550

539-
if (header.PlaybackTime == 0)
551+
if (IsHeaderCorrupted)
540552
{
541-
Console.WriteLine("WARNING: The header for this demo file is corrupted. TickRate, TickTime, ParsingProgress, CurrentTime will be 0 for the first 50 ticks. PlaybackFrames, PlaybackTicks, PlaybackTime will always be 0. HeaderCorrupted event triggered.");
553+
Console.WriteLine("WARNING: The header for this demo file is corrupted. TickRate, TickTime, CurrentTime will be 0 for ticks at the start of the demo. ParsingProgress, PlaybackFrames, PlaybackTicks, PlaybackTime will always be 0. HeaderCorrupted event triggered.");
542554

543555
if (HeaderCorrupted != null)
544556
{
@@ -571,6 +583,25 @@ public void ParseToEnd(CancellationToken token)
571583
}
572584
}
573585

586+
private void FixTickTime()
587+
{
588+
// at the beginning of demos the tickgap can be erratic, so make sure we have 10 consecutive that are the same
589+
int gap = TickGaps[1] - TickGaps[0];
590+
bool isConsecutive = true;
591+
for (int i = 1; i < TickGaps.Count - 1; i++) {
592+
if (TickGaps[i + 1] - TickGaps[i] != gap)
593+
{
594+
TickGaps.Clear();
595+
isConsecutive = false;
596+
break;
597+
}
598+
}
599+
600+
if (isConsecutive) {
601+
_ticktime = gap * TickInterval;
602+
}
603+
}
604+
574605
/// <summary>
575606
/// Parses the next tick of the demo.
576607
/// </summary>
@@ -580,6 +611,15 @@ public bool ParseNextTick()
580611
if (Header == null)
581612
throw new InvalidOperationException ("You need to call ParseHeader first before you call ParseToEnd or ParseNextTick!");
582613

614+
int consecutiveGaps = 10;
615+
if (IsHeaderCorrupted && _ticktime == 0 && IngameTick > 20) {
616+
if (TickGaps.Count < consecutiveGaps)
617+
TickGaps.Add(IngameTick);
618+
else if (TickGaps.Count == consecutiveGaps) {
619+
FixTickTime();
620+
}
621+
}
622+
583623
bool b = ParseTick();
584624

585625
for (int i = 0; i < RawPlayers.Length; i++) {

0 commit comments

Comments
 (0)