From 8bc55e8dda82cf30b42e9d674999d168762d26f6 Mon Sep 17 00:00:00 2001 From: kralverde Date: Wed, 21 Aug 2024 12:34:25 -0400 Subject: [PATCH] parse fixed bitset from player chat --- pumpkin-protocol/src/bytebuf/mod.rs | 10 ++++++++-- pumpkin-protocol/src/lib.rs | 1 + pumpkin-protocol/src/server/play/s_chat_message.rs | 6 +++--- pumpkin/src/client/player_packet.rs | 6 ++++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pumpkin-protocol/src/bytebuf/mod.rs b/pumpkin-protocol/src/bytebuf/mod.rs index fa275bb15..250d4fa1f 100644 --- a/pumpkin-protocol/src/bytebuf/mod.rs +++ b/pumpkin-protocol/src/bytebuf/mod.rs @@ -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(&mut self, val: &Option, 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(&mut self, val: impl Fn(&mut Self) -> T) -> Vec { diff --git a/pumpkin-protocol/src/lib.rs b/pumpkin-protocol/src/lib.rs index 3f7a4b607..f44a0a811 100644 --- a/pumpkin-protocol/src/lib.rs +++ b/pumpkin-protocol/src/lib.rs @@ -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]); diff --git a/pumpkin-protocol/src/server/play/s_chat_message.rs b/pumpkin-protocol/src/server/play/s_chat_message.rs index fc153094a..e644a5bc3 100644 --- a/pumpkin-protocol/src/server/play/s_chat_message.rs +++ b/pumpkin-protocol/src/server/play/s_chat_message.rs @@ -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, 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), }) } } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index b8a7853d7..6f7a4d9e5 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -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();