saul / demofile-net

Blazing fast cross-platform demo parser library for Counter-Strike 2, written in C#.
MIT License
76 stars 7 forks source link

Unexpected EOF aka InvalidOperationException on 2 demos #43

Closed xGuysOG closed 4 months ago

xGuysOG commented 4 months ago

Research

Description

I have 2 demos that are resulting in EOF. Demos works fine on 2D demo sites and in cs2 itself. It seems to be a contained error to this parser.

Code to reproduce

public static async Task Main(string[] args)
    {
        bool bombExploded = false;
        var demo = new DemoParser();

        demo.Source1GameEvents.BombExploded += e =>
        {
            Console.WriteLine("Bomb exploded");
            bombExploded = true;
        };
        demo.Source1GameEvents.PlayerDeath += e =>
        {
            Console.WriteLine($"{e.Attacker?.PlayerName} from {e.Attacker?.Team} [{e.Weapon}] {e.Player?.PlayerName} from {e.Player?.Team}");
        };

        demo.EntityEvents.CCSGameRulesProxy.AddChangeCallback(proxy => proxy.GameRules?.RoundStartCount, (proxy, _, _) =>
        {
            var roundStartCountStuff = proxy.GameRules!.RoundStartCount;
            var roundStartCountStuff2 = proxy.GameRules!.RoundStartRoundNumber;
            bombExploded = false;

            Console.WriteLine($"count 1{roundStartCountStuff} count 2 {roundStartCountStuff2} ct score: {demo.TeamCounterTerrorist.Score} t score: {demo.TeamTerrorist.Score}");
        });

        demo.Source1GameEvents.CsWinPanelMatch += e =>
        {
            Console.WriteLine("WIn match has ended");
        };

        await demo.ReadAllAsync(File.OpenRead(baseDirectory + "\\FatalUtility_vs_EgedalEsport_map1_de_nuke_2024-01-16_18-00-45_6599f0f1ca2465f94caa2607_1705428045421.dem"));
        //await demo.
        Console.WriteLine("\nFinished!");
    }

Affected demos

https://drive.google.com/drive/folders/1dgPmwyjy5UlEsDW-eF8rdyRNFxAmEzCa?usp=sharing

saul commented 4 months ago

The reason these demos are failing to read fully is because they are not complete recordings. They have enough data for the match end at least, but after that they appear to have incomplete data. I have just raised a PR (#44) to not throw when trying to read CDemoFileInfo (which is at the end of the file).

Even when the PR is merged, you will still need to wrap ReadAllAsync in a try/catch to catch EndOfStreamException. I'm not a huge fan of having to catch errors, so I may make further API changes to catch this at the top level.

saul commented 4 months ago

The fix is released as v0.11.1