Skip to content

Introducing CS2DemoKit - A High-Performance .NET Counter-Strike 2 Demo Toolkit

Today we’re announcing CS2DemoKit, a fast Counter-Strike 2 demo parsing and analysis toolkit that has been in the works for about 18 months. The goal is to give the Counter-Strike community high-quality, well-maintained libraries that anyone can use to build the next generation of analysis tools. From the start, that meant fast libraries for reading, working with, and analyzing both server-side and POV (client-side) demos of real matches from a variety of sources.

If you want to know more about the CS2OpenDev initiative and what we’re committing to for the CS community, take a look at our CS2OpenDev organization introduction.

Three NuGet packages, all released with the same version number, targeting net10.0. There are no UI dependencies and nothing to install beyond the packages themselves:

Package What it gives you
CS2DemoKit.Parser Reads a .dem file: frames and network messages, 270-plus typed game events, and optional entity tracking (player positions, health, weapons and so on) that you can seek to any tick.
CS2DemoKit.Analysis Turns a parsed demo into numbers: per-player stats, highlights, clip planning, and a 3D line-of-sight engine, all driven by rule files.
CS2DemoKit.Analysis.Rules The core of the rules language (parser, type checker and so on) on its own, with zero dependencies. Useful for editors and validation services.
flowchart TD
  DEM[".dem bytes"] --> P["CS2DemoKit.Parser<br/>three-pass parse"]
  P --> PD["ParsedDemo (immutable)<br/>frames, game events, players,<br/>schema, health"]
  PD --> ET["EntityTracker (opt-in replay)<br/>entity bit stream to typed lanes"]
  ET --> SEAM["frozen read contract<br/>(two small interfaces)"]
  SEAM --> SDK["CS2OpenDev.Sdk.Entities wrappers<br/>(external package)"]
  PD --> AN["CS2DemoKit.Analysis<br/>Build: YAML to state graph<br/>Evaluate: one pass over frames"]
  ET --> SCAN["EntityChangeScanner<br/>per-frame change digests"]
  SCAN --> AN
  RULES["CS2DemoKit.Analysis.Rules<br/>lexer / parser / checker / hasher<br/>(zero dependencies)"] --> AN
  BAKE["map bake<br/>(collision triangles, shipped separately)"] --> VIS["Visibility engine (in CS2DemoKit.Analysis)<br/>raycasts against map geometry"]
  VIS -- "only when a rule needs it" --> AN
  AN --> OUT["metric tables, golden stats,<br/>highlights to clip plans"]
How the three packages, the opt-in entity replay, the visibility engine, and the external SDK contract fit together.

CS2DemoKit.Analysis depends on the other two, so installing it alone gets you a matching set of all three. The smallest useful program is two statements:

using CS2DemoKit.Parser;
using CS2DemoKit.Analysis;
using CS2DemoKit.Analysis.Yaml;
ParsedDemo demo = MemoryMappedDemoSource.ParseFile("match.dem");
AnalysisRun run = DemoAnalysis.Run(demo, YamlConfigLoader.LoadShippedEmbedded().Rulesets);

Four starter rulesets (KAST, per-player stats, weapon stats, and post-plant multi-kills) are built into the package, so you get a working scoreboard without needing any custom rule files. When you want different numbers based on any desired situation or event in a match, you write a YAML file, not C#. The whole rating calculation lives in those files, coefficients included, so if you disagree with how something is computed, you edit a file instead of forking the library.

On a ten-core machine, a 290 MB full-match demo parses in about 384 ms. Evaluating the four starter rulesets took about 1.4 seconds when we measured our baseline, and later work (described below) has cut evaluation time by about a fifth since then. Those numbers, the demos they came from, and the ways they can mislead you are all written down in docs/perf/baseline.md in the repo.

Those are measurements of CS2DemoKit against itself, which is useful for telling whether a change helped but says nothing about how it compares to other parsers. We have shared more information about the comparisons to other libraries in CS2DemoKit Technical Blog - Part 1.

Stats and scoreboards, without writing code. The rules language is the main way in. You describe what you want measured in a small YAML file (count these events, add up this value, keep the highest value over the match) and the engine goes through every round of the demo and produces the numbers. A ruleset can measure each player, each side, each grenade thrown, or the match as a whole. A rule file can’t crash the parser. The worst that happens is a validation error telling you what to fix, with the line and column and a “did you mean” list for typos.

Match review and highlight reels. Highlights are per-round achievements stamped with the tick they happened on, and CS2DemoKit.Analysis.Clips turns them into clip windows you can hand to any video tool: it adds lead-in time, merges overlapping clips, and groups them by round. Rendering the video is up to you. Deciding which twelve seconds matter is what this does.

Aim and movement analysis. The shot and shot_landed views have sixteen and twenty-one attributes respectively, including counter-strafe detection, spray tracking, first-bullet detection and, if you provide map geometry, how far the crosshair moved after first seeing an enemy and how much it overshot. Rules can also read each player’s own input, rebuilt from the commands their client sent: buttons, movement and view angles. This part of the toolkit comes with the most caveats (see below).

Interactive tools. If you want to build a demo browser that shows raw bytes next to the decoded entity changes, the API is designed for it. The parser gives you every byte offset an inspector needs: the raw byte range of each frame and each message, decompression of a single frame on demand, and an entity tracker you can jump to any frame, snapshot, or peek at without disturbing its state. If you want per-frame state for your own code, such as player positions for a radar, a frame observer hands you each frame’s values during the same run that computes the stats, so the demo isn’t decoded twice.

Rule tools don’t need the engine at all. CS2DemoKit.Analysis.Rules uses only the .NET base library, with no protobuf, YAML, or demo parser, so if you’re writing an editor extension, a linter, or a service that validates uploaded rules, you can check a rule expression without pulling in anything that knows what a demo is.

Batch pipelines. Nothing in the library depends on a UI toolkit or a folder layout you can’t override. Results are deterministic: the same demo produces the same output every time, down to the order of its warnings, and the multi-threaded geometry build produces the identical tree with one thread or thirty-two.

The compatibility work you’d otherwise do yourself. Tournament demos recorded through SourceTV look the same as Valve matchmaking demos in the file header, but use a different event to mark the end of a round. round_end, the event most stats tools reach for first, has never fired in any demo we’ve checked. The winner of each round is read from the server’s own round-win status instead of guessed from who’s left alive, which matters when a team surrenders. Player positions are stored as a grid cell plus an offset, not as a plain vector. A field that was never sent is not the same as a field that is zero. None of that is interesting work, but all of it has to be right, and it’s already done.

Several of these look like they should work, so it’s worth being clear.

It is not a demo player. There’s no renderer, no 2D radar, no playback controls, and no UI of any kind. It gives you the data a player would draw. Drawing it is a different project.

It doesn’t find or download demos. There’s no Steam integration, match-history API, or download manager. You give it a .dem file you already have.

It doesn’t include map geometry. Line of sight is computed from map collision geometry (“bakes”), because the demo doesn’t contain a usable “this player can see that one” signal. That geometry comes from Valve’s files and is large, so it’s distributed separately rather than inside a NuGet package. The package knows how to find a bake (via an environment variable or by searching parent folders) and reports cleanly when there isn’t one. Without a bake, visibility-based stats report zero instead of guessing, and the analysis output includes a coverage note naming the stat and what’s missing.

It doesn’t know the exact shape of bombsites. Telling whether a player is standing inside a bombsite needs the map’s trigger volumes, and those aren’t in a demo file. You can register your own zone provider and use it from rules like any built-in one. What is available is the map’s named location for each player (like BombsiteA), which also tells you which site the bomb was planted at. That covers most of what people actually want.

It is not an anti-cheat, and we’re not going to make it one. A demo is a recording of what the server sent, and several of the things people want when they’re trying to prove something aren’t in it. The aim-punch (recoil) values are the clearest example. They don’t currently decode to a real recoil value: on our sample demo they come back clustered near -94 and +89 degrees, when real recoil is a couple of degrees. So the library rejects every value from that column instead of reporting a 300-degree recoil error as data.

It can’t recover data that was never sent. flash_duration is a good example: the demo stores how long a flash was set to last, not a live countdown, and the two fields you’d need to work out how much blind time is left are never sent at all. We confirmed that across 230,856 frames of player data. When a value can’t be recovered, the library says so instead of making up a plausible one.

POV demos decode partially. A demo recorded on a player’s own client contains updates for entities that client never received a full copy of. We skip those instead of failing, which gives you a usable but incomplete picture of entity state. EntityTracker.DeltaUnknownCount tells you how many were skipped, so you can check before trusting per-tick entity values. Game events aren’t affected: anything based on kills, deaths, damage or shots is correct either way.

It is not a server plugin, and it doesn’t read a live connection. The messages in a demo file are the same ones a server sends to players during a match, so a live mode isn’t impossible in principle. Nothing in the library supports one today.

0.14.0 is on nuget.org now. We’re pre-1.0 and the API still changes between minor versions. Every breaking change is written down in docs/releasing.md with what changed and how to fix your code, and each minor release has a migration guide next to it in docs/.

These are the four things in front of us:

Make evaluation faster. Parsing is about 21% of the time it takes to load and analyze a demo. Evaluating rules is 72 to 81%, and roughly 30% of that is .NET garbage collection. The last round of work stopped re-sending every player’s full state on every frame and sent only what changed. That cut evaluation time by 22% and total time by 17% across fifteen demos, and every one of them got faster. There’s more of that kind of improvement available.

Decode the aim-punch values properly. Until they decode correctly, spray-control metrics measure our decoder rather than the player, and we’d rather say so in the documentation than ship a number that only looks right.

Run full-match demos in CI. Today the automated tests run against a small four-round sample, so the tests that compare results across full matches only run on developer machines. Fixing that means getting full-match demos somewhere CI can reach without committing them to the repository.

1.0 when the API stops changing. Not on a fixed date, but once a release goes by without a compatibility note.

If you use it and something is wrong, missing, or worse than what you were using before, file an issue on the GitHub repo. A bug report with a demo attached is far easier to fix than one without.

If you want the technical details, there’s a three-part series: Part 1: the parser, Part 2: the analysis engine, and Part 3: the rules language.