Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, the script should be added to https://docs.npmjs.com/cli/v11/configuring-npm/package-json#bin and then people can use npx to run the parser without forking/installing etc

21 changes: 21 additions & 0 deletions parse-demo.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
13 changes: 10 additions & 3 deletions src/demo-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DemoModel.Packet.AbstractPacket> = new Signal();

Expand Down Expand Up @@ -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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely do not want to aggregate unconditionally all packets in memory. The aggregation must be done on the client side.


this.statistics = this.parseStatistics(this.bufferStream.read());

Expand Down Expand Up @@ -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
};
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -234,7 +241,7 @@ export class DemoParser {
}
}

return { startPositions, factions, colors };
return { startPositions, factions, colors, packets };
}

protected parseStatistics(buffer: Buffer) : DemoModel.Statistics.Statistics {
Expand Down
1 change: 1 addition & 0 deletions src/model/demo-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export namespace DemoModel {
script: string;
statistics?: Statistics.Statistics;
chatlog?: ChatMessage[];
packets?: DemoModel.Packet.AbstractPacket[];
}

export namespace Info {
Expand Down
Loading