Skip to content

Commit

Permalink
added checkpoint detection
Browse files Browse the repository at this point in the history
  • Loading branch information
UncraftedName committed Nov 11, 2019
1 parent 346bbf1 commit d71727c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 38 deletions.
Binary file removed UncraftedDemoParser/demos/ademo.dem
Binary file not shown.
Binary file removed UncraftedDemoParser/demos/problem demos/ademo.dem
Binary file not shown.
8 changes: 0 additions & 8 deletions UncraftedDemoParser/demos/problem demos/other issues.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static void MainLoop() {
foreach (string demoPath in _demoPaths) {
_currentDemo = new SourceDemo(demoPath, parse: false);
string relativeDir = demoPath.GetPathRelativeTo(Application.ExecutablePath);
if (relativeDir.Substring(0, 9) == "../../../")
if (relativeDir.Length >= 9 && relativeDir.Substring(0, 9) == "../../../")
relativeDir = demoPath;
Console.WriteLine($"Parsing {relativeDir}...");
Debug.WriteLine($"Parsing {Path.GetFullPath(demoPath)}...");
Expand Down
20 changes: 13 additions & 7 deletions UncraftedDemoParser/src/ConsoleParsing/ConsoleParsingSetup.cs
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.1";
private const string Version = "0.2";

// A list of all the options; each option has the following properties:
// the character to set the option, the priority of that option:
Expand All @@ -29,6 +29,7 @@ internal static partial class ConsoleOptions {
{'p', Tuple.Create<int, bool, Action, string, Type[]>(5, false, DumpPositions, "positions", new[] {typeof(Packet)})},
{'r', Tuple.Create<int, bool, Action, string, Type[]>(5, true, DumpRegexMatches, "regex matches", new[] {typeof(ConsoleCmd)})},
{'R', Tuple.Create<int, bool, Action, string, Type[]>(8, false, SearchRecursively, null, new Type[0])},
{'j', Tuple.Create<int, bool, Action, string, Type[]>(5, false, DumpJumps, "jump list", new[] {typeof(ConsoleCmd)})}
};

// this is a separate thing cuz the above list is already supa fat
Expand All @@ -41,7 +42,8 @@ internal static partial class ConsoleOptions {
{"-r", $" <regex> (def.=\"{DefaultRegex}\") Dumps all console commands where the regex matches"},
{"-p", $" Dump every |tick|~|x,y,z|pitch,yaw,roll|~{{player2}}~... for all players"},
{"-c", $" Dump all suspicious commands and cheats"},
{"-R", $" Search folders recursively"}
{"-R", $" Search folders recursively"},
{"-h", $" Dump all jumps found in the demo"}
};

// pre-parsing/const stuff
Expand All @@ -56,11 +58,14 @@ internal static partial class ConsoleOptions {

public static void ParseOptions(string[] args) {
if (args.Length == 1) {
if (args[0] == "-h" || args[0] == "--help" || args[0] == "/?")
if (args[0] == "-h" || args[0] == "--help" || args[0] == "/?") {
PrintFullHelp();
if (args[0] == "--version")
Environment.Exit(0);
}
if (args[0] == "--version") {
Console.WriteLine($"v{Version}");
Environment.Exit(0);
Environment.Exit(0);
}
}

bool helpOptionSet = false; // handled separately
Expand Down Expand Up @@ -199,8 +204,9 @@ private static void PrintFullHelp() {

private static void PrintOptionHelp() {
foreach (string option in UserOptions.Select(option => option.Substring(0, 2))) {
if (HelpMessages.ContainsKey(option))
Console.WriteLine($"{option}{HelpMessages[option]}");
Console.WriteLine(HelpMessages.ContainsKey(option)
? $"{option}{HelpMessages[option]}"
: "No help description written");
}
}

Expand Down
2 changes: 1 addition & 1 deletion UncraftedDemoParser/src/Parser/Components/PacketFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public PacketFrame(byte[] data, ref int pointer, SourceDemo demoRef) : base(data
}

int packetLength;

switch (Type) {
case PacketType.SignOn:
packetLength = demoRef.DemoSettings.Header.SignOnLength;
Expand Down Expand Up @@ -78,6 +77,7 @@ public PacketFrame(byte[] data, ref int pointer, SourceDemo demoRef) : base(data
packetLength = 0;
break;
default:
Console.WriteLine($"unexpected packet number: {Type}");
throw new ArgumentOutOfRangeException();
}

Expand Down
10 changes: 4 additions & 6 deletions UncraftedDemoParser/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ public static void Main(string[] args) {
ConsoleOptions.ParseOptions(args);
Environment.Exit(0);
} else {
/*ConsoleOptions.ParseOptions(new[]
{@"../../demos/cm-08-inbounds-1182.dem", "-r", "-v", "-l", "-m", "-c", "-p", "-R", "-f", "ya boi", "-s"});*/
ConsoleOptions.ParseOptions(new[]
{@"escape_02_1.dem"});

const string demoName = "problem demos\\chapter7";
SourceDemo sd = new SourceDemo(new DirectoryInfo($@"..\..\demos\{demoName}.dem"), false);
//sd.QuickParse();
sd.ParseBytes();

Console.WriteLine(sd.FilteredForPacketType<Packet>().Where(packet => packet.MessageType == SvcMessageType.NetTick)
.Select(packet => packet.SvcNetMessage).Cast<NetTick>().Select(netTick => netTick.EngineTick - netTick.Tick).Distinct().QuickToString());



//File.WriteAllText(@"B:\Projects\Rider\UncraftedDemoParser\UncraftedDemoParser\bin\Debug\output.txt", sd.AsVerboseString());
//sd.UpdateBytes();
//File.WriteAllBytes(@"B:\Projects\Rider\UncraftedDemoParser\UncraftedDemoParser\bin\Debug\ademo.dem", sd.Bytes);
Expand Down
14 changes: 12 additions & 2 deletions UncraftedDemoParser/src/Utils/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using static UncraftedDemoParser.Parser.Components.Packets.Packet;

namespace UncraftedDemoParser.Utils {

public static class Extensions {

public static T[] SubArray<T>(this T[] data, int index, int length) {
public static T[] SubArray<T>(this T[] data, int index, int length, [CallerMemberName] string caller = "") {
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
if (index + length > data.Length) {
Array.Copy(data, index, result, 0, data.Length - index);
Debug.WriteLine(
$"SubArray() trying to go out of bounds, writing {(data.Length - index)} bytes instead of {length}; caller: {caller}");
} else {
Array.Copy(data, index, result, 0, length);
}

return result;
}

Expand Down Expand Up @@ -97,6 +106,7 @@ public static string QuickToString<T>(this IEnumerable<T> enumerable, string sep


public static string GetPathRelativeTo(this string relativeTo, string path) {
//Console.WriteLine();
return Uri.UnescapeDataString(new Uri(Path.GetFullPath(path)).MakeRelativeUri(new Uri(Path.GetFullPath(relativeTo))).ToString());
}
}
Expand Down
37 changes: 24 additions & 13 deletions UncraftedDemoParser/src/Utils/ParserTextUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static string AsVerboseString(this SourceDemo sd) {
}


// prints basically what listdemo prints, optionally prints the "correct" timing for length
// prints the same "wrong" tick count as listdemo+ for consistency, not including the 0th tick
public static void PrintListdemoOutput(this SourceDemo sd, bool printHeader, bool printName, TextWriter writer = null) {
if (!sd.FilteredForPacketType<Packet>().Any())
throw new FailedToParseException("this demo is missing regular packets");
Expand All @@ -85,19 +85,30 @@ public static void PrintListdemoOutput(this SourceDemo sd, bool printHeader, boo
$"\n{"Frames", -25}: {h.FrameCount}" +
$"\n{"SignOn Length", -25}: {h.SignOnLength}\n\n");
}

StringBuilder builder = new StringBuilder("\n");
foreach (ConsoleCmd i in sd.PacketsWhereRegexMatches(new Regex("autosave")))
builder.AppendLine($"Autosave command detected on tick {i.Tick}, time {i.Tick / sd.DemoSettings.TicksPerSeoncd:F3}");
foreach (ConsoleCmd i in sd.PacketsWhereRegexMatches(new Regex("#save#", RegexOptions.IgnoreCase)))
builder.AppendLine($"Save flag detected on tick {i.Tick}, time {i.Tick / sd.DemoSettings.TicksPerSeoncd:F3}");
foreach (ConsoleCmd i in sd.PacketsWhereRegexMatches(new Regex("startneurotoxins 99999")))
builder.AppendLine(
$"End of normal game detected on tick {i.Tick}, time {i.Tick / sd.DemoSettings.TicksPerSeoncd:F3}");

if (builder.Length > 1) {
Console.ForegroundColor = ConsoleColor.Yellow;
writer.Write(builder.ToString());
Regex[] regexes = {
new Regex("autosave"),
new Regex("#checkpoint#", RegexOptions.IgnoreCase),
new Regex("#save#", RegexOptions.IgnoreCase),
new Regex("startneurotoxins 99999")
};
string[] names = { // all appended by "detected on tick..."
"Autosave command",
"Checkpoint flag",
"Save flag",
"End of game"
};
ConsoleColor[] colors = {
ConsoleColor.DarkYellow,
ConsoleColor.Magenta,
ConsoleColor.Yellow,
ConsoleColor.Green
};
for (int i = 0; i < regexes.Length; i++) {
foreach (ConsoleCmd cmd in sd.PacketsWhereRegexMatches(regexes[i])) {
Console.ForegroundColor = colors[i];
writer.WriteLine($"{names[i]} detected on tick {cmd.Tick}, time {cmd.Tick / sd.DemoSettings.TicksPerSeoncd:F3}");
}
}

Console.ForegroundColor = ConsoleColor.Cyan;
Expand Down
Binary file added github-resources/example_usage.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d71727c

Please sign in to comment.