Skip to content

Commit

Permalink
added arbitrary flag detection
Browse files Browse the repository at this point in the history
  • Loading branch information
UncraftedName committed Nov 16, 2019
1 parent 310a3b7 commit 142951d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace UncraftedDemoParser.ConsoleParsing {
// I tried to make this better than it was. I'm not sure if I did, but at least I made some tasty spaghetti.
internal static partial class ConsoleOptions {

private const string Version = "0.6";
private const string Version = "0.7";

// A list of all the options; each option has the following properties:
// the character to set the option, the priority of that option:
Expand Down
31 changes: 22 additions & 9 deletions UncraftedDemoParser/src/Utils/ParserTextUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,19 @@ public static void PrintListdemoOutput(this SourceDemo sd, bool printHeader, boo
Regex[] regexes = {
new Regex("^autosave$"),
new Regex("^autosavedangerous$"),
new Regex("^autosavedangerousissafe$"),
new Regex("[ \\t]*echo #checkpoint#[ \\t]*$", RegexOptions.IgnoreCase),
new Regex("[ \\t]*echo #SAVE#$[ \\t]*", RegexOptions.IgnoreCase),
new Regex("^autosavedangerousissafe$"),
new Regex("^startneurotoxins 99999$")
};
string[] names = { // all appended by "detected on tick..."
"Autosavedangerous command",
"Autosavedangerousissafe command",
"Autosave command",
"Checkpoint flag",
"Save flag",
"End of game"
};
ConsoleColor[] colors = {
ConsoleColor.DarkYellow,
ConsoleColor.DarkYellow,
ConsoleColor.DarkYellow,
ConsoleColor.Magenta,
ConsoleColor.Yellow,
ConsoleColor.Green
};
for (int i = 0; i < regexes.Length; i++) {
Expand All @@ -116,11 +110,30 @@ public static void PrintListdemoOutput(this SourceDemo sd, bool printHeader, boo
writer.WriteLine($"{names[i]} detected on tick {cmd.Tick}, time {cmd.Tick / sd.DemoSettings.TicksPerSecond:F3}");
}
}
// matches any flag like 'echo #some flag name#`
Regex flagMatcher = new Regex(@"^\s*echo\s+#(?<flag_name>\S*?)#\s*$", RegexOptions.IgnoreCase);

// gets all flags, then groups them by the flag type, and sorts the ticks of each type
// in this case, #flag# is treated the same as #FLAG#
var flagGroups = sd.PacketsWhereRegexMatches(flagMatcher)
.Select(cmd =>
Tuple.Create(flagMatcher.Matches(cmd.Command)[0].Groups["flag_name"].Value.ToUpper(), cmd.Tick))
.GroupBy(tuple => tuple.Item1)
.ToDictionary(tuples => tuples.Key,
tuples => tuples.Select(tuple => tuple.Item2).Distinct().OrderBy(i => i).ToList())
.Select(pair => Tuple.Create(pair.Key, pair.Value));

foreach (Tuple<string, List<int>> flagType in flagGroups) {
Console.ForegroundColor = Console.ForegroundColor == ConsoleColor.Yellow ? ConsoleColor.Magenta : ConsoleColor.Yellow;
foreach (int i in flagType.Item2) {
Console.WriteLine($"'{flagType.Item1}' flag detected on tick {i}, time {i / sd.DemoSettings.TicksPerSecond:F3}");
}
}

Console.ForegroundColor = ConsoleColor.Cyan;
writer.WriteLine(
$"\n{"Calculated total time", -25}: {(sd.TickCount() - 1) / sd.DemoSettings.TicksPerSecond}" +
$"\n{"Calculated total ticks", -25}: {sd.TickCount() - 1}\n");
$"\n{"Last tick time: ", -25}: {(sd.TickCount() - 1) / sd.DemoSettings.TicksPerSecond}" +
$"\n{"Last tick: ", -25}: {sd.TickCount() - 1}\n");
Console.ForegroundColor = ConsoleColor.Gray;
}
}
Expand Down
2 changes: 1 addition & 1 deletion UncraftedDemoParser/src/Utils/ParserUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static List<PacketFrame> ControlPacketFrameList(this SourceDemo sd) {

public static int TickCount(this SourceDemo sd) {
List<int> packetTicks = sd.FilteredForPacketType<Packet>().Select(packet => packet.Tick).Where(i => i >= 0).ToList();
return packetTicks.Max() - packetTicks.Min() + 1;
return packetTicks.Max() - packetTicks.Min() + 1; // accounts for 0th tick
}


Expand Down

0 comments on commit 142951d

Please sign in to comment.