|
|
|
|
@ -0,0 +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];
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|