Skip to content

Commit

Permalink
Updated all packets to use resource id instead of hardcoded packet id…
Browse files Browse the repository at this point in the history
… values.
  • Loading branch information
Sweattypalms committed Jan 4, 2025
1 parent 927c05b commit b1339a1
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/lib/derive_macros/src/net/encode.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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) => {
Expand Down
29 changes: 22 additions & 7 deletions src/lib/derive_macros/src/net/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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\")]",
Expand Down Expand Up @@ -169,18 +183,19 @@ pub fn bake_registry(input: TokenStream) -> TokenStream {
TokenStream::from(output)
}

fn parse_packet_id(state: &str, value: String) -> syn::Result<u8> {
fn parse_packet_id(state: &str, value: String, bound_to: PacketBoundiness) -> syn::Result<u8> {
//! 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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/client_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/keep_alive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/login_acknowledged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/login_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/player_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackOwned>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/set_player_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/set_player_rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/status_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/swing_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit b1339a1

Please sign in to comment.