1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-10-01 08:44:05 +02:00

Use the demo's protocol version when reencoding

DemoHandler::default() starts out at protocol 24 and relies on handle_header
to replace that with whatever the demo actually is. Parsing through DemoParser
does this for you, but reencode drives the handler itself and never did, so
every demo got read as protocol 24.

Older protocols put a few fields at different widths, so the message stream
desyncs partway in. RawPacketStream reports that as a truncated demo rather
than an error, which is why nothing complains about it: reencode_demo on
test_data/protocol23.dem writes 1076 bytes, prints a truncation warning and
still exits 0.

The reencode test missed this because protocol23.dem was never in its list,
and because nothing checked the demo had been read to the end. Add a test for
that and the same assert to the existing round trip.

Output for protocol 24 demos is unchanged, byte for byte.
This commit is contained in:
Peter 2026-08-31 10:50:23 +01:00 • committed by pet0ruk
commit 7c5c596d17
2 changed files with 40 additions and 0 deletions

View file

@ -44,6 +44,7 @@ fn re_encode_test(input_file: &str) {
let mut packets = RawPacketStream::new(stream);
let mut handler = DemoHandler::parse_all_with_analyser(NullHandler);
handler.handle_header(&header);
{
let mut out_stream = BitWriteStream::new(&mut out_buffer, LittleEndian);
@ -150,4 +151,36 @@ fn re_encode_test(input_file: &str) {
handler.handle_packet(packet).unwrap();
}
// a desync shows up as the packet loop quietly stopping early, so the
// round trip above passes while most of the demo is never looked at
assert!(!packets.incomplete, "{} was not read to the end", input_file);
}
/// A demo recorded on an older protocol must be parsed with that protocol, not
/// the protocol 24 that `ParserState` starts out with. Getting it wrong shifts
/// the width of a handful of fields (`ServerInfo`'s map hash, `PreFetch`), the
/// message stream desyncs, and `RawPacketStream` quietly stops early rather
/// than returning an error - so this checks the whole demo was actually read.
#[cfg(feature = "write")]
#[test]
fn parse_with_header_protocol() {
let file = fs::read("test_data/protocol23.dem").expect("Unable to read file");
let demo = Demo::new(&file);
let mut stream = demo.get_stream();
let header = Header::read(&mut stream).unwrap();
assert_eq!(23, header.protocol);
let mut packets = RawPacketStream::new(stream);
let mut handler = DemoHandler::parse_all_with_analyser(NullHandler);
handler.handle_header(&header);
let mut count = 0;
while let Some(packet) = packets.next(&handler.state_handler).unwrap() {
handler.handle_packet(packet).unwrap();
count += 1;
}
assert!(!packets.incomplete, "demo was not read to the end");
assert_eq!(128235, count);
}