From 7f8749228146184592f1bfdd4b93f9f58102f4f9 Mon Sep 17 00:00:00 2001 From: Beherith Date: Mon, 20 Jan 2025 09:57:11 +0100 Subject: [PATCH] Dump entire demo and all packets to console if needed --- README.md | 10 ++++++++++ parse-demo.ts | 21 +++++++++++++++++++++ src/demo-parser.ts | 13 ++++++++++--- src/model/demo-model.ts | 1 + 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 parse-demo.ts diff --git a/README.md b/README.md index 4dc1e94..53960f2 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,13 @@ import { DemoParser } from "sdfz-demo-parser"; console.log(demo.info.spectators[1].name); // [Fx]Jazcash })(); ``` + +## Dump entire demo + +To just dump the entire contents and all packets to a json on the console + +`npm install` + +`npm run build` + +`npx ts-node parse-demo.ts mydemo.sdfz > mydemo.json` diff --git a/parse-demo.ts b/parse-demo.ts new file mode 100644 index 0000000..4045d56 --- /dev/null +++ b/parse-demo.ts @@ -0,0 +1,21 @@ +import { DemoParser } from "./src/demo-parser"; +import * as fs from 'fs'; + +(async () => { + const demoPath = process.argv[2]; + + if (!demoPath) { + console.error("Please provide the path to the demo file as a command line argument."); + process.exit(1); + } + + const parser = new DemoParser(); + + try { + const demo = await parser.parseDemo(demoPath); + + console.log(JSON.stringify(demo, null, 2)); + } catch (error) { + console.error("Error parsing demo file:", error); + } +})(); \ No newline at end of file diff --git a/src/demo-parser.ts b/src/demo-parser.ts index f062e33..3bd2d65 100644 --- a/src/demo-parser.ts +++ b/src/demo-parser.ts @@ -73,6 +73,7 @@ export class DemoParser { protected header!: DemoModel.Header; protected statistics!: DemoModel.Statistics.Statistics; protected chatlog: DemoModel.ChatMessage[] = []; + protected packets: DemoModel.Packet.AbstractPacket[] = []; public onPacket: Signal = new Signal(); @@ -121,7 +122,9 @@ export class DemoParser { }; } - const { startPositions, factions, colors } = await this.parsePackets(this.bufferStream.read(this.header.demoStreamSize)); + const { startPositions, factions, colors, packets} = await this.parsePackets(this.bufferStream.read(this.header.demoStreamSize)); + + this.packets = packets; this.statistics = this.parseStatistics(this.bufferStream.read()); @@ -155,7 +158,8 @@ export class DemoParser { header: this.header, script: script.toString(), statistics: this.statistics, - chatlog: this.chatlog + chatlog: this.chatlog, + packets: this.packets }; } @@ -188,6 +192,7 @@ export class DemoParser { const startPositions: { [teamId: number]: DemoModel.Command.Type.MapPos } = {}; const factions: { [playerId: number]: string } = {}; let colors: Array<{ teamID: number, r: number, g: number, b: number }> = []; + const packets: DemoModel.Packet.AbstractPacket[] = []; let counter = 0; while (bufferStream.readStream.readableLength > 0) { @@ -198,6 +203,8 @@ export class DemoParser { const packet = packetParser.parsePacket(packetData, modGameTime, this.header.wallclockTime - this.header.gameTime); if (packet && packet.data) { + packets.push(packet); + if (packet.data.playerNum !== undefined && this.config.includePlayerIds?.length && !this.config.includePlayerIds?.includes(packet.data.playerNum)) { continue; } @@ -234,7 +241,7 @@ export class DemoParser { } } - return { startPositions, factions, colors }; + return { startPositions, factions, colors, packets }; } protected parseStatistics(buffer: Buffer) : DemoModel.Statistics.Statistics { diff --git a/src/model/demo-model.ts b/src/model/demo-model.ts index 4ddf5c4..429cd9c 100644 --- a/src/model/demo-model.ts +++ b/src/model/demo-model.ts @@ -5,6 +5,7 @@ export namespace DemoModel { script: string; statistics?: Statistics.Statistics; chatlog?: ChatMessage[]; + packets?: DemoModel.Packet.AbstractPacket[]; } export namespace Info {