Skip to content

Commit

Permalink
parse fixed bitset from player chat
Browse files Browse the repository at this point in the history
  • Loading branch information
kralverde committed Aug 21, 2024
1 parent 0d05e5e commit 8bc55e8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
10 changes: 8 additions & 2 deletions pumpkin-protocol/src/bytebuf/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/server/play/s_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pumpkin_macros::packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
ServerPacket, VarInt,
FixedBitSet, ServerPacket, VarInt,
};

// derive(Deserialize)]
Expand All @@ -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
Expand All @@ -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),
})
}
}
6 changes: 6 additions & 0 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 8bc55e8

Please sign in to comment.