diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-11-06 14:05:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-06 14:05:01 -0600 |
| commit | d112856ff6353592a50658b0ddd316f54dd97b87 (patch) | |
| tree | 6c408ecab96012e3bffaf15843fad5b9b6817796 /azalea-buf | |
| parent | 9bc517511663193f3166320db4cb5ca02701b039 (diff) | |
| download | azalea-drasl-d112856ff6353592a50658b0ddd316f54dd97b87.tar.xz | |
Entity metadata (#37)
* add example generated metadata.rs
* metadata.rs codegen
* add the files
* add comment to top of metadata.rs
* avoid clone
* metadata
* defaults
* defaults
* fix metadata readers and writers
* fix bad bitmasks and ignore some clippy warnings in generated code
* add set_index function to entity metadatas
* applying metadata
Diffstat (limited to 'azalea-buf')
| -rw-r--r-- | azalea-buf/azalea-buf-macros/src/lib.rs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/azalea-buf/azalea-buf-macros/src/lib.rs b/azalea-buf/azalea-buf-macros/src/lib.rs index 801b7b71..a247db0f 100644 --- a/azalea-buf/azalea-buf-macros/src/lib.rs +++ b/azalea-buf/azalea-buf-macros/src/lib.rs @@ -105,9 +105,14 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt quote! { impl azalea_buf::McBufReadable for #ident { - fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> - { + 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 #ident { + pub fn read_from_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 @@ -170,6 +175,7 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt // remember whether it's a data variant so we can do an optimization later let mut is_data_enum = false; let mut match_arms = quote!(); + let mut match_arms_without_id = quote!(); let mut variant_discrim: u32 = 0; let mut first = true; for variant in variants { @@ -208,6 +214,9 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt azalea_buf::McBufVarWritable::var_write_into(&#variant_discrim, buf)?; } }); + match_arms_without_id.extend(quote! { + Self::#variant_name => {} + }); } syn::Fields::Unnamed(_) => { is_data_enum = true; @@ -218,6 +227,11 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt azalea_buf::McBufWritable::write_into(data, buf)?; } }); + match_arms_without_id.extend(quote! { + Self::#variant_name(data) => { + azalea_buf::McBufWritable::write_into(data, buf)?; + } + }); } } } @@ -231,6 +245,14 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt Ok(()) } } + impl #ident { + pub fn write_without_id(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { + match self { + #match_arms_without_id + } + Ok(()) + } + } } } else { // optimization: if it doesn't have data we can just do `as u32` |
