diff --git a/azalea-buf/Cargo.toml b/azalea-buf/Cargo.toml index 79f9d64d..7497f947 100644 --- a/azalea-buf/Cargo.toml +++ b/azalea-buf/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "azalea-buf" +name = "azalea_buf" version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/azalea-buf/buf-macros/src/lib.rs b/azalea-buf/buf-macros/src/lib.rs index 3afeaeed..4cc397a2 100644 --- a/azalea-buf/buf-macros/src/lib.rs +++ b/azalea-buf/buf-macros/src/lib.rs @@ -21,11 +21,11 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt syn::Type::Path(_) => { if f.attrs.iter().any(|a| a.path.is_ident("var")) { quote! { - let #field_name = crate::McBufVarReadable::var_read_into(buf)?; + let #field_name = azalea_buf::McBufVarReadable::var_read_into(buf)?; } } else { quote! { - let #field_name = crate::McBufReadable::read_into(buf)?; + let #field_name = azalea_buf::McBufReadable::read_into(buf)?; } } } @@ -40,7 +40,7 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt let read_field_names = named.iter().map(|f| &f.ident).collect::>(); quote! { - impl crate::McBufReadable for #ident { + impl azalea_buf::McBufReadable for #ident { fn read_into(buf: &mut impl std::io::Read) -> Result { #(#read_fields)* Ok(#ident { @@ -75,10 +75,10 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt } quote! { - impl crate::McBufReadable for #ident { + impl azalea_buf::McBufReadable for #ident { fn read_into(buf: &mut impl std::io::Read) -> Result { - let id = crate::McBufVarReadable::var_read_into(buf)?; + let id = azalea_buf::McBufVarReadable::var_read_into(buf)?; match id { #match_contents _ => Err(format!("Unknown enum variant {}", id)), @@ -110,11 +110,11 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt syn::Type::Path(_) => { if f.attrs.iter().any(|attr| attr.path.is_ident("var")) { quote! { - crate::McBufVarWritable::var_write_into(&self.#field_name, buf)?; + azalea_buf::McBufVarWritable::var_write_into(&self.#field_name, buf)?; } } else { quote! { - crate::McBufWritable::write_into(&self.#field_name, buf)?; + azalea_buf::McBufWritable::write_into(&self.#field_name, buf)?; } } } @@ -128,7 +128,7 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt .collect::>(); quote! { - impl crate::McBufWritable for #ident { + impl azalea_buf::McBufWritable for #ident { fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { #(#write_fields)* Ok(()) @@ -138,9 +138,9 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt } syn::Data::Enum(syn::DataEnum { .. }) => { quote! { - impl crate::McBufWritable for #ident { + impl azalea_buf::McBufWritable for #ident { fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { - crate::Writable::write_varint(buf, *self as i32) + azalea_buf::Writable::write_varint(buf, *self as i32) } } } diff --git a/azalea-buf/src/lib.rs b/azalea-buf/src/lib.rs index 2ba17ac2..bd9f43be 100644 --- a/azalea-buf/src/lib.rs +++ b/azalea-buf/src/lib.rs @@ -7,6 +7,7 @@ mod definitions; mod read; mod write; +pub use buf_macros::*; pub use definitions::*; pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable}; pub use write::{McBufVarWritable, McBufWritable, Writable}; @@ -18,7 +19,7 @@ const MAX_STRING_LENGTH: u16 = 32767; #[cfg(test)] mod tests { use super::*; - use std::{collections::HashMap, io::Cursor}; + use std::io::Cursor; #[test] fn test_write_varint() { diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 872a0a5c..d307bcbc 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -1,3 +1,6 @@ +use std::io::{Read, Write}; + +use azalea_buf::{McBufReadable, McBufWritable}; use serde::{de, Deserialize, Deserializer}; use crate::{ @@ -267,7 +270,7 @@ impl<'de> Deserialize<'de> for Component { impl McBufReadable for Component { fn read_into(buf: &mut impl Read) -> Result { - let string = buf.read_utf()?; + let string = String::read_into(buf)?; let json: serde_json::Value = serde_json::from_str(string.as_str()) .map_err(|_| "Component isn't valid JSON".to_string())?; let component = Component::deserialize(json).map_err(|e| e.to_string())?; diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 32517e0d..a4d02907 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,3 +1,5 @@ +pub use azalea_buf::McBuf; + /// Only works for up to 8 blocks #[derive(Clone, Debug, McBuf)] pub struct PositionDelta { diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs index fc815a0b..5ca8ac8e 100644 --- a/azalea-core/src/particle/mod.rs +++ b/azalea-core/src/particle/mod.rs @@ -1,3 +1,5 @@ +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; #[derive(Debug, Clone, McBuf)] pub struct Particle { diff --git a/azalea-crypto/src/signing.rs b/azalea-crypto/src/signing.rs index a5280a18..4fc07d30 100644 --- a/azalea-crypto/src/signing.rs +++ b/azalea-crypto/src/signing.rs @@ -1,3 +1,6 @@ +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Debug, Clone)] pub struct SaltSignaturePair { pub salt: u64, diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs index 73cd613e..1c011839 100755 --- a/azalea-nbt/src/decode.rs +++ b/azalea-nbt/src/decode.rs @@ -1,5 +1,6 @@ use crate::Error; use crate::Tag; +use azalea_buf::McBufReadable; use byteorder::{ReadBytesExt, BE}; use flate2::read::{GzDecoder, ZlibDecoder}; use std::collections::HashMap; @@ -139,7 +140,7 @@ impl Tag { impl McBufReadable for Tag { fn read_into(buf: &mut impl Read) -> Result { - match Tag::read(self) { + match Tag::read(buf) { Ok(r) => Ok(r), // Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()).unwrap(), diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 17d20270..3763ffd1 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -1,5 +1,6 @@ use crate::Error; use crate::Tag; +use azalea_buf::McBufWritable; use byteorder::{WriteBytesExt, BE}; use flate2::write::{GzEncoder, ZlibEncoder}; use std::collections::HashMap; diff --git a/azalea-nbt/src/lib.rs b/azalea-nbt/src/lib.rs index 8cca1f2b..cb2eb28e 100755 --- a/azalea-nbt/src/lib.rs +++ b/azalea-nbt/src/lib.rs @@ -9,23 +9,24 @@ pub use tag::Tag; #[cfg(test)] mod tests { use super::*; + use azalea_buf::{McBufReadable, McBufWritable}; use std::{collections::HashMap, io::Cursor}; #[test] fn mcbuf_nbt() { let mut buf = Vec::new(); - buf.write_nbt(&Tag::Compound(HashMap::from_iter(vec![( + let tag = Tag::Compound(HashMap::from_iter(vec![( "hello world".to_string(), Tag::Compound(HashMap::from_iter(vec![( "name".to_string(), Tag::String("Bananrama".to_string()), )])), - )]))) - .unwrap(); + )])); + tag.write_into(&mut buf).unwrap(); let mut buf = Cursor::new(buf); - let result = buf.read_nbt().unwrap(); + let result = Tag::read_into(&mut buf).unwrap(); assert_eq!( result, Tag::Compound(HashMap::from_iter(vec![(