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

Commit d3bd3ac

Browse files
committed
TickTime and TickRate fixed for corrupted headers. HeaderCorrupted event and IsCorruptedHeader bool added.
1 parent dfa5ea0 commit d3bd3ac

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
@@ -253,6 +253,16 @@ public string Map {
253253
/// <value>The header.</value>
254254
public DemoHeader Header { get; private set; }
255255

256+
/// <summary>
257+
/// True when header is corrupted
258+
/// </summary>
259+
public bool IsHeaderCorrupted { get; private set; }
260+
261+
/// <summary>
262+
/// Used to sample gap in ticks when header is corrupted
263+
/// </summary>
264+
internal List<int> TickGaps = new List<int>();
265+
256266
/// <summary>
257267
/// Gets the participants of this game
258268
/// </summary>
@@ -436,15 +446,16 @@ public string TFlag
436446
/// </summary>
437447
/// <value>The tick rate.</value>
438448
public float TickRate {
439-
get { return this.Header.PlaybackFrames / this.Header.PlaybackTime; }
449+
get { return IsHeaderCorrupted ? 1/_ticktime : this.Header.PlaybackFrames / this.Header.PlaybackTime; }
440450
}
441451

442452
/// <summary>
443453
/// How long a tick of the demo is in s^-1
444454
/// </summary>
445455
/// <value>The tick time.</value>
456+
private float _ticktime;
446457
public float TickTime {
447-
get { return this.Header.PlaybackTime / this.Header.PlaybackFrames; }
458+
get { return IsHeaderCorrupted ? _ticktime : this.Header.PlaybackTime / this.Header.PlaybackFrames; }
448459
}
449460

450461
/// <summary>
@@ -518,10 +529,11 @@ public void ParseHeader()
518529
throw new InvalidDataException("Invalid Demo-Protocol");
519530

520531
Header = header;
532+
IsHeaderCorrupted = (header.PlaybackTime == 0);
521533

522-
if (header.PlaybackTime == 0)
534+
if (IsHeaderCorrupted)
523535
{
524-
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.");
536+
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.");
525537

526538
if (HeaderCorrupted != null)
527539
{
@@ -554,6 +566,25 @@ public void ParseToEnd(CancellationToken token)
554566
}
555567
}
556568

569+
private void FixTickTime()
570+
{
571+
// at the beginning of demos the tickgap can be erratic, so make sure we have 10 consecutive that are the same
572+
int gap = TickGaps[1] - TickGaps[0];
573+
bool isConsecutive = true;
574+
for (int i = 1; i < TickGaps.Count - 1; i++) {
575+
if (TickGaps[i + 1] - TickGaps[i] != gap)
576+
{
577+
TickGaps.Clear();
578+
isConsecutive = false;
579+
break;
580+
}
581+
}
582+
583+
if (isConsecutive) {
584+
_ticktime = gap * TickInterval;
585+
}
586+
}
587+
557588
/// <summary>
558589
/// Parses the next tick of the demo.
559590
/// </summary>
@@ -563,6 +594,15 @@ public bool ParseNextTick()
563594
if (Header == null)
564595
throw new InvalidOperationException ("You need to call ParseHeader first before you call ParseToEnd or ParseNextTick!");
565596

597+
int consecutiveGaps = 10;
598+
if (IsHeaderCorrupted && _ticktime == 0 && IngameTick > 20) {
599+
if (TickGaps.Count < consecutiveGaps)
600+
TickGaps.Add(IngameTick);
601+
else if (TickGaps.Count == consecutiveGaps) {
602+
FixTickTime();
603+
}
604+
}
605+
566606
bool b = ParseTick();
567607

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

0 commit comments

Comments
 (0)