diff --git a/crates/proto/src/gamepackets.rs b/crates/proto/src/gamepackets.rs index 2e77de47..b21d1bcc 100644 --- a/crates/proto/src/gamepackets.rs +++ b/crates/proto/src/gamepackets.rs @@ -282,10 +282,10 @@ fn read_gamepacket_header( // Get the next 2 bits as the sub client sender id // Can never be more than an 8-bit integer due to being 2 bits big - let subclient_sender_id = (game_packet_header & 0b0000_1100_0000_0000 >> 10) as u8; + let subclient_sender_id = SubClientID::proto_from((game_packet_header & 0b0000_1100_0000_0000 >> 10) as u8)?; // Get the next 2 bits as the sub client target id // Can never be more than an 8-bit integer due to being 2 bits big - let subclient_target_id = (game_packet_header & 0b0011_0000_0000_0000 >> 12) as u8; + let subclient_target_id = SubClientID::proto_from((game_packet_header & 0b0011_0000_0000_0000 >> 12) as u8)?; Ok(( length, diff --git a/crates/proto/src/login/network_settings.rs b/crates/proto/src/login/network_settings.rs index b5091e17..ca6cae17 100644 --- a/crates/proto/src/login/network_settings.rs +++ b/crates/proto/src/login/network_settings.rs @@ -15,7 +15,7 @@ pub async fn network_settings( ////////////////////////////////////// let mut network_settings_request = match conn.recv().await { - Ok(GamePackets::RequestNetworkSettings(pk)) => pk, + Ok(GamePackets::NetworkSettingsRequest(pk)) => pk, Ok(other) => { return Err(LoginError::FormatError(format!( "Expected RequestNetworkSettings packet, got: {other:?}" diff --git a/crates/proto/src/packets/player_transfer.rs b/crates/proto/src/packets/player_transfer.rs index ec405eeb..826dcbb4 100644 --- a/crates/proto/src/packets/player_transfer.rs +++ b/crates/proto/src/packets/player_transfer.rs @@ -1,8 +1,9 @@ +use bedrockrs_core::int::LE; use bedrockrs_proto_derive::{gamepacket, gamepackets, ProtoCodec}; #[gamepacket(id = 85)] #[derive(ProtoCodec, Debug, Clone)] pub struct TransferPlayerPacket { addr: String, - port: u16, + port: LE, } diff --git a/crates/proto/src/sub_client.rs b/crates/proto/src/sub_client.rs index 0fc578d5..5f743720 100644 --- a/crates/proto/src/sub_client.rs +++ b/crates/proto/src/sub_client.rs @@ -1,11 +1,35 @@ use bedrockrs_proto_derive::ProtoCodec; use bedrockrs_core::int::LE; +use bedrockrs_proto_core::error::ProtoCodecError; -#[derive(ProtoCodec, Debug, Clone)] -#[enum_repr(LE::)] +#[derive(Debug, Clone)] pub enum SubClientID { - PrimaryClient = 0, - Client2 = 1, - Client3 = 2, - Client4 = 3, + PrimaryClient, + Client2, + Client3, + Client4, +} + +impl SubClientID { + pub fn proto_from(val: u8) -> Result { + match val { + 0 => Ok(SubClientID::PrimaryClient), + 1 => Ok(SubClientID::Client2), + 2 => Ok(SubClientID::Client3), + 3 => Ok(SubClientID::Client4), + other => Err(ProtoCodecError::InvalidEnumID( + format!("{other:?}"), + String::from("SubClientID"), + )) + } + } + + pub fn proto_to(&self) -> u8 { + match self { + SubClientID::PrimaryClient => 0, + SubClientID::Client2 => 1, + SubClientID::Client3 => 2, + SubClientID::Client4 => 3, + } + } } diff --git a/crates/proto_derive/src/lib.rs b/crates/proto_derive/src/lib.rs index ac51cf68..5295feda 100644 --- a/crates/proto_derive/src/lib.rs +++ b/crates/proto_derive/src/lib.rs @@ -203,7 +203,7 @@ pub fn gamepackets(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let compress = compress.iter().map(|(name, value)| { if let Some(v) = value { quote! { - GamePackets::#name(_) => { return #v::<::bedrockrs_proto_core::GamePacket>::COMPRESS; }, + GamePackets::#name(_) => { return <#v as ::bedrockrs_proto_core::GamePacket>::COMPRESS; }, } } else { quote! { @@ -216,7 +216,7 @@ pub fn gamepackets(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let encrypt = encrypt.iter().map(|(name, value)| { if let Some(v) = value { quote! { - GamePackets::#name(_) => { return #v::<::bedrockrs_proto_core::GamePacket>::ENCRYPT; }, + GamePackets::#name(_) => { return <#v as ::bedrockrs_proto_core::GamePacket>::ENCRYPT; }, } } else { quote! { @@ -230,19 +230,19 @@ pub fn gamepackets(input: proc_macro::TokenStream) -> proc_macro::TokenStream { if let Some(v) = value { quote! { GamePackets::#name(pk) => { - let buf = Vec::new(); + let mut buf = Vec::new(); - match #v::<::bedrockrs_proto_core::ProtoCodec>::proto_serialize(pk, &mut buf) { + match <#v as bedrockrs_proto_core::ProtoCodec>::proto_serialize(pk, &mut buf) { Ok(_) => {}, Err(err) => return Err(err), }; let len: u32 = match buf.len().try_into() { Ok(len) => len, - Err(err) => return Err(::bedrockrs_proto_core::error::ProtoCodecError::FromIntError(err)); + Err(err) => return Err(::bedrockrs_proto_core::error::ProtoCodecError::FromIntError(err)), }; - match write_gamepacket_header(stream, len, #v::<::bedrockrs_proto_core::GamePacket>::ID, subclient_sender_id, subclient_target_id) { + match write_gamepacket_header(stream, len, <#v as ::bedrockrs_proto_core::GamePacket>::ID, subclient_sender_id, subclient_target_id) { Ok(_) => {}, Err(err) => return Err(err), }; @@ -264,8 +264,8 @@ pub fn gamepackets(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let de = de.iter().map(|(name, value)| { if let Some(v) = value { quote! { - #v::<::bedrockrs_proto_core::GamePacket>::ID => { - match #v::<::bedrockrs_proto_core::ProtoCodec>::proto_deserialize(stream) { + <#v as ::bedrockrs_proto_core::GamePacket>::ID => { + match <#v as ::bedrockrs_proto_core::ProtoCodec>::proto_deserialize(stream) { Ok(pk) => GamePackets::#name(pk), Err(e) => return Err(e), }