From 070de5d9cb7748aa3f2e0c5bc02360d92c151e7e Mon Sep 17 00:00:00 2001 From: theaddon Date: Tue, 6 Aug 2024 08:52:57 +0200 Subject: [PATCH] refactor more of the proto --- crates/proto/src/connection.rs | 2 +- crates/proto/src/gamepacket.rs | 2 +- crates/proto/src/packets/text_message.rs | 27 +++++-------------- .../proto/src/transport_layer/connection.rs | 2 +- crates/proto/src/types/connection_request.rs | 13 ++++----- 5 files changed, 16 insertions(+), 30 deletions(-) diff --git a/crates/proto/src/connection.rs b/crates/proto/src/connection.rs index 05c1282..d8ff123 100644 --- a/crates/proto/src/connection.rs +++ b/crates/proto/src/connection.rs @@ -282,7 +282,7 @@ impl Connection { } } Err(e) => { - if if task_pk_sender.send(Err(e)).is_err() { + if task_pk_sender.send(Err(e)).is_err() { break 'select_loop } } diff --git a/crates/proto/src/gamepacket.rs b/crates/proto/src/gamepacket.rs index d2e07df..5ea225c 100644 --- a/crates/proto/src/gamepacket.rs +++ b/crates/proto/src/gamepacket.rs @@ -394,7 +394,7 @@ impl GamePacket { pub fn pk_serialize(&self, stream: &mut Vec) -> Result<(), ProtoCodecError> { match self { GamePacket::Login(pk) => { - ser_packet!(stream, GamePacket::Login, pk) + ser_packet!(stream, GamePacket::LoginID, pk) } GamePacket::PlayStatus(pk) => { ser_packet!(stream, GamePacket::PlayStatusID, pk) diff --git a/crates/proto/src/packets/text_message.rs b/crates/proto/src/packets/text_message.rs index 00c90a8..1c34feb 100644 --- a/crates/proto/src/packets/text_message.rs +++ b/crates/proto/src/packets/text_message.rs @@ -50,11 +50,7 @@ impl ProtoCodec for TextMessagePacket { } => { message.proto_serialize(stream)?; - let len = VAR::::new(match Vec::len(parameters).try_into() { - Ok(v) => v, - Err(e) => return Err(ProtoCodecError::FromIntError(e.into())), - }); - + let len = VAR::::new(Vec::len(¶meters).try_into().map_err(ProtoCodecError::FromIntError)?); len.proto_serialize(stream)?; for parameter in parameters { @@ -67,11 +63,7 @@ impl ProtoCodec for TextMessagePacket { } => { message.proto_serialize(stream)?; - let len = VAR::::new(match Vec::len(¶meters).try_into() { - Ok(v) => v, - Err(e) => return Err(ProtoCodecError::FromIntError(e.into())), - }); - + let len = VAR::::new(Vec::len(¶meters).try_into().map_err(ProtoCodecError::FromIntError)?); len.proto_serialize(stream)?; for parameter in parameters { @@ -84,11 +76,7 @@ impl ProtoCodec for TextMessagePacket { } => { message.proto_serialize(stream)?; - let len = VAR::::new(match Vec::len(¶meters).try_into() { - Ok(v) => v, - Err(e) => return Err(ProtoCodecError::FromIntError(e.into())), - }); - + let len = VAR::::new(Vec::len(¶meters).try_into().map_err(ProtoCodecError::FromIntError)?); len.proto_serialize(stream)?; for parameter in parameters { @@ -149,7 +137,7 @@ impl ProtoCodec for TextMessagePacket { let len = VAR::::proto_deserialize(stream)?.into_inner(); let mut parameters = Vec::with_capacity(match len.try_into() { Ok(v) => v, - Err(e) => return Err(ProtoCodecError::FromIntError(e.into())), + Err(e) => return Err(ProtoCodecError::FromIntError(e)), }); for _ in 0..len { @@ -165,10 +153,7 @@ impl ProtoCodec for TextMessagePacket { let message = String::proto_deserialize(stream)?; let len = VAR::::proto_deserialize(stream)?.into_inner(); - let mut parameters = Vec::with_capacity(match len.try_into() { - Ok(v) => v, - Err(e) => return Err(ProtoCodecError::FromIntError(e.into())), - }); + let mut parameters = Vec::with_capacity(len.try_into().map_err(ProtoCodecError::FromIntError)?); for _ in 0..len { parameters.push(String::proto_deserialize(stream)?); @@ -185,7 +170,7 @@ impl ProtoCodec for TextMessagePacket { let len = VAR::::proto_deserialize(stream)?.into_inner(); let mut parameters = Vec::with_capacity(match len.try_into() { Ok(v) => v, - Err(e) => return Err(ProtoCodecError::FromIntError(e.into())), + Err(e) => return Err(ProtoCodecError::FromIntError(e)), }); for _ in 0..len { diff --git a/crates/proto/src/transport_layer/connection.rs b/crates/proto/src/transport_layer/connection.rs index 2f5a648..e77de66 100644 --- a/crates/proto/src/transport_layer/connection.rs +++ b/crates/proto/src/transport_layer/connection.rs @@ -6,7 +6,7 @@ use bedrockrs_core::int::LE; use crate::error::{RaknetError, TransportLayerError}; use crate::info::RAKNET_GAME_PACKET_ID; -/// + pub enum TransportLayerConnection { RaknetUDP(rak_rs::connection::Connection), // TODO RaknetTCP(...), diff --git a/crates/proto/src/types/connection_request.rs b/crates/proto/src/types/connection_request.rs index 0c0b6f1..e11aace 100644 --- a/crates/proto/src/types/connection_request.rs +++ b/crates/proto/src/types/connection_request.rs @@ -189,7 +189,7 @@ impl ProtoCodec for ConnectionRequest { key_data = BASE64_STANDARD .decode(x5u) - .map_err(|e| ProtoCodecError::Base64DecodeError(e))?; + .map_err(ProtoCodecError::Base64DecodeError)?; } // Decode the jwt string into a jwt object @@ -198,7 +198,7 @@ impl ProtoCodec for ConnectionRequest { &DecodingKey::from_ec_der(&key_data), &jwt_validation, ) - .map_err(|e| ProtoCodecError::JwtError(e))?; + .map_err(ProtoCodecError::JwtError)?; key_data = match jwt.claims.get("identityPublicKey") { None => return Err(ProtoCodecError::FormatMismatch(String::from("Expected identityPublicKey field in JWT for validation"))), @@ -221,7 +221,7 @@ impl ProtoCodec for ConnectionRequest { let raw_token_len = raw_token_len .try_into() - .map_err(|e| ProtoCodecError::FromIntError(e))?; + .map_err(ProtoCodecError::FromIntError)?; let mut raw_token_buf = vec![0; raw_token_len]; @@ -232,11 +232,12 @@ impl ProtoCodec for ConnectionRequest { // transform into string let raw_token_string = - String::from_utf8(raw_token_buf).map_err(|e| ProtoCodecError::UTF8Error(e))?; + String::from_utf8(raw_token_buf) + .map_err(ProtoCodecError::UTF8Error)?; // Extract header let raw_token_jwt_header = jsonwebtoken::decode_header(&raw_token_string) - .map_err(|e| ProtoCodecError::JwtError(e))?; + .map_err(ProtoCodecError::JwtError)?; let mut jwt_validation = Validation::new(raw_token_jwt_header.alg); // TODO: This definitely is not right. Even Zuri-MC doesn't understand this.. I may understand it.. I do understand it, update I don't. @@ -250,7 +251,7 @@ impl ProtoCodec for ConnectionRequest { &DecodingKey::from_ec_der(&vec![]), &jwt_validation, ) - .map_err(|e| ProtoCodecError::JwtError(e))?; + .map_err(ProtoCodecError::JwtError)?; Ok(Self { certificate_chain,