mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 10:14:06 +02:00
bitbuffer update
This commit is contained in:
parent
d2e0d72f80
commit
54f16e2031
4 changed files with 62 additions and 26 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -79,9 +79,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "bitbuffer"
|
||||
version = "0.9.1"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "926a312ce87154f17fc6c0285d916f9ea23b12fc1e8c580ad40c9605941a320d"
|
||||
checksum = "3e73a154e33cb38a0561f1ba4677c66f2dfd7db72300e49d81ab3125f54a6a01"
|
||||
dependencies = [
|
||||
"bitbuffer_derive",
|
||||
"err-derive",
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use crate::demo::handle_utf8_error;
|
|||
|
||||
use crate::{ReadResult, Stream};
|
||||
|
||||
#[derive(TryFromPrimitive, Clone, Copy, Debug)]
|
||||
#[derive(TryFromPrimitive, Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum UserMessageType {
|
||||
Geiger = 0,
|
||||
|
|
@ -71,7 +71,7 @@ pub enum UserMessageType {
|
|||
Unknown = 255,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserMessage<'a> {
|
||||
SayText2(Box<SayText2Message>),
|
||||
Text(Box<TextMessage>),
|
||||
|
|
@ -138,6 +138,18 @@ impl<'a> BitWrite<LittleEndian> for UserMessage<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_user_message_roundtrip() {
|
||||
crate::test_roundtrip_encode(UserMessage::Train(TrainMessage { data: 12 }));
|
||||
crate::test_roundtrip_encode(UserMessage::SayText2(Box::new(SayText2Message {
|
||||
client: 3,
|
||||
raw: 1,
|
||||
kind: ChatMessageKind::ChatTeamDead,
|
||||
from: Some("Old Billy Riley".into()),
|
||||
text: "[P-REC] Stop record.".into(),
|
||||
})));
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
|
||||
pub enum ChatMessageKind {
|
||||
#[serde(rename = "TF_Chat_All")]
|
||||
|
|
@ -184,7 +196,7 @@ impl BitWrite<LittleEndian> for ChatMessageKind {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct SayText2Message {
|
||||
pub client: u8,
|
||||
pub raw: u8,
|
||||
|
|
@ -283,7 +295,18 @@ impl BitWrite<LittleEndian> for SayText2Message {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(BitRead, BitWrite, Debug, Clone)]
|
||||
#[test]
|
||||
fn test_say_text2_roundtrip() {
|
||||
crate::test_roundtrip_encode(SayText2Message {
|
||||
client: 3,
|
||||
raw: 1,
|
||||
kind: ChatMessageKind::ChatTeamDead,
|
||||
from: Some("Old Billy Riley".into()),
|
||||
text: "[P-REC] Stop record.".into(),
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq)]
|
||||
#[discriminant_bits = 8]
|
||||
pub enum HudTextLocation {
|
||||
PrintNotify = 1,
|
||||
|
|
@ -292,31 +315,31 @@ pub enum HudTextLocation {
|
|||
PrintCenter,
|
||||
}
|
||||
|
||||
#[derive(BitRead, BitWrite, Debug, Clone)]
|
||||
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq)]
|
||||
pub struct TextMessage {
|
||||
pub location: HudTextLocation,
|
||||
pub text: String,
|
||||
pub substitute: [String; 4],
|
||||
}
|
||||
|
||||
#[derive(BitRead, BitWrite, Debug, Clone)]
|
||||
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq)]
|
||||
pub struct ResetHudMessage {
|
||||
pub data: u8,
|
||||
}
|
||||
|
||||
#[derive(BitRead, BitWrite, Debug, Clone)]
|
||||
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq)]
|
||||
pub struct TrainMessage {
|
||||
pub data: u8,
|
||||
}
|
||||
|
||||
#[derive(BitRead, BitWrite, Debug, Clone)]
|
||||
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq)]
|
||||
pub struct VoiceSubtitleMessage {
|
||||
client: u8,
|
||||
menu: u8,
|
||||
item: u8,
|
||||
}
|
||||
|
||||
#[derive(BitRead, BitWrite, Debug, Clone)]
|
||||
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq)]
|
||||
pub struct ShakeMessage {
|
||||
command: u8,
|
||||
amplitude: f32,
|
||||
|
|
@ -324,7 +347,7 @@ pub struct ShakeMessage {
|
|||
duration: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct UnknownUserMessage<'a> {
|
||||
data: Stream<'a>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -843,15 +843,18 @@ fn bit_coord_roundtrip() {
|
|||
use bitbuffer::BitReadBuffer;
|
||||
|
||||
let mut data = Vec::with_capacity(128);
|
||||
let mut write = BitWriteStream::new(&mut data, LittleEndian);
|
||||
write_bit_coord(0.0, &mut write).unwrap();
|
||||
let pos1 = write.bit_len();
|
||||
write_bit_coord(123.0, &mut write).unwrap();
|
||||
let pos2 = write.bit_len();
|
||||
write_bit_coord(123.4375, &mut write).unwrap();
|
||||
let pos3 = write.bit_len();
|
||||
write_bit_coord(-0.4375, &mut write).unwrap();
|
||||
let pos4 = write.bit_len();
|
||||
let (pos1, pos2, pos3, pos4) = {
|
||||
let mut write = BitWriteStream::new(&mut data, LittleEndian);
|
||||
write_bit_coord(0.0, &mut write).unwrap();
|
||||
let pos1 = write.bit_len();
|
||||
write_bit_coord(123.0, &mut write).unwrap();
|
||||
let pos2 = write.bit_len();
|
||||
write_bit_coord(123.4375, &mut write).unwrap();
|
||||
let pos3 = write.bit_len();
|
||||
write_bit_coord(-0.4375, &mut write).unwrap();
|
||||
let pos4 = write.bit_len();
|
||||
(pos1, pos2, pos3, pos4)
|
||||
};
|
||||
|
||||
let mut read = Stream::from(BitReadBuffer::new(&data, LittleEndian));
|
||||
assert_eq!(0.0, read_bit_coord(&mut read).unwrap());
|
||||
|
|
|
|||
20
src/lib.rs
20
src/lib.rs
|
|
@ -26,11 +26,21 @@ fn test_roundtrip_encode<
|
|||
) {
|
||||
let mut data = Vec::with_capacity(128);
|
||||
use bitbuffer::{BitReadBuffer, BitReadStream, BitWriteStream, LittleEndian};
|
||||
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
|
||||
val.write(&mut stream).unwrap();
|
||||
let pos = stream.bit_len();
|
||||
let pos = {
|
||||
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
|
||||
val.write(&mut stream).unwrap();
|
||||
stream.bit_len()
|
||||
};
|
||||
|
||||
let mut read = BitReadStream::new(BitReadBuffer::new_owned(data, LittleEndian));
|
||||
assert_eq!(val, read.read().unwrap());
|
||||
assert_eq!(pos, read.pos());
|
||||
assert_eq!(
|
||||
val,
|
||||
read.read().unwrap(),
|
||||
"Failed to assert the parsed message is equal to the original"
|
||||
);
|
||||
assert_eq!(
|
||||
pos,
|
||||
read.pos(),
|
||||
"Failed to assert that all encoded bytes are used for decoding"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue