diff --git a/readme.md b/readme.md index f062125..17a927f 100644 --- a/readme.md +++ b/readme.md @@ -22,3 +22,8 @@ Analysis of TF2 Demo files. To help me get more _data driven_ approaches to get Parsing the demo files. Hopefully once, maybe multiple times. Only intrested after the start of the official match. Warmup should not count. + + +Big up for (DemoInfo)[https://github.com/StatsHelix/demoinfo/tree/master/DemoInfo] for providing some inspiration + +And thanks to the lads at (demostf)[https://github.com/demostf/parser] for TF2 specific code \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs index 3751555..5d3e1a1 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,2 +1,112 @@ // See https://aka.ms/new-console-template for more information +using System.Text; +using DemoParser; + Console.WriteLine("Hello, World!"); +//TODO: Config +const string _DemoPath = "/home/sherwood/projects/tf2DemoAnalysis/demos/"; + +var demoFilePaths = Directory.GetFiles(_DemoPath, "*.dem"); + +foreach(var currentDemoPath in demoFilePaths){ + var demo = File.OpenRead(Path.Combine(_DemoPath, currentDemoPath)); + + var info = DemoParsing.ReadDemoHeader(demo); + Console.WriteLine($"{currentDemoPath}: Header parsed "); +} + + + + + + +namespace DemoParser{ + + public record demoHeader( + int demoProtocol, + int networkProtocol, + string ServerName, + string ClientName, + string MapName, + string GameDirectory, + float playbackTime, + int Ticks, + int Frames, + int SignOnLength + ); + + public class DemoParsing{ + public static demoHeader ReadDemoHeader(FileStream stream){ + // 8 chars + // int + // int + // 260 chars + // 260 chars + // 260 chars + // 260 chars + //float + //int + //int + //int + + //Read the header. + + + // If the first 8 chars are not "HL2DEMO" then it is not valid. + var buffer = new Byte[8]; + stream.Read(buffer, 0, 8); + + if(Encoding.ASCII.GetString(buffer) != "HL2DEMO\0"){ + throw new Exception($"DemoFile not valid! Filestamp: {Encoding.ASCII.GetString(buffer)}"); + } + + return new demoHeader( + readInt(stream), + readInt(stream), + readString(stream, 260, Encoding.ASCII), + readString(stream, 260, Encoding.ASCII), + readString(stream, 260, Encoding.ASCII), + readString(stream, 260, Encoding.ASCII), + readFloat(stream), + readInt(stream), + readInt(stream), + readInt(stream) + ); + + + } + + public static int readInt(FileStream stream, int offset = 0){ + var buffer = new Byte[4]; + stream.Read(buffer, offset, 4); + return BitConverter.ToInt32(buffer,0); + } + + public static float readFloat(FileStream stream, int offset = 0){ + var buffer = new Byte[4]; + stream.Read(buffer, offset, 4); + return BitConverter.ToSingle(buffer,0); + } + + //Reads until end of string + public static string readString(FileStream stream, int maxLength, Encoding encoding,int offset = 0){ + var buffer = new Byte[maxLength]; + var readBytes = stream.Read(buffer, offset, maxLength); + + var readString = encoding.GetString(buffer).Split('\0')[0]; + + return readString; + + } + + + } + + // static class helper{ + + // public static string ReadCString(this BinaryReader reader, int length, Encoding encoding) + // { + // return encoding.GetString(reader.ReadBytes(length)).Split(new char[] { '\0' }, 2)[0]; + // } + // } +} \ No newline at end of file