parse fixed bitset from player chat

This commit is contained in:
kralverde
2024-08-21 12:34:25 -04:00
parent 0d05e5ef36
commit 8bc55e8dda
4 changed files with 18 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
use crate::{BitSet, VarInt, VarLongType};
use crate::{BitSet, FixedBitSet, VarInt, VarLongType};
use bytes::{Buf, BufMut, BytesMut};
use core::str;
use std::io::{self, Error, ErrorKind};
@@ -107,6 +107,10 @@ impl ByteBuffer {
uuid::Uuid::from_slice(&bytes).expect("Failed to parse UUID")
}
pub fn get_fixed_bitset(&mut self, bits: usize) -> FixedBitSet {
self.copy_to_bytes(bits.div_ceil(8))
}
pub fn put_bool(&mut self, v: bool) {
if v {
self.buffer.put_u8(1);
@@ -168,7 +172,9 @@ impl ByteBuffer {
/// some, then it also calls the `write` closure.
pub fn put_option<T>(&mut self, val: &Option<T>, write: impl FnOnce(&mut Self, &T)) {
self.put_bool(val.is_some());
if let Some(v) = val { write(self, v) }
if let Some(v) = val {
write(self, v)
}
}
pub fn get_list<T>(&mut self, val: impl Fn(&mut Self) -> T) -> Vec<T> {

View File

@@ -21,6 +21,7 @@ pub const MAX_PACKET_SIZE: i32 = 2097152;
pub type Identifier = String;
pub type VarIntType = i32;
pub type VarLongType = i64;
pub type FixedBitSet = bytes::Bytes;
pub struct BitSet<'a>(pub VarInt, pub &'a [i64]);

View File

@@ -3,7 +3,7 @@ use pumpkin_macros::packet;
use crate::{
bytebuf::{ByteBuffer, DeserializerError},
ServerPacket, VarInt,
FixedBitSet, ServerPacket, VarInt,
};
// derive(Deserialize)]
@@ -14,8 +14,7 @@ pub struct SChatMessage {
pub salt: i64,
pub signature: Option<Bytes>,
pub messagee_count: VarInt,
// TODO: Properly implement BitSet decoding
// acknowledged: BitSet,
pub acknowledged: FixedBitSet,
}
// TODO
@@ -27,6 +26,7 @@ impl ServerPacket for SChatMessage {
salt: bytebuf.get_i64(),
signature: bytebuf.get_option(|v| v.copy_to_bytes(256)),
messagee_count: bytebuf.get_var_int(),
acknowledged: bytebuf.get_fixed_bitset(20),
})
}
}

View File

@@ -212,7 +212,13 @@ impl Client {
pub fn handle_chat_message(&mut self, server: &mut Server, chat_message: SChatMessage) {
dbg!("got message");
let message = chat_message.message;
if message.len() > 256 {
self.kick("Oversized message");
return;
}
// TODO: filter message & validation
let gameprofile = self.gameprofile.as_ref().unwrap();