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

switch things to use azalea-buf

This commit is contained in:
mat 2022-06-23 15:28:33 -05:00
parent 5ca49e680e
commit ba399c4440
10 changed files with 32 additions and 18 deletions

View file

@ -1,6 +1,6 @@
[package]
edition = "2021"
name = "azalea-buf"
name = "azalea_buf"
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -21,11 +21,11 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
syn::Type::Path(_) => {
if f.attrs.iter().any(|a| a.path.is_ident("var")) {
quote! {
let #field_name = crate::McBufVarReadable::var_read_into(buf)?;
let #field_name = azalea_buf::McBufVarReadable::var_read_into(buf)?;
}
} else {
quote! {
let #field_name = crate::McBufReadable::read_into(buf)?;
let #field_name = azalea_buf::McBufReadable::read_into(buf)?;
}
}
}
@ -40,7 +40,7 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
let read_field_names = named.iter().map(|f| &f.ident).collect::<Vec<_>>();
quote! {
impl crate::McBufReadable for #ident {
impl azalea_buf::McBufReadable for #ident {
fn read_into(buf: &mut impl std::io::Read) -> Result<Self, String> {
#(#read_fields)*
Ok(#ident {
@ -75,10 +75,10 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
}
quote! {
impl crate::McBufReadable for #ident {
impl azalea_buf::McBufReadable for #ident {
fn read_into(buf: &mut impl std::io::Read) -> Result<Self, String>
{
let id = crate::McBufVarReadable::var_read_into(buf)?;
let id = azalea_buf::McBufVarReadable::var_read_into(buf)?;
match id {
#match_contents
_ => Err(format!("Unknown enum variant {}", id)),
@ -110,11 +110,11 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
syn::Type::Path(_) => {
if f.attrs.iter().any(|attr| attr.path.is_ident("var")) {
quote! {
crate::McBufVarWritable::var_write_into(&self.#field_name, buf)?;
azalea_buf::McBufVarWritable::var_write_into(&self.#field_name, buf)?;
}
} else {
quote! {
crate::McBufWritable::write_into(&self.#field_name, buf)?;
azalea_buf::McBufWritable::write_into(&self.#field_name, buf)?;
}
}
}
@ -128,7 +128,7 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
.collect::<Vec<_>>();
quote! {
impl crate::McBufWritable for #ident {
impl azalea_buf::McBufWritable for #ident {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
#(#write_fields)*
Ok(())
@ -138,9 +138,9 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
}
syn::Data::Enum(syn::DataEnum { .. }) => {
quote! {
impl crate::McBufWritable for #ident {
impl azalea_buf::McBufWritable for #ident {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
crate::Writable::write_varint(buf, *self as i32)
azalea_buf::Writable::write_varint(buf, *self as i32)
}
}
}

View file

@ -7,6 +7,7 @@ mod definitions;
mod read;
mod write;
pub use buf_macros::*;
pub use definitions::*;
pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable};
pub use write::{McBufVarWritable, McBufWritable, Writable};
@ -18,7 +19,7 @@ const MAX_STRING_LENGTH: u16 = 32767;
#[cfg(test)]
mod tests {
use super::*;
use std::{collections::HashMap, io::Cursor};
use std::io::Cursor;
#[test]
fn test_write_varint() {

View file

@ -1,3 +1,6 @@
use std::io::{Read, Write};
use azalea_buf::{McBufReadable, McBufWritable};
use serde::{de, Deserialize, Deserializer};
use crate::{
@ -267,7 +270,7 @@ impl<'de> Deserialize<'de> for Component {
impl McBufReadable for Component {
fn read_into(buf: &mut impl Read) -> Result<Self, String> {
let string = buf.read_utf()?;
let string = String::read_into(buf)?;
let json: serde_json::Value = serde_json::from_str(string.as_str())
.map_err(|_| "Component isn't valid JSON".to_string())?;
let component = Component::deserialize(json).map_err(|e| e.to_string())?;

View file

@ -1,3 +1,5 @@
pub use azalea_buf::McBuf;
/// Only works for up to 8 blocks
#[derive(Clone, Debug, McBuf)]
pub struct PositionDelta {

View file

@ -1,3 +1,5 @@
use azalea_buf::{McBufReadable, McBufWritable};
use std::io::{Read, Write};
#[derive(Debug, Clone, McBuf)]
pub struct Particle {

View file

@ -1,3 +1,6 @@
use azalea_buf::{McBufReadable, McBufWritable};
use std::io::{Read, Write};
#[derive(Debug, Clone)]
pub struct SaltSignaturePair {
pub salt: u64,

View file

@ -1,5 +1,6 @@
use crate::Error;
use crate::Tag;
use azalea_buf::McBufReadable;
use byteorder::{ReadBytesExt, BE};
use flate2::read::{GzDecoder, ZlibDecoder};
use std::collections::HashMap;
@ -139,7 +140,7 @@ impl Tag {
impl McBufReadable for Tag {
fn read_into(buf: &mut impl Read) -> Result<Self, String> {
match Tag::read(self) {
match Tag::read(buf) {
Ok(r) => Ok(r),
// Err(e) => Err(e.to_string()),
Err(e) => Err(e.to_string()).unwrap(),

View file

@ -1,5 +1,6 @@
use crate::Error;
use crate::Tag;
use azalea_buf::McBufWritable;
use byteorder::{WriteBytesExt, BE};
use flate2::write::{GzEncoder, ZlibEncoder};
use std::collections::HashMap;

View file

@ -9,23 +9,24 @@ pub use tag::Tag;
#[cfg(test)]
mod tests {
use super::*;
use azalea_buf::{McBufReadable, McBufWritable};
use std::{collections::HashMap, io::Cursor};
#[test]
fn mcbuf_nbt() {
let mut buf = Vec::new();
buf.write_nbt(&Tag::Compound(HashMap::from_iter(vec![(
let tag = Tag::Compound(HashMap::from_iter(vec![(
"hello world".to_string(),
Tag::Compound(HashMap::from_iter(vec![(
"name".to_string(),
Tag::String("Bananrama".to_string()),
)])),
)])))
.unwrap();
)]));
tag.write_into(&mut buf).unwrap();
let mut buf = Cursor::new(buf);
let result = buf.read_nbt().unwrap();
let result = Tag::read_into(&mut buf).unwrap();
assert_eq!(
result,
Tag::Compound(HashMap::from_iter(vec![(