Skip to content

Commit

Permalink
macro does work
Browse files Browse the repository at this point in the history
  • Loading branch information
theaddonn committed Sep 1, 2024
1 parent dbeb608 commit abb3f53
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/proto/src/gamepackets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/proto/src/login/network_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"
Expand Down
3 changes: 2 additions & 1 deletion crates/proto/src/packets/player_transfer.rs
Original file line number Diff line number Diff line change
@@ -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<u16>,
}
36 changes: 30 additions & 6 deletions crates/proto/src/sub_client.rs
Original file line number Diff line number Diff line change
@@ -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::<u8>)]
#[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<Self, ProtoCodecError> {
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,
}
}
}
16 changes: 8 additions & 8 deletions crates/proto_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand All @@ -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! {
Expand All @@ -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),
};
Expand All @@ -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),
}
Expand Down

0 comments on commit abb3f53

Please sign in to comment.