public virtual async Task ReadMessagesAsync(SourceBufferReader buf)
        {
            var newEngine    = Game.MaxSplitscreenClients.HasValue;
            var minBytesLeft = newEngine ? 6 : 5;

            while (buf.BytesLeft > minBytesLeft)
            {
                var code = buf.ReadByte();
                var type = Game.DefaultMessages[code];

                if (type == null)
                {
                    throw new MessageTypeException(code, buf.CurrentByte);
                }

                var message = (DemoMessage)Activator.CreateInstance(type);

                message.Code = code;
                message.Tick = buf.Read <int>();
                message.Slot = newEngine ? buf.Read <byte>() : default;

                await message.Read(buf, this).ConfigureAwait(false);

                Messages.Add(message);
            }
        }
Beispiel #2
0
        public virtual async Task <SourceDemo> ParseAsync(byte[] content)
        {
            var demo = new SourceDemo();
            var buf  = new SourceBufferReader(content);

            if (Options.ReadHeader)
            {
                await demo.ReadHeader(buf).ConfigureAwait(false);
            }

            if (Options.ReadMessages)
            {
                await Task.Run <SourceGame>(() => demo.Game = GameBuilder.Invoke(demo)).ConfigureAwait(false);

                if (!Options.ReadHeader)
                {
                    buf.SeekBytes(1072);
                }

                await demo.ReadMessagesAsync(buf).ConfigureAwait(false);

                foreach (var message in demo.Messages)
                {
                    if (Options.ReadPackets && message is Packet pa)
                    {
                        await demo.Read(pa).ConfigureAwait(false);

                        continue;
                    }
                    if (Options.ReadStringTables && message is StringTables st)
                    {
                        await demo.Read(st).ConfigureAwait(false);

                        continue;
                    }
                    if (Options.ReadDataTables && message is DataTables dt)
                    {
                        await demo.Read(dt).ConfigureAwait(false);

                        continue;
                    }
                    if (Options.ReadUserCmds && message is UserCmd uc)
                    {
                        await demo.Read(uc).ConfigureAwait(false);

                        continue;
                    }
                }
            }

            return(demo);
        }
        public virtual Task ReadHeader(SourceBufferReader buf)
        {
            HeaderId = buf.ReadString(8);
            if (HeaderId != "HL2DEMO\0")
            {
                throw new SourceException(HeaderId);
            }

            Protocol        = buf.Read <int>();
            NetworkProtocol = buf.Read <int>();
            ServerName      = buf.ReadString(260).TrimEnd('\0');
            ClientName      = buf.ReadString(260).TrimEnd('\0');
            MapName         = buf.ReadString(260).TrimEnd('\0');
            GameDirectory   = buf.ReadString(260).TrimEnd('\0');
            PlaybackTime    = buf.Read <float>();
            PlaybackTicks   = buf.Read <int>();
            PlaybackFrames  = buf.Read <int>();
            SignOnLength    = buf.Read <int>();

            return(Task.CompletedTask);
        }