diff options
| author | Ubuntu <github@matdoes.dev> | 2022-09-06 18:41:59 +0000 |
|---|---|---|
| committer | Ubuntu <github@matdoes.dev> | 2022-09-06 18:41:59 +0000 |
| commit | ab45bb7825b41c745f89a1dc6c1213742c7b8512 (patch) | |
| tree | 213524948068bb8672302374283077b087668b2c /azalea-protocol | |
| parent | eb6328ddc6d3a7ca83e98d0297b7513c406dca08 (diff) | |
| download | azalea-drasl-ab45bb7825b41c745f89a1dc6c1213742c7b8512.tar.xz | |
rename variants in packet enums to be shorter
Diffstat (limited to 'azalea-protocol')
4 files changed, 30 insertions, 9 deletions
diff --git a/azalea-protocol/azalea-protocol-macros/src/lib.rs b/azalea-protocol/azalea-protocol-macros/src/lib.rs index 4f47c9a9..d15fecf0 100644 --- a/azalea-protocol/azalea-protocol-macros/src/lib.rs +++ b/azalea-protocol/azalea-protocol-macros/src/lib.rs @@ -17,11 +17,12 @@ fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> Toke syn::Fields::Named(f) => f, _ => panic!("#[derive(*Packet)] can only be used on structs with named fields"), }; + let variant_name = variant_name_from(&ident); let contents = quote! { impl #ident { pub fn get(self) -> #state { - #state::#ident(self) + #state::#variant_name(self) } pub fn write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { @@ -206,15 +207,17 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { let mut clientbound_read_match_contents = quote!(); for PacketIdPair { id, module, name } in input.serverbound.packets { + let variant_name = variant_name_from(&name); + let name_litstr = syn::LitStr::new(&name.to_string(), name.span()); serverbound_enum_contents.extend(quote! { - #name(#module::#name), + #variant_name(#module::#name), }); serverbound_id_match_contents.extend(quote! { - #serverbound_state_name::#name(_packet) => #id, + #serverbound_state_name::#variant_name(_packet) => #id, }); serverbound_write_match_contents.extend(quote! { - #serverbound_state_name::#name(packet) => packet.write(buf), + #serverbound_state_name::#variant_name(packet) => packet.write(buf), }); serverbound_read_match_contents.extend(quote! { #id => { @@ -230,14 +233,16 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { } for PacketIdPair { id, module, name } in input.clientbound.packets { let name_litstr = syn::LitStr::new(&name.to_string(), name.span()); + let variant_name = variant_name_from(&name); + clientbound_enum_contents.extend(quote! { - #name(#module::#name), + #variant_name(#module::#name), }); clientbound_id_match_contents.extend(quote! { - #clientbound_state_name::#name(_packet) => #id, + #clientbound_state_name::#variant_name(_packet) => #id, }); clientbound_write_match_contents.extend(quote! { - #clientbound_state_name::#name(packet) => packet.write(buf), + #clientbound_state_name::#variant_name(packet) => packet.write(buf), }); clientbound_read_match_contents.extend(quote! { #id => { @@ -350,3 +355,17 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { contents.into() } + +fn variant_name_from(name: &syn::Ident) -> syn::Ident { + // remove "<direction>Bound" from the start and "Packet" from the end + let mut variant_name = name.to_string(); + if variant_name.starts_with("Clientbound") { + variant_name = variant_name["Clientbound".len()..].to_string(); + } else if variant_name.starts_with("Serverbound") { + variant_name = variant_name["Serverbound".len()..].to_string(); + } + if variant_name.ends_with("Packet") { + variant_name = variant_name[..variant_name.len()-"Packet".len()].to_string(); + } + syn::Ident::new(&variant_name, name.span()) +} diff --git a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs index 61c281f5..6ecbfb66 100755 --- a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -12,7 +12,7 @@ pub struct ClientboundLoginCompressionPacket { impl ClientboundLoginCompressionPacket { pub fn get(self) -> ClientboundLoginPacket { - ClientboundLoginPacket::ClientboundLoginCompressionPacket(self) + ClientboundLoginPacket::LoginCompression(self) } pub fn write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index 06647540..110929a1 100644 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -7,6 +7,8 @@ use crate::read::ReadPacketError; use azalea_buf::{BufReadError, McBufVarReadable, McBufVarWritable, McBufWritable}; use std::io::{Read, Write}; +// TODO: rename the packet files to just like clientbound_add_entity instead of clientbound_add_entity_packet + pub const PROTOCOL_VERSION: u32 = 760; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 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 5382f195..f7a349e2 100755 --- 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) -> ClientboundStatusPacket { - ClientboundStatusPacket::ClientboundStatusResponsePacket(self) + ClientboundStatusPacket::StatusResponse(self) } pub fn write(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> { |
