Home

Awesome

demofile npm CI

A node.js library for parsing Counter-Strike Global Offensive (CSGO) demo files. The library is also Browserify-able, and a standalone bundle that you can <script src="..."> is available in browser/bundle.js.

This library also supports streaming from GOTV broadcasts over HTTP, with DemoFile#parseBroadcast.

⚠️ This library requires Node v14 or later.

❓ Need help

Supported demo features

Running the examples

Getting started is simple:

.../demofile$ npm ci
.../demofile$ cd examples
.../demofile/examples$ npm ci

You can now run the example scripts. Take a look in the examples folder for some scripts to try out. Detailed descriptions of these scripts can be found below in the Examples section below.

If you don't have any demo files to hand, use the demos/download.sh Bash script to download the ones used for testing.

.../demofile/examples$ npx ts-node dumpfile.ts ../demos/pc419-vs-chiefs-mirage.dem
npx: installed 14 in 1.883s
Demo header: {
  magic: 'HL2DEMO',
  protocol: 4,
  networkProtocol: 13753,
  serverName: 'Counter-Strike: Global Offensive',
  clientName: 'GOTV Demo',
  mapName: 'de_mirage',
  gameDirectory: 'csgo',
  playbackTime: 2569.375,
  playbackTicks: 328880,
  playbackFrames: 164271,
  signonLength: 433479
}
...

Installation

Node

npm install --save demofile

Browser

<script src="browser/bundle.js"></script>

The DemoFile module will be available as window.demofile.

Screenshot

Using the dumpfile example:

Example output

Documentation

Auto-generated API documentation is available at saul.github.io/demofile.

ClassDescription
DemoFileRepresents a demo file for parsing.

The DemoFile object has properties which point to instances of several other classes that can be used to inspect and listen to changes in the game world:

ClassPropertyDescription
ConVarsdemoFile.conVarsManages console variables. (Only FCVAR_NOTIFY and FCVAR_REPLICATED are available.)
EntitiesdemoFile.entitiesRepresents entities and networked properties within a demo.
GameEventsdemoFile.gameEventsManages game events for a demo file. (e.g. player_death, bomb_defused)
StringTablesdemoFile.stringTablesHandles string tables for a demo file. (e.g. userinfo)
UserMessagesdemoFile.userMessagesHandles user messages for a demo file. (e.g. SayText for in-game chat messages)

There are several classes which allow access to different types of entities (e.g. players, items, props). These are summarised below:

EntityUsageDescription
NetworkabledemoFile.entities.getByHandle<br>demoFile.entities.entities.get(entIndex)Base class of all in-game entities, even non-renderable entities (e.g. CCSTeam).
BaseEntityBase class of the vast majority of in-game entities (e.g. players, weapons, all other renderable entities).
PlayerdemoFile.entities.players<br>demoFile.entities.getByUserIdRepresents an in-game player.
TeamdemoFile.entities.teams<br>player.teamRepresents a team (terrorists, counter-terrorists, spectators).
WeapondemoFile.entities.weapons<br>player.weapon<br>player.weaponsRepresents an in-game weapon (guns, grenades, knifes).
ProjectileRepresents a thrown grenade projectile (e.g. a flying smoke grenade).
GameRulesdemoFile.gameRulesRepresents the game rules and parts of the match state (e.g. round number, is warmup)

API

This library provides full access to all data available in CSGO demo files. Unlike some other libraries, demofile is feature complete and supports the latest demos. As well as providing high-level APIs to access the state of the game, low-level access is available and is not discouraged.

Note that events are fired at the end of a tick, after all entity props and string tables have been updated.

Examples

Various examples are available in the examples folder:

ExampleDescription
join-leave.tsPrint all players that join and leave the game during the course of the demo.
molotov.tsPrints the location of molotov/incendiary grenade explosions.
paintkits.tsCollects paint kits of each weapon that is used in a kill.
plant-site.tsPrints which player planted the bomb and at which site.
purchases.tsPrints which items are purchased by each player.
rank.tsAt the end of the game, prints all player ranks.
scores.tsPrints team scores after each round.
tickrate.tsPrints demo tick rate and duration in seconds.
trajectory.tsPrints grenade trajectories and who threw them.
⚠ Advanced: dumpfile.tsAdvanced example of recreating coloured chat messages, round scores and the kill feed.

Print kills

const fs = require("fs");
const demofile = require("demofile");

const demoFile = new demofile.DemoFile();

demoFile.gameEvents.on("player_death", e => {
  const victim = demoFile.entities.getByUserId(e.userid);
  const victimName = victim ? victim.name : "unnamed";

  // Attacker may have disconnected so be aware.
  // e.g. attacker could have thrown a grenade, disconnected, then that grenade
  // killed another player.
  const attacker = demoFile.entities.getByUserId(e.attacker);
  const attackerName = attacker ? attacker.name : "unnamed";

  const headshotText = e.headshot ? " HS" : "";

  console.log(`${attackerName} [${e.weapon}${headshotText}] ${victimName}`);
});

demoFile.parseStream(fs.createReadStream("test.dem"));

/* Outputs:

HS [cz75a HS] flusha
Lekr0 [ak47 HS] friberg
KRIMZ [ak47] HS
JW [mac10 HS] Mixwell
JW [hegrenade] HS
JW [mac10 HS] Magisk

*/

Print player information when it changes

const fs = require("fs");
const demofile = require("demofile");

const demoFile = new demofile.DemoFile();

demoFile.stringTables.on("update", e => {
  if (e.table.name === "userinfo" && e.userData != null) {
    console.log("\nPlayer info updated:");
    console.log(e.entryIndex, e.userData);
  }
});

demoFile.parseStream(fs.createReadStream("test.dem"));

/* Outputs:

Player info updated:
0 {
  xuid: Long { low: 0, high: 0, unsigned: false },
  name: 'ESEA SourceTV',
  userId: 2,
  guid: 'BOT',
  friendsId: 0,
  friendsName: '',
  fakePlayer: true,
  isHltv: false
}

Player info updated:
1 {
  xuid: Long { low: 32578248, high: 17825793, unsigned: false },
  name: 'PC419 m0nt-S-',
  userId: 3,
  guid: 'STEAM_1:0:16289124',
  friendsId: 32578248,
  friendsName: '',
  fakePlayer: false,
  isHltv: false
}

[repeated for other players]
*/

Useful links

Contributing

Please read the Contributing Guidelines to learn how you can help out on the project.