Skip to content

Commit

Permalink
refactor more of the proto
Browse files Browse the repository at this point in the history
  • Loading branch information
theaddonn committed Aug 6, 2024
1 parent 3b53f8e commit 070de5d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/proto/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/proto/src/gamepacket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl GamePacket {
pub fn pk_serialize(&self, stream: &mut Vec<u8>) -> 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)
Expand Down
27 changes: 6 additions & 21 deletions crates/proto/src/packets/text_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ impl ProtoCodec for TextMessagePacket {
} => {
message.proto_serialize(stream)?;

let len = VAR::<u32>::new(match Vec::len(parameters).try_into() {
Ok(v) => v,
Err(e) => return Err(ProtoCodecError::FromIntError(e.into())),
});

let len = VAR::<u32>::new(Vec::len(&parameters).try_into().map_err(ProtoCodecError::FromIntError)?);
len.proto_serialize(stream)?;

for parameter in parameters {
Expand All @@ -67,11 +63,7 @@ impl ProtoCodec for TextMessagePacket {
} => {
message.proto_serialize(stream)?;

let len = VAR::<u32>::new(match Vec::len(&parameters).try_into() {
Ok(v) => v,
Err(e) => return Err(ProtoCodecError::FromIntError(e.into())),
});

let len = VAR::<u32>::new(Vec::len(&parameters).try_into().map_err(ProtoCodecError::FromIntError)?);
len.proto_serialize(stream)?;

for parameter in parameters {
Expand All @@ -84,11 +76,7 @@ impl ProtoCodec for TextMessagePacket {
} => {
message.proto_serialize(stream)?;

let len = VAR::<u32>::new(match Vec::len(&parameters).try_into() {
Ok(v) => v,
Err(e) => return Err(ProtoCodecError::FromIntError(e.into())),
});

let len = VAR::<u32>::new(Vec::len(&parameters).try_into().map_err(ProtoCodecError::FromIntError)?);
len.proto_serialize(stream)?;

for parameter in parameters {
Expand Down Expand Up @@ -149,7 +137,7 @@ impl ProtoCodec for TextMessagePacket {
let len = VAR::<u32>::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 {
Expand All @@ -165,10 +153,7 @@ impl ProtoCodec for TextMessagePacket {
let message = String::proto_deserialize(stream)?;

let len = VAR::<u32>::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)?);
Expand All @@ -185,7 +170,7 @@ impl ProtoCodec for TextMessagePacket {
let len = VAR::<u32>::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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/proto/src/transport_layer/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(...),
Expand Down
13 changes: 7 additions & 6 deletions crates/proto/src/types/connection_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))),
Expand All @@ -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];

Expand All @@ -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.
Expand All @@ -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,
Expand Down

0 comments on commit 070de5d

Please sign in to comment.