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

Commit 0d0fd86

Browse files
committed
Add TimeFixed event
1 parent 730b797 commit 0d0fd86

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

DemoInfo/DemoParser.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public class DemoParser : IDisposable
3939
/// </summary>
4040
public event EventHandler<HeaderParsedEventArgs> HeaderCorrupted;
4141

42+
/// <summary>
43+
/// Raised when the time variables have been fixed for a demo with a corrupted header
44+
/// </summary>
45+
public event EventHandler<TimeFixedEventArgs> TimeFixed;
46+
4247
/// <summary>
4348
/// Occurs when the match started, so when the "begin_new_match"-GameEvent is dropped.
4449
/// This usually right before the freezetime of the 1st round. Be careful, since the players
@@ -269,11 +274,6 @@ public string Map {
269274
/// </summary>
270275
public bool IsHeaderCorrupted { get; private set; }
271276

272-
/// <summary>
273-
/// Used to sample gap in ticks when header is corrupted
274-
/// </summary>
275-
internal List<int> TickGaps = new List<int>();
276-
277277
/// <summary>
278278
/// Gets the participants of this game
279279
/// </summary>
@@ -470,10 +470,11 @@ public float TickRate {
470470
/// How long a tick of the demo is in s^-1
471471
/// </summary>
472472
/// <value>The tick time.</value>
473-
private float _ticktime;
474473
public float TickTime {
475474
get { return IsHeaderCorrupted ? _ticktime : this.Header.PlaybackTime / this.Header.PlaybackFrames; }
476475
}
476+
private List<int> tickGaps = new List<int>();
477+
private float _ticktime;
477478

478479
/// <summary>
479480
/// Gets the parsing progess. 0 = beginning, ~1 = finished (it can actually be > 1, so be careful!)
@@ -550,12 +551,13 @@ public void ParseHeader()
550551

551552
if (IsHeaderCorrupted)
552553
{
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.");
554+
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.");
555+
Console.WriteLine("HeaderCorrupted event raised, TimeFixed event will be raised when time variables are repaired.");
554556

555557
if (HeaderCorrupted != null)
556558
{
557559
HeaderCorrupted(this, new HeaderParsedEventArgs(Header));
558-
}
560+
}
559561
}
560562

561563
if (HeaderParsed != null)
@@ -586,19 +588,22 @@ public void ParseToEnd(CancellationToken token)
586588
private void FixTickTime()
587589
{
588590
// 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];
591+
int gap = tickGaps[1] - tickGaps[0];
590592
bool isConsecutive = true;
591-
for (int i = 1; i < TickGaps.Count - 1; i++) {
592-
if (TickGaps[i + 1] - TickGaps[i] != gap)
593+
for (int i = 1; i < tickGaps.Count - 1; i++) {
594+
if (tickGaps[i + 1] - tickGaps[i] != gap)
593595
{
594-
TickGaps.Clear();
596+
tickGaps.Clear();
595597
isConsecutive = false;
596598
break;
597599
}
598600
}
599601

600602
if (isConsecutive) {
601603
_ticktime = gap * TickInterval;
604+
605+
if (TimeFixed != null)
606+
TimeFixed(this, new TimeFixedEventArgs());
602607
}
603608
}
604609

@@ -611,11 +616,11 @@ public bool ParseNextTick()
611616
if (Header == null)
612617
throw new InvalidOperationException ("You need to call ParseHeader first before you call ParseToEnd or ParseNextTick!");
613618

614-
int consecutiveGaps = 10;
615619
if (IsHeaderCorrupted && _ticktime == 0 && IngameTick > 20) {
616-
if (TickGaps.Count < consecutiveGaps)
617-
TickGaps.Add(IngameTick);
618-
else if (TickGaps.Count == consecutiveGaps) {
620+
int consecutiveGaps = 10;
621+
if (tickGaps.Count < consecutiveGaps)
622+
tickGaps.Add(IngameTick);
623+
else if (tickGaps.Count == consecutiveGaps) {
619624
FixTickTime();
620625
}
621626
}
@@ -1522,6 +1527,7 @@ public void Dispose ()
15221527
this.FlashNadeExploded = null;
15231528
this.HeaderParsed = null;
15241529
this.HeaderCorrupted = null;
1530+
this.TimeFixed = null;
15251531
this.MatchStarted = null;
15261532
this.NadeReachedTarget = null;
15271533
this.PlayerKilled = null;

DemoInfo/Events.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public class TickDoneEventArgs : EventArgs
2121
{
2222
}
2323

24+
public class TimeFixedEventArgs : EventArgs
25+
{
26+
}
27+
2428
public class MatchStartedEventArgs : EventArgs
2529
{
2630
}

0 commit comments

Comments
 (0)