From b1339a1cabf9e93a0570b331af90e2d7c7641dcf Mon Sep 17 00:00:00 2001 From: Sweattypalms Date: Sat, 4 Jan 2025 20:42:11 +0900 Subject: [PATCH] Updated all packets to use resource id instead of hardcoded packet id values. --- src/lib/derive_macros/src/net/encode.rs | 7 +++-- src/lib/derive_macros/src/net/packets/mod.rs | 29 ++++++++++++++----- .../packets/incoming/client_information.rs | 2 +- src/lib/net/src/packets/incoming/handshake.rs | 2 +- .../net/src/packets/incoming/keep_alive.rs | 2 +- .../packets/incoming/login_acknowledged.rs | 2 +- .../net/src/packets/incoming/login_start.rs | 2 +- src/lib/net/src/packets/incoming/ping.rs | 2 +- .../src/packets/incoming/player_command.rs | 2 +- .../incoming/server_bound_known_packs.rs | 2 +- .../incoming/server_bound_plugin_message.rs | 2 +- .../packets/incoming/set_player_position.rs | 2 +- .../set_player_position_and_rotation.rs | 2 +- .../packets/incoming/set_player_rotation.rs | 2 +- .../src/packets/incoming/status_request.rs | 2 +- src/lib/net/src/packets/incoming/swing_arm.rs | 2 +- 16 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/lib/derive_macros/src/net/encode.rs b/src/lib/derive_macros/src/net/encode.rs index 0e8797a3..483dde3b 100644 --- a/src/lib/derive_macros/src/net/encode.rs +++ b/src/lib/derive_macros/src/net/encode.rs @@ -1,8 +1,9 @@ use crate::helpers::{get_derive_attributes, StructInfo}; +use crate::net::packets::get_packet_details_from_attributes; use proc_macro::TokenStream; use quote::quote; -use syn::{parse_macro_input, Attribute, DeriveInput, Fields, LitInt}; -use crate::net::packets::get_packet_details_from_attributes; +use syn::{parse_macro_input, DeriveInput, Fields}; +use crate::static_loading::packets::PacketBoundiness; // Generate packet ID encoding snippets fn generate_packet_id_snippets( @@ -136,7 +137,7 @@ pub(crate) fn derive(input: TokenStream) -> TokenStream { let packet_attr = get_derive_attributes(&input, "packet"); let (packet_id_snippet, async_packet_id_snippet) = - generate_packet_id_snippets(get_packet_details_from_attributes(packet_attr.as_slice()).unzip().1); + generate_packet_id_snippets(get_packet_details_from_attributes(packet_attr.as_slice(), PacketBoundiness::Clientbound).unzip().1); let (sync_impl, async_impl) = match &input.data { syn::Data::Struct(data) => { diff --git a/src/lib/derive_macros/src/net/packets/mod.rs b/src/lib/derive_macros/src/net/packets/mod.rs index 2d2a1af2..29bfad5b 100644 --- a/src/lib/derive_macros/src/net/packets/mod.rs +++ b/src/lib/derive_macros/src/net/packets/mod.rs @@ -25,7 +25,7 @@ fn parse_packet_attribute(attr: &Attribute) -> Option<(String, String)> { } /// Returns: (state, packet_id) -pub(crate) fn get_packet_details_from_attributes(attrs: &[Attribute]) -> Option<(String, u8)> { +pub(crate) fn get_packet_details_from_attributes(attrs: &[Attribute], bound_to: PacketBoundiness) -> Option<(String, u8)> { let mut val = Option::<(String, String)>::None; for attr in attrs { @@ -38,7 +38,7 @@ pub(crate) fn get_packet_details_from_attributes(attrs: &[Attribute]) -> Option< let (state, packet_id) = val?; - let packet_id = parse_packet_id(state.as_str(), packet_id).expect("parse_packet_id failed"); + let packet_id = parse_packet_id(state.as_str(), packet_id, bound_to).expect("parse_packet_id failed"); Some((state, packet_id)) } @@ -78,11 +78,20 @@ pub fn bake_registry(input: TokenStream) -> TokenStream { let start = std::time::Instant::now(); - for entry in std::fs::read_dir(dir_path).expect("read_dir call failed") { + let entries = std::fs::read_dir(dir_path).expect("read_dir call failed"); + + for entry in entries { let entry = entry.expect("entry failed"); let path = entry.path(); let file_name = path.file_name().expect("file_name failed").to_os_string(); + + println!( + " {} {}", + "[FERRUMC_MACROS]".bold().blue(), + format!("Parsing file: {}", file_name.to_string_lossy()).white().bold() + ); + if !path.is_file() { continue; } @@ -95,8 +104,13 @@ pub fn bake_registry(input: TokenStream) -> TokenStream { continue; }; + // If the struct does not have the #[packet(...)] attribute, then skip it. + if !item_struct.attrs.iter().any(|attr| attr.path().is_ident("packet")) { + continue; + } + // format: #[packet(packet_id = 0x00, state = "handshake")] - let (state, packet_id) = get_packet_details_from_attributes(&item_struct.attrs).expect( + let (state, packet_id) = get_packet_details_from_attributes(&item_struct.attrs, PacketBoundiness::Serverbound).expect( "parse_packet_attribute failed\ \nPlease provide the packet_id and state fields in the #[packet(...)] attribute.\ \nExample: #[packet(packet_id = 0x00, state = \"handshake\")]", @@ -169,18 +183,19 @@ pub fn bake_registry(input: TokenStream) -> TokenStream { TokenStream::from(output) } -fn parse_packet_id(state: &str, value: String) -> syn::Result { +fn parse_packet_id(state: &str, value: String, bound_to: PacketBoundiness) -> syn::Result { //! Sorry to anyone reading this code. The get_packet_id method PANICS if there is any type of error. //! these macros are treated like trash gah damn. they need better care 😔 // If the user provided a direct integer (like 0x01, or any number) value. if value.starts_with("0x") { - let n = u8::from_str_radix(&value[2..], 16).expect("from_str_radix failed"); + let value = value.strip_prefix("0x").expect("strip_prefix failed"); + let n = u8::from_str_radix(value, 16).expect("from_str_radix failed"); return Ok(n); } // If the user provided referencing packet id, then just get that. - let n = get_packet_id(state, PacketBoundiness::Clientbound, value.as_str()); + let n = get_packet_id(state, bound_to, value.as_str()); Ok(n) } diff --git a/src/lib/net/src/packets/incoming/client_information.rs b/src/lib/net/src/packets/incoming/client_information.rs index 5cd7c1a6..19c6949c 100644 --- a/src/lib/net/src/packets/incoming/client_information.rs +++ b/src/lib/net/src/packets/incoming/client_information.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use tracing::debug; #[derive(Debug, NetDecode)] -#[packet(packet_id = 0x00, state = "configuration")] +#[packet(packet_id = "client_information", state = "configuration")] pub struct ClientInformation { pub locale: String, pub view_distance: u8, diff --git a/src/lib/net/src/packets/incoming/handshake.rs b/src/lib/net/src/packets/incoming/handshake.rs index ce9b6ae5..167a2a73 100644 --- a/src/lib/net/src/packets/incoming/handshake.rs +++ b/src/lib/net/src/packets/incoming/handshake.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use tracing::trace; #[derive(NetDecode, Debug)] -#[packet(packet_id = 0x00, state = "handshake")] +#[packet(packet_id = "intention", state = "handshake")] pub struct Handshake { pub protocol_version: VarInt, pub server_address: String, diff --git a/src/lib/net/src/packets/incoming/keep_alive.rs b/src/lib/net/src/packets/incoming/keep_alive.rs index a71239f0..df1f9425 100644 --- a/src/lib/net/src/packets/incoming/keep_alive.rs +++ b/src/lib/net/src/packets/incoming/keep_alive.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use tracing::debug; #[derive(NetDecode)] -#[packet(packet_id = 0x18, state = "play")] +#[packet(packet_id = "keep_alive", state = "play")] pub struct IncomingKeepAlivePacket { pub timestamp: i64, } diff --git a/src/lib/net/src/packets/incoming/login_acknowledged.rs b/src/lib/net/src/packets/incoming/login_acknowledged.rs index 10c4050f..025c09f0 100644 --- a/src/lib/net/src/packets/incoming/login_acknowledged.rs +++ b/src/lib/net/src/packets/incoming/login_acknowledged.rs @@ -6,7 +6,7 @@ use ferrumc_state::ServerState; use std::sync::Arc; #[derive(Debug, NetDecode)] -#[packet(packet_id = 0x03, state = "login")] +#[packet(packet_id = "login_acknowledged", state = "login")] pub struct LoginAcknowledgedPacket {} #[derive(Event)] diff --git a/src/lib/net/src/packets/incoming/login_start.rs b/src/lib/net/src/packets/incoming/login_start.rs index 6a9c03d8..987aaaf7 100644 --- a/src/lib/net/src/packets/incoming/login_start.rs +++ b/src/lib/net/src/packets/incoming/login_start.rs @@ -6,7 +6,7 @@ use ferrumc_state::ServerState; use std::sync::Arc; #[derive(Debug, NetDecode)] -#[packet(packet_id = 0x00, state = "login")] +#[packet(packet_id = "hello", state = "login")] pub struct LoginStartPacket { pub username: String, pub uuid: u128, diff --git a/src/lib/net/src/packets/incoming/ping.rs b/src/lib/net/src/packets/incoming/ping.rs index 071dbf72..bfa74264 100644 --- a/src/lib/net/src/packets/incoming/ping.rs +++ b/src/lib/net/src/packets/incoming/ping.rs @@ -8,7 +8,7 @@ use ferrumc_state::ServerState; use std::sync::Arc; #[derive(NetDecode, Debug)] -#[packet(packet_id = 0x01, state = "status")] +#[packet(packet_id = "ping_request", state = "status")] pub struct PingPacket { payload: i64, } diff --git a/src/lib/net/src/packets/incoming/player_command.rs b/src/lib/net/src/packets/incoming/player_command.rs index e0cb73ee..ee4ed9db 100644 --- a/src/lib/net/src/packets/incoming/player_command.rs +++ b/src/lib/net/src/packets/incoming/player_command.rs @@ -9,7 +9,7 @@ use std::sync::Arc; // Mojang surely has SOME naming schemes.. commands?? #[derive(NetDecode)] -#[packet(packet_id = 0x25, state = "play")] +#[packet(packet_id = "player_command", state = "play")] pub struct PlayerCommandPacket { entity_id: VarInt, // Originally: Action Id = VarInt Enum diff --git a/src/lib/net/src/packets/incoming/server_bound_known_packs.rs b/src/lib/net/src/packets/incoming/server_bound_known_packs.rs index 2e182d72..318875b9 100644 --- a/src/lib/net/src/packets/incoming/server_bound_known_packs.rs +++ b/src/lib/net/src/packets/incoming/server_bound_known_packs.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use tracing::debug; #[derive(Debug, NetDecode)] -#[packet(packet_id = 0x07, state = "configuration")] +#[packet(packet_id = "select_known_packs", state = "configuration")] pub struct ServerBoundKnownPacks { #[allow(dead_code)] pub packs: LengthPrefixedVec, diff --git a/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs b/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs index a23a532f..2917488c 100644 --- a/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs +++ b/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use tracing::debug; #[derive(Debug)] -#[packet(packet_id = 0x02, state = "configuration")] +#[packet(packet_id = "custom_payload", state = "configuration")] pub struct ServerBoundPluginMessage { channel: String, data: Vec, diff --git a/src/lib/net/src/packets/incoming/set_player_position.rs b/src/lib/net/src/packets/incoming/set_player_position.rs index f02b6949..63c0e518 100644 --- a/src/lib/net/src/packets/incoming/set_player_position.rs +++ b/src/lib/net/src/packets/incoming/set_player_position.rs @@ -7,7 +7,7 @@ use ferrumc_state::ServerState; use std::sync::Arc; #[derive(NetDecode)] -#[packet(packet_id = 0x1A, state = "play")] +#[packet(packet_id = "move_player_pos", state = "play")] pub struct SetPlayerPositionPacket { pub x: f64, pub feet_y: f64, diff --git a/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs b/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs index afae64d9..8697d83f 100644 --- a/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs +++ b/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs @@ -7,7 +7,7 @@ use ferrumc_state::ServerState; use std::sync::Arc; #[derive(NetDecode)] -#[packet(packet_id = 0x1B, state = "play")] +#[packet(packet_id = "move_player_pos_rot", state = "play")] pub struct SetPlayerPositionAndRotationPacket { pub x: f64, pub feet_y: f64, diff --git a/src/lib/net/src/packets/incoming/set_player_rotation.rs b/src/lib/net/src/packets/incoming/set_player_rotation.rs index 99413cc9..5b138525 100644 --- a/src/lib/net/src/packets/incoming/set_player_rotation.rs +++ b/src/lib/net/src/packets/incoming/set_player_rotation.rs @@ -7,7 +7,7 @@ use ferrumc_state::ServerState; use std::sync::Arc; #[derive(NetDecode)] -#[packet(packet_id = 0x1C, state = "play")] +#[packet(packet_id = "move_player_rot", state = "play")] pub struct SetPlayerRotationPacket { pub yaw: f32, pub pitch: f32, diff --git a/src/lib/net/src/packets/incoming/status_request.rs b/src/lib/net/src/packets/incoming/status_request.rs index a5f349d1..d0b330f6 100644 --- a/src/lib/net/src/packets/incoming/status_request.rs +++ b/src/lib/net/src/packets/incoming/status_request.rs @@ -12,7 +12,7 @@ use rand::seq::IndexedRandom; use std::sync::Arc; #[derive(NetDecode, Debug)] -#[packet(packet_id = 0x00, state = "status")] +#[packet(packet_id = "status_request", state = "status")] pub struct StatusRequestPacket {} impl IncomingPacket for StatusRequestPacket { diff --git a/src/lib/net/src/packets/incoming/swing_arm.rs b/src/lib/net/src/packets/incoming/swing_arm.rs index 315e6abc..d4e3dac7 100644 --- a/src/lib/net/src/packets/incoming/swing_arm.rs +++ b/src/lib/net/src/packets/incoming/swing_arm.rs @@ -9,7 +9,7 @@ use ferrumc_state::ServerState; use std::sync::Arc; #[derive(NetDecode)] -#[packet(packet_id = 0x36, state = "play")] +#[packet(packet_id = "swing", state = "play")] pub struct SwingArmPacket { hand: VarInt, }