From 747bfa568cb1da8afdeb29005bdd2836963ed613 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 16 Apr 2022 14:56:27 -0500 Subject: [PATCH 1/6] Add doc to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ea8c4bf7..4990fff7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/doc From f60426cd5a8a276adcb88988cef38d27f48de912 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 00:48:13 -0500 Subject: [PATCH 2/6] start adding declare_state_packets --- azalea-protocol/packet-macros/src/lib.rs | 135 ++++++++++++++++++++-- azalea-protocol/src/packets/game/mod.rs | 139 +++++++++++++---------- 2 files changed, 201 insertions(+), 73 deletions(-) diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 957515c4..0e6ad0e1 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -1,10 +1,14 @@ -use quote::{quote, ToTokens}; -use syn::{self, parse_macro_input, DeriveInput, FieldsNamed}; +use std::collections::{BTreeMap, HashMap}; -fn as_packet_derive( - input: proc_macro::TokenStream, - state: proc_macro2::TokenStream, -) -> proc_macro::TokenStream { +use proc_macro::TokenStream; +use quote::{quote, ToTokens}; +use syn::{ + self, braced, + parse::{Parse, ParseStream, Result}, + parse_macro_input, DeriveInput, Expr, FieldsNamed, Ident, LitInt, Token, Type, Visibility, +}; + +fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); let fields = match data { @@ -99,21 +103,132 @@ fn as_packet_derive( } #[proc_macro_derive(GamePacket, attributes(varint))] -pub fn derive_game_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_game_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::game::GamePacket}) } #[proc_macro_derive(HandshakePacket, attributes(varint))] -pub fn derive_handshake_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_handshake_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::handshake::HandshakePacket}) } #[proc_macro_derive(LoginPacket, attributes(varint))] -pub fn derive_login_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_login_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::login::LoginPacket}) } #[proc_macro_derive(StatusPacket, attributes(varint))] -pub fn derive_status_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_status_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::status::StatusPacket}) } + +#[derive(Debug)] +struct PacketIdPair { + id: u32, + module: Ident, + name: Ident, +} +#[derive(Debug)] +struct PacketIdMap { + packets: Vec, +} + +impl Parse for PacketIdMap { + fn parse(input: ParseStream) -> Result { + let mut packets = vec![]; + loop { + // 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, + // 0x0e + let packet_id: LitInt = match input.parse() { + Ok(i) => i, + Err(_) => break, + }; + let packet_id = packet_id.base10_parse::()?; + // : + input.parse::()?; + // clientbound_change_difficulty_packet + let module: Ident = input.parse()?; + // :: + input.parse::()?; + // ClientboundChangeDifficultyPacket + let name: Ident = input.parse()?; + input.parse::()?; + + packets.push(PacketIdPair { + id: packet_id, + module, + name, + }); + } + + Ok(PacketIdMap { packets }) + } +} + +#[derive(Debug)] +struct DeclareStatePackets { + name: Ident, + serverbound: PacketIdMap, + clientbound: PacketIdMap, +} + +impl Parse for DeclareStatePackets { + fn parse(input: ParseStream) -> Result { + let name = input.parse()?; + input.parse::()?; + + let serverbound_token: Ident = input.parse()?; + if serverbound_token != "Serverbound" { + return Err(syn::Error::new( + serverbound_token.span(), + "Expected `Serverbound`", + )); + } + input.parse::]>()?; + let content; + braced!(content in input); + let serverbound = content.parse()?; + + input.parse::()?; + + let clientbound_token: Ident = input.parse()?; + if clientbound_token != "Clientbound" { + return Err(syn::Error::new( + clientbound_token.span(), + "Expected `Clientbound`", + )); + } + input.parse::]>()?; + let content; + braced!(content in input); + let clientbound = content.parse()?; + + Ok(DeclareStatePackets { + name, + serverbound, + clientbound, + }) + } +} +#[proc_macro] +pub fn declare_state_packets(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeclareStatePackets); + + let name = input.name; + + let mut enum_contents = quote!(); + for PacketIdPair { id, module, name } in input.serverbound.packets { + enum_contents.extend(quote! {#name(#module::#name)}); + } + + quote! { + #[derive(Clone, Debug)] + pub enum #name + where + Self: Sized, + { + #enum_contents + } + } + .into() +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 4efe72fb..cb07cf97 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -6,70 +6,83 @@ pub mod clientbound_update_view_distance_packet; use super::ProtocolPacket; use crate::connect::PacketFlow; use async_trait::async_trait; +use packet_macros::declare_state_packets; -#[derive(Clone, Debug)] -pub enum GamePacket -where - Self: Sized, -{ - ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), - ClientboundUpdateViewDistancePacket( - clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, - ), - ClientboundCustomPayloadPacket( - clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, - ), - ClientboundChangeDifficultyPacket( - clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, - ), -} - -#[async_trait] -impl ProtocolPacket for GamePacket { - fn id(&self) -> u32 { - match self { - GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, - GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, - GamePacket::ClientboundLoginPacket(_packet) => 0x26, - GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, - } +declare_state_packets!( + GamePacket, + // no serverbound packets implemented yet + Serverbound => {}, + Clientbound => { + 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, + 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, + 0x26: clientbound_login_packet::ClientboundLoginPacket, + 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, } +); - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), - GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), - GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), - GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), - } - } +// #[derive(Clone, Debug)] +// pub enum GamePacket +// where +// Self: Sized, +// { +// ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), +// ClientboundUpdateViewDistancePacket( +// clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, +// ), +// ClientboundCustomPayloadPacket( +// clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, +// ), +// ClientboundChangeDifficultyPacket( +// clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, +// ), +// } - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - Ok(match flow { - PacketFlow::ServerToClient => match id { - 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket - ::read(buf) - .await?, - 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, - 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, - 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket - ::read(buf) - .await?, - // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), - _ => panic!("Unknown ServerToClient game packet id: {}", id), - }, - PacketFlow::ClientToServer => match id { - // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, - _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)), - }, - }) - } -} +// #[async_trait] +// impl ProtocolPacket for GamePacket { +// fn id(&self) -> u32 { +// match self { +// GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, +// GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, +// GamePacket::ClientboundLoginPacket(_packet) => 0x26, +// GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, +// } +// } + +// fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { +// match self { +// GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), +// GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), +// GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), +// GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), +// } +// } + +// /// Read a packet by its id, ConnectionProtocol, and flow +// async fn read( +// id: u32, +// flow: &PacketFlow, +// buf: &mut T, +// ) -> Result +// where +// Self: Sized, +// { +// Ok(match flow { +// PacketFlow::ServerToClient => match id { +// 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket +// ::read(buf) +// .await?, +// 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, +// 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, +// 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket +// ::read(buf) +// .await?, +// // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), +// _ => panic!("Unknown ServerToClient game packet id: {}", id), +// }, +// PacketFlow::ClientToServer => match id { +// // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, +// _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)), +// }, +// }) +// } +// } From c04a28621e53129c03f8aee8cbee89556abf137f Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 00:53:51 -0500 Subject: [PATCH 3/6] start adding more stuff --- azalea-protocol/packet-macros/src/lib.rs | 71 +++++++++++++++++++++++- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 0e6ad0e1..8442b921 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -214,21 +214,86 @@ impl Parse for DeclareStatePackets { pub fn declare_state_packets(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeclareStatePackets); - let name = input.name; + let state_name = input.name; let mut enum_contents = quote!(); + let mut id_match_contents = quote!(); + let mut write_match_contents = quote!(); for PacketIdPair { id, module, name } in input.serverbound.packets { - enum_contents.extend(quote! {#name(#module::#name)}); + enum_contents.extend(quote! { + #name(#module::#name) + }); + id_match_contents.extend(quote! { + #state_name::#name(_packet) => #id + }); + write_match_contents.extend(quote! { + #state_name::#name(packet) => packet.write(buf) + }); + } + for PacketIdPair { id, module, name } in input.clientbound.packets { + enum_contents.extend(quote! { + #name(#module::#name) + }); + id_match_contents.extend(quote! { + #state_name::#name(_packet) => #id + }); + write_match_contents.extend(quote! { + #state_name::#name(packet) => packet.write(buf) + }); } quote! { #[derive(Clone, Debug)] - pub enum #name + pub enum #state_name where Self: Sized, { #enum_contents } + + #[async_trait] + impl ProtocolPacket for GamePacket { + fn id(&self) -> u32 { + match self { + #id_match_contents + } + } + + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + match self { + #write_match_contents + } + } + + /// Read a packet by its id, ConnectionProtocol, and flow + async fn read( + id: u32, + flow: &PacketFlow, + buf: &mut T, + ) -> Result + where + Self: Sized, + { + Ok(match flow { + PacketFlow::ServerToClient => match id { + 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket + ::read(buf) + .await?, + 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, + 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, + 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket + ::read(buf) + .await?, + // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), + _ => panic!("Unknown ServerToClient game packet id: {}", id), + }, + PacketFlow::ClientToServer => match id { + // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, + _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)), + }, + }) + } + } } .into() } From 9633874d23f3baa8e5d5c33f3fd51ae6aa880a88 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 18:53:13 -0500 Subject: [PATCH 4/6] finish declare_state_packets implementation --- azalea-protocol/packet-macros/src/lib.rs | 35 ++++++------- azalea-protocol/src/packets/game/mod.rs | 67 ------------------------ 2 files changed, 17 insertions(+), 85 deletions(-) diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 8442b921..ab062550 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -1,11 +1,9 @@ -use std::collections::{BTreeMap, HashMap}; - use proc_macro::TokenStream; use quote::{quote, ToTokens}; use syn::{ self, braced, parse::{Parse, ParseStream, Result}, - parse_macro_input, DeriveInput, Expr, FieldsNamed, Ident, LitInt, Token, Type, Visibility, + parse_macro_input, DeriveInput, FieldsNamed, Ident, LitInt, Token, }; fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> TokenStream { @@ -215,10 +213,13 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeclareStatePackets); let state_name = input.name; + let state_name_litstr = syn::LitStr::new(&state_name.to_string(), state_name.span()); let mut enum_contents = quote!(); let mut id_match_contents = quote!(); let mut write_match_contents = quote!(); + let mut serverbound_read_match_contents = quote!(); + let mut clientbound_read_match_contents = quote!(); for PacketIdPair { id, module, name } in input.serverbound.packets { enum_contents.extend(quote! { #name(#module::#name) @@ -229,16 +230,22 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { write_match_contents.extend(quote! { #state_name::#name(packet) => packet.write(buf) }); + serverbound_read_match_contents.extend(quote! { + #id => #module::#name::read(buf).await?, + }); } for PacketIdPair { id, module, name } in input.clientbound.packets { enum_contents.extend(quote! { - #name(#module::#name) + #name(#module::#name), }); id_match_contents.extend(quote! { - #state_name::#name(_packet) => #id + #state_name::#name(_packet) => #id, }); write_match_contents.extend(quote! { - #state_name::#name(packet) => packet.write(buf) + #state_name::#name(packet) => packet.write(buf), + }); + clientbound_read_match_contents.extend(quote! { + #id => #module::#name::read(buf).await?, }); } @@ -276,20 +283,12 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { { Ok(match flow { PacketFlow::ServerToClient => match id { - 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket - ::read(buf) - .await?, - 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, - 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, - 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket - ::read(buf) - .await?, - // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), - _ => panic!("Unknown ServerToClient game packet id: {}", id), + #serverbound_read_match_contents + _ => panic!("Unknown ServerToClient {} packet id: {}", #state_name_litstr, id), }, PacketFlow::ClientToServer => match id { - // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, - _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)), + #clientbound_read_match_contents + _ => return Err(format!("Unknown ClientToServer {} packet id: {}", #state_name_litstr, id)), }, }) } diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index cb07cf97..2affd49e 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -19,70 +19,3 @@ declare_state_packets!( 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, } ); - -// #[derive(Clone, Debug)] -// pub enum GamePacket -// where -// Self: Sized, -// { -// ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), -// ClientboundUpdateViewDistancePacket( -// clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, -// ), -// ClientboundCustomPayloadPacket( -// clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, -// ), -// ClientboundChangeDifficultyPacket( -// clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, -// ), -// } - -// #[async_trait] -// impl ProtocolPacket for GamePacket { -// fn id(&self) -> u32 { -// match self { -// GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, -// GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, -// GamePacket::ClientboundLoginPacket(_packet) => 0x26, -// GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, -// } -// } - -// fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { -// match self { -// GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), -// GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), -// GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), -// GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), -// } -// } - -// /// Read a packet by its id, ConnectionProtocol, and flow -// async fn read( -// id: u32, -// flow: &PacketFlow, -// buf: &mut T, -// ) -> Result -// where -// Self: Sized, -// { -// Ok(match flow { -// PacketFlow::ServerToClient => match id { -// 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket -// ::read(buf) -// .await?, -// 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, -// 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, -// 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket -// ::read(buf) -// .await?, -// // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), -// _ => panic!("Unknown ServerToClient game packet id: {}", id), -// }, -// PacketFlow::ClientToServer => match id { -// // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, -// _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)), -// }, -// }) -// } -// } From cbdab27cb555e38b39fe0f600ff945090ec10dcb Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 19:17:36 -0500 Subject: [PATCH 5/6] add declare_state_packets to the other states --- azalea-protocol/packet-macros/src/lib.rs | 18 ++-- azalea-protocol/src/packets/game/mod.rs | 4 - azalea-protocol/src/packets/handshake/mod.rs | 51 ++---------- azalea-protocol/src/packets/login/mod.rs | 83 +++---------------- .../clientbound_status_response_packet.rs | 2 +- azalea-protocol/src/packets/status/mod.rs | 69 ++------------- 6 files changed, 38 insertions(+), 189 deletions(-) diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index ab062550..3cc3677a 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -222,13 +222,13 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { let mut clientbound_read_match_contents = quote!(); for PacketIdPair { id, module, name } in input.serverbound.packets { enum_contents.extend(quote! { - #name(#module::#name) + #name(#module::#name), }); id_match_contents.extend(quote! { - #state_name::#name(_packet) => #id + #state_name::#name(_packet) => #id, }); write_match_contents.extend(quote! { - #state_name::#name(packet) => packet.write(buf) + #state_name::#name(packet) => packet.write(buf), }); serverbound_read_match_contents.extend(quote! { #id => #module::#name::read(buf).await?, @@ -258,8 +258,8 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { #enum_contents } - #[async_trait] - impl ProtocolPacket for GamePacket { + #[async_trait::async_trait] + impl crate::packets::ProtocolPacket for #state_name { fn id(&self) -> u32 { match self { #id_match_contents @@ -275,18 +275,18 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { /// Read a packet by its id, ConnectionProtocol, and flow async fn read( id: u32, - flow: &PacketFlow, + flow: &crate::connect::PacketFlow, buf: &mut T, - ) -> Result + ) -> Result<#state_name, String> where Self: Sized, { Ok(match flow { - PacketFlow::ServerToClient => match id { + crate::connect::PacketFlow::ServerToClient => match id { #serverbound_read_match_contents _ => panic!("Unknown ServerToClient {} packet id: {}", #state_name_litstr, id), }, - PacketFlow::ClientToServer => match id { + crate::connect::PacketFlow::ClientToServer => match id { #clientbound_read_match_contents _ => return Err(format!("Unknown ClientToServer {} packet id: {}", #state_name_litstr, id)), }, diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 2affd49e..a4304d9f 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -3,14 +3,10 @@ pub mod clientbound_custom_payload_packet; pub mod clientbound_login_packet; pub mod clientbound_update_view_distance_packet; -use super::ProtocolPacket; -use crate::connect::PacketFlow; -use async_trait::async_trait; use packet_macros::declare_state_packets; declare_state_packets!( GamePacket, - // no serverbound packets implemented yet Serverbound => {}, Clientbound => { 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, diff --git a/azalea-protocol/src/packets/handshake/mod.rs b/azalea-protocol/src/packets/handshake/mod.rs index 17465fca..88d9939b 100644 --- a/azalea-protocol/src/packets/handshake/mod.rs +++ b/azalea-protocol/src/packets/handshake/mod.rs @@ -1,48 +1,11 @@ pub mod client_intention_packet; -use async_trait::async_trait; +use packet_macros::declare_state_packets; -use crate::connect::PacketFlow; - -use super::ProtocolPacket; - -#[derive(Clone, Debug)] -pub enum HandshakePacket -where - Self: Sized, -{ - ClientIntentionPacket(client_intention_packet::ClientIntentionPacket), -} - -#[async_trait] -impl ProtocolPacket for HandshakePacket { - fn id(&self) -> u32 { - match self { - HandshakePacket::ClientIntentionPacket(_packet) => 0x00, - } +declare_state_packets!( + HandshakePacket, + Serverbound => {}, + Clientbound => { + 0x00: client_intention_packet::ClientIntentionPacket, } - - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - HandshakePacket::ClientIntentionPacket(packet) => packet.write(buf), - } - } - - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - match flow { - PacketFlow::ServerToClient => Err("HandshakePacket::read not implemented".to_string()), - PacketFlow::ClientToServer => match id { - 0x00 => Ok(client_intention_packet::ClientIntentionPacket::read(buf).await?), - _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), - }, - } - } -} +); diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index b1f61746..ef5f15c1 100644 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -4,76 +4,17 @@ pub mod clientbound_hello_packet; pub mod clientbound_login_compression_packet; pub mod serverbound_hello_packet; -use super::ProtocolPacket; -use crate::connect::PacketFlow; -use async_trait::async_trait; +use packet_macros::declare_state_packets; -#[derive(Clone, Debug)] -pub enum LoginPacket -where - Self: Sized, -{ - ClientboundCustomQueryPacket(clientbound_custom_query_packet::ClientboundCustomQueryPacket), - ClientboundGameProfilePacket(clientbound_game_profile_packet::ClientboundGameProfilePacket), - ClientboundHelloPacket(clientbound_hello_packet::ClientboundHelloPacket), - ClientboundLoginCompressionPacket( - clientbound_login_compression_packet::ClientboundLoginCompressionPacket, - ), - ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket), -} - -#[async_trait] -impl ProtocolPacket for LoginPacket { - fn id(&self) -> u32 { - match self { - LoginPacket::ClientboundCustomQueryPacket(_packet) => 0x04, - LoginPacket::ClientboundGameProfilePacket(_packet) => 0x02, - LoginPacket::ClientboundHelloPacket(_packet) => 0x01, - LoginPacket::ClientboundLoginCompressionPacket(_packet) => 0x03, - LoginPacket::ServerboundHelloPacket(_packet) => 0x00, - } +declare_state_packets!( + LoginPacket, + Serverbound => { + 0x00: serverbound_hello_packet::ServerboundHelloPacket, + }, + Clientbound => { + 0x00: clientbound_hello_packet::ClientboundHelloPacket, + 0x02: clientbound_game_profile_packet::ClientboundGameProfilePacket, + 0x03: clientbound_login_compression_packet::ClientboundLoginCompressionPacket, + 0x04: clientbound_custom_query_packet::ClientboundCustomQueryPacket, } - - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - LoginPacket::ClientboundCustomQueryPacket(packet) => packet.write(buf), - LoginPacket::ClientboundGameProfilePacket(packet) => packet.write(buf), - LoginPacket::ClientboundHelloPacket(packet) => packet.write(buf), - LoginPacket::ClientboundLoginCompressionPacket(packet) => packet.write(buf), - LoginPacket::ServerboundHelloPacket(packet) => packet.write(buf), - } - } - - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - Ok(match flow { - PacketFlow::ServerToClient => match id { - 0x01 => clientbound_hello_packet::ClientboundHelloPacket::read(buf).await?, - 0x02 => { - clientbound_game_profile_packet::ClientboundGameProfilePacket::read(buf).await? - } - 0x04 => { - clientbound_custom_query_packet::ClientboundCustomQueryPacket::read(buf).await? - } - 0x03 => { - clientbound_login_compression_packet::ClientboundLoginCompressionPacket::read( - buf, - ) - .await? - } - _ => return Err(format!("Unknown ServerToClient login packet id: {}", id)), - }, - PacketFlow::ClientToServer => match id { - 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, - _ => return Err(format!("Unknown ClientToServer login packet id: {}", id)), - }, - }) - } -} +); diff --git a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs index 58f5b701..884cf408 100644 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -36,7 +36,7 @@ pub struct ClientboundStatusResponsePacket { impl ClientboundStatusResponsePacket { pub fn get(self) -> StatusPacket { - StatusPacket::ClientboundStatusResponsePacket(Box::new(self)) + StatusPacket::ClientboundStatusResponsePacket(self) } pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { diff --git a/azalea-protocol/src/packets/status/mod.rs b/azalea-protocol/src/packets/status/mod.rs index 31fedfb9..56aa577e 100644 --- a/azalea-protocol/src/packets/status/mod.rs +++ b/azalea-protocol/src/packets/status/mod.rs @@ -1,65 +1,14 @@ pub mod clientbound_status_response_packet; pub mod serverbound_status_request_packet; -use async_trait::async_trait; +use packet_macros::declare_state_packets; -use crate::connect::PacketFlow; - -use super::ProtocolPacket; - -#[derive(Clone, Debug)] -pub enum StatusPacket -where - Self: Sized, -{ - ServerboundStatusRequestPacket( - serverbound_status_request_packet::ServerboundStatusRequestPacket, - ), - ClientboundStatusResponsePacket( - Box, - ), -} - -#[async_trait] -impl ProtocolPacket for StatusPacket { - fn id(&self) -> u32 { - match self { - StatusPacket::ServerboundStatusRequestPacket(_packet) => 0x00, - StatusPacket::ClientboundStatusResponsePacket(_packet) => 0x00, - } +declare_state_packets!( + StatusPacket, + Serverbound => { + 0x00: serverbound_status_request_packet::ServerboundStatusRequestPacket, + }, + Clientbound => { + 0x00: clientbound_status_response_packet::ClientboundStatusResponsePacket, } - - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - StatusPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf), - StatusPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf), - } - } - - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - match flow { - PacketFlow::ServerToClient => match id { - 0x00 => Ok( - clientbound_status_response_packet::ClientboundStatusResponsePacket::read(buf) - .await?, - ), - _ => Err(format!("Unknown ServerToClient status packet id: {}", id)), - }, - PacketFlow::ClientToServer => match id { - 0x00 => Ok( - serverbound_status_request_packet::ServerboundStatusRequestPacket::read(buf) - .await?, - ), - _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), - }, - } - } -} +); From bc22faf3b0ba6cc8cc8de6276ff998690ba87bab Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 20:33:17 -0500 Subject: [PATCH 6/6] remove a couple of imports --- azalea-protocol/src/mc_buf/read.rs | 1 - azalea-protocol/src/mc_buf/write.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 0fa1d099..20b69238 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -2,7 +2,6 @@ use async_trait::async_trait; use azalea_core::{ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, }; -use num_traits::FromPrimitive; use tokio::io::{AsyncRead, AsyncReadExt}; use super::MAX_STRING_LENGTH; diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index fd9faeb4..26c0ef35 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -3,7 +3,6 @@ use azalea_core::{ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, }; use byteorder::{BigEndian, WriteBytesExt}; -use num_traits::FromPrimitive; use std::io::Write; use super::MAX_STRING_LENGTH;