mirror of
https://codeberg.org/demostf/parser.git
synced 2026-11-30 02:30:46 +01:00
make Message::encode write the message type instead of requiring to do so manually
This commit is contained in:
parent
9b0e19a7e7
commit
2bb0a60171
4 changed files with 16 additions and 8 deletions
|
|
@ -1,8 +1,10 @@
|
|||
## 0.7.3
|
||||
## Unreleased
|
||||
|
||||
- `SetConVar` message now uses `Cow` instead of `String`.
|
||||
- Add `Message::Disconnect` and `MessageType::Disconnect`. These never appears
|
||||
in demos, but are used in the network protocol.
|
||||
- `Message::encode` now write the message type by itself, instead of requiring
|
||||
the caller to do so beforehand.
|
||||
|
||||
## 0.7.2
|
||||
|
||||
|
|
|
|||
|
|
@ -255,6 +255,7 @@ impl<'a> Message<'a> {
|
|||
#[cfg(feature = "write")]
|
||||
impl Encode for Message<'_> {
|
||||
fn encode(&self, stream: &mut BitWriteStream<LittleEndian>, state: &ParserState) -> Result<()> {
|
||||
self.get_message_type().write(stream)?;
|
||||
match self {
|
||||
Message::Empty => Ok(()),
|
||||
Message::Disconnect(reason) => reason.encode(stream, state),
|
||||
|
|
|
|||
|
|
@ -107,7 +107,6 @@ impl Encode for MessagePacket<'_> {
|
|||
self.meta.write(stream)?;
|
||||
stream.reserve_byte_length(32, |stream| {
|
||||
for message in self.messages.iter() {
|
||||
message.get_message_type().write(stream)?;
|
||||
message.encode(stream, state)?;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -69,19 +69,25 @@ fn message_reencode(
|
|||
let mut write = BitWriteStream::new(&mut out, LittleEndian);
|
||||
parsed.encode(&mut write, state).unwrap();
|
||||
write.bit_len()
|
||||
};
|
||||
} - 6; // 6 for the message type
|
||||
out.push(0);
|
||||
|
||||
let mut re_stream = BitReadStream::new(BitReadBuffer::new(&out, LittleEndian));
|
||||
let re_parsed = Message::from_type(ty, &mut re_stream, state).unwrap();
|
||||
let re_parsed = Message::parse(&mut re_stream, state).unwrap();
|
||||
assert_eq!(ty, re_parsed.get_message_type());
|
||||
|
||||
assert_eq!(parsed, re_parsed);
|
||||
assert_eq!(written_bits, stream.pos());
|
||||
|
||||
assert_eq!(data.len(), out.len());
|
||||
// cut of the message type
|
||||
let mut out_stream = BitReadStream::<LittleEndian>::from(out.as_slice());
|
||||
out_stream.skip_bits(6).unwrap();
|
||||
let out_body = out_stream.read_bytes(data.len()).unwrap();
|
||||
|
||||
if data.len() > 16 {
|
||||
pretty_assertions::assert_eq!(&data[0..data.len() - 8], &out[0..out.len() - 8]);
|
||||
pretty_assertions::assert_eq!(&data[data.len() - 8..], &out[out.len() - 8..]);
|
||||
pretty_assertions::assert_eq!(&data[0..8], &out_body[0..8]);
|
||||
pretty_assertions::assert_eq!(&data[8..], &out_body[8..]);
|
||||
} else {
|
||||
pretty_assertions::assert_eq!(data, out);
|
||||
pretty_assertions::assert_eq!(data.as_slice(), out_body.as_ref());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue