diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2024-11-27 19:31:40 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-27 19:31:40 -0600 |
| commit | 08958c2278b15ebeac8a964f392ebb792e479b61 (patch) | |
| tree | 4ae3664cea38d7fd1a8f1e95ed06fac04ffe519e /azalea-buf/azalea-buf-macros/src | |
| parent | 139d77d3c2b0922fba5e9d4fa2bd9819d78bd773 (diff) | |
| download | azalea-drasl-08958c2278b15ebeac8a964f392ebb792e479b61.tar.xz | |
Refactor azalea-protocol (#190)
* start updating to 1.21.4
* fix block codegen and stop using block data from burger
* rename packet related modules and structs to be simpler
* ItemSlot -> ItemStack for more consistency with mojmap
* .get() -> .into_packet()
* simplify declare_state_packets by removing packet ids
* rename read_from and write_into to azalea_read and azalea_write
* rename McBufReadable and McBufWritable to AzaleaRead and AzaleaWrite
* McBuf -> AzBuf
* remove most uses of into_variant
* update codegen and use resourcelocation names for packets
* implement #[limit(i)] attribute for AzBuf derive macro
* fixes for 1.21.4
* fix examples
* update some physics code and fix ChatType
* remove unused imports in codegen
* re-add some things to migrate.py and update +mc version numbers automatically
* downgrade to 1.21.3 lol
Diffstat (limited to 'azalea-buf/azalea-buf-macros/src')
| -rwxr-xr-x | azalea-buf/azalea-buf-macros/src/lib.rs | 20 | ||||
| -rw-r--r-- | azalea-buf/azalea-buf-macros/src/read.rs | 78 | ||||
| -rw-r--r-- | azalea-buf/azalea-buf-macros/src/write.rs | 36 |
3 files changed, 88 insertions, 46 deletions
diff --git a/azalea-buf/azalea-buf-macros/src/lib.rs b/azalea-buf/azalea-buf-macros/src/lib.rs index 4d17daa6..dec624e3 100755 --- a/azalea-buf/azalea-buf-macros/src/lib.rs +++ b/azalea-buf/azalea-buf-macros/src/lib.rs @@ -5,26 +5,26 @@ use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, DeriveInput}; -#[proc_macro_derive(McBufReadable, attributes(var))] -pub fn derive_mcbufreadable(input: TokenStream) -> TokenStream { +#[proc_macro_derive(AzaleaRead, attributes(var))] +pub fn derive_azalearead(input: TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); - read::create_impl_mcbufreadable(&ident, &data).into() + read::create_impl_azalearead(&ident, &data).into() } -#[proc_macro_derive(McBufWritable, attributes(var))] -pub fn derive_mcbufwritable(input: TokenStream) -> TokenStream { +#[proc_macro_derive(AzaleaWrite, attributes(var))] +pub fn derive_azaleawrite(input: TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); - write::create_impl_mcbufwritable(&ident, &data).into() + write::create_impl_azaleawrite(&ident, &data).into() } -#[proc_macro_derive(McBuf, attributes(var))] -pub fn derive_mcbuf(input: TokenStream) -> TokenStream { +#[proc_macro_derive(AzBuf, attributes(var, limit))] +pub fn derive_azbuf(input: TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); - let writable = write::create_impl_mcbufwritable(&ident, &data); - let readable = read::create_impl_mcbufreadable(&ident, &data); + let writable = write::create_impl_azaleawrite(&ident, &data); + let readable = read::create_impl_azalearead(&ident, &data); quote! { #writable #readable diff --git a/azalea-buf/azalea-buf-macros/src/read.rs b/azalea-buf/azalea-buf-macros/src/read.rs index c5c484e7..c093d4a1 100644 --- a/azalea-buf/azalea-buf-macros/src/read.rs +++ b/azalea-buf/azalea-buf-macros/src/read.rs @@ -9,17 +9,38 @@ fn read_named_fields( .map(|f| { let field_name = &f.ident; let field_type = &f.ty; + + let is_variable_length = f.attrs.iter().any(|a| a.path().is_ident("var")); + let limit = f + .attrs + .iter() + .find(|a| a.path().is_ident("limit")) + .map(|a| { + a.parse_args::<syn::LitInt>() + .unwrap() + .base10_parse::<usize>() + .unwrap() + }); + + if is_variable_length && limit.is_some() { + panic!("Fields cannot have both var and limit attributes"); + } + // do a different buf.write_* for each field depending on the type // if it's a string, use buf.write_string match field_type { syn::Type::Path(_) | syn::Type::Array(_) => { - if f.attrs.iter().any(|a| a.path().is_ident("var")) { + if is_variable_length { + quote! { + let #field_name = azalea_buf::AzaleaReadVar::azalea_read_var(buf)?; + } + } else if let Some(limit) = limit { quote! { - let #field_name = azalea_buf::McBufVarReadable::var_read_from(buf)?; + let #field_name = azalea_buf::AzaleaReadLimited::azalea_read_limited(buf, #limit)?; } } else { quote! { - let #field_name = azalea_buf::McBufReadable::read_from(buf)?; + let #field_name = azalea_buf::AzaleaRead::azalea_read(buf)?; } } } @@ -36,15 +57,15 @@ fn read_named_fields( (read_fields, read_field_names) } -pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenStream { +pub fn create_impl_azalearead(ident: &Ident, data: &Data) -> proc_macro2::TokenStream { match data { syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields { syn::Fields::Named(FieldsNamed { named, .. }) => { let (read_fields, read_field_names) = read_named_fields(named); quote! { - impl azalea_buf::McBufReadable for #ident { - fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { + impl azalea_buf::AzaleaRead for #ident { + fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { #(#read_fields)* Ok(Self { #(#read_field_names: #read_field_names),* @@ -55,15 +76,15 @@ pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::Tok } syn::Fields::Unit => { quote! { - impl azalea_buf::McBufReadable for #ident { - fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { + impl azalea_buf::AzaleaRead for #ident { + fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { Ok(Self) } } } } _ => { - panic!("#[derive(McBuf)] can only be used on structs with named fields") + panic!("#[derive(AzBuf)] can only be used on structs with named fields") } }, syn::Data::Enum(syn::DataEnum { variants, .. }) => { @@ -108,13 +129,34 @@ pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::Tok syn::Fields::Unnamed(fields) => { let mut reader_code = quote! {}; for f in &fields.unnamed { - if f.attrs.iter().any(|attr| attr.path().is_ident("var")) { + let is_variable_length = + f.attrs.iter().any(|a| a.path().is_ident("var")); + let limit = + f.attrs + .iter() + .find(|a| a.path().is_ident("limit")) + .map(|a| { + a.parse_args::<syn::LitInt>() + .unwrap() + .base10_parse::<usize>() + .unwrap() + }); + + if is_variable_length && limit.is_some() { + panic!("Fields cannot have both var and limit attributes"); + } + + if is_variable_length { + reader_code.extend(quote! { + Self::#variant_name(azalea_buf::AzaleaReadVar::azalea_read_var(buf)?), + }); + } else if let Some(limit) = limit { reader_code.extend(quote! { - Self::#variant_name(azalea_buf::McBufVarReadable::var_read_from(buf)?), + Self::#variant_name(azalea_buf::AzaleaReadLimited::azalea_read_limited(buf, #limit)?), }); } else { reader_code.extend(quote! { - Self::#variant_name(azalea_buf::McBufReadable::read_from(buf)?), + Self::#variant_name(azalea_buf::AzaleaRead::azalea_read(buf)?), }); } } @@ -139,15 +181,15 @@ pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::Tok let first_reader = first_reader.expect("There should be at least one variant"); quote! { - impl azalea_buf::McBufReadable for #ident { - fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { - let id = azalea_buf::McBufVarReadable::var_read_from(buf)?; - Self::read_from_id(buf, id) + impl azalea_buf::AzaleaRead for #ident { + fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> { + let id = azalea_buf::AzaleaReadVar::azalea_read_var(buf)?; + Self::azalea_read_id(buf, id) } } impl #ident { - pub fn read_from_id(buf: &mut std::io::Cursor<&[u8]>, id: u32) -> Result<Self, azalea_buf::BufReadError> { + pub fn azalea_read_id(buf: &mut std::io::Cursor<&[u8]>, id: u32) -> Result<Self, azalea_buf::BufReadError> { match id { #match_contents // you'd THINK this throws an error, but mojang decided to make it default for some reason @@ -157,6 +199,6 @@ pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::Tok } } } - _ => panic!("#[derive(McBuf)] can only be used on structs"), + _ => panic!("#[derive(AzBuf)] can only be used on structs"), } } diff --git a/azalea-buf/azalea-buf-macros/src/write.rs b/azalea-buf/azalea-buf-macros/src/write.rs index 4d31f39e..df461d59 100644 --- a/azalea-buf/azalea-buf-macros/src/write.rs +++ b/azalea-buf/azalea-buf-macros/src/write.rs @@ -19,11 +19,11 @@ fn write_named_fields( syn::Type::Path(_) | syn::Type::Array(_) => { if f.attrs.iter().any(|attr| attr.path().is_ident("var")) { quote! { - azalea_buf::McBufVarWritable::var_write_into(#ident_dot_field, buf)?; + azalea_buf::AzaleaWriteVar::azalea_write_var(#ident_dot_field, buf)?; } } else { quote! { - azalea_buf::McBufWritable::write_into(#ident_dot_field, buf)?; + azalea_buf::AzaleaWrite::azalea_write(#ident_dot_field, buf)?; } } } @@ -37,7 +37,7 @@ fn write_named_fields( quote! { #(#write_fields)* } } -pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenStream { +pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::TokenStream { match data { syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields { syn::Fields::Named(FieldsNamed { named, .. }) => { @@ -45,8 +45,8 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok write_named_fields(named, Some(&Ident::new("self", Span::call_site()))); quote! { - impl azalea_buf::McBufWritable for #ident { - fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { + impl azalea_buf::AzaleaWrite for #ident { + fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { #write_fields Ok(()) } @@ -55,15 +55,15 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok } syn::Fields::Unit => { quote! { - impl azalea_buf::McBufWritable for #ident { - fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { + impl azalea_buf::AzaleaWrite for #ident { + fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { Ok(()) } } } } _ => { - panic!("#[derive(McBuf)] can only be used on structs with named fields") + panic!("#[derive(AzBuf)] can only be used on structs with named fields") } }, syn::Data::Enum(syn::DataEnum { variants, .. }) => { @@ -103,7 +103,7 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok // the variant number that we're going to write let write_the_variant = quote! { - azalea_buf::McBufVarWritable::var_write_into(&#variant_discrim, buf)?; + azalea_buf::AzaleaWriteVar::azalea_write_var(&#variant_discrim, buf)?; }; match &variant.fields { syn::Fields::Named(f) => { @@ -145,11 +145,11 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok params_code.extend(quote! { #param_ident, }); if f.attrs.iter().any(|attr| attr.path().is_ident("var")) { writers_code.extend(quote! { - azalea_buf::McBufVarWritable::var_write_into(#param_ident, buf)?; + azalea_buf::AzaleaWriteVar::azalea_write_var(#param_ident, buf)?; }); } else { writers_code.extend(quote! { - azalea_buf::McBufWritable::write_into(#param_ident, buf)?; + azalea_buf::AzaleaWrite::azalea_write(#param_ident, buf)?; }); } } @@ -161,7 +161,7 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok }); match_arms_without_id.extend(quote! { Self::#variant_name(data) => { - azalea_buf::McBufWritable::write_into(data, buf)?; + azalea_buf::AzaleaWrite::azalea_write(data, buf)?; } }); } @@ -169,8 +169,8 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok } if is_data_enum { quote! { - impl azalea_buf::McBufWritable for #ident { - fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { + impl azalea_buf::AzaleaWrite for #ident { + fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { match self { #match_arms } @@ -189,14 +189,14 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok } else { // optimization: if it doesn't have data we can just do `as u32` quote! { - impl azalea_buf::McBufWritable for #ident { - fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { - azalea_buf::McBufVarWritable::var_write_into(&(*self as u32), buf) + impl azalea_buf::AzaleaWrite for #ident { + fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { + azalea_buf::AzaleaWriteVar::azalea_write_var(&(*self as u32), buf) } } } } } - _ => panic!("#[derive(McBuf)] can only be used on structs"), + _ => panic!("#[derive(AzBuf)] can only be used on structs"), } } |
