1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

rename read_from and write_into to azalea_read and azalea_write

This commit is contained in:
mat 2024-11-27 01:41:02 +00:00
parent e41d35c1c1
commit c69ab5cc9b
63 changed files with 781 additions and 776 deletions

View file

@ -81,16 +81,16 @@ impl TryFrom<u32> for BlockState {
}
impl McBufReadable for BlockState {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let state_id = u32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let state_id = u32::azalea_read_var(buf)?;
Self::try_from(state_id).map_err(|_| BufReadError::UnexpectedEnumVariant {
id: state_id as i32,
})
}
}
impl McBufWritable for BlockState {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::var_write_into(&self.id, buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::azalea_write_var(&self.id, buf)
}
}

View file

@ -138,12 +138,12 @@ impl PartialOrd for SuggestionValue {
#[cfg(feature = "azalea-buf")]
impl McBufWritable for Suggestion {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.value.to_string().write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.value.to_string().azalea_write(buf)?;
self.tooltip
.clone()
.map(FormattedText::from)
.write_into(buf)?;
.azalea_write(buf)?;
Ok(())
}
}

View file

@ -80,20 +80,20 @@ impl Suggestions {
#[cfg(feature = "azalea-buf")]
impl McBufReadable for Suggestions {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
#[derive(McBuf)]
struct StandaloneSuggestion {
pub text: String,
pub tooltip: Option<FormattedText>,
}
let start = u32::var_read_from(buf)? as usize;
let length = u32::var_read_from(buf)? as usize;
let start = u32::azalea_read_var(buf)? as usize;
let length = u32::azalea_read_var(buf)? as usize;
let range = StringRange::between(start, start + length);
// the range of a Suggestion depends on the Suggestions containing it,
// so we can't just `impl McBufReadable for Suggestion`
let mut suggestions = Vec::<StandaloneSuggestion>::read_from(buf)?
let mut suggestions = Vec::<StandaloneSuggestion>::azalea_read(buf)?
.into_iter()
.map(|s| Suggestion {
value: SuggestionValue::Text(s.text),
@ -109,10 +109,10 @@ impl McBufReadable for Suggestions {
#[cfg(feature = "azalea-buf")]
impl McBufWritable for Suggestions {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.range.start() as u32).var_write_into(buf)?;
(self.range.length() as u32).var_write_into(buf)?;
self.suggestions.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.range.start() as u32).azalea_write_var(buf)?;
(self.range.length() as u32).azalea_write_var(buf)?;
self.suggestions.azalea_write(buf)?;
Ok(())
}
}

View file

@ -15,11 +15,11 @@ fn read_named_fields(
syn::Type::Path(_) | syn::Type::Array(_) => {
if f.attrs.iter().any(|a| a.path().is_ident("var")) {
quote! {
let #field_name = azalea_buf::McBufVarReadable::var_read_from(buf)?;
let #field_name = azalea_buf::McBufVarReadable::azalea_read_var(buf)?;
}
} else {
quote! {
let #field_name = azalea_buf::McBufReadable::read_from(buf)?;
let #field_name = azalea_buf::McBufReadable::azalea_read(buf)?;
}
}
}
@ -44,7 +44,7 @@ pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::Tok
quote! {
impl azalea_buf::McBufReadable for #ident {
fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
#(#read_fields)*
Ok(Self {
#(#read_field_names: #read_field_names),*
@ -56,7 +56,7 @@ 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> {
fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
Ok(Self)
}
}
@ -110,11 +110,11 @@ pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::Tok
for f in &fields.unnamed {
if f.attrs.iter().any(|attr| attr.path().is_ident("var")) {
reader_code.extend(quote! {
Self::#variant_name(azalea_buf::McBufVarReadable::var_read_from(buf)?),
Self::#variant_name(azalea_buf::McBufVarReadable::azalea_read_var(buf)?),
});
} else {
reader_code.extend(quote! {
Self::#variant_name(azalea_buf::McBufReadable::read_from(buf)?),
Self::#variant_name(azalea_buf::McBufReadable::azalea_read(buf)?),
});
}
}
@ -140,14 +140,14 @@ pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::Tok
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)
fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let id = azalea_buf::McBufVarReadable::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

View file

@ -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::McBufVarWritable::azalea_write_var(#ident_dot_field, buf)?;
}
} else {
quote! {
azalea_buf::McBufWritable::write_into(#ident_dot_field, buf)?;
azalea_buf::McBufWritable::azalea_write(#ident_dot_field, buf)?;
}
}
}
@ -46,7 +46,7 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok
quote! {
impl azalea_buf::McBufWritable for #ident {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
#write_fields
Ok(())
}
@ -56,7 +56,7 @@ 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> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
Ok(())
}
}
@ -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::McBufVarWritable::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::McBufVarWritable::azalea_write_var(#param_ident, buf)?;
});
} else {
writers_code.extend(quote! {
azalea_buf::McBufWritable::write_into(#param_ident, buf)?;
azalea_buf::McBufWritable::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::McBufWritable::azalea_write(data, buf)?;
}
});
}
@ -170,7 +170,7 @@ 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> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
match self {
#match_arms
}
@ -190,8 +190,8 @@ pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::Tok
// 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)
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
azalea_buf::McBufVarWritable::azalea_write_var(&(*self as u32), buf)
}
}
}

View file

@ -27,110 +27,113 @@ mod tests {
#[test]
fn test_write_varint() {
let mut buf = Vec::new();
0.var_write_into(&mut buf).unwrap();
0.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![0]);
let mut buf = Vec::new();
1.var_write_into(&mut buf).unwrap();
1.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![1]);
let mut buf = Vec::new();
2.var_write_into(&mut buf).unwrap();
2.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![2]);
let mut buf = Vec::new();
127.var_write_into(&mut buf).unwrap();
127.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![127]);
let mut buf = Vec::new();
128.var_write_into(&mut buf).unwrap();
128.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![128, 1]);
let mut buf = Vec::new();
255.var_write_into(&mut buf).unwrap();
255.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![255, 1]);
let mut buf = Vec::new();
25565.var_write_into(&mut buf).unwrap();
25565.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![221, 199, 1]);
let mut buf = Vec::new();
2097151.var_write_into(&mut buf).unwrap();
2097151.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![255, 255, 127]);
let mut buf = Vec::new();
2147483647.var_write_into(&mut buf).unwrap();
2147483647.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![255, 255, 255, 255, 7]);
let mut buf = Vec::new();
(-1).var_write_into(&mut buf).unwrap();
(-1).azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![255, 255, 255, 255, 15]);
let mut buf = Vec::new();
(-2147483648).var_write_into(&mut buf).unwrap();
(-2147483648).azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![128, 128, 128, 128, 8]);
}
#[test]
fn test_read_varint() {
// let buf = &mut &vec![0][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 0);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 0);
let buf = vec![0];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 0);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 0);
// let buf = &mut &vec![1][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 1);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 1);
let buf = vec![1];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 1);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 1);
// let buf = &mut &vec![2][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 2);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 2);
let buf = vec![2];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 2);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 2);
// let buf = &mut &vec![127][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 127);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 127);
let buf = vec![127];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 127);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 127);
// let buf = &mut &vec![128, 1][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 128);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 128);
let buf = vec![128, 1];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 128);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 128);
// let buf = &mut &vec![255, 1][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 255);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 255);
let buf = vec![255, 1];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 255);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 255);
// let buf = &mut &vec![221, 199, 1][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 25565);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 25565);
let buf = vec![221, 199, 1];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 25565);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 25565);
// let buf = &mut &vec![255, 255, 127][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 2097151);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 2097151);
let buf = vec![255, 255, 127];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 2097151);
assert_eq!(
i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(),
2097151
);
// let buf = &mut &vec![255, 255, 255, 255, 7][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), 2147483647);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), 2147483647);
let buf = vec![255, 255, 255, 255, 7];
assert_eq!(
i32::var_read_from(&mut Cursor::new(&buf)).unwrap(),
i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(),
2147483647
);
// let buf = &mut &vec![255, 255, 255, 255, 15][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), -1);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), -1);
let buf = vec![255, 255, 255, 255, 15];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), -1);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), -1);
// let buf = &mut &vec![128, 128, 128, 128, 8][..];
// assert_eq!(i32::var_read_from(buf).unwrap(), -2147483648);
// assert_eq!(i32::azalea_read_var(buf).unwrap(), -2147483648);
let buf = vec![128, 128, 128, 128, 8];
assert_eq!(
i32::var_read_from(&mut Cursor::new(&buf)).unwrap(),
i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(),
-2147483648
);
}
@ -138,21 +141,21 @@ mod tests {
#[test]
fn test_read_varint_longer() {
let buf = vec![138, 56, 0, 135, 56, 123];
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 7178);
assert_eq!(i32::azalea_read_var(&mut Cursor::new(&buf)).unwrap(), 7178);
}
#[test]
fn test_write_varlong() {
let mut buf = Vec::new();
0u64.var_write_into(&mut buf).unwrap();
0u64.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![0]);
let mut buf = Vec::new();
1u64.var_write_into(&mut buf).unwrap();
1u64.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![1]);
let mut buf = Vec::new();
9223372036854775807u64.var_write_into(&mut buf).unwrap();
9223372036854775807u64.azalea_write_var(&mut buf).unwrap();
assert_eq!(buf, vec![255, 255, 255, 255, 255, 255, 255, 255, 127]);
}
@ -161,20 +164,20 @@ mod tests {
let original_vec = vec!["a".to_string(), "bc".to_string(), "def".to_string()];
let mut buf = Vec::new();
original_vec.write_into(&mut buf).unwrap();
original_vec.azalea_write(&mut buf).unwrap();
dbg!(&buf);
let result = Vec::<String>::read_from(&mut Cursor::new(&buf)).unwrap();
let result = Vec::<String>::azalea_read(&mut Cursor::new(&buf)).unwrap();
assert_eq!(result, original_vec);
}
#[test]
fn test_int_id_list() {
let mut buf = Vec::new();
vec![1, 2, 3].var_write_into(&mut buf).unwrap();
vec![1, 2, 3].azalea_write_var(&mut buf).unwrap();
let result = Vec::<i32>::var_read_from(&mut Cursor::new(&buf)).unwrap();
let result = Vec::<i32>::azalea_read_var(&mut Cursor::new(&buf)).unwrap();
assert_eq!(result, vec![1, 2, 3]);
}
@ -186,9 +189,9 @@ mod tests {
("def".to_string(), 456),
]);
let mut buf = Vec::new();
original_map.var_write_into(&mut buf).unwrap();
original_map.azalea_write_var(&mut buf).unwrap();
let result = HashMap::<String, i32>::var_read_from(&mut Cursor::new(&buf)).unwrap();
let result = HashMap::<String, i32>::azalea_read_var(&mut Cursor::new(&buf)).unwrap();
assert_eq!(result, original_map);
}
@ -196,8 +199,8 @@ mod tests {
#[test]
fn test_long() {
let mut buf: Vec<u8> = Vec::new();
123456u64.write_into(&mut buf).unwrap();
123456u64.azalea_write(&mut buf).unwrap();
assert_eq!(u64::read_from(&mut Cursor::new(&buf)).unwrap(), 123456);
assert_eq!(u64::azalea_read(&mut Cursor::new(&buf)).unwrap(), 123456);
}
}

View file

@ -81,7 +81,7 @@ fn read_bytes<'a>(buf: &'a mut Cursor<&[u8]>, length: usize) -> Result<&'a [u8],
}
fn read_utf_with_len(buf: &mut Cursor<&[u8]>, max_length: u32) -> Result<String, BufReadError> {
let length = u32::var_read_from(buf)?;
let length = u32::azalea_read_var(buf)?;
// i don't know why it's multiplied by 4 but it's like that in mojang's code so
if length > max_length * 4 {
return Err(BufReadError::StringLengthTooLong {
@ -109,18 +109,18 @@ pub trait McBufReadable
where
Self: Sized,
{
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError>;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError>;
}
pub trait McBufVarReadable
where
Self: Sized,
{
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError>;
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError>;
}
impl McBufReadable for i32 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(buf.read_i32::<BE>()?)
}
}
@ -128,7 +128,7 @@ impl McBufReadable for i32 {
impl McBufVarReadable for i32 {
// fast varints modified from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67
/// Read a single varint from the reader and return the value
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut buffer = [0];
let mut ans = 0;
for i in 0..5 {
@ -144,7 +144,7 @@ impl McBufVarReadable for i32 {
impl McBufVarReadable for i64 {
// fast varints modified from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L54
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut buffer = [0];
let mut ans = 0;
for i in 0..10 {
@ -159,13 +159,13 @@ impl McBufVarReadable for i64 {
}
}
impl McBufVarReadable for u64 {
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
i64::var_read_from(buf).map(|i| i as u64)
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
i64::azalea_read_var(buf).map(|i| i as u64)
}
}
impl McBufReadable for UnsizedByteArray {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
// read to end of the buffer
let data = buf.get_ref()[buf.position() as usize..].to_vec();
buf.set_position((buf.position()) + data.len() as u64);
@ -174,23 +174,23 @@ impl McBufReadable for UnsizedByteArray {
}
impl<T: McBufReadable + Send> McBufReadable for Vec<T> {
default fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = u32::var_read_from(buf)? as usize;
default fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = u32::azalea_read_var(buf)? as usize;
// we limit the capacity to not get exploited into allocating a bunch
let mut contents = Vec::with_capacity(usize::min(length, 65536));
for _ in 0..length {
contents.push(T::read_from(buf)?);
contents.push(T::azalea_read(buf)?);
}
Ok(contents)
}
}
impl<K: McBufReadable + Send + Eq + Hash, V: McBufReadable + Send> McBufReadable for HashMap<K, V> {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::var_read_from(buf)? as usize;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::azalea_read_var(buf)? as usize;
let mut contents = HashMap::with_capacity(usize::min(length, 65536));
for _ in 0..length {
contents.insert(K::read_from(buf)?, V::read_from(buf)?);
contents.insert(K::azalea_read(buf)?, V::azalea_read(buf)?);
}
Ok(contents)
}
@ -199,85 +199,85 @@ impl<K: McBufReadable + Send + Eq + Hash, V: McBufReadable + Send> McBufReadable
impl<K: McBufReadable + Send + Eq + Hash, V: McBufVarReadable + Send> McBufVarReadable
for HashMap<K, V>
{
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::var_read_from(buf)? as usize;
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::azalea_read_var(buf)? as usize;
let mut contents = HashMap::with_capacity(usize::min(length, 65536));
for _ in 0..length {
contents.insert(K::read_from(buf)?, V::var_read_from(buf)?);
contents.insert(K::azalea_read(buf)?, V::azalea_read_var(buf)?);
}
Ok(contents)
}
}
impl McBufReadable for Vec<u8> {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::var_read_from(buf)? as usize;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::azalea_read_var(buf)? as usize;
read_bytes(buf, length).map(|b| b.to_vec())
}
}
impl McBufReadable for String {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
read_utf_with_len(buf, MAX_STRING_LENGTH.into())
}
}
impl McBufReadable for u32 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(i32::read_from(buf)? as u32)
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(i32::azalea_read(buf)? as u32)
}
}
impl McBufVarReadable for u32 {
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(i32::var_read_from(buf)? as u32)
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(i32::azalea_read_var(buf)? as u32)
}
}
impl McBufReadable for u16 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
i16::read_from(buf).map(|i| i as u16)
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
i16::azalea_read(buf).map(|i| i as u16)
}
}
impl McBufReadable for i16 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(buf.read_i16::<BE>()?)
}
}
impl McBufVarReadable for u16 {
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(i32::var_read_from(buf)? as u16)
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(i32::azalea_read_var(buf)? as u16)
}
}
impl<T: McBufVarReadable> McBufVarReadable for Vec<T> {
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::var_read_from(buf)? as usize;
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = i32::azalea_read_var(buf)? as usize;
let mut contents = Vec::with_capacity(usize::min(length, 65536));
for _ in 0..length {
contents.push(T::var_read_from(buf)?);
contents.push(T::azalea_read_var(buf)?);
}
Ok(contents)
}
}
impl McBufReadable for i64 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(buf.read_i64::<BE>()?)
}
}
impl McBufReadable for u64 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
i64::read_from(buf).map(|i| i as u64)
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
i64::azalea_read(buf).map(|i| i as u64)
}
}
impl McBufReadable for bool {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let byte = u8::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let byte = u8::azalea_read(buf)?;
if byte > 1 {
warn!("Boolean value was not 0 or 1, but {}", byte);
}
@ -286,34 +286,34 @@ impl McBufReadable for bool {
}
impl McBufReadable for u8 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(buf.read_u8()?)
}
}
impl McBufReadable for i8 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
u8::read_from(buf).map(|i| i as i8)
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
u8::azalea_read(buf).map(|i| i as i8)
}
}
impl McBufReadable for f32 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(buf.read_f32::<BE>()?)
}
}
impl McBufReadable for f64 {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(buf.read_f64::<BE>()?)
}
}
impl<T: McBufReadable> McBufReadable for Option<T> {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let present = bool::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let present = bool::azalea_read(buf)?;
Ok(if present {
Some(T::read_from(buf)?)
Some(T::azalea_read(buf)?)
} else {
None
})
@ -321,10 +321,10 @@ impl<T: McBufReadable> McBufReadable for Option<T> {
}
impl<T: McBufVarReadable> McBufVarReadable for Option<T> {
fn var_read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let present = bool::read_from(buf)?;
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let present = bool::azalea_read(buf)?;
Ok(if present {
Some(T::var_read_from(buf)?)
Some(T::azalea_read_var(buf)?)
} else {
None
})
@ -333,10 +333,10 @@ impl<T: McBufVarReadable> McBufVarReadable for Option<T> {
// [String; 4]
impl<T: McBufReadable, const N: usize> McBufReadable for [T; N] {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut contents = Vec::with_capacity(N);
for _ in 0..N {
contents.push(T::read_from(buf)?);
contents.push(T::azalea_read(buf)?);
}
contents.try_into().map_err(|_| {
unreachable!("Panic is not possible since the Vec is the same size as the array")
@ -345,13 +345,13 @@ impl<T: McBufReadable, const N: usize> McBufReadable for [T; N] {
}
impl McBufReadable for simdnbt::owned::NbtTag {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(simdnbt::owned::read_tag(buf).map_err(simdnbt::Error::from)?)
}
}
impl McBufReadable for simdnbt::owned::NbtCompound {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
match simdnbt::owned::read_tag(buf).map_err(simdnbt::Error::from)? {
simdnbt::owned::NbtTag::Compound(compound) => Ok(compound),
_ => Err(BufReadError::Custom("Expected compound tag".to_string())),
@ -360,7 +360,7 @@ impl McBufReadable for simdnbt::owned::NbtCompound {
}
impl McBufReadable for simdnbt::owned::Nbt {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(simdnbt::owned::read_unnamed(buf)?)
}
}
@ -369,7 +369,7 @@ impl<T> McBufReadable for Box<T>
where
T: McBufReadable,
{
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(Box::new(T::read_from(buf)?))
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(Box::new(T::azalea_read(buf)?))
}
}

View file

@ -35,23 +35,23 @@ impl SerializableUuid for Uuid {
}
impl McBufReadable for Uuid {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(Uuid::from_int_array([
u32::read_from(buf)?,
u32::read_from(buf)?,
u32::read_from(buf)?,
u32::read_from(buf)?,
u32::azalea_read(buf)?,
u32::azalea_read(buf)?,
u32::azalea_read(buf)?,
u32::azalea_read(buf)?,
]))
}
}
impl McBufWritable for Uuid {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let [a, b, c, d] = self.to_int_array();
a.write_into(buf)?;
b.write_into(buf)?;
c.write_into(buf)?;
d.write_into(buf)?;
a.azalea_write(buf)?;
b.azalea_write(buf)?;
c.azalea_write(buf)?;
d.azalea_write(buf)?;
Ok(())
}
}
@ -79,10 +79,10 @@ mod tests {
fn read_write() {
let u = Uuid::parse_str("6536bfed-8695-48fd-83a1-ecd24cf2a0fd").unwrap();
let mut buf = Vec::new();
u.write_into(&mut buf).unwrap();
u.azalea_write(&mut buf).unwrap();
println!("{buf:?}");
assert_eq!(buf.len(), 16);
let u2 = Uuid::read_from(&mut Cursor::new(&buf)).unwrap();
let u2 = Uuid::azalea_read(&mut Cursor::new(&buf)).unwrap();
assert_eq!(u, u2);
}
}

View file

@ -16,26 +16,26 @@ fn write_utf_with_len(
len
);
}
string.as_bytes().to_vec().write_into(buf)?;
string.as_bytes().to_vec().azalea_write(buf)?;
Ok(())
}
pub trait McBufWritable {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error>;
}
pub trait McBufVarWritable {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>;
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error>;
}
impl McBufWritable for i32 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
WriteBytesExt::write_i32::<BigEndian>(buf, *self)
}
}
impl McBufVarWritable for i32 {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut buffer = [0];
let mut value = *self;
if value == 0 {
@ -54,33 +54,33 @@ impl McBufVarWritable for i32 {
}
impl McBufWritable for UnsizedByteArray {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
buf.write_all(self)
}
}
impl<T: McBufWritable> McBufWritable for Vec<T> {
default fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self[..].write_into(buf)
default fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self[..].azalea_write(buf)
}
}
impl<T: McBufWritable> McBufWritable for [T] {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).var_write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).azalea_write_var(buf)?;
for item in self {
T::write_into(item, buf)?;
T::azalea_write(item, buf)?;
}
Ok(())
}
}
impl<K: McBufWritable, V: McBufWritable> McBufWritable for HashMap<K, V> {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::var_write_into(&(self.len() as u32), buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::azalea_write_var(&(self.len() as u32), buf)?;
for (key, value) in self {
key.write_into(buf)?;
value.write_into(buf)?;
key.azalea_write(buf)?;
value.azalea_write(buf)?;
}
Ok(())
@ -88,11 +88,11 @@ impl<K: McBufWritable, V: McBufWritable> McBufWritable for HashMap<K, V> {
}
impl<K: McBufWritable, V: McBufVarWritable> McBufVarWritable for HashMap<K, V> {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::var_write_into(&(self.len() as u32), buf)?;
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::azalea_write_var(&(self.len() as u32), buf)?;
for (key, value) in self {
key.write_into(buf)?;
value.var_write_into(buf)?;
key.azalea_write(buf)?;
value.azalea_write_var(buf)?;
}
Ok(())
@ -100,38 +100,38 @@ impl<K: McBufWritable, V: McBufVarWritable> McBufVarWritable for HashMap<K, V> {
}
impl McBufWritable for Vec<u8> {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).var_write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).azalea_write_var(buf)?;
buf.write_all(self)
}
}
impl McBufWritable for String {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
write_utf_with_len(buf, self, MAX_STRING_LENGTH.into())
}
}
impl McBufWritable for &str {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
write_utf_with_len(buf, self, MAX_STRING_LENGTH.into())
}
}
impl McBufWritable for u32 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::write_into(&(*self as i32), buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::azalea_write(&(*self as i32), buf)
}
}
impl McBufVarWritable for u32 {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::var_write_into(&(*self as i32), buf)
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::azalea_write_var(&(*self as i32), buf)
}
}
impl McBufVarWritable for i64 {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut buffer = [0];
let mut value = *self;
if value == 0 {
@ -150,101 +150,101 @@ impl McBufVarWritable for i64 {
}
impl McBufVarWritable for u64 {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i64::var_write_into(&(*self as i64), buf)
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i64::azalea_write_var(&(*self as i64), buf)
}
}
impl McBufWritable for u16 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i16::write_into(&(*self as i16), buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i16::azalea_write(&(*self as i16), buf)
}
}
impl McBufVarWritable for u16 {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::var_write_into(&(*self as i32), buf)
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::azalea_write_var(&(*self as i32), buf)
}
}
impl<T: McBufVarWritable> McBufVarWritable for Vec<T> {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::var_write_into(&(self.len() as u32), buf)?;
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u32::azalea_write_var(&(self.len() as u32), buf)?;
for i in self {
i.var_write_into(buf)?;
i.azalea_write_var(buf)?;
}
Ok(())
}
}
impl McBufWritable for u8 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
WriteBytesExt::write_u8(buf, *self)
}
}
impl McBufWritable for i16 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
WriteBytesExt::write_i16::<BigEndian>(buf, *self)
}
}
impl McBufWritable for i64 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
WriteBytesExt::write_i64::<BigEndian>(buf, *self)
}
}
impl McBufWritable for u64 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i64::write_into(&(*self as i64), buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i64::azalea_write(&(*self as i64), buf)
}
}
impl McBufWritable for bool {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let byte = u8::from(*self);
byte.write_into(buf)
byte.azalea_write(buf)
}
}
impl McBufWritable for i8 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(*self as u8).write_into(buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(*self as u8).azalea_write(buf)
}
}
impl McBufWritable for f32 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
WriteBytesExt::write_f32::<BigEndian>(buf, *self)
}
}
impl McBufWritable for f64 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
WriteBytesExt::write_f64::<BigEndian>(buf, *self)
}
}
impl<T: McBufWritable> McBufWritable for Option<T> {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
if let Some(s) = self {
true.write_into(buf)?;
s.write_into(buf)?;
true.azalea_write(buf)?;
s.azalea_write(buf)?;
} else {
false.write_into(buf)?;
false.azalea_write(buf)?;
};
Ok(())
}
}
impl<T: McBufVarWritable> McBufVarWritable for Option<T> {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write_var(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
if let Some(s) = self {
true.write_into(buf)?;
s.var_write_into(buf)?;
true.azalea_write(buf)?;
s.azalea_write_var(buf)?;
} else {
false.write_into(buf)?;
false.azalea_write(buf)?;
};
Ok(())
}
@ -252,16 +252,16 @@ impl<T: McBufVarWritable> McBufVarWritable for Option<T> {
// [T; N]
impl<T: McBufWritable, const N: usize> McBufWritable for [T; N] {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
for i in self {
i.write_into(buf)?;
i.azalea_write(buf)?;
}
Ok(())
}
}
impl McBufWritable for simdnbt::owned::NbtTag {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut data = Vec::new();
self.write(&mut data);
buf.write_all(&data)
@ -269,7 +269,7 @@ impl McBufWritable for simdnbt::owned::NbtTag {
}
impl McBufWritable for simdnbt::owned::NbtCompound {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut data = Vec::new();
simdnbt::owned::NbtTag::Compound(self.clone()).write(&mut data);
buf.write_all(&data)
@ -277,7 +277,7 @@ impl McBufWritable for simdnbt::owned::NbtCompound {
}
impl McBufWritable for simdnbt::owned::Nbt {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut data = Vec::new();
self.write_unnamed(&mut data);
buf.write_all(&data)
@ -288,7 +288,7 @@ impl<T> McBufWritable for Box<T>
where
T: McBufWritable,
{
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
T::write_into(&**self, buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
T::azalea_write(&**self, buf)
}
}

View file

@ -457,7 +457,7 @@ impl From<&simdnbt::Mutf8Str> for FormattedText {
#[cfg(feature = "azalea-buf")]
#[cfg(feature = "simdnbt")]
impl McBufReadable for FormattedText {
fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, BufReadError> {
let nbt = simdnbt::borrow::read_optional_tag(buf)?;
if let Some(nbt) = nbt {
FormattedText::from_nbt_tag(nbt.as_tag()).ok_or(BufReadError::Custom(
@ -472,7 +472,7 @@ impl McBufReadable for FormattedText {
#[cfg(feature = "azalea-buf")]
#[cfg(feature = "simdnbt")]
impl McBufWritable for FormattedText {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut out = Vec::new();
simdnbt::owned::BaseNbt::write_unnamed(&(self.clone().to_compound().into()), &mut out);
buf.write_all(&out)

View file

@ -19,15 +19,15 @@ pub enum NumberFormat {
#[cfg(feature = "azalea-buf")]
impl McBufReadable for NumberFormat {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let kind = NumberFormatKind::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let kind = NumberFormatKind::azalea_read(buf)?;
match kind {
NumberFormatKind::Blank => Ok(NumberFormat::Blank),
NumberFormatKind::Styled => Ok(NumberFormat::Styled {
style: simdnbt::owned::read(buf)?,
}),
NumberFormatKind::Fixed => Ok(NumberFormat::Fixed {
value: FormattedText::read_from(buf)?,
value: FormattedText::azalea_read(buf)?,
}),
}
}
@ -35,16 +35,16 @@ impl McBufReadable for NumberFormat {
#[cfg(feature = "azalea-buf")]
impl McBufWritable for NumberFormat {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
NumberFormat::Blank => NumberFormatKind::Blank.write_into(buf)?,
NumberFormat::Blank => NumberFormatKind::Blank.azalea_write(buf)?,
NumberFormat::Styled { style } => {
NumberFormatKind::Styled.write_into(buf)?;
style.write_into(buf)?;
NumberFormatKind::Styled.azalea_write(buf)?;
style.azalea_write(buf)?;
}
NumberFormat::Fixed { value } => {
NumberFormatKind::Fixed.write_into(buf)?;
value.write_into(buf)?;
NumberFormatKind::Fixed.azalea_write(buf)?;
value.azalea_write(buf)?;
}
}
Ok(())

View file

@ -30,7 +30,7 @@ fn handle_in_configuration_state(
for (entity, client_information) in query.iter() {
let mut brand_data = Vec::new();
// they don't have to know :)
"vanilla".write_into(&mut brand_data).unwrap();
"vanilla".azalea_write(&mut brand_data).unwrap();
send_packet_events.send(SendConfigurationEvent {
entity,
packet: ServerboundCustomPayload {

View file

@ -163,10 +163,10 @@ impl<const N: usize> McBufReadable for FixedBitSet<N>
where
[u8; N.div_ceil(8)]: Sized,
{
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut data = [0; N.div_ceil(8)];
for item in data.iter_mut().take(N.div_ceil(8)) {
*item = u8::read_from(buf)?;
*item = u8::azalea_read(buf)?;
}
Ok(FixedBitSet { data })
}
@ -175,9 +175,9 @@ impl<const N: usize> McBufWritable for FixedBitSet<N>
where
[u8; N.div_ceil(8)]: Sized,
{
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
for i in 0..N.div_ceil(8) {
self.data[i].write_into(buf)?;
self.data[i].azalea_write(buf)?;
}
Ok(())
}

View file

@ -67,14 +67,14 @@ impl Difficulty {
}
impl McBufReadable for Difficulty {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(Difficulty::by_id(u8::read_from(buf)?))
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(Difficulty::by_id(u8::azalea_read(buf)?))
}
}
impl McBufWritable for Difficulty {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u8::write_into(&self.id(), buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u8::azalea_write(&self.id(), buf)
}
}

View file

@ -94,8 +94,8 @@ impl GameMode {
}
impl McBufReadable for GameMode {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = u32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = u32::azalea_read_var(buf)?;
let id = id.try_into().unwrap_or_else(|_| {
debug!("Unknown game mode id {id}, defaulting to survival");
0
@ -108,8 +108,8 @@ impl McBufReadable for GameMode {
}
impl McBufWritable for GameMode {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u8::write_into(&self.to_id(), buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u8::azalea_write(&self.to_id(), buf)
}
}
@ -131,14 +131,14 @@ impl From<OptionalGameType> for Option<GameMode> {
}
impl McBufReadable for OptionalGameType {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = i8::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = i8::azalea_read(buf)?;
GameMode::from_optional_id(id).ok_or(BufReadError::UnexpectedEnumVariant { id: id as i32 })
}
}
impl McBufWritable for OptionalGameType {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
GameMode::to_optional_id(*self).write_into(buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
GameMode::to_optional_id(*self).azalea_write(buf)
}
}

View file

@ -316,14 +316,14 @@ impl From<u64> for ChunkPos {
}
}
impl McBufReadable for ChunkPos {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let long = u64::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let long = u64::azalea_read(buf)?;
Ok(ChunkPos::from(long))
}
}
impl McBufWritable for ChunkPos {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u64::from(*self).write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
u64::from(*self).azalea_write(buf)?;
Ok(())
}
}
@ -587,8 +587,8 @@ const Z_OFFSET: u64 = PACKED_Y_LENGTH;
const X_OFFSET: u64 = PACKED_Y_LENGTH + PACKED_Z_LENGTH;
impl McBufReadable for BlockPos {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let val = i64::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let val = i64::azalea_read(buf)?;
let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32;
let y = (val << (64 - PACKED_Y_LENGTH) >> (64 - PACKED_Y_LENGTH)) as i32;
let z = (val << (64 - Z_OFFSET - PACKED_Z_LENGTH) >> (64 - PACKED_Z_LENGTH)) as i32;
@ -597,17 +597,17 @@ impl McBufReadable for BlockPos {
}
impl McBufReadable for GlobalPos {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(GlobalPos {
world: ResourceLocation::read_from(buf)?,
pos: BlockPos::read_from(buf)?,
world: ResourceLocation::azalea_read(buf)?,
pos: BlockPos::azalea_read(buf)?,
})
}
}
impl McBufReadable for ChunkSectionPos {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let long = i64::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let long = i64::azalea_read(buf)?;
Ok(ChunkSectionPos {
x: (long >> 42) as i32,
y: (long << 44 >> 44) as i32,
@ -617,30 +617,30 @@ impl McBufReadable for ChunkSectionPos {
}
impl McBufWritable for BlockPos {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut val: u64 = 0;
val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET;
val |= (self.y as u64) & PACKED_Y_MASK;
val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET;
val.write_into(buf)
val.azalea_write(buf)
}
}
impl McBufWritable for GlobalPos {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
ResourceLocation::write_into(&self.world, buf)?;
BlockPos::write_into(&self.pos, buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
ResourceLocation::azalea_write(&self.world, buf)?;
BlockPos::azalea_write(&self.pos, buf)?;
Ok(())
}
}
impl McBufWritable for ChunkSectionPos {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let long = (((self.x & 0x3FFFFF) as i64) << 42)
| (self.y & 0xFFFFF) as i64
| (((self.z & 0x3FFFFF) as i64) << 20);
long.write_into(buf)?;
long.azalea_write(buf)?;
Ok(())
}
}
@ -733,9 +733,9 @@ mod tests {
#[test]
fn test_read_blockpos_from() {
let mut buf = Vec::new();
13743895338965u64.write_into(&mut buf).unwrap();
13743895338965u64.azalea_write(&mut buf).unwrap();
let mut buf = Cursor::new(&buf[..]);
let block_pos = BlockPos::read_from(&mut buf).unwrap();
let block_pos = BlockPos::azalea_read(&mut buf).unwrap();
assert_eq!(block_pos, BlockPos::new(49, -43, -3));
}
@ -751,9 +751,9 @@ mod tests {
#[test]
fn test_read_chunk_pos_from() {
let mut buf = Vec::new();
ChunkPos::new(2, -1).write_into(&mut buf).unwrap();
ChunkPos::new(2, -1).azalea_write(&mut buf).unwrap();
let mut buf = Cursor::new(&buf[..]);
let chunk_pos = ChunkPos::from(u64::read_from(&mut buf).unwrap());
let chunk_pos = ChunkPos::from(u64::azalea_read(&mut buf).unwrap());
assert_eq!(chunk_pos, ChunkPos::new(2, -1));
}
}

View file

@ -61,14 +61,14 @@ impl FromStr for ResourceLocation {
}
impl McBufReadable for ResourceLocation {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let location_string = String::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let location_string = String::azalea_read(buf)?;
Ok(ResourceLocation::new(&location_string))
}
}
impl McBufWritable for ResourceLocation {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.to_string().write_into(buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.to_string().azalea_write(buf)
}
}
@ -145,13 +145,13 @@ mod tests {
fn mcbuf_resource_location() {
let mut buf = Vec::new();
ResourceLocation::new("minecraft:dirt")
.write_into(&mut buf)
.azalea_write(&mut buf)
.unwrap();
let mut buf = Cursor::new(&buf[..]);
assert_eq!(
ResourceLocation::read_from(&mut buf).unwrap(),
ResourceLocation::azalea_read(&mut buf).unwrap(),
ResourceLocation::new("minecraft:dirt")
);
}

View file

@ -51,17 +51,17 @@ pub struct SignChatMessageOptions {
pub fn sign_chat_message(opts: &SignChatMessageOptions) -> MessageSignature {
let mut data_to_sign = Vec::new();
// always 1 for some reason
1i32.write_into(&mut data_to_sign).unwrap();
1i32.azalea_write(&mut data_to_sign).unwrap();
// player uuid
opts.account_uuid.write_into(&mut data_to_sign).unwrap();
opts.account_uuid.azalea_write(&mut data_to_sign).unwrap();
// chat session uuid
opts.chat_session_uuid
.write_into(&mut data_to_sign)
.azalea_write(&mut data_to_sign)
.unwrap();
// message index
opts.message_index.write_into(&mut data_to_sign).unwrap();
opts.message_index.azalea_write(&mut data_to_sign).unwrap();
// salt
opts.salt.write_into(&mut data_to_sign).unwrap();
opts.salt.azalea_write(&mut data_to_sign).unwrap();
// timestamp as seconds
let seconds_since_epoch = opts
@ -69,16 +69,16 @@ pub fn sign_chat_message(opts: &SignChatMessageOptions) -> MessageSignature {
.duration_since(UNIX_EPOCH)
.expect("timestamp must be after epoch")
.as_secs();
seconds_since_epoch.write_into(&mut data_to_sign).unwrap();
seconds_since_epoch.azalea_write(&mut data_to_sign).unwrap();
// message length as u32
let message_len: u32 = opts.message.len().try_into().unwrap();
message_len.write_into(&mut data_to_sign).unwrap();
message_len.azalea_write(&mut data_to_sign).unwrap();
// message bytes
data_to_sign.extend_from_slice(opts.message.as_bytes());
// last seen messages length
0i32.write_into(&mut data_to_sign).unwrap();
0i32.azalea_write(&mut data_to_sign).unwrap();
// signatures of last seen messages
// ... not implemented yet

View file

@ -37,14 +37,14 @@ impl EntityMetadataItems {
}
impl McBufReadable for EntityMetadataItems {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut metadata = Vec::new();
loop {
let id = u8::read_from(buf)?;
let id = u8::azalea_read(buf)?;
if id == 0xff {
break;
}
let value = EntityDataValue::read_from(buf)?;
let value = EntityDataValue::azalea_read(buf)?;
metadata.push(EntityDataItem { index: id, value });
}
Ok(EntityMetadataItems(metadata))
@ -52,12 +52,12 @@ impl McBufReadable for EntityMetadataItems {
}
impl McBufWritable for EntityMetadataItems {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
for item in &self.0 {
item.index.write_into(buf)?;
item.value.write_into(buf)?;
item.index.azalea_write(buf)?;
item.value.azalea_write(buf)?;
}
0xffu8.write_into(buf)?;
0xffu8.azalea_write(buf)?;
Ok(())
}
}
@ -123,8 +123,8 @@ pub enum ArmadilloStateKind {
}
impl McBufReadable for OptionalUnsignedInt {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let val = u32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let val = u32::azalea_read_var(buf)?;
Ok(OptionalUnsignedInt(if val == 0 {
None
} else {
@ -133,10 +133,10 @@ impl McBufReadable for OptionalUnsignedInt {
}
}
impl McBufWritable for OptionalUnsignedInt {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self.0 {
Some(val) => (val + 1).var_write_into(buf),
None => 0u32.var_write_into(buf),
Some(val) => (val + 1).azalea_write_var(buf),
None => 0u32.azalea_write_var(buf),
}
}
}

View file

@ -29,7 +29,7 @@ where
T: DataComponent + Clone + McBufWritable + McBufReadable + PartialEq,
{
fn encode(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
self.write_into(buf)
self.azalea_write(buf)
}
fn clone(&self) -> Box<dyn EncodableDataComponent> {
let cloned = self.clone();
@ -54,81 +54,83 @@ pub fn from_kind(
// note that this match statement is updated by genitemcomponents.py
Ok(match kind {
DataComponentKind::CustomData => Box::new(CustomData::read_from(buf)?),
DataComponentKind::MaxStackSize => Box::new(MaxStackSize::read_from(buf)?),
DataComponentKind::MaxDamage => Box::new(MaxDamage::read_from(buf)?),
DataComponentKind::Damage => Box::new(Damage::read_from(buf)?),
DataComponentKind::Unbreakable => Box::new(Unbreakable::read_from(buf)?),
DataComponentKind::CustomName => Box::new(CustomName::read_from(buf)?),
DataComponentKind::ItemName => Box::new(ItemName::read_from(buf)?),
DataComponentKind::Lore => Box::new(Lore::read_from(buf)?),
DataComponentKind::Rarity => Box::new(Rarity::read_from(buf)?),
DataComponentKind::Enchantments => Box::new(Enchantments::read_from(buf)?),
DataComponentKind::CanPlaceOn => Box::new(CanPlaceOn::read_from(buf)?),
DataComponentKind::CanBreak => Box::new(CanBreak::read_from(buf)?),
DataComponentKind::AttributeModifiers => Box::new(AttributeModifiers::read_from(buf)?),
DataComponentKind::CustomModelData => Box::new(CustomModelData::read_from(buf)?),
DataComponentKind::CustomData => Box::new(CustomData::azalea_read(buf)?),
DataComponentKind::MaxStackSize => Box::new(MaxStackSize::azalea_read(buf)?),
DataComponentKind::MaxDamage => Box::new(MaxDamage::azalea_read(buf)?),
DataComponentKind::Damage => Box::new(Damage::azalea_read(buf)?),
DataComponentKind::Unbreakable => Box::new(Unbreakable::azalea_read(buf)?),
DataComponentKind::CustomName => Box::new(CustomName::azalea_read(buf)?),
DataComponentKind::ItemName => Box::new(ItemName::azalea_read(buf)?),
DataComponentKind::Lore => Box::new(Lore::azalea_read(buf)?),
DataComponentKind::Rarity => Box::new(Rarity::azalea_read(buf)?),
DataComponentKind::Enchantments => Box::new(Enchantments::azalea_read(buf)?),
DataComponentKind::CanPlaceOn => Box::new(CanPlaceOn::azalea_read(buf)?),
DataComponentKind::CanBreak => Box::new(CanBreak::azalea_read(buf)?),
DataComponentKind::AttributeModifiers => Box::new(AttributeModifiers::azalea_read(buf)?),
DataComponentKind::CustomModelData => Box::new(CustomModelData::azalea_read(buf)?),
DataComponentKind::HideAdditionalTooltip => {
Box::new(HideAdditionalTooltip::read_from(buf)?)
Box::new(HideAdditionalTooltip::azalea_read(buf)?)
}
DataComponentKind::HideTooltip => Box::new(HideTooltip::read_from(buf)?),
DataComponentKind::RepairCost => Box::new(RepairCost::read_from(buf)?),
DataComponentKind::CreativeSlotLock => Box::new(CreativeSlotLock::read_from(buf)?),
DataComponentKind::HideTooltip => Box::new(HideTooltip::azalea_read(buf)?),
DataComponentKind::RepairCost => Box::new(RepairCost::azalea_read(buf)?),
DataComponentKind::CreativeSlotLock => Box::new(CreativeSlotLock::azalea_read(buf)?),
DataComponentKind::EnchantmentGlintOverride => {
Box::new(EnchantmentGlintOverride::read_from(buf)?)
Box::new(EnchantmentGlintOverride::azalea_read(buf)?)
}
DataComponentKind::IntangibleProjectile => Box::new(IntangibleProjectile::read_from(buf)?),
DataComponentKind::Food => Box::new(Food::read_from(buf)?),
DataComponentKind::Tool => Box::new(Tool::read_from(buf)?),
DataComponentKind::StoredEnchantments => Box::new(StoredEnchantments::read_from(buf)?),
DataComponentKind::DyedColor => Box::new(DyedColor::read_from(buf)?),
DataComponentKind::MapColor => Box::new(MapColor::read_from(buf)?),
DataComponentKind::MapId => Box::new(MapId::read_from(buf)?),
DataComponentKind::MapDecorations => Box::new(MapDecorations::read_from(buf)?),
DataComponentKind::MapPostProcessing => Box::new(MapPostProcessing::read_from(buf)?),
DataComponentKind::ChargedProjectiles => Box::new(ChargedProjectiles::read_from(buf)?),
DataComponentKind::BundleContents => Box::new(BundleContents::read_from(buf)?),
DataComponentKind::PotionContents => Box::new(PotionContents::read_from(buf)?),
DataComponentKind::IntangibleProjectile => {
Box::new(IntangibleProjectile::azalea_read(buf)?)
}
DataComponentKind::Food => Box::new(Food::azalea_read(buf)?),
DataComponentKind::Tool => Box::new(Tool::azalea_read(buf)?),
DataComponentKind::StoredEnchantments => Box::new(StoredEnchantments::azalea_read(buf)?),
DataComponentKind::DyedColor => Box::new(DyedColor::azalea_read(buf)?),
DataComponentKind::MapColor => Box::new(MapColor::azalea_read(buf)?),
DataComponentKind::MapId => Box::new(MapId::azalea_read(buf)?),
DataComponentKind::MapDecorations => Box::new(MapDecorations::azalea_read(buf)?),
DataComponentKind::MapPostProcessing => Box::new(MapPostProcessing::azalea_read(buf)?),
DataComponentKind::ChargedProjectiles => Box::new(ChargedProjectiles::azalea_read(buf)?),
DataComponentKind::BundleContents => Box::new(BundleContents::azalea_read(buf)?),
DataComponentKind::PotionContents => Box::new(PotionContents::azalea_read(buf)?),
DataComponentKind::SuspiciousStewEffects => {
Box::new(SuspiciousStewEffects::read_from(buf)?)
Box::new(SuspiciousStewEffects::azalea_read(buf)?)
}
DataComponentKind::WritableBookContent => Box::new(WritableBookContent::read_from(buf)?),
DataComponentKind::WrittenBookContent => Box::new(WrittenBookContent::read_from(buf)?),
DataComponentKind::Trim => Box::new(Trim::read_from(buf)?),
DataComponentKind::DebugStickState => Box::new(DebugStickState::read_from(buf)?),
DataComponentKind::EntityData => Box::new(EntityData::read_from(buf)?),
DataComponentKind::BucketEntityData => Box::new(BucketEntityData::read_from(buf)?),
DataComponentKind::BlockEntityData => Box::new(BlockEntityData::read_from(buf)?),
DataComponentKind::Instrument => Box::new(Instrument::read_from(buf)?),
DataComponentKind::WritableBookContent => Box::new(WritableBookContent::azalea_read(buf)?),
DataComponentKind::WrittenBookContent => Box::new(WrittenBookContent::azalea_read(buf)?),
DataComponentKind::Trim => Box::new(Trim::azalea_read(buf)?),
DataComponentKind::DebugStickState => Box::new(DebugStickState::azalea_read(buf)?),
DataComponentKind::EntityData => Box::new(EntityData::azalea_read(buf)?),
DataComponentKind::BucketEntityData => Box::new(BucketEntityData::azalea_read(buf)?),
DataComponentKind::BlockEntityData => Box::new(BlockEntityData::azalea_read(buf)?),
DataComponentKind::Instrument => Box::new(Instrument::azalea_read(buf)?),
DataComponentKind::OminousBottleAmplifier => {
Box::new(OminousBottleAmplifier::read_from(buf)?)
Box::new(OminousBottleAmplifier::azalea_read(buf)?)
}
DataComponentKind::Recipes => Box::new(Recipes::read_from(buf)?),
DataComponentKind::LodestoneTracker => Box::new(LodestoneTracker::read_from(buf)?),
DataComponentKind::FireworkExplosion => Box::new(FireworkExplosion::read_from(buf)?),
DataComponentKind::Fireworks => Box::new(Fireworks::read_from(buf)?),
DataComponentKind::Profile => Box::new(Profile::read_from(buf)?),
DataComponentKind::NoteBlockSound => Box::new(NoteBlockSound::read_from(buf)?),
DataComponentKind::BannerPatterns => Box::new(BannerPatterns::read_from(buf)?),
DataComponentKind::BaseColor => Box::new(BaseColor::read_from(buf)?),
DataComponentKind::PotDecorations => Box::new(PotDecorations::read_from(buf)?),
DataComponentKind::Container => Box::new(Container::read_from(buf)?),
DataComponentKind::BlockState => Box::new(BlockState::read_from(buf)?),
DataComponentKind::Bees => Box::new(Bees::read_from(buf)?),
DataComponentKind::Lock => Box::new(Lock::read_from(buf)?),
DataComponentKind::ContainerLoot => Box::new(ContainerLoot::read_from(buf)?),
DataComponentKind::JukeboxPlayable => Box::new(JukeboxPlayable::read_from(buf)?),
DataComponentKind::Consumable => Box::new(Consumable::read_from(buf)?),
DataComponentKind::UseRemainder => Box::new(UseRemainder::read_from(buf)?),
DataComponentKind::UseCooldown => Box::new(UseCooldown::read_from(buf)?),
DataComponentKind::Enchantable => Box::new(Enchantable::read_from(buf)?),
DataComponentKind::Repairable => Box::new(Repairable::read_from(buf)?),
DataComponentKind::ItemModel => Box::new(ItemModel::read_from(buf)?),
DataComponentKind::DamageResistant => Box::new(DamageResistant::read_from(buf)?),
DataComponentKind::Equippable => Box::new(Equippable::read_from(buf)?),
DataComponentKind::Glider => Box::new(Glider::read_from(buf)?),
DataComponentKind::TooltipStyle => Box::new(TooltipStyle::read_from(buf)?),
DataComponentKind::DeathProtection => Box::new(DeathProtection::read_from(buf)?),
DataComponentKind::Recipes => Box::new(Recipes::azalea_read(buf)?),
DataComponentKind::LodestoneTracker => Box::new(LodestoneTracker::azalea_read(buf)?),
DataComponentKind::FireworkExplosion => Box::new(FireworkExplosion::azalea_read(buf)?),
DataComponentKind::Fireworks => Box::new(Fireworks::azalea_read(buf)?),
DataComponentKind::Profile => Box::new(Profile::azalea_read(buf)?),
DataComponentKind::NoteBlockSound => Box::new(NoteBlockSound::azalea_read(buf)?),
DataComponentKind::BannerPatterns => Box::new(BannerPatterns::azalea_read(buf)?),
DataComponentKind::BaseColor => Box::new(BaseColor::azalea_read(buf)?),
DataComponentKind::PotDecorations => Box::new(PotDecorations::azalea_read(buf)?),
DataComponentKind::Container => Box::new(Container::azalea_read(buf)?),
DataComponentKind::BlockState => Box::new(BlockState::azalea_read(buf)?),
DataComponentKind::Bees => Box::new(Bees::azalea_read(buf)?),
DataComponentKind::Lock => Box::new(Lock::azalea_read(buf)?),
DataComponentKind::ContainerLoot => Box::new(ContainerLoot::azalea_read(buf)?),
DataComponentKind::JukeboxPlayable => Box::new(JukeboxPlayable::azalea_read(buf)?),
DataComponentKind::Consumable => Box::new(Consumable::azalea_read(buf)?),
DataComponentKind::UseRemainder => Box::new(UseRemainder::azalea_read(buf)?),
DataComponentKind::UseCooldown => Box::new(UseCooldown::azalea_read(buf)?),
DataComponentKind::Enchantable => Box::new(Enchantable::azalea_read(buf)?),
DataComponentKind::Repairable => Box::new(Repairable::azalea_read(buf)?),
DataComponentKind::ItemModel => Box::new(ItemModel::azalea_read(buf)?),
DataComponentKind::DamageResistant => Box::new(DamageResistant::azalea_read(buf)?),
DataComponentKind::Equippable => Box::new(Equippable::azalea_read(buf)?),
DataComponentKind::Glider => Box::new(Glider::azalea_read(buf)?),
DataComponentKind::TooltipStyle => Box::new(TooltipStyle::azalea_read(buf)?),
DataComponentKind::DeathProtection => Box::new(DeathProtection::azalea_read(buf)?),
})
}

View file

@ -141,13 +141,13 @@ impl ItemStackData {
}
impl McBufReadable for ItemStack {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let count = i32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let count = i32::azalea_read_var(buf)?;
if count <= 0 {
Ok(ItemStack::Empty)
} else {
let kind = azalea_registry::Item::read_from(buf)?;
let components = DataComponentPatch::read_from(buf)?;
let kind = azalea_registry::Item::azalea_read(buf)?;
let components = DataComponentPatch::azalea_read(buf)?;
Ok(ItemStack::Present(ItemStackData {
count,
kind,
@ -158,13 +158,13 @@ impl McBufReadable for ItemStack {
}
impl McBufWritable for ItemStack {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
ItemStack::Empty => 0.var_write_into(buf)?,
ItemStack::Empty => 0.azalea_write_var(buf)?,
ItemStack::Present(i) => {
i.count.var_write_into(buf)?;
i.kind.write_into(buf)?;
i.components.write_into(buf)?;
i.count.azalea_write_var(buf)?;
i.kind.azalea_write(buf)?;
i.components.azalea_write(buf)?;
}
};
Ok(())
@ -183,9 +183,9 @@ impl DataComponentPatch {
}
impl McBufReadable for DataComponentPatch {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let components_with_data_count = u32::var_read_from(buf)?;
let components_without_data_count = u32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let components_with_data_count = u32::azalea_read_var(buf)?;
let components_without_data_count = u32::azalea_read_var(buf)?;
if components_without_data_count == 0 && components_with_data_count == 0 {
return Ok(DataComponentPatch::default());
@ -193,13 +193,13 @@ impl McBufReadable for DataComponentPatch {
let mut components = HashMap::new();
for _ in 0..components_with_data_count {
let component_kind = DataComponentKind::read_from(buf)?;
let component_kind = DataComponentKind::azalea_read(buf)?;
let component_data = components::from_kind(component_kind, buf)?;
components.insert(component_kind, Some(component_data));
}
for _ in 0..components_without_data_count {
let component_kind = DataComponentKind::read_from(buf)?;
let component_kind = DataComponentKind::azalea_read(buf)?;
components.insert(component_kind, None);
}
@ -208,7 +208,7 @@ impl McBufReadable for DataComponentPatch {
}
impl McBufWritable for DataComponentPatch {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut components_with_data_count = 0;
let mut components_without_data_count = 0;
for component in self.components.values() {
@ -219,21 +219,21 @@ impl McBufWritable for DataComponentPatch {
}
}
components_with_data_count.write_into(buf)?;
components_without_data_count.write_into(buf)?;
components_with_data_count.azalea_write(buf)?;
components_without_data_count.azalea_write(buf)?;
for (kind, component) in &self.components {
if let Some(component) = component {
kind.write_into(buf)?;
kind.azalea_write(buf)?;
let mut component_buf = Vec::new();
component.encode(&mut component_buf).unwrap();
component_buf.write_into(buf)?;
component_buf.azalea_write(buf)?;
}
}
for (kind, component) in &self.components {
if component.is_none() {
kind.write_into(buf)?;
kind.azalea_write(buf)?;
}
}

View file

@ -21,14 +21,14 @@ fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> Toke
let contents = quote! {
impl #ident {
pub fn write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
azalea_buf::McBufWritable::write_into(self, buf)
azalea_buf::McBufWritable::azalea_write(self, buf)
}
pub fn read(
buf: &mut std::io::Cursor<&[u8]>,
) -> Result<#state, azalea_buf::BufReadError> {
use azalea_buf::McBufReadable;
Ok(Self::read_from(buf)?.into_variant())
Ok(Self::azalea_read(buf)?.into_variant())
}
/// Convert this packet into an variant for the enum of the state and direction.

View file

@ -98,8 +98,8 @@ impl Default for ModelCustomization {
}
impl McBufReadable for ModelCustomization {
fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let set = FixedBitSet::<7>::read_from(buf)?;
fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let set = FixedBitSet::<7>::azalea_read(buf)?;
Ok(Self {
cape: set.index(0),
jacket: set.index(1),
@ -113,7 +113,7 @@ impl McBufReadable for ModelCustomization {
}
impl McBufWritable for ModelCustomization {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<7>::new();
if self.cape {
set.set(0);
@ -136,7 +136,7 @@ impl McBufWritable for ModelCustomization {
if self.hat {
set.set(6);
}
set.write_into(buf)
set.azalea_write(buf)
}
}
@ -153,10 +153,10 @@ mod tests {
{
let data = ClientInformation::default();
let mut buf = Vec::new();
data.write_into(&mut buf).unwrap();
data.azalea_write(&mut buf).unwrap();
let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
let read_data = ClientInformation::read_from(&mut data_cursor).unwrap();
let read_data = ClientInformation::azalea_read(&mut data_cursor).unwrap();
assert_eq!(read_data, data);
}
@ -180,10 +180,10 @@ mod tests {
particle_status: ParticleStatus::Decreased,
};
let mut buf = Vec::new();
data.write_into(&mut buf).unwrap();
data.azalea_write(&mut buf).unwrap();
let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
let read_data = ClientInformation::read_from(&mut data_cursor).unwrap();
let read_data = ClientInformation::azalea_read(&mut data_cursor).unwrap();
assert_eq!(read_data, data);
}
}

View file

@ -22,15 +22,15 @@ pub struct Tags {
pub struct TagMap(pub HashMap<ResourceLocation, Vec<Tags>>);
impl McBufReadable for TagMap {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = u32::var_read_from(buf)? as usize;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = u32::azalea_read_var(buf)? as usize;
let mut data = HashMap::with_capacity(length);
for _ in 0..length {
let tag_type = ResourceLocation::read_from(buf)?;
let tags_count = i32::var_read_from(buf)? as usize;
let tag_type = ResourceLocation::azalea_read(buf)?;
let tags_count = i32::azalea_read_var(buf)? as usize;
let mut tags_vec = Vec::with_capacity(tags_count);
for _ in 0..tags_count {
let tags = Tags::read_from(buf)?;
let tags = Tags::azalea_read(buf)?;
tags_vec.push(tags);
}
data.insert(tag_type, tags_vec);
@ -40,27 +40,27 @@ impl McBufReadable for TagMap {
}
impl McBufWritable for TagMap {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).var_write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).azalea_write_var(buf)?;
for (k, v) in &self.0 {
k.write_into(buf)?;
v.write_into(buf)?;
k.azalea_write(buf)?;
v.azalea_write(buf)?;
}
Ok(())
}
}
impl McBufReadable for Tags {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let name = ResourceLocation::read_from(buf)?;
let elements = Vec::<i32>::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let name = ResourceLocation::azalea_read(buf)?;
let elements = Vec::<i32>::azalea_read_var(buf)?;
Ok(Tags { name, elements })
}
}
impl McBufWritable for Tags {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.name.write_into(buf)?;
self.elements.var_write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.name.azalea_write(buf)?;
self.elements.azalea_write_var(buf)?;
Ok(())
}
}

View file

@ -26,15 +26,15 @@ pub enum Operation {
}
impl McBufReadable for Operation {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let operation_id = u32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let operation_id = u32::azalea_read_var(buf)?;
Ok(match operation_id {
0 => Operation::Add(AddOperation::read_from(buf)?),
0 => Operation::Add(AddOperation::azalea_read(buf)?),
1 => Operation::Remove,
2 => Operation::UpdateProgress(f32::read_from(buf)?),
3 => Operation::UpdateName(FormattedText::read_from(buf)?),
4 => Operation::UpdateStyle(Style::read_from(buf)?),
5 => Operation::UpdateProperties(Properties::read_from(buf)?),
2 => Operation::UpdateProgress(f32::azalea_read(buf)?),
3 => Operation::UpdateName(FormattedText::azalea_read(buf)?),
4 => Operation::UpdateStyle(Style::azalea_read(buf)?),
5 => Operation::UpdateProperties(Properties::azalea_read(buf)?),
_ => {
return Err(BufReadError::UnexpectedEnumVariant {
id: operation_id as i32,
@ -45,30 +45,30 @@ impl McBufReadable for Operation {
}
impl McBufWritable for Operation {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
Operation::Add(add) => {
0u32.var_write_into(buf)?;
add.write_into(buf)?;
0u32.azalea_write_var(buf)?;
add.azalea_write(buf)?;
}
Operation::Remove => {
1u32.var_write_into(buf)?;
1u32.azalea_write_var(buf)?;
}
Operation::UpdateProgress(progress) => {
2u32.var_write_into(buf)?;
progress.write_into(buf)?;
2u32.azalea_write_var(buf)?;
progress.azalea_write(buf)?;
}
Operation::UpdateName(name) => {
3u32.var_write_into(buf)?;
name.write_into(buf)?;
3u32.azalea_write_var(buf)?;
name.azalea_write(buf)?;
}
Operation::UpdateStyle(style) => {
4u32.var_write_into(buf)?;
style.write_into(buf)?;
4u32.azalea_write_var(buf)?;
style.azalea_write(buf)?;
}
Operation::UpdateProperties(properties) => {
5u32.var_write_into(buf)?;
properties.write_into(buf)?;
5u32.azalea_write_var(buf)?;
properties.azalea_write(buf)?;
}
}
Ok(())
@ -117,8 +117,8 @@ pub struct Properties {
}
impl McBufReadable for Properties {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<3>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<3>::azalea_read(buf)?;
Ok(Self {
darken_screen: set.index(0),
play_music: set.index(1),
@ -128,7 +128,7 @@ impl McBufReadable for Properties {
}
impl McBufWritable for Properties {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<3>::new();
if self.darken_screen {
set.set(0);
@ -139,7 +139,7 @@ impl McBufWritable for Properties {
if self.create_world_fog {
set.set(2);
}
set.write_into(buf)?;
set.azalea_write(buf)?;
Ok(())
}
}

View file

@ -29,9 +29,9 @@ mod tests {
)],
);
let mut buf = Vec::new();
suggestions.write_into(&mut buf).unwrap();
suggestions.azalea_write(&mut buf).unwrap();
let mut cursor = Cursor::new(&buf[..]);
let suggestions = Suggestions::read_from(&mut cursor).unwrap();
let suggestions = Suggestions::azalea_read(&mut cursor).unwrap();
assert_eq!(suggestions, suggestions);
}
}

View file

@ -47,15 +47,15 @@ impl<T: PartialEq> PartialEq for BrigadierNumber<T> {
}
impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<2>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<2>::azalea_read(buf)?;
let min = if flags.index(0) {
Some(T::read_from(buf)?)
Some(T::azalea_read(buf)?)
} else {
None
};
let max = if flags.index(1) {
Some(T::read_from(buf)?)
Some(T::azalea_read(buf)?)
} else {
None
};
@ -63,7 +63,7 @@ impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> {
}
}
impl<T: McBufWritable> McBufWritable for BrigadierNumber<T> {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut flags = FixedBitSet::<2>::new();
if self.min.is_some() {
flags.set(0);
@ -71,12 +71,12 @@ impl<T: McBufWritable> McBufWritable for BrigadierNumber<T> {
if self.max.is_some() {
flags.set(1);
}
flags.write_into(buf)?;
flags.azalea_write(buf)?;
if let Some(min) = &self.min {
min.write_into(buf)?;
min.azalea_write(buf)?;
}
if let Some(max) = &self.max {
max.write_into(buf)?;
max.azalea_write(buf)?;
}
Ok(())
}
@ -157,8 +157,8 @@ pub struct EntityParser {
pub players_only: bool,
}
impl McBufReadable for EntityParser {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<2>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<2>::azalea_read(buf)?;
Ok(EntityParser {
single: flags.index(0),
players_only: flags.index(1),
@ -166,7 +166,7 @@ impl McBufReadable for EntityParser {
}
}
impl McBufWritable for EntityParser {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut flags = FixedBitSet::<2>::new();
if self.single {
flags.set(0);
@ -174,15 +174,15 @@ impl McBufWritable for EntityParser {
if self.players_only {
flags.set(1);
}
flags.write_into(buf)?;
flags.azalea_write(buf)?;
Ok(())
}
}
// TODO: BrigadierNodeStub should have more stuff
impl McBufReadable for BrigadierNodeStub {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<8>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let flags = FixedBitSet::<8>::azalea_read(buf)?;
if flags.index(5) || flags.index(6) || flags.index(7) {
warn!("Warning: The flags from a Brigadier node are over 31. This is probably a bug.",);
}
@ -192,19 +192,19 @@ impl McBufReadable for BrigadierNodeStub {
let has_redirect = flags.index(3);
let has_suggestions_type = flags.index(4);
let children = Vec::<u32>::var_read_from(buf)?;
let children = Vec::<u32>::azalea_read_var(buf)?;
let redirect_node = if has_redirect {
Some(u32::var_read_from(buf)?)
Some(u32::azalea_read_var(buf)?)
} else {
None
};
// argument node
if node_type == 2 {
let name = String::read_from(buf)?;
let parser = BrigadierParser::read_from(buf)?;
let name = String::azalea_read(buf)?;
let parser = BrigadierParser::azalea_read(buf)?;
let suggestions_type = if has_suggestions_type {
Some(ResourceLocation::read_from(buf)?)
Some(ResourceLocation::azalea_read(buf)?)
} else {
None
};
@ -222,7 +222,7 @@ impl McBufReadable for BrigadierNodeStub {
}
// literal node
else if node_type == 1 {
let name = String::read_from(buf)?;
let name = String::azalea_read(buf)?;
return Ok(BrigadierNodeStub {
is_executable,
children,
@ -240,7 +240,7 @@ impl McBufReadable for BrigadierNodeStub {
}
impl McBufWritable for BrigadierNodeStub {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut flags = FixedBitSet::<4>::new();
if self.is_executable {
flags.set(2);
@ -251,25 +251,25 @@ impl McBufWritable for BrigadierNodeStub {
match &self.node_type {
NodeType::Root => {
flags.write_into(buf)?;
flags.azalea_write(buf)?;
self.children.var_write_into(buf)?;
self.children.azalea_write_var(buf)?;
if let Some(redirect) = self.redirect_node {
redirect.var_write_into(buf)?;
redirect.azalea_write_var(buf)?;
}
}
NodeType::Literal { name } => {
flags.set(0);
flags.write_into(buf)?;
flags.azalea_write(buf)?;
self.children.var_write_into(buf)?;
self.children.azalea_write_var(buf)?;
if let Some(redirect) = self.redirect_node {
redirect.var_write_into(buf)?;
redirect.azalea_write_var(buf)?;
}
name.write_into(buf)?;
name.azalea_write(buf)?;
}
NodeType::Argument {
name,
@ -280,19 +280,19 @@ impl McBufWritable for BrigadierNodeStub {
if suggestions_type.is_some() {
flags.set(4);
}
flags.write_into(buf)?;
flags.azalea_write(buf)?;
self.children.var_write_into(buf)?;
self.children.azalea_write_var(buf)?;
if let Some(redirect) = self.redirect_node {
redirect.var_write_into(buf)?;
redirect.azalea_write_var(buf)?;
}
name.write_into(buf)?;
parser.write_into(buf)?;
name.azalea_write(buf)?;
parser.azalea_write(buf)?;
if let Some(suggestion) = suggestions_type {
suggestion.write_into(buf)?;
suggestion.azalea_write(buf)?;
}
}
}
@ -336,9 +336,9 @@ mod tests {
node_type: NodeType::Root,
};
let mut buf = Vec::new();
data.write_into(&mut buf).unwrap();
data.azalea_write(&mut buf).unwrap();
let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
let read_data = BrigadierNodeStub::read_from(&mut data_cursor).unwrap();
let read_data = BrigadierNodeStub::azalea_read(&mut data_cursor).unwrap();
assert_eq!(data, read_data);
}
@ -353,9 +353,9 @@ mod tests {
},
};
let mut buf = Vec::new();
data.write_into(&mut buf).unwrap();
data.azalea_write(&mut buf).unwrap();
let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
let read_data = BrigadierNodeStub::read_from(&mut data_cursor).unwrap();
let read_data = BrigadierNodeStub::azalea_read(&mut data_cursor).unwrap();
assert_eq!(data, read_data);
}
@ -372,9 +372,9 @@ mod tests {
},
};
let mut buf = Vec::new();
data.write_into(&mut buf).unwrap();
data.azalea_write(&mut buf).unwrap();
let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
let read_data = BrigadierNodeStub::read_from(&mut data_cursor).unwrap();
let read_data = BrigadierNodeStub::azalea_read(&mut data_cursor).unwrap();
assert_eq!(data, read_data);
}
}

View file

@ -18,18 +18,18 @@ pub struct ClientboundDamageEvent {
#[derive(Clone, Debug)]
pub struct OptionalEntityId(pub Option<u32>);
impl McBufReadable for OptionalEntityId {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
match u32::var_read_from(buf)? {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
match u32::azalea_read_var(buf)? {
0 => Ok(OptionalEntityId(None)),
id => Ok(OptionalEntityId(Some(id - 1))),
}
}
}
impl McBufWritable for OptionalEntityId {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self.0 {
Some(id) => (id + 1).var_write_into(buf),
None => 0u32.var_write_into(buf),
Some(id) => (id + 1).azalea_write_var(buf),
None => 0u32.azalea_write_var(buf),
}
}
}

View file

@ -35,35 +35,35 @@ pub enum BlockInteraction {
}
impl McBufReadable for ClientboundExplode {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let x = f64::read_from(buf)?;
let y = f64::read_from(buf)?;
let z = f64::read_from(buf)?;
let power = f32::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let x = f64::azalea_read(buf)?;
let y = f64::azalea_read(buf)?;
let z = f64::azalea_read(buf)?;
let power = f32::azalea_read(buf)?;
let x_floor = x.floor() as i32;
let y_floor = y.floor() as i32;
let z_floor = z.floor() as i32;
let to_blow_len = u32::var_read_from(buf)?;
let to_blow_len = u32::azalea_read_var(buf)?;
let mut to_blow = Vec::with_capacity(to_blow_len as usize);
for _ in 0..to_blow_len {
// the bytes are offsets from the main x y z
let x = x_floor + i32::from(i8::read_from(buf)?);
let y = y_floor + i32::from(i8::read_from(buf)?);
let z = z_floor + i32::from(i8::read_from(buf)?);
let x = x_floor + i32::from(i8::azalea_read(buf)?);
let y = y_floor + i32::from(i8::azalea_read(buf)?);
let z = z_floor + i32::from(i8::azalea_read(buf)?);
to_blow.push(BlockPos { x, y, z });
}
let knockback_x = f32::read_from(buf)?;
let knockback_y = f32::read_from(buf)?;
let knockback_z = f32::read_from(buf)?;
let knockback_x = f32::azalea_read(buf)?;
let knockback_y = f32::azalea_read(buf)?;
let knockback_z = f32::azalea_read(buf)?;
let block_interaction = BlockInteraction::read_from(buf)?;
let small_explosion_particles = ParticleKind::read_from(buf)?;
let large_explosion_particles = ParticleKind::read_from(buf)?;
let block_interaction = BlockInteraction::azalea_read(buf)?;
let small_explosion_particles = ParticleKind::azalea_read(buf)?;
let large_explosion_particles = ParticleKind::azalea_read(buf)?;
let sound_event_resource_location = ResourceLocation::read_from(buf)?.to_string();
let sound_event_resource_location = ResourceLocation::azalea_read(buf)?.to_string();
let explosion_sound =
SoundEvent::from_str(&sound_event_resource_location).map_err(|_| {
BufReadError::UnexpectedStringEnumVariant {
@ -89,14 +89,14 @@ impl McBufReadable for ClientboundExplode {
}
impl McBufWritable for ClientboundExplode {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.x.write_into(buf)?;
self.y.write_into(buf)?;
self.z.write_into(buf)?;
self.power.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.x.azalea_write(buf)?;
self.y.azalea_write(buf)?;
self.z.azalea_write(buf)?;
self.power.azalea_write(buf)?;
let to_blow_len = self.to_blow.len() as u32;
to_blow_len.var_write_into(buf)?;
to_blow_len.azalea_write_var(buf)?;
let x_floor = self.x.floor() as i32;
let y_floor = self.y.floor() as i32;
@ -106,22 +106,22 @@ impl McBufWritable for ClientboundExplode {
let x = (pos.x - x_floor) as i8;
let y = (pos.y - y_floor) as i8;
let z = (pos.z - z_floor) as i8;
x.write_into(buf)?;
y.write_into(buf)?;
z.write_into(buf)?;
x.azalea_write(buf)?;
y.azalea_write(buf)?;
z.azalea_write(buf)?;
}
self.knockback_x.write_into(buf)?;
self.knockback_y.write_into(buf)?;
self.knockback_z.write_into(buf)?;
self.knockback_x.azalea_write(buf)?;
self.knockback_y.azalea_write(buf)?;
self.knockback_z.azalea_write(buf)?;
self.block_interaction.write_into(buf)?;
self.small_explosion_particles.write_into(buf)?;
self.large_explosion_particles.write_into(buf)?;
self.block_interaction.azalea_write(buf)?;
self.small_explosion_particles.azalea_write(buf)?;
self.large_explosion_particles.azalea_write(buf)?;
let sound_event_resource_location =
ResourceLocation::new(&self.explosion_sound.to_string());
sound_event_resource_location.write_into(buf)?;
sound_event_resource_location.azalea_write(buf)?;
Ok(())
}
@ -159,8 +159,8 @@ mod tests {
explosion_sound: SoundEvent::EntityGenericExplode,
};
let mut buf = Vec::new();
packet.write_into(&mut buf).unwrap();
let packet2 = ClientboundExplode::read_from(&mut Cursor::new(&buf)).unwrap();
packet.azalea_write(&mut buf).unwrap();
let packet2 = ClientboundExplode::azalea_read(&mut Cursor::new(&buf)).unwrap();
assert_eq!(packet, packet2);
}
}

File diff suppressed because one or more lines are too long

View file

@ -33,7 +33,7 @@ mod tests {
][..];
let mut bytes = Cursor::new(slice);
let _packet = ClientboundLevelParticles::read_from(&mut bytes).unwrap();
let _packet = ClientboundLevelParticles::azalea_read(&mut bytes).unwrap();
assert_eq!(bytes.position(), slice.len() as u64);
}
}

View file

@ -27,22 +27,22 @@ pub struct MapDecoration {
pub struct OptionalMapPatch(pub Option<MapPatch>);
impl McBufReadable for OptionalMapPatch {
fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let pos = buf.position();
Ok(Self(if u8::read_from(buf)? == 0 {
Ok(Self(if u8::azalea_read(buf)? == 0 {
None
} else {
buf.set_position(pos);
Some(MapPatch::read_from(buf)?)
Some(MapPatch::azalea_read(buf)?)
}))
}
}
impl McBufWritable for OptionalMapPatch {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
match &self.0 {
None => 0u8.write_into(buf),
Some(m) => m.write_into(buf),
None => 0u8.azalea_write(buf),
Some(m) => m.azalea_write(buf),
}
}
}

View file

@ -22,8 +22,8 @@ pub struct PlayerAbilitiesFlags {
}
impl McBufReadable for PlayerAbilitiesFlags {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<4>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<4>::azalea_read(buf)?;
Ok(PlayerAbilitiesFlags {
invulnerable: set.index(0),
flying: set.index(1),
@ -34,7 +34,7 @@ impl McBufReadable for PlayerAbilitiesFlags {
}
impl McBufWritable for PlayerAbilitiesFlags {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<4>::new();
if self.invulnerable {
set.set(0);
@ -48,6 +48,6 @@ impl McBufWritable for PlayerAbilitiesFlags {
if self.instant_break {
set.set(3);
}
set.write_into(buf)
set.azalea_write(buf)
}
}

View file

@ -145,10 +145,10 @@ impl ChatType {
}
impl McBufReadable for PackedMessageSignature {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = u32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = u32::azalea_read_var(buf)?;
if id == 0 {
let full_signature = MessageSignature::read_from(buf)?;
let full_signature = MessageSignature::azalea_read(buf)?;
Ok(PackedMessageSignature::Signature(Box::new(full_signature)))
} else {
@ -157,14 +157,14 @@ impl McBufReadable for PackedMessageSignature {
}
}
impl McBufWritable for PackedMessageSignature {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
PackedMessageSignature::Signature(full_signature) => {
0u32.var_write_into(buf)?;
full_signature.write_into(buf)?;
0u32.azalea_write_var(buf)?;
full_signature.azalea_write(buf)?;
}
PackedMessageSignature::Id(id) => {
(id + 1).var_write_into(buf)?;
(id + 1).azalea_write_var(buf)?;
}
}
Ok(())
@ -197,6 +197,6 @@ mod tests {
105, 115, 97, 8, 0, 4, 116, 101, 120, 116, 0, 0, 0, 0,
][..],
);
let _packet = ClientboundPlayerChat::read_from(&mut bytes).unwrap();
let _packet = ClientboundPlayerChat::azalea_read(&mut bytes).unwrap();
}
}

View file

@ -64,43 +64,43 @@ pub struct UpdateListOrderAction {
}
impl McBufReadable for ClientboundPlayerInfoUpdate {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let actions = ActionEnumSet::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let actions = ActionEnumSet::azalea_read(buf)?;
let mut entries = Vec::new();
let entry_count = u32::var_read_from(buf)?;
let entry_count = u32::azalea_read_var(buf)?;
for _ in 0..entry_count {
let profile_id = Uuid::read_from(buf)?;
let profile_id = Uuid::azalea_read(buf)?;
let mut entry = PlayerInfoEntry::default();
entry.profile.uuid = profile_id;
if actions.add_player {
let action = AddPlayerAction::read_from(buf)?;
let action = AddPlayerAction::azalea_read(buf)?;
entry.profile.name = action.name;
entry.profile.properties = action.properties;
}
if actions.initialize_chat {
let action = InitializeChatAction::read_from(buf)?;
let action = InitializeChatAction::azalea_read(buf)?;
entry.chat_session = action.chat_session;
}
if actions.update_game_mode {
let action = UpdateGameModeAction::read_from(buf)?;
let action = UpdateGameModeAction::azalea_read(buf)?;
entry.game_mode = action.game_mode;
}
if actions.update_listed {
let action = UpdateListedAction::read_from(buf)?;
let action = UpdateListedAction::azalea_read(buf)?;
entry.listed = action.listed;
}
if actions.update_latency {
let action = UpdateLatencyAction::read_from(buf)?;
let action = UpdateLatencyAction::azalea_read(buf)?;
entry.latency = action.latency;
}
if actions.update_display_name {
let action = UpdateDisplayNameAction::read_from(buf)?;
let action = UpdateDisplayNameAction::azalea_read(buf)?;
entry.display_name = action.display_name;
}
if actions.update_list_order {
let action = UpdateListOrderAction::read_from(buf)?;
let action = UpdateListOrderAction::azalea_read(buf)?;
entry.list_order = action.list_order;
}
@ -112,49 +112,49 @@ impl McBufReadable for ClientboundPlayerInfoUpdate {
}
impl McBufWritable for ClientboundPlayerInfoUpdate {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.actions.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.actions.azalea_write(buf)?;
(self.entries.len() as u32).var_write_into(buf)?;
(self.entries.len() as u32).azalea_write_var(buf)?;
for entry in &self.entries {
entry.profile.uuid.write_into(buf)?;
entry.profile.uuid.azalea_write(buf)?;
if self.actions.add_player {
AddPlayerAction {
name: entry.profile.name.clone(),
properties: entry.profile.properties.clone(),
}
.write_into(buf)?;
.azalea_write(buf)?;
}
if self.actions.initialize_chat {
InitializeChatAction {
chat_session: entry.chat_session.clone(),
}
.write_into(buf)?;
.azalea_write(buf)?;
}
if self.actions.update_game_mode {
UpdateGameModeAction {
game_mode: entry.game_mode,
}
.write_into(buf)?;
.azalea_write(buf)?;
}
if self.actions.update_listed {
UpdateListedAction {
listed: entry.listed,
}
.write_into(buf)?;
.azalea_write(buf)?;
}
if self.actions.update_latency {
UpdateLatencyAction {
latency: entry.latency,
}
.write_into(buf)?;
.azalea_write(buf)?;
}
if self.actions.update_display_name {
UpdateDisplayNameAction {
display_name: entry.display_name.clone(),
}
.write_into(buf)?;
.azalea_write(buf)?;
}
}
@ -174,8 +174,8 @@ pub struct ActionEnumSet {
}
impl McBufReadable for ActionEnumSet {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<7>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<7>::azalea_read(buf)?;
Ok(ActionEnumSet {
add_player: set.index(0),
initialize_chat: set.index(1),
@ -189,7 +189,7 @@ impl McBufReadable for ActionEnumSet {
}
impl McBufWritable for ActionEnumSet {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<7>::new();
if self.add_player {
set.set(0);
@ -212,7 +212,7 @@ impl McBufWritable for ActionEnumSet {
if self.update_list_order {
set.set(6);
}
set.write_into(buf)?;
set.azalea_write(buf)?;
Ok(())
}
}
@ -233,9 +233,9 @@ mod tests {
update_list_order: true,
};
let mut buf = Vec::new();
data.write_into(&mut buf).unwrap();
data.azalea_write(&mut buf).unwrap();
let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
let read_data = ActionEnumSet::read_from(&mut data_cursor).unwrap();
let read_data = ActionEnumSet::azalea_read(&mut data_cursor).unwrap();
assert_eq!(read_data, data);
}
@ -308,6 +308,6 @@ mod tests {
0,
][..],
);
let _packet = ClientboundPlayerInfoUpdate::read_from(&mut bytes).unwrap();
let _packet = ClientboundPlayerInfoUpdate::azalea_read(&mut bytes).unwrap();
}
}

View file

@ -25,9 +25,9 @@ pub struct RelativeMovements {
}
impl McBufReadable for RelativeMovements {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
// yes minecraft seriously wastes that many bits, smh
let set = FixedBitSet::<32>::read_from(buf)?;
let set = FixedBitSet::<32>::azalea_read(buf)?;
Ok(RelativeMovements {
x: set.index(0),
y: set.index(1),
@ -39,7 +39,7 @@ impl McBufReadable for RelativeMovements {
}
impl McBufWritable for RelativeMovements {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<5>::new();
if self.x {
set.set(0);
@ -56,6 +56,6 @@ impl McBufWritable for RelativeMovements {
if self.x_rot {
set.set(4);
}
set.write_into(buf)
set.azalea_write(buf)
}
}

View file

@ -20,8 +20,8 @@ pub struct BlockStateWithPosition {
}
impl McBufReadable for BlockStateWithPosition {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let data = u64::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let data = u64::azalea_read_var(buf)?;
let position_part = data & 4095;
let state = (data >> 12) as u32;
let state = BlockState::try_from(state)
@ -36,10 +36,10 @@ impl McBufReadable for BlockStateWithPosition {
}
impl McBufWritable for BlockStateWithPosition {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let data = (self.state.id as u64) << 12
| (u64::from(self.pos.x) << 8 | u64::from(self.pos.z) << 4 | u64::from(self.pos.y));
u64::var_write_into(&data, buf)?;
u64::azalea_write_var(&data, buf)?;
Ok(())
}
}

View file

@ -18,18 +18,18 @@ pub struct EquipmentSlots {
}
impl McBufReadable for EquipmentSlots {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut slots = vec![];
loop {
let equipment_byte = u8::read_from(buf)?;
let equipment_byte = u8::azalea_read(buf)?;
let equipment_slot =
EquipmentSlot::from_byte(equipment_byte & 127).ok_or_else(|| {
BufReadError::UnexpectedEnumVariant {
id: equipment_byte.into(),
}
})?;
let item = ItemStack::read_from(buf)?;
let item = ItemStack::azalea_read(buf)?;
slots.push((equipment_slot, item));
if equipment_byte & 128 == 0 {
break;
@ -40,15 +40,15 @@ impl McBufReadable for EquipmentSlots {
}
}
impl McBufWritable for EquipmentSlots {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
for i in 0..self.slots.len() {
let (equipment_slot, item) = &self.slots[i];
let mut equipment_byte = *equipment_slot as u8;
if i != self.slots.len() - 1 {
equipment_byte |= 128;
}
equipment_byte.write_into(buf)?;
item.write_into(buf)?;
equipment_byte.azalea_write(buf)?;
item.azalea_write(buf)?;
}
Ok(())

View file

@ -34,47 +34,47 @@ pub enum Method {
}
impl McBufReadable for Method {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let kind = MethodKind::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let kind = MethodKind::azalea_read(buf)?;
match kind {
MethodKind::Add => Ok(Method::Add {
display_name: FormattedText::read_from(buf)?,
render_type: ObjectiveCriteria::read_from(buf)?,
number_format: NumberFormat::read_from(buf)?,
display_name: FormattedText::azalea_read(buf)?,
render_type: ObjectiveCriteria::azalea_read(buf)?,
number_format: NumberFormat::azalea_read(buf)?,
}),
MethodKind::Remove => Ok(Method::Remove),
MethodKind::Change => Ok(Method::Change {
display_name: FormattedText::read_from(buf)?,
render_type: ObjectiveCriteria::read_from(buf)?,
number_format: NumberFormat::read_from(buf)?,
display_name: FormattedText::azalea_read(buf)?,
render_type: ObjectiveCriteria::azalea_read(buf)?,
number_format: NumberFormat::azalea_read(buf)?,
}),
}
}
}
impl McBufWritable for Method {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
Method::Add {
display_name,
render_type,
number_format,
} => {
MethodKind::Add.write_into(buf)?;
display_name.write_into(buf)?;
render_type.write_into(buf)?;
number_format.write_into(buf)?;
MethodKind::Add.azalea_write(buf)?;
display_name.azalea_write(buf)?;
render_type.azalea_write(buf)?;
number_format.azalea_write(buf)?;
}
Method::Remove => MethodKind::Remove.write_into(buf)?,
Method::Remove => MethodKind::Remove.azalea_write(buf)?,
Method::Change {
display_name,
render_type,
number_format,
} => {
MethodKind::Change.write_into(buf)?;
display_name.write_into(buf)?;
render_type.write_into(buf)?;
number_format.write_into(buf)?;
MethodKind::Change.azalea_write(buf)?;
display_name.azalea_write(buf)?;
render_type.azalea_write(buf)?;
number_format.azalea_write(buf)?;
}
}
Ok(())

View file

@ -20,40 +20,40 @@ pub enum Method {
}
impl McBufReadable for Method {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(match u8::read_from(buf)? {
0 => Method::Add((Parameters::read_from(buf)?, PlayerList::read_from(buf)?)),
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(match u8::azalea_read(buf)? {
0 => Method::Add((Parameters::azalea_read(buf)?, PlayerList::azalea_read(buf)?)),
1 => Method::Remove,
2 => Method::Change(Parameters::read_from(buf)?),
3 => Method::Join(PlayerList::read_from(buf)?),
4 => Method::Leave(PlayerList::read_from(buf)?),
2 => Method::Change(Parameters::azalea_read(buf)?),
3 => Method::Join(PlayerList::azalea_read(buf)?),
4 => Method::Leave(PlayerList::azalea_read(buf)?),
id => return Err(BufReadError::UnexpectedEnumVariant { id: i32::from(id) }),
})
}
}
impl McBufWritable for Method {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
Method::Add((parameters, playerlist)) => {
0u8.write_into(buf)?;
parameters.write_into(buf)?;
playerlist.write_into(buf)?;
0u8.azalea_write(buf)?;
parameters.azalea_write(buf)?;
playerlist.azalea_write(buf)?;
}
Method::Remove => {
1u8.write_into(buf)?;
1u8.azalea_write(buf)?;
}
Method::Change(parameters) => {
2u8.write_into(buf)?;
parameters.write_into(buf)?;
2u8.azalea_write(buf)?;
parameters.azalea_write(buf)?;
}
Method::Join(playerlist) => {
3u8.write_into(buf)?;
playerlist.write_into(buf)?;
3u8.azalea_write(buf)?;
playerlist.azalea_write(buf)?;
}
Method::Leave(playerlist) => {
4u8.write_into(buf)?;
playerlist.write_into(buf)?;
4u8.azalea_write(buf)?;
playerlist.azalea_write(buf)?;
}
}
Ok(())

View file

@ -13,15 +13,15 @@ pub struct ClientboundStopSound {
}
impl McBufReadable for ClientboundStopSound {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<2>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<2>::azalea_read(buf)?;
let source = if set.index(0) {
Some(SoundSource::read_from(buf)?)
Some(SoundSource::azalea_read(buf)?)
} else {
None
};
let name = if set.index(1) {
Some(ResourceLocation::read_from(buf)?)
Some(ResourceLocation::azalea_read(buf)?)
} else {
None
};
@ -31,7 +31,7 @@ impl McBufReadable for ClientboundStopSound {
}
impl McBufWritable for ClientboundStopSound {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<2>::new();
if self.source.is_some() {
set.set(0);
@ -39,12 +39,12 @@ impl McBufWritable for ClientboundStopSound {
if self.name.is_some() {
set.set(1);
}
set.write_into(buf)?;
set.azalea_write(buf)?;
if let Some(source) = &self.source {
source.write_into(buf)?;
source.azalea_write(buf)?;
}
if let Some(name) = &self.name {
name.write_into(buf)?;
name.azalea_write(buf)?;
}
Ok(())
}

View file

@ -23,7 +23,7 @@ mod tests {
10, 9, 0, 4, 119, 105, 116, 104, 10, 0, 0, 0, 2, 10, 0, 10, 104, 111, 118, 101, 114, 69, 118, 101, 110, 116, 10, 0, 8, 99, 111, 110, 116, 101, 110, 116, 115, 8, 0, 4, 110, 97, 109, 101, 0, 3, 112, 121, 53, 11, 0, 2, 105, 100, 0, 0, 0, 4, 101, 54, 191, 237, 134, 149, 72, 253, 131, 161, 236, 210, 76, 242, 160, 253, 8, 0, 4, 116, 121, 112, 101, 0, 16, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 112, 108, 97, 121, 101, 114, 0, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 11, 115, 104, 111, 119, 95, 101, 110, 116, 105, 116, 121, 0, 10, 0, 10, 99, 108, 105, 99, 107, 69, 118, 101, 110, 116, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 15, 115, 117, 103, 103, 101, 115, 116, 95, 99, 111, 109, 109, 97, 110, 100, 8, 0, 5, 118, 97, 108, 117, 101, 0, 10, 47, 116, 101, 108, 108, 32, 112, 121, 53, 32, 0, 8, 0, 9, 105, 110, 115, 101, 114, 116, 105, 111, 110, 0, 3, 112, 121, 53, 8, 0, 4, 116, 101, 120, 116, 0, 3, 112, 121, 53, 0, 9, 0, 4, 119, 105, 116, 104, 10, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 1, 0, 10, 0, 10, 104, 111, 118, 101, 114, 69, 118, 101, 110, 116, 10, 0, 8, 99, 111, 110, 116, 101, 110, 116, 115, 8, 0, 2, 105, 100, 0, 25, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 100, 105, 97, 109, 111, 110, 100, 95, 112, 105, 99, 107, 97, 120, 101, 8, 0, 3, 116, 97, 103, 0, 10, 123, 68, 97, 109, 97, 103, 101, 58, 48, 125, 0, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 9, 115, 104, 111, 119, 95, 105, 116, 101, 109, 0, 9, 0, 4, 119, 105, 116, 104, 10, 0, 0, 0, 1, 9, 0, 5, 101, 120, 116, 114, 97, 10, 0, 0, 0, 1, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 30, 105, 116, 101, 109, 46, 109, 105, 110, 101, 99, 114, 97, 102, 116, 46, 100, 105, 97, 109, 111, 110, 100, 95, 112, 105, 99, 107, 97, 120, 101, 0, 8, 0, 4, 116, 101, 120, 116, 0, 0, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 5, 119, 104, 105, 116, 101, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 20, 99, 104, 97, 116, 46, 115, 113, 117, 97, 114, 101, 95, 98, 114, 97, 99, 107, 101, 116, 115, 0, 10, 0, 10, 104, 111, 118, 101, 114, 69, 118, 101, 110, 116, 10, 0, 8, 99, 111, 110, 116, 101, 110, 116, 115, 8, 0, 4, 110, 97, 109, 101, 0, 3, 112, 121, 53, 11, 0, 2, 105, 100, 0, 0, 0, 4, 101, 54, 191, 237, 134, 149, 72, 253, 131, 161, 236, 210, 76, 242, 160, 253, 8, 0, 4, 116, 121, 112, 101, 0, 16, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 112, 108, 97, 121, 101, 114, 0, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 11, 115, 104, 111, 119, 95, 101, 110, 116, 105, 116, 121, 0, 10, 0, 10, 99, 108, 105, 99, 107, 69, 118, 101, 110, 116, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 15, 115, 117, 103, 103, 101, 115, 116, 95, 99, 111, 109, 109, 97, 110, 100, 8, 0, 5, 118, 97, 108, 117, 101, 0, 10, 47, 116, 101, 108, 108, 32, 112, 121, 53, 32, 0, 8, 0, 9, 105, 110, 115, 101, 114, 116, 105, 111, 110, 0, 3, 112, 121, 53, 8, 0, 4, 116, 101, 120, 116, 0, 3, 112, 121, 53, 0, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 28, 99, 111, 109, 109, 97, 110, 100, 115, 46, 103, 105, 118, 101, 46, 115, 117, 99, 99, 101, 115, 115, 46, 115, 105, 110, 103, 108, 101, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 114, 97, 121, 1, 0, 6, 105, 116, 97, 108, 105, 99, 1, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 15, 99, 104, 97, 116, 46, 116, 121, 112, 101, 46, 97, 100, 109, 105, 110, 0, 0
];
let packet = ClientboundSystemChat::read_from(&mut Cursor::new(&bytes)).unwrap();
let packet = ClientboundSystemChat::azalea_read(&mut Cursor::new(&bytes)).unwrap();
assert_eq!(
packet.content.to_string(),
"[py5: Gave 1 [Diamond Pickaxe] to py5]".to_string()
@ -36,7 +36,7 @@ mod tests {
let bytes = [
10, 9, 0, 4, 119, 105, 116, 104, 8, 0, 0, 0, 1, 0, 14, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 100, 117, 115, 116, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 25, 99, 111, 109, 109, 97, 110, 100, 115, 46, 112, 97, 114, 116, 105, 99, 108, 101, 46, 115, 117, 99, 99, 101, 115, 115, 0, 0
];
let packet = ClientboundSystemChat::read_from(&mut Cursor::new(&bytes)).unwrap();
let packet = ClientboundSystemChat::azalea_read(&mut Cursor::new(&bytes)).unwrap();
assert_eq!(
packet.content.to_string(),
"Displaying particle minecraft:dust".to_string()

View file

@ -22,7 +22,7 @@ mod tests {
let mut bytes = Cursor::new(&[
10, 9, 0, 5, 101, 120, 116, 114, 97, 10, 0, 0, 0, 3, 1, 0, 4, 98, 111, 108, 100, 1, 8, 0, 4, 116, 101, 120, 116, 0, 16, 50, 66, 85, 73, 76, 68, 69, 82, 83, 50, 84, 79, 79, 76, 83, 10, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 114, 97, 121, 0, 8, 0, 0, 0, 1, 10, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 111, 108, 100, 8, 0, 4, 116, 101, 120, 116, 0, 27, 80, 101, 110, 100, 105, 110, 103, 32, 99, 111, 110, 110, 101, 99, 116, 105, 111, 110, 32, 116, 111, 32, 50, 98, 50, 116, 10, 0, 8, 0, 4, 116, 101, 120, 116, 0, 1, 10, 0, 10, 9, 0, 5, 101, 120, 116, 114, 97, 10, 0, 0, 0, 1, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 111, 108, 100, 8, 0, 4, 116, 101, 120, 116, 0, 72, 84, 104, 105, 115, 32, 97, 99, 99, 111, 117, 110, 116, 32, 104, 97, 115, 32, 112, 114, 105, 111, 114, 105, 116, 121, 32, 115, 116, 97, 116, 117, 115, 32, 97, 110, 100, 32, 119, 105, 108, 108, 32, 98, 101, 32, 112, 108, 97, 99, 101, 100, 32, 105, 110, 32, 97, 32, 115, 104, 111, 114, 116, 101, 114, 32, 113, 117, 101, 117, 101, 46, 10, 0, 8, 0, 4, 116, 101, 120, 116, 0, 1, 10, 0
][..]);
let _packet = ClientboundTabList::read_from(&mut bytes).unwrap();
let _packet = ClientboundTabList::azalea_read(&mut bytes).unwrap();
assert!(bytes.get_ref()[bytes.position() as usize..].is_empty());
}
}

View file

@ -37,11 +37,11 @@ pub struct DisplayInfo {
}
impl azalea_buf::McBufWritable for DisplayInfo {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
self.title.write_into(buf)?;
self.description.write_into(buf)?;
self.icon.write_into(buf)?;
self.frame.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
self.title.azalea_write(buf)?;
self.description.azalea_write(buf)?;
self.icon.azalea_write(buf)?;
self.frame.azalea_write(buf)?;
let mut data: u32 = 0;
if self.background.is_some() {
@ -53,35 +53,35 @@ impl azalea_buf::McBufWritable for DisplayInfo {
if self.hidden {
data |= 0b100;
}
data.write_into(buf)?;
data.azalea_write(buf)?;
if let Some(background) = &self.background {
background.write_into(buf)?;
background.azalea_write(buf)?;
}
self.x.write_into(buf)?;
self.y.write_into(buf)?;
self.x.azalea_write(buf)?;
self.y.azalea_write(buf)?;
Ok(())
}
}
impl azalea_buf::McBufReadable for DisplayInfo {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let title = azalea_buf::McBufReadable::read_from(buf)?;
let description = azalea_buf::McBufReadable::read_from(buf)?;
let icon = azalea_buf::McBufReadable::read_from(buf)?;
let frame = azalea_buf::McBufReadable::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
let title = azalea_buf::McBufReadable::azalea_read(buf)?;
let description = azalea_buf::McBufReadable::azalea_read(buf)?;
let icon = azalea_buf::McBufReadable::azalea_read(buf)?;
let frame = azalea_buf::McBufReadable::azalea_read(buf)?;
let data = u32::read_from(buf)?;
let data = u32::azalea_read(buf)?;
let has_background = (data & 0b1) != 0;
let show_toast = (data & 0b10) != 0;
let hidden = (data & 0b100) != 0;
let background = if has_background {
Some(ResourceLocation::read_from(buf)?)
Some(ResourceLocation::azalea_read(buf)?)
} else {
None
};
let x = azalea_buf::McBufReadable::read_from(buf)?;
let y = azalea_buf::McBufReadable::read_from(buf)?;
let x = azalea_buf::McBufReadable::azalea_read(buf)?;
let y = azalea_buf::McBufReadable::azalea_read(buf)?;
Ok(DisplayInfo {
title,
description,
@ -164,10 +164,10 @@ mod tests {
};
let mut data = Vec::new();
packet.write_into(&mut data).unwrap();
packet.azalea_write(&mut data).unwrap();
let mut buf: Cursor<&[u8]> = Cursor::new(&data);
let read_packet = ClientboundUpdateAdvancements::read_from(&mut buf).unwrap();
let read_packet = ClientboundUpdateAdvancements::azalea_read(&mut buf).unwrap();
assert_eq!(packet.reset, read_packet.reset);
assert_eq!(packet.removed, read_packet.removed);

View file

@ -22,15 +22,15 @@ pub struct Tags {
pub struct TagMap(pub HashMap<ResourceLocation, Vec<Tags>>);
impl McBufReadable for TagMap {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = u32::var_read_from(buf)? as usize;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let length = u32::azalea_read_var(buf)? as usize;
let mut data = HashMap::with_capacity(length);
for _ in 0..length {
let tag_type = ResourceLocation::read_from(buf)?;
let tags_count = i32::var_read_from(buf)? as usize;
let tag_type = ResourceLocation::azalea_read(buf)?;
let tags_count = i32::azalea_read_var(buf)? as usize;
let mut tags_vec = Vec::with_capacity(tags_count);
for _ in 0..tags_count {
let tags = Tags::read_from(buf)?;
let tags = Tags::azalea_read(buf)?;
tags_vec.push(tags);
}
data.insert(tag_type, tags_vec);
@ -40,27 +40,27 @@ impl McBufReadable for TagMap {
}
impl McBufWritable for TagMap {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).var_write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).azalea_write_var(buf)?;
for (k, v) in &self.0 {
k.write_into(buf)?;
v.write_into(buf)?;
k.azalea_write(buf)?;
v.azalea_write(buf)?;
}
Ok(())
}
}
impl McBufReadable for Tags {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let name = ResourceLocation::read_from(buf)?;
let elements = Vec::<i32>::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let name = ResourceLocation::azalea_read(buf)?;
let elements = Vec::<i32>::azalea_read_var(buf)?;
Ok(Tags { name, elements })
}
}
impl McBufWritable for Tags {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.name.write_into(buf)?;
self.elements.var_write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.name.azalea_write(buf)?;
self.elements.azalea_write_var(buf)?;
Ok(())
}
}

View file

@ -28,21 +28,21 @@ pub enum ActionType {
}
impl McBufWritable for ActionType {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
ActionType::Interact { hand } => {
0u32.var_write_into(buf)?;
hand.write_into(buf)?;
0u32.azalea_write_var(buf)?;
hand.azalea_write(buf)?;
}
ActionType::Attack => {
1u32.var_write_into(buf)?;
1u32.azalea_write_var(buf)?;
}
ActionType::InteractAt { location, hand } => {
2u32.var_write_into(buf)?;
(location.x as f32).write_into(buf)?;
(location.y as f32).write_into(buf)?;
(location.z as f32).write_into(buf)?;
hand.write_into(buf)?;
2u32.azalea_write_var(buf)?;
(location.x as f32).azalea_write(buf)?;
(location.y as f32).azalea_write(buf)?;
(location.z as f32).azalea_write(buf)?;
hand.azalea_write(buf)?;
}
}
Ok(())
@ -50,19 +50,19 @@ impl McBufWritable for ActionType {
}
impl McBufReadable for ActionType {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let action_type = u32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let action_type = u32::azalea_read_var(buf)?;
match action_type {
0 => {
let hand = InteractionHand::read_from(buf)?;
let hand = InteractionHand::azalea_read(buf)?;
Ok(ActionType::Interact { hand })
}
1 => Ok(ActionType::Attack),
2 => {
let x = f32::read_from(buf)?;
let y = f32::read_from(buf)?;
let z = f32::read_from(buf)?;
let hand = InteractionHand::read_from(buf)?;
let x = f32::azalea_read(buf)?;
let y = f32::azalea_read(buf)?;
let z = f32::azalea_read(buf)?;
let hand = InteractionHand::azalea_read(buf)?;
Ok(ActionType::InteractAt {
location: Vec3 {
x: f64::from(x),

View file

@ -12,8 +12,8 @@ pub struct ServerboundPlayerAbilities {
}
impl McBufReadable for ServerboundPlayerAbilities {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<2>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<2>::azalea_read(buf)?;
Ok(Self {
is_flying: set.index(1),
})
@ -21,11 +21,11 @@ impl McBufReadable for ServerboundPlayerAbilities {
}
impl McBufWritable for ServerboundPlayerAbilities {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<2>::new();
if self.is_flying {
set.set(1);
}
set.write_into(buf)
set.azalea_write(buf)
}
}

View file

@ -17,8 +17,8 @@ pub struct ServerboundPlayerInput {
}
impl McBufReadable for ServerboundPlayerInput {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<7>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<7>::azalea_read(buf)?;
Ok(Self {
forward: set.index(0),
backward: set.index(1),
@ -32,7 +32,7 @@ impl McBufReadable for ServerboundPlayerInput {
}
impl McBufWritable for ServerboundPlayerInput {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<7>::new();
if self.forward {
set.set(0);
@ -55,6 +55,6 @@ impl McBufWritable for ServerboundPlayerInput {
if self.sprint {
set.set(6);
}
set.write_into(buf)
set.azalea_write(buf)
}
}

View file

@ -19,10 +19,10 @@ pub enum Action {
}
impl McBufReadable for ServerboundSeenAdvancements {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let action = Action::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let action = Action::azalea_read(buf)?;
let tab = if action == Action::OpenedTab {
Some(ResourceLocation::read_from(buf)?)
Some(ResourceLocation::azalea_read(buf)?)
} else {
None
};
@ -31,10 +31,10 @@ impl McBufReadable for ServerboundSeenAdvancements {
}
impl McBufWritable for ServerboundSeenAdvancements {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
self.action.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
self.action.azalea_write(buf)?;
if let Some(tab) = &self.tab {
tab.write_into(buf)?;
tab.azalea_write(buf)?;
}
Ok(())
}

View file

@ -25,12 +25,12 @@ pub enum Mode {
}
impl McBufReadable for ServerboundSetCommandBlock {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let pos = BlockPos::read_from(buf)?;
let command = String::read_from(buf)?;
let mode = Mode::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let pos = BlockPos::azalea_read(buf)?;
let command = String::azalea_read(buf)?;
let mode = Mode::azalea_read(buf)?;
let set = FixedBitSet::<3>::read_from(buf)?;
let set = FixedBitSet::<3>::azalea_read(buf)?;
Ok(Self {
pos,
command,
@ -43,10 +43,10 @@ impl McBufReadable for ServerboundSetCommandBlock {
}
impl McBufWritable for ServerboundSetCommandBlock {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
self.pos.write_into(buf)?;
self.command.write_into(buf)?;
self.mode.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
self.pos.azalea_write(buf)?;
self.command.azalea_write(buf)?;
self.mode.azalea_write(buf)?;
let mut set = FixedBitSet::<3>::new();
if self.track_output {
@ -58,6 +58,6 @@ impl McBufWritable for ServerboundSetCommandBlock {
if self.automatic {
set.set(2);
}
set.write_into(buf)
set.azalea_write(buf)
}
}

View file

@ -30,8 +30,8 @@ pub enum JointType {
}
impl McBufReadable for JointType {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let name = String::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let name = String::azalea_read(buf)?;
match name.as_str() {
"rollable" => Ok(JointType::Rollable),
"aligned" => Ok(JointType::Aligned),
@ -41,10 +41,10 @@ impl McBufReadable for JointType {
}
impl McBufWritable for JointType {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
JointType::Rollable => "rollable".to_string().write_into(buf)?,
JointType::Aligned => "aligned".to_string().write_into(buf)?,
JointType::Rollable => "rollable".to_string().azalea_write(buf)?,
JointType::Aligned => "aligned".to_string().azalea_write(buf)?,
};
Ok(())
}

View file

@ -70,8 +70,8 @@ pub struct Flags {
}
impl McBufReadable for Flags {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<3>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = FixedBitSet::<3>::azalea_read(buf)?;
Ok(Self {
ignore_entities: set.index(0),
show_air: set.index(1),
@ -81,7 +81,7 @@ impl McBufReadable for Flags {
}
impl McBufWritable for Flags {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<3>::new();
if self.ignore_entities {
set.set(0);
@ -92,7 +92,7 @@ impl McBufWritable for Flags {
if self.show_bounding_box {
set.set(2);
}
set.write_into(buf)?;
set.azalea_write(buf)?;
Ok(())
}
}

View file

@ -32,34 +32,34 @@ pub struct BlockHit {
}
impl McBufWritable for BlockHit {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.block_pos.write_into(buf)?;
self.direction.write_into(buf)?;
f32::write_into(
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.block_pos.azalea_write(buf)?;
self.direction.azalea_write(buf)?;
f32::azalea_write(
&((self.location.x - f64::from(self.block_pos.x)) as f32),
buf,
)?;
f32::write_into(
f32::azalea_write(
&((self.location.y - f64::from(self.block_pos.y)) as f32),
buf,
)?;
f32::write_into(
f32::azalea_write(
&((self.location.z - f64::from(self.block_pos.z)) as f32),
buf,
)?;
self.inside.write_into(buf)?;
self.inside.azalea_write(buf)?;
Ok(())
}
}
impl McBufReadable for BlockHit {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let block_pos = BlockPos::read_from(buf)?;
let direction = Direction::read_from(buf)?;
let cursor_x = f32::read_from(buf)?;
let cursor_y = f32::read_from(buf)?;
let cursor_z = f32::read_from(buf)?;
let inside = bool::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let block_pos = BlockPos::azalea_read(buf)?;
let direction = Direction::azalea_read(buf)?;
let cursor_x = f32::azalea_read(buf)?;
let cursor_y = f32::azalea_read(buf)?;
let cursor_z = f32::azalea_read(buf)?;
let inside = bool::azalea_read(buf)?;
Ok(Self {
block_pos,
direction,

View file

@ -11,8 +11,8 @@ pub struct ClientboundLoginDisconnect {
}
impl McBufReadable for ClientboundLoginDisconnect {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<ClientboundLoginDisconnect, BufReadError> {
let disconnect_string = String::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<ClientboundLoginDisconnect, BufReadError> {
let disconnect_string = String::azalea_read(buf)?;
let disconnect_json: serde_json::Value = serde_json::from_str(disconnect_string.as_str())?;
Ok(ClientboundLoginDisconnect {
@ -22,11 +22,11 @@ impl McBufReadable for ClientboundLoginDisconnect {
}
impl McBufWritable for ClientboundLoginDisconnect {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let status_string = FormattedText::serialize(&self.reason, serde_json::value::Serializer)
.unwrap()
.to_string();
status_string.write_into(buf)?;
status_string.azalea_write(buf)?;
Ok(())
}
}

View file

@ -23,8 +23,8 @@ mod tests {
profile_id: Uuid::nil(),
};
let mut buf: Vec<u8> = Vec::new();
packet.write_into(&mut buf).unwrap();
let packet2 = ServerboundHello::read_from(&mut Cursor::new(&buf)).unwrap();
packet.azalea_write(&mut buf).unwrap();
let packet2 = ServerboundHello::azalea_read(&mut Cursor::new(&buf)).unwrap();
assert_eq!(packet, packet2);
}
}

View file

@ -82,15 +82,15 @@ impl From<ClientIntention> for ConnectionProtocol {
}
impl azalea_buf::McBufReadable for ClientIntention {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = i32::var_read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = i32::azalea_read_var(buf)?;
id.try_into()
.map_err(|_| BufReadError::UnexpectedEnumVariant { id })
}
}
impl McBufWritable for ClientIntention {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(*self as i32).var_write_into(buf)
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(*self as i32).azalea_write_var(buf)
}
}

View file

@ -42,8 +42,8 @@ pub struct ClientboundStatusResponse {
}
impl McBufReadable for ClientboundStatusResponse {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<ClientboundStatusResponse, BufReadError> {
let status_string = String::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<ClientboundStatusResponse, BufReadError> {
let status_string = String::azalea_read(buf)?;
let status_json: serde_json::Value = serde_json::from_str(status_string.as_str())?;
Ok(ClientboundStatusResponse::deserialize(status_json)?)
@ -51,11 +51,11 @@ impl McBufReadable for ClientboundStatusResponse {
}
impl McBufWritable for ClientboundStatusResponse {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let status_string = ClientboundStatusResponse::serialize(self, Serializer)
.unwrap()
.to_string();
status_string.write_into(buf)?;
status_string.azalea_write(buf)?;
Ok(())
}
}

View file

@ -86,7 +86,7 @@ fn parse_frame(buffer: &mut BytesMut) -> Result<BytesMut, FrameSplitterError> {
// the packet is all good we read it fully
let mut buffer_copy = Cursor::new(&buffer[..]);
// Packet Length
let length = match u32::var_read_from(&mut buffer_copy) {
let length = match u32::azalea_read_var(&mut buffer_copy) {
Ok(length) => length as usize,
Err(err) => match err {
BufReadError::Io { source } => return Err(FrameSplitterError::Io { source }),
@ -134,7 +134,7 @@ pub fn deserialize_packet<P: ProtocolPacket + Debug>(
) -> Result<P, Box<ReadPacketError>> {
// Packet ID
let packet_id =
u32::var_read_from(stream).map_err(|e| ReadPacketError::ReadPacketId { source: e })?;
u32::azalea_read_var(stream).map_err(|e| ReadPacketError::ReadPacketId { source: e })?;
P::read(packet_id, stream)
}
@ -171,7 +171,7 @@ pub fn compression_decoder(
compression_threshold: u32,
) -> Result<Vec<u8>, DecompressionError> {
// Data Length
let n = u32::var_read_from(stream)?;
let n = u32::azalea_read_var(stream)?;
if n == 0 {
// no data size, no compression
let mut buf = vec![];

View file

@ -14,7 +14,7 @@ use crate::{packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH};
/// Prepend the length of the packet to it.
fn frame_prepender(mut data: Vec<u8>) -> Result<Vec<u8>, std::io::Error> {
let mut buf = Vec::new();
(data.len() as u32).var_write_into(&mut buf)?;
(data.len() as u32).azalea_write_var(&mut buf)?;
buf.append(&mut data);
Ok(buf)
}
@ -35,7 +35,7 @@ pub fn serialize_packet<P: ProtocolPacket + Debug>(
packet: &P,
) -> Result<Vec<u8>, PacketEncodeError> {
let mut buf = Vec::new();
packet.id().var_write_into(&mut buf)?;
packet.id().azalea_write_var(&mut buf)?;
packet.write(&mut buf)?;
if buf.len() > MAXIMUM_UNCOMPRESSED_LENGTH as usize {
return Err(PacketEncodeError::TooBig {
@ -61,7 +61,7 @@ pub fn compression_encoder(
// if it's less than the compression threshold, don't compress
if n < compression_threshold as usize {
let mut buf = Vec::new();
0.var_write_into(&mut buf)?;
0.azalea_write_var(&mut buf)?;
std::io::Write::write_all(&mut buf, data)?;
Ok(buf)
} else {
@ -73,7 +73,7 @@ pub fn compression_encoder(
// prepend the length
let mut len_prepended_compressed_data = Vec::new();
(data.len() as u32).var_write_into(&mut len_prepended_compressed_data)?;
(data.len() as u32).azalea_write_var(&mut len_prepended_compressed_data)?;
len_prepended_compressed_data.append(&mut compressed_data);
Ok(len_prepended_compressed_data)

View file

@ -29,8 +29,8 @@ where
pub struct OptionalRegistry<T: Registry>(Option<T>);
impl<T: Registry> McBufReadable for OptionalRegistry<T> {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(OptionalRegistry(match u32::var_read_from(buf)? {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(OptionalRegistry(match u32::azalea_read_var(buf)? {
0 => None,
value => Some(
T::from_u32(value - 1)
@ -40,10 +40,10 @@ impl<T: Registry> McBufReadable for OptionalRegistry<T> {
}
}
impl<T: Registry> McBufWritable for OptionalRegistry<T> {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match &self.0 {
None => 0u32.var_write_into(buf),
Some(value) => (value.to_u32() + 1).var_write_into(buf),
None => 0u32.azalea_write_var(buf),
Some(value) => (value.to_u32() + 1).azalea_write_var(buf),
}
}
}
@ -56,25 +56,25 @@ pub enum CustomRegistry<D: Registry, C: McBufReadable + McBufWritable> {
}
impl<D: Registry, C: McBufReadable + McBufWritable> McBufReadable for CustomRegistry<D, C> {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let direct_registry = OptionalRegistry::<D>::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let direct_registry = OptionalRegistry::<D>::azalea_read(buf)?;
if let Some(direct_registry) = direct_registry.0 {
return Ok(CustomRegistry::Direct(direct_registry));
}
Ok(CustomRegistry::Custom(C::read_from(buf)?))
Ok(CustomRegistry::Custom(C::azalea_read(buf)?))
}
}
impl<D: Registry, C: McBufReadable + McBufWritable> McBufWritable for CustomRegistry<D, C> {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
CustomRegistry::Direct(direct_registry) => {
// write the id + 1
(direct_registry.to_u32() + 1).var_write_into(buf)
(direct_registry.to_u32() + 1).azalea_write_var(buf)
}
CustomRegistry::Custom(custom_registry) => {
// write 0, then the custom registry
0u32.var_write_into(buf)?;
custom_registry.write_into(buf)
0u32.azalea_write_var(buf)?;
custom_registry.azalea_write(buf)
}
}
}
@ -94,10 +94,10 @@ pub enum HolderSet<D: Registry, ResourceLocation: McBufReadable + McBufWritable>
impl<D: Registry, ResourceLocation: McBufReadable + McBufWritable> McBufReadable
for HolderSet<D, ResourceLocation>
{
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let size = i32::var_read_from(buf)? - 1;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let size = i32::azalea_read_var(buf)? - 1;
if size == -1 {
let key = ResourceLocation::read_from(buf)?;
let key = ResourceLocation::azalea_read(buf)?;
Ok(Self::Named {
key,
contents: Vec::new(),
@ -105,7 +105,7 @@ impl<D: Registry, ResourceLocation: McBufReadable + McBufWritable> McBufReadable
} else {
let mut contents = Vec::new();
for _ in 0..size {
contents.push(D::read_from(buf)?);
contents.push(D::azalea_read(buf)?);
}
Ok(Self::Direct { contents })
}
@ -114,17 +114,17 @@ impl<D: Registry, ResourceLocation: McBufReadable + McBufWritable> McBufReadable
impl<D: Registry, ResourceLocation: McBufReadable + McBufWritable> McBufWritable
for HolderSet<D, ResourceLocation>
{
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
Self::Direct { contents } => {
(contents.len() as i32 + 1).var_write_into(buf)?;
(contents.len() as i32 + 1).azalea_write_var(buf)?;
for item in contents {
item.write_into(buf)?;
item.azalea_write(buf)?;
}
}
Self::Named { key, .. } => {
0i32.var_write_into(buf)?;
key.write_into(buf)?;
0i32.azalea_write_var(buf)?;
key.azalea_write(buf)?;
}
}
Ok(())

View file

@ -332,7 +332,7 @@ impl Chunk {
let section_count = dimension_height / SECTION_HEIGHT;
let mut sections = Vec::with_capacity(section_count as usize);
for _ in 0..section_count {
let section = Section::read_from(buf)?;
let section = Section::azalea_read(buf)?;
sections.push(section);
}
@ -417,9 +417,9 @@ pub fn get_block_state_from_sections(
}
impl McBufWritable for Chunk {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
for section in &self.sections {
section.write_into(buf)?;
section.azalea_write(buf)?;
}
Ok(())
}
@ -438,8 +438,8 @@ impl Debug for PartialChunkStorage {
}
impl McBufReadable for Section {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let block_count = u16::read_from(buf)?;
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let block_count = u16::azalea_read(buf)?;
// this is commented out because the vanilla server is wrong
// assert!(
@ -468,10 +468,10 @@ impl McBufReadable for Section {
}
impl McBufWritable for Section {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.block_count.write_into(buf)?;
self.states.write_into(buf)?;
self.biomes.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.block_count.azalea_write(buf)?;
self.states.azalea_write(buf)?;
self.biomes.azalea_write(buf)?;
Ok(())
}
}

View file

@ -44,11 +44,11 @@ impl PalettedContainer {
buf: &mut Cursor<&[u8]>,
container_type: &'static PalettedContainerKind,
) -> Result<Self, BufReadError> {
let server_bits_per_entry = u8::read_from(buf)?;
let server_bits_per_entry = u8::azalea_read(buf)?;
let palette_type = PaletteKind::from_bits_and_type(server_bits_per_entry, container_type);
let palette = palette_type.read(buf)?;
let size = container_type.size();
let data = Vec::<u64>::read_from(buf)?;
let data = Vec::<u64>::azalea_read(buf)?;
// we can only trust the bits per entry that we're sent if there's enough data
// that it'd be global. if it's not global, then we have to calculate it
@ -225,10 +225,10 @@ impl PalettedContainer {
}
impl McBufWritable for PalettedContainer {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.bits_per_entry.write_into(buf)?;
self.palette.write_into(buf)?;
self.storage.data.write_into(buf)?;
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.bits_per_entry.azalea_write(buf)?;
self.palette.azalea_write(buf)?;
self.storage.data.azalea_write(buf)?;
Ok(())
}
}
@ -265,16 +265,16 @@ impl Palette {
}
impl McBufWritable for Palette {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
Palette::SingleValue(value) => {
value.var_write_into(buf)?;
value.azalea_write_var(buf)?;
}
Palette::Linear(values) => {
values.var_write_into(buf)?;
values.azalea_write_var(buf)?;
}
Palette::Hashmap(values) => {
values.var_write_into(buf)?;
values.azalea_write_var(buf)?;
}
Palette::Global => {}
}
@ -301,9 +301,9 @@ impl PaletteKind {
pub fn read(&self, buf: &mut Cursor<&[u8]>) -> Result<Palette, BufReadError> {
Ok(match self {
PaletteKind::SingleValue => Palette::SingleValue(u32::var_read_from(buf)?),
PaletteKind::Linear => Palette::Linear(Vec::<u32>::var_read_from(buf)?),
PaletteKind::Hashmap => Palette::Hashmap(Vec::<u32>::var_read_from(buf)?),
PaletteKind::SingleValue => Palette::SingleValue(u32::azalea_read_var(buf)?),
PaletteKind::Linear => Palette::Linear(Vec::<u32>::azalea_read_var(buf)?),
PaletteKind::Hashmap => Palette::Hashmap(Vec::<u32>::azalea_read_var(buf)?),
PaletteKind::Global => Palette::Global,
})
}

View file

@ -144,7 +144,7 @@ def add_variant(variant: str):
raise ValueError('Couldn\'t find end of match')
code = code[:last_line_in_match] + [
f' DataComponentKind::{variant} => Box::new({variant}::read_from(buf)?),',
f' DataComponentKind::{variant} => Box::new({variant}::azalea_read(buf)?),',
] + code[last_line_in_match:]
# now insert the struct