Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
theaddonn committed Aug 3, 2024
1 parent f56bf95 commit bd1efd9
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 62 deletions.
22 changes: 3 additions & 19 deletions crates/nbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,30 +413,14 @@ impl NbtTag {
let mut map = HashMap::new();

loop {
let id = match T::read_u8(stream) {
Ok(v) => v,
Err(e) => {
return Err(e);
}
};
let id = T::read_u8(stream)?;

if id == Self::EMPTY_ID {
break;
}

let key = match T::read_string(stream) {
Ok(v) => v,
Err(e) => {
return Err(e);
}
};

let tag = match Self::nbt_deserialize_val::<T>(stream, id) {
Ok(v) => v,
Err(e) => {
return Err(e);
}
};
let key = T::read_string(stream)?;
let tag = Self::nbt_deserialize_val::<T>(stream, id)?;

map.insert(key, tag);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/paletted_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bedrockrs_nbt::endian::little_endian_network::NbtLittleEndianNetwork;
use bedrockrs_nbt::NbtTag;
use byteorder::ReadBytesExt;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct PalettedStorage {
pub blocks: [u32; 4096],
pub palette: Vec<NbtTag>,
Expand Down
6 changes: 3 additions & 3 deletions crates/proto/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Compression {
match self {
Compression::Zlib { .. } => 0x00,
Compression::Snappy { .. } => 0x01,
Compression::None => 0xFF,
Compression::None => u8::MAX,
}
}

Expand All @@ -41,7 +41,7 @@ impl Compression {
match self {
Compression::Zlib { .. } => 0x0000,
Compression::Snappy { .. } => 0x0001,
Compression::None => 0xFFFF,
Compression::None => u16::MAX,
}
}

Expand All @@ -62,7 +62,7 @@ impl Compression {
match self {
Compression::Zlib { threshold, .. } => *threshold,
Compression::Snappy { threshold, .. } => *threshold,
Compression::None => 0x0000,
Compression::None => 0,
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/proto/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ impl Connection {
Err(_) => {}
};

let pos = decrypted_stream.position() as usize;

compression
.decompress(decrypted_stream.into_inner(), &mut decompressed_stream)
.decompress(&decrypted_stream.into_inner()[pos..], &mut decompressed_stream)
.map_err(|e| ConnectionError::CompressError(e))?;

Cursor::new(decompressed_stream.as_slice())
Expand Down
17 changes: 3 additions & 14 deletions crates/proto/src/gamepacket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,24 +846,13 @@ impl GamePacket {
// Read the game packet length
// We don't need it, yet?
// TODO: Use this to possibly async the packet handling
match VAR::<u32>::read(stream) {
Ok(_) => {}
Err(e) => return Err(ProtoCodecError::IOError(Arc::new(e))),
};
VAR::<u32>::proto_deserialize(stream)?;

// Read the game packet header and parse it into an u16
let game_packet_header: u16 = match VAR::<u32>::read(stream) {
Ok(v) => match v.into_inner().try_into() {
Ok(v) => v,
Err(e) => {
return Err(ProtoCodecError::FromIntError(e));
}
},
Err(e) => return Err(ProtoCodecError::IOError(Arc::new(e))),
};
let game_packet_header: u16 = VAR::<u32>::proto_deserialize(stream)?.into_inner().try_into().map_err(ProtoCodecError::FromIntError)?;

// Get the first 10 bits as the packet id
// Can never be more than a 16-bit integer due to being 10 bits big
// Can never be more than a 16-bit integer due to being 10-bits big
// Gamepacket IDs through 200-299 are used for spin-offs, they are free to use for custom packets
let game_packet_id = game_packet_header & 0b0000_0011_1111_1111;

Expand Down
4 changes: 3 additions & 1 deletion crates/proto/src/login/provider/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ impl DefaultLoginProvider {

impl LoginProviderServer for DefaultLoginProvider {
fn compression(&self) -> Compression {
Compression::None
Compression::Snappy {
threshold: 1,
}
}
fn encryption_enabled(&self) -> bool {
false
Expand Down
18 changes: 14 additions & 4 deletions crates/proto/src/packets/level_chunk.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::types::chunk_pos::ChunkPos;
use bedrockrs_core::int::{LE, VAR};
use bedrockrs_proto_core::error::ProtoCodecError;
use bedrockrs_proto_core::ProtoCodec;

#[derive(Debug, Clone)]
pub struct LevelChunkPacket {
pub chunk_position: ChunkPos,
pub dimension_id: VAR<i32>,
pub sub_chunk_count: VAR<i32>,
pub sub_chunk_count: VAR<u32>,
pub cache_enabled: bool,
pub serialized_chunk_data: Vec<u8>,

Expand All @@ -26,10 +27,10 @@ impl ProtoCodec for LevelChunkPacket {
self.sub_chunk_count.proto_serialize(stream)?;
} else {
if !(self.client_request_subchunk_limit.into_inner() < 0) {
VAR::<u32>::new(0xFFFFFFFE).proto_serialize(stream)?;
VAR::<u32>::new(u32::MAX - 1).proto_serialize(stream)?;
self.client_request_subchunk_limit.proto_serialize(stream)?;
} else {
VAR::<u32>::new(0xFFFFFFFF).proto_serialize(stream)?;
VAR::<u32>::new(u32::MAX).proto_serialize(stream)?;
}
}

Expand All @@ -39,9 +40,18 @@ impl ProtoCodec for LevelChunkPacket {
unimplemented!()
}

let len = self.serialized_chunk_data
.len()
.try_into()
.map_err(ProtoCodecError::FromIntError)?;

VAR::<u32>::new(len).proto_serialize(stream)?;

stream.extend_from_slice(&self.serialized_chunk_data);

return Ok(());
println!("finish");

Ok(())
}

fn proto_deserialize(
Expand Down
7 changes: 0 additions & 7 deletions crates/proto/src/packets/text_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,12 @@ impl ProtoCodec for TextMessagePacket {
}

fn proto_deserialize(stream: &mut Cursor<&[u8]>) -> Result<Self, ProtoCodecError> {
println!("#0");
let message_type = u8::proto_deserialize(stream)?;

println!("#1");
let localize = bool::proto_deserialize(stream)?;

let message_type = match message_type {
0 => TextMessageData::Raw(String::proto_deserialize(stream)?),
1 => {
println!("#2");
TextMessageData::Chat {
player_name: String::proto_deserialize(stream)?,
message: String::proto_deserialize(stream)?,
Expand Down Expand Up @@ -224,11 +220,8 @@ impl ProtoCodec for TextMessagePacket {
}
};

println!("#3");
let sender_xuid = String::proto_deserialize(stream)?;
println!("#4");
let platform_id = String::proto_deserialize(stream)?;
println!("#5");
let filtered_message = String::proto_deserialize(stream)?;

Ok(Self {
Expand Down
7 changes: 2 additions & 5 deletions crates/proto/src/types/connection_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,10 @@ impl ProtoCodec for ConnectionRequest {
// (certificate_chain len + raw_token len + 8)
// 8 = i32 len + i32 len (length of certificate_chain's len and raw_token's len)
// can be ignored, other lengths are provided
VAR::<u32>::read(stream).map_err(|e| ProtoCodecError::IOError(Arc::new(e)))?;
VAR::<u32>::proto_deserialize(stream)?;

// read length of certificate_chain vec
let certificate_chain_len = match LE::<i32>::read(stream) {
Ok(l) => l.into_inner(),
Err(e) => return Err(ProtoCodecError::IOError(Arc::new(e))),
};
let certificate_chain_len = LE::<i32>::proto_deserialize(stream)?.into_inner();

let certificate_chain_len = certificate_chain_len
.try_into()
Expand Down
8 changes: 2 additions & 6 deletions crates/proto/src/types/gamerule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ impl ProtoCodec for GameRuleValue {
{
1 => GameRuleValue::ValBool(bool::proto_deserialize(stream)?),
2 => GameRuleValue::ValVarU32(
VAR::<u32>::read(stream)
.map_err(|e| ProtoCodecError::IOError(Arc::new(e)))?
.into_inner(),
VAR::<u32>::proto_deserialize(stream)?.into_inner(),
),
3 => GameRuleValue::ValF32(
LE::<f32>::read(stream)
.map_err(|e| ProtoCodecError::IOError(Arc::new(e)))?
.into_inner(),
LE::<f32>::proto_deserialize(stream)?.into_inner(),
),
other => {
return Err(ProtoCodecError::InvalidEnumID(
Expand Down
2 changes: 1 addition & 1 deletion crates/world/src/world_db/subchunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Cursor;
use bedrockrs_paletted_storage::PalettedStorage;
use byteorder::ReadBytesExt;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct SubChunk {
pub paletted_storage: Vec<PalettedStorage>,
}
Expand Down

0 comments on commit bd1efd9

Please sign in to comment.