diff options
| author | mat <github@matdoes.dev> | 2022-01-02 17:03:34 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-01-02 17:03:34 -0600 |
| commit | bb57273f48294355d7ac863c291d80878f711c16 (patch) | |
| tree | 51586d63834d6cc6f2e036da8c67f63f2eae0113 /azalea-protocol/packet-macros/src | |
| parent | 3ec7352da2c8a9870718e6a81913f6aa1b576b95 (diff) | |
| download | azalea-drasl-bb57273f48294355d7ac863c291d80878f711c16.tar.xz | |
start improving packet macros
Diffstat (limited to 'azalea-protocol/packet-macros/src')
| -rw-r--r-- | azalea-protocol/packet-macros/src/lib.rs | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 11878abf..22c39ea9 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -32,13 +32,6 @@ fn as_packet_derive( // i don't know how to do this in a way that isn't terrible } else if path.to_token_stream().to_string() == "Vec < u8 >" { quote! { buf.write_bytes(&self.#field_name)?; } - } else if path.is_ident("i32") { - // only treat it as a varint if it has the varint attribute - if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { - quote! { buf.write_varint(self.#field_name)?; } - } else { - quote! { buf.write_i32(self.#field_name)?; } - } } else if path.is_ident("u32") { if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { quote! { buf.write_varint(self.#field_name as i32)?; } @@ -50,10 +43,16 @@ fn as_packet_derive( } else if path.is_ident("ConnectionProtocol") { quote! { buf.write_varint(self.#field_name.clone() as i32)?; } } else { - panic!( - "#[derive(*Packet)] doesn't know how to write {}", - path.to_token_stream() - ); + if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + quote! { + crate::mc_buf::McBufVarintWritable::varint_write_into(&self.#field_name, buf)?; + } + } else { + quote! { + crate::mc_buf::McBufVarintWritable::write_into(&self.#field_name, buf)?; + } + } + } } _ => panic!( @@ -78,16 +77,8 @@ fn as_packet_derive( quote! { let #field_name = buf.read_utf().await?; } } else if path.is_ident("ResourceLocation") { quote! { let #field_name = buf.read_resource_location().await?; } - // i don't know how to do this in a way that isn't terrible } else if path.to_token_stream().to_string() == "Vec < u8 >" { quote! { let #field_name = buf.read_bytes().await?; } - } else if path.is_ident("i32") { - // only treat it as a varint if it has the varint attribute - if f.attrs.iter().any(|a| a.path.is_ident("varint")) { - quote! { let #field_name = buf.read_varint().await?; } - } else { - quote! { let #field_name = buf.read_i32().await?; } - } } else if path.is_ident("u32") { if f.attrs.iter().any(|a| a.path.is_ident("varint")) { quote! { let #field_name = buf.read_varint().await? as u32; } @@ -102,10 +93,15 @@ fn as_packet_derive( .ok_or_else(|| "Invalid intention".to_string())?; } } else { - panic!( - "#[derive(*Packet)] doesn't know how to read {}", - path.to_token_stream() - ); + if f.attrs.iter().any(|a| a.path.is_ident("varint")) { + quote! { + let #field_name = crate::mc_buf::McBufVarintReadable::varint_read_into(buf).await?; + } + } else { + quote! { + let #field_name = crate::mc_buf::McBufReadable::read_into(buf).await?; + } + } } } _ => panic!( |
