aboutsummaryrefslogtreecommitdiff
path: root/azalea-buf
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-buf')
-rw-r--r--azalea-buf/azalea-buf-macros/src/lib.rs26
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`