From 248f752748a0033db7f8242ee0ecd73ea8ce8ec9 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 04:33:58 +0000 Subject: simplify error handling --- azalea-nbt/src/encode.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 azalea-nbt/src/encode.rs (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs old mode 100644 new mode 100755 -- cgit v1.2.3 From 39d77475d383cea2aaee5deaf7235306b64b6d1d Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 04:37:45 +0000 Subject: remove some useless code --- azalea-nbt/src/encode.rs | 68 +++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 47 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 9ce4faf4..51c5968c 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -6,12 +6,8 @@ use std::io::Write; #[inline] fn write_string(writer: &mut dyn Write, string: &str) -> Result<(), Error> { - writer - .write_i16::(string.len() as i16) - .map_err(|_| Error::WriteError)?; - writer - .write_all(string.as_bytes()) - .map_err(|_| Error::WriteError)?; + writer.write_i16::(string.len() as i16)?; + writer.write_all(string.as_bytes())?; Ok(()) } @@ -20,28 +16,16 @@ impl Tag { pub fn write_without_end(&self, writer: &mut dyn Write) -> Result<(), Error> { match self { Tag::End => {} - Tag::Byte(value) => writer.write_i8(*value).map_err(|_| Error::WriteError)?, - Tag::Short(value) => writer - .write_i16::(*value) - .map_err(|_| Error::WriteError)?, - Tag::Int(value) => writer - .write_i32::(*value) - .map_err(|_| Error::WriteError)?, - Tag::Long(value) => writer - .write_i64::(*value) - .map_err(|_| Error::WriteError)?, - Tag::Float(value) => writer - .write_f32::(*value) - .map_err(|_| Error::WriteError)?, - Tag::Double(value) => writer - .write_f64::(*value) - .map_err(|_| Error::WriteError)?, + Tag::Byte(value) => writer.write_i8(*value)?, + Tag::Short(value) => writer.write_i16::(*value)?, + Tag::Int(value) => writer.write_i32::(*value)?, + Tag::Long(value) => writer.write_i64::(*value)?, + Tag::Float(value) => writer.write_f32::(*value)?, + Tag::Double(value) => writer.write_f64::(*value)?, Tag::ByteArray(value) => { - writer - .write_i32::(value.len() as i32) - .map_err(|_| Error::WriteError)?; + writer.write_i32::(value.len() as i32)?; for &byte in value { - writer.write_i8(byte).map_err(|_| Error::WriteError)?; + writer.write_i8(byte)?; } } Tag::String(value) => { @@ -50,14 +34,12 @@ impl Tag { Tag::List(value) => { // we just get the type from the first item, or default the type to END if value.is_empty() { - writer.write_i8(0).map_err(|_| Error::WriteError)?; - writer.write_i32::(0).map_err(|_| Error::WriteError)?; + writer.write_i8(0)?; + writer.write_i32::(0)?; } else { let type_id = value[0].id(); - writer.write_u8(type_id).map_err(|_| Error::WriteError)?; - writer - .write_i32::(value.len() as i32) - .map_err(|_| Error::WriteError)?; + writer.write_u8(type_id)?; + writer.write_i32::(value.len() as i32)?; for tag in value { tag.write_without_end(writer)?; } @@ -65,30 +47,22 @@ impl Tag { } Tag::Compound(value) => { for (key, tag) in value { - writer.write_u8(tag.id()).map_err(|_| Error::WriteError)?; + writer.write_u8(tag.id())?; write_string(writer, key)?; tag.write_without_end(writer)?; } - writer - .write_u8(Tag::End.id()) - .map_err(|_| Error::WriteError)?; + writer.write_u8(Tag::End.id())?; } Tag::IntArray(value) => { - writer - .write_i32::(value.len() as i32) - .map_err(|_| Error::WriteError)?; + writer.write_i32::(value.len() as i32)?; for &int in value { - writer.write_i32::(int).map_err(|_| Error::WriteError)?; + writer.write_i32::(int)?; } } Tag::LongArray(value) => { - writer - .write_i32::(value.len() as i32) - .map_err(|_| Error::WriteError)?; + writer.write_i32::(value.len() as i32)?; for &long in value { - writer - .write_i64::(long) - .map_err(|_| Error::WriteError)?; + writer.write_i64::(long)?; } } } @@ -100,7 +74,7 @@ impl Tag { match self { Tag::Compound(value) => { for (key, tag) in value { - writer.write_u8(tag.id()).map_err(|_| Error::WriteError)?; + writer.write_u8(tag.id())?; write_string(writer, key)?; tag.write_without_end(writer)?; } -- cgit v1.2.3 From 1bbd8b99b39a9c75f9d902d50852d91b3d04add9 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 05:02:45 +0000 Subject: nbt optimizations --- azalea-nbt/src/encode.rs | 63 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 14 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 51c5968c..ebd1070f 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -2,6 +2,7 @@ use crate::Error; use crate::Tag; use byteorder::{WriteBytesExt, BE}; use flate2::write::{GzEncoder, ZlibEncoder}; +use std::collections::HashMap; use std::io::Write; #[inline] @@ -11,8 +12,19 @@ fn write_string(writer: &mut dyn Write, string: &str) -> Result<(), Error> { Ok(()) } +#[inline] +fn write_compound(writer: &mut dyn Write, value: &HashMap) -> Result<(), Error> { + for (key, tag) in value { + writer.write_u8(tag.id())?; + write_string(writer, key)?; + tag.write_without_end(writer)?; + } + writer.write_u8(Tag::End.id())?; + Ok(()) +} impl Tag { + #[inline] pub fn write_without_end(&self, writer: &mut dyn Write) -> Result<(), Error> { match self { Tag::End => {} @@ -34,25 +46,48 @@ impl Tag { Tag::List(value) => { // we just get the type from the first item, or default the type to END if value.is_empty() { - writer.write_i8(0)?; - writer.write_i32::(0)?; + writer.write_all(&[0; 5])?; } else { - let type_id = value[0].id(); - writer.write_u8(type_id)?; + let first_tag = &value[0]; + writer.write_u8(first_tag.id())?; writer.write_i32::(value.len() as i32)?; - for tag in value { - tag.write_without_end(writer)?; + match first_tag { + Self::Int(_) => { + for i in value { + if let Tag::Int(v) = i { + writer.write_i32::(*v)? + } else { + panic!("List of Ints should only contain Ints") + } + } + } + Self::String(_) => { + for i in value { + if let Tag::String(v) = i { + write_string(writer, v)?; + } else { + panic!("List of Strings should only contain Strings") + } + } + } + &Self::Compound(_) => { + for i in value { + if let Tag::Compound(v) = i { + write_compound(writer, v)?; + } else { + panic!("List of Compounds should only contain Compounds") + } + } + } + _ => { + for tag in value { + tag.write_without_end(writer)?; + } + } } } } - Tag::Compound(value) => { - for (key, tag) in value { - writer.write_u8(tag.id())?; - write_string(writer, key)?; - tag.write_without_end(writer)?; - } - writer.write_u8(Tag::End.id())?; - } + Tag::Compound(value) => write_compound(writer, value)?, Tag::IntArray(value) => { writer.write_i32::(value.len() as i32)?; for &int in value { -- cgit v1.2.3 From 99652200aa2840e9a3075f238d9eb001aadb7e80 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 18:03:57 +0000 Subject: simplify nbt list optimization --- azalea-nbt/README.md | 5 +---- azalea-nbt/benches/my_benchmark.rs | 17 +++++++++-------- azalea-nbt/src/encode.rs | 28 +++++++++++++--------------- azalea-nbt/src/tag.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 27 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/README.md b/azalea-nbt/README.md index 0573bce2..19498cf3 100755 --- a/azalea-nbt/README.md +++ b/azalea-nbt/README.md @@ -1,6 +1,3 @@ # Azalea NBT -Deserialize Minecraft NBT. This is somewhat based on [Hermatite NBT](https://github.com/PistonDevelopers/hematite_nbt). - - - +A fast NBT serializer and deserializer. diff --git a/azalea-nbt/benches/my_benchmark.rs b/azalea-nbt/benches/my_benchmark.rs index 51e574e3..528475d3 100755 --- a/azalea-nbt/benches/my_benchmark.rs +++ b/azalea-nbt/benches/my_benchmark.rs @@ -25,17 +25,18 @@ fn bench_serialize(filename: &str, c: &mut Criterion) { .block_on(async { Tag::read(&mut decoded_src_stream).await.unwrap() }); let mut group = c.benchmark_group(filename); + group.sample_size(1000); group.throughput(Throughput::Bytes(decoded_src.len() as u64)); - group.bench_function("Decode", |b| { - b.to_async(tokio::runtime::Runtime::new().unwrap()) - .iter(|| async { - let mut owned_decoded_src_stream = decoded_src_stream.clone(); - owned_decoded_src_stream.seek(SeekFrom::Start(0)).unwrap(); - Tag::read(&mut owned_decoded_src_stream).await.unwrap(); - }) - }); + // group.bench_function("Decode", |b| { + // b.to_async(tokio::runtime::Runtime::new().unwrap()) + // .iter(|| async { + // let mut owned_decoded_src_stream = decoded_src_stream.clone(); + // owned_decoded_src_stream.seek(SeekFrom::Start(0)).unwrap(); + // Tag::read(&mut owned_decoded_src_stream).await.unwrap(); + // }) + // }); group.bench_function("Encode", |b| { b.iter(|| { diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index ebd1070f..ef72dce2 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -54,29 +54,27 @@ impl Tag { match first_tag { Self::Int(_) => { for i in value { - if let Tag::Int(v) = i { - writer.write_i32::(*v)? - } else { - panic!("List of Ints should only contain Ints") - } + writer.write_i32::( + *i.as_int().expect("List of Int should only contains Int"), + )?; } } Self::String(_) => { for i in value { - if let Tag::String(v) = i { - write_string(writer, v)?; - } else { - panic!("List of Strings should only contain Strings") - } + write_string( + writer, + i.as_string() + .expect("List of String should only contain String"), + )?; } } &Self::Compound(_) => { for i in value { - if let Tag::Compound(v) = i { - write_compound(writer, v)?; - } else { - panic!("List of Compounds should only contain Compounds") - } + write_compound( + writer, + i.as_compound() + .expect("List of Compound should only contain Compound"), + )?; } } _ => { diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs index f11b8889..705dff6a 100755 --- a/azalea-nbt/src/tag.rs +++ b/azalea-nbt/src/tag.rs @@ -36,4 +36,31 @@ impl Tag { Tag::LongArray(_) => 12, } } + + #[inline] + pub fn as_int(&self) -> Option<&i32> { + if let Tag::Int(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_string(&self) -> Option<&str> { + if let Tag::String(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_compound(&self) -> Option<&HashMap> { + if let Tag::Compound(v) = self { + Some(v) + } else { + None + } + } } -- cgit v1.2.3 From 3057ae8b4ad616258f9f4fd777d85fb20a6eef9c Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 20:49:42 -0500 Subject: maybe optimization --- azalea-nbt/benches/my_benchmark.rs | 27 +++++++++++++-------------- azalea-nbt/src/encode.rs | 5 ++++- 2 files changed, 17 insertions(+), 15 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/benches/my_benchmark.rs b/azalea-nbt/benches/my_benchmark.rs index 00d12dc8..fcb44f90 100755 --- a/azalea-nbt/benches/my_benchmark.rs +++ b/azalea-nbt/benches/my_benchmark.rs @@ -25,24 +25,23 @@ fn bench_serialize(filename: &str, c: &mut Criterion) { .block_on(async { Tag::read(&mut decoded_src_stream).await.unwrap() }); let mut group = c.benchmark_group(filename); - group.sample_size(1000); group.throughput(Throughput::Bytes(decoded_src.len() as u64)); - group.bench_function("Decode", |b| { - b.to_async(tokio::runtime::Runtime::new().unwrap()) - .iter(|| async { - let mut owned_decoded_src_stream = decoded_src_stream.clone(); - owned_decoded_src_stream.seek(SeekFrom::Start(0)).unwrap(); - Tag::read(&mut owned_decoded_src_stream).await.unwrap(); - }) - }); - - // group.bench_function("Encode", |b| { - // b.iter(|| { - // nbt.write(&mut io::sink()).unwrap(); - // }) + // group.bench_function("Decode", |b| { + // b.to_async(tokio::runtime::Runtime::new().unwrap()) + // .iter(|| async { + // let mut owned_decoded_src_stream = decoded_src_stream.clone(); + // owned_decoded_src_stream.seek(SeekFrom::Start(0)).unwrap(); + // Tag::read(&mut owned_decoded_src_stream).await.unwrap(); + // }) // }); + + group.bench_function("Encode", |b| { + b.iter(|| { + nbt.write(&mut io::sink()).unwrap(); + }) + }); group.finish(); } diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index ef72dce2..d1126ee0 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -54,6 +54,7 @@ impl Tag { match first_tag { Self::Int(_) => { for i in value { + assert!(matches!(i, Tag::Int(_))); writer.write_i32::( *i.as_int().expect("List of Int should only contains Int"), )?; @@ -61,6 +62,7 @@ impl Tag { } Self::String(_) => { for i in value { + assert!(matches!(i, Tag::String(_))); write_string( writer, i.as_string() @@ -68,8 +70,9 @@ impl Tag { )?; } } - &Self::Compound(_) => { + Self::Compound(_) => { for i in value { + assert!(matches!(i, Tag::Compound(_))); write_compound( writer, i.as_compound() -- cgit v1.2.3 From 3a6810ca1dd86d45e5102a839a916e16e6a52130 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 22:38:42 -0500 Subject: vroom vroom --- azalea-nbt/src/encode.rs | 230 +++++++++++++++++++++++++++++++++-------------- azalea-nbt/src/tag.rs | 81 +++++++++++++++++ 2 files changed, 242 insertions(+), 69 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index d1126ee0..27cfe3af 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -12,96 +12,192 @@ fn write_string(writer: &mut dyn Write, string: &str) -> Result<(), Error> { Ok(()) } + +// who needs friends when you've got code that runs in nanoseconds? + #[inline] -fn write_compound(writer: &mut dyn Write, value: &HashMap) -> Result<(), Error> { +fn write_compound( + writer: &mut dyn Write, + value: &HashMap, + end_tag: bool, +) -> Result<(), Error> { for (key, tag) in value { - writer.write_u8(tag.id())?; - write_string(writer, key)?; - tag.write_without_end(writer)?; - } - writer.write_u8(Tag::End.id())?; - Ok(()) -} - -impl Tag { - #[inline] - pub fn write_without_end(&self, writer: &mut dyn Write) -> Result<(), Error> { - match self { + match tag { Tag::End => {} - Tag::Byte(value) => writer.write_i8(*value)?, - Tag::Short(value) => writer.write_i16::(*value)?, - Tag::Int(value) => writer.write_i32::(*value)?, - Tag::Long(value) => writer.write_i64::(*value)?, - Tag::Float(value) => writer.write_f32::(*value)?, - Tag::Double(value) => writer.write_f64::(*value)?, + Tag::Byte(value) => { + writer.write_u8(1)?; + write_string(writer, key)?; + writer.write_i8(*value)? + } + Tag::Short(value) => { + writer.write_u8(2)?; + write_string(writer, key)?; + writer.write_i16::(*value)? + } + Tag::Int(value) => { + writer.write_u8(3)?; + write_string(writer, key)?; + writer.write_i32::(*value)? + } + Tag::Long(value) => { + writer.write_u8(4)?; + write_string(writer, key)?; + writer.write_i64::(*value)? + } + Tag::Float(value) => { + writer.write_u8(5)?; + write_string(writer, key)?; + writer.write_f32::(*value)? + } + Tag::Double(value) => { + writer.write_u8(6)?; + write_string(writer, key)?; + writer.write_f64::(*value)? + } Tag::ByteArray(value) => { + writer.write_u8(7)?; + write_string(writer, key)?; writer.write_i32::(value.len() as i32)?; for &byte in value { writer.write_i8(byte)?; } } Tag::String(value) => { - write_string(writer, value)?; + writer.write_u8(9)?; + write_string(writer, key)?; + write_string(writer, value)? } Tag::List(value) => { - // we just get the type from the first item, or default the type to END - if value.is_empty() { - writer.write_all(&[0; 5])?; - } else { - let first_tag = &value[0]; - writer.write_u8(first_tag.id())?; - writer.write_i32::(value.len() as i32)?; - match first_tag { - Self::Int(_) => { - for i in value { - assert!(matches!(i, Tag::Int(_))); - writer.write_i32::( - *i.as_int().expect("List of Int should only contains Int"), - )?; - } - } - Self::String(_) => { - for i in value { - assert!(matches!(i, Tag::String(_))); - write_string( - writer, - i.as_string() - .expect("List of String should only contain String"), - )?; - } - } - Self::Compound(_) => { - for i in value { - assert!(matches!(i, Tag::Compound(_))); - write_compound( - writer, - i.as_compound() - .expect("List of Compound should only contain Compound"), - )?; - } - } - _ => { - for tag in value { - tag.write_without_end(writer)?; - } - } - } - } + writer.write_u8(10)?; + write_string(writer, key)?; + write_list(writer, value)? + } + Tag::Compound(value) => { + writer.write_u8(11)?; + write_string(writer, key)?; + write_compound(writer, value, true)? } - Tag::Compound(value) => write_compound(writer, value)?, Tag::IntArray(value) => { + writer.write_u8(12)?; + write_string(writer, key)?; writer.write_i32::(value.len() as i32)?; for &int in value { writer.write_i32::(int)?; } } Tag::LongArray(value) => { + writer.write_u8(13)?; + write_string(writer, key)?; writer.write_i32::(value.len() as i32)?; for &long in value { writer.write_i64::(long)?; } } } + } + if end_tag { + writer.write_u8(Tag::End.id())?; + } + return Ok(()); +} + +#[inline] +fn write_list(writer: &mut dyn Write, value: &Vec) -> Result<(), Error> { + // we just get the type from the first item, or default the type to END + if value.is_empty() { + writer.write_all(&[0; 5])?; + } else { + let first_tag = &value[0]; + writer.write_u8(first_tag.id())?; + writer.write_i32::(value.len() as i32)?; + match first_tag { + Tag::Int(_) => { + // assert all items are the same variant + // assert_eq!(value.iter().all(|tag| tag.id() == 1), true); + for i in value { + assert!(matches!(i, Tag::Int(_))); + writer.write_i32::( + *i.as_int().expect("List of Int should only contains Int"), + )?; + } + } + Tag::String(_) => { + for i in value { + assert!(matches!(i, Tag::String(_))); + write_string( + writer, + i.as_string() + .expect("List of String should only contain String"), + )?; + } + } + Tag::Compound(_) => { + for i in value { + assert!(matches!(i, Tag::Compound(_))); + write_compound( + writer, + i.as_compound() + .expect("List of Compound should only contain Compound"), + true, + )?; + } + } + _ => { + for i in value { + i.write_without_end(writer)?; + } + } + } + } + + Ok(()) +} + +#[inline] +fn write_bytearray(writer: &mut dyn Write, value: &Vec) -> Result<(), Error> { + writer.write_i32::(value.len() as i32)?; + for &byte in value { + writer.write_i8(byte)?; + } + Ok(()) +} + +#[inline] +fn write_intarray(writer: &mut dyn Write, value: &Vec) -> Result<(), Error> { + writer.write_i32::(value.len() as i32)?; + for &int in value { + writer.write_i32::(int)?; + } + Ok(()) +} + +#[inline] +fn write_longarray(writer: &mut dyn Write, value: &Vec) -> Result<(), Error> { + writer.write_i32::(value.len() as i32)?; + for &long in value { + writer.write_i64::(long)?; + } + Ok(()) +} + +impl Tag { + #[inline] + pub fn write_without_end(&self, writer: &mut dyn Write) -> Result<(), Error> { + match self { + Tag::End => {} + Tag::Byte(value) => writer.write_i8(*value)?, + Tag::Short(value) => writer.write_i16::(*value)?, + Tag::Int(value) => writer.write_i32::(*value)?, + Tag::Long(value) => writer.write_i64::(*value)?, + Tag::Float(value) => writer.write_f32::(*value)?, + Tag::Double(value) => writer.write_f64::(*value)?, + Tag::ByteArray(value) => write_bytearray(writer, value)?, + Tag::String(value) => write_string(writer, value)?, + Tag::List(value) => write_list(writer, value)?, + Tag::Compound(value) => write_compound(writer, value, true)?, + Tag::IntArray(value) => write_intarray(writer, value)?, + Tag::LongArray(value) => write_longarray(writer, value)?, + } Ok(()) } @@ -109,11 +205,7 @@ impl Tag { pub fn write(&self, writer: &mut impl Write) -> Result<(), Error> { match self { Tag::Compound(value) => { - for (key, tag) in value { - writer.write_u8(tag.id())?; - write_string(writer, key)?; - tag.write_without_end(writer)?; - } + write_compound(writer, value, false)?; Ok(()) } _ => Err(Error::InvalidTag), diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs index 705dff6a..f8960aa6 100755 --- a/azalea-nbt/src/tag.rs +++ b/azalea-nbt/src/tag.rs @@ -37,6 +37,24 @@ impl Tag { } } + #[inline] + pub fn as_byte(&self) -> Option<&i8> { + if let Tag::Byte(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_short(&self) -> Option<&i16> { + if let Tag::Short(v) = self { + Some(v) + } else { + None + } + } + #[inline] pub fn as_int(&self) -> Option<&i32> { if let Tag::Int(v) = self { @@ -46,6 +64,33 @@ impl Tag { } } + #[inline] + pub fn as_long(&self) -> Option<&i64> { + if let Tag::Long(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_float(&self) -> Option<&f32> { + if let Tag::Float(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_double(&self) -> Option<&f64> { + if let Tag::Double(v) = self { + Some(v) + } else { + None + } + } + #[inline] pub fn as_string(&self) -> Option<&str> { if let Tag::String(v) = self { @@ -63,4 +108,40 @@ impl Tag { None } } + + #[inline] + pub fn as_bytearray(&self) -> Option<&Vec> { + if let Tag::ByteArray(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_intarray(&self) -> Option<&Vec> { + if let Tag::IntArray(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_longarray(&self) -> Option<&Vec> { + if let Tag::LongArray(v) = self { + Some(v) + } else { + None + } + } + + #[inline] + pub fn as_list(&self) -> Option<&Vec> { + if let Tag::List(v) = self { + Some(v) + } else { + None + } + } } -- cgit v1.2.3 From 46454793e041dcecb252084b6616bd8f7011b2e1 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sat, 23 Apr 2022 04:14:19 +0000 Subject: clean up some code --- azalea-nbt/src/encode.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 27cfe3af..08d41e89 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -112,39 +112,34 @@ fn write_list(writer: &mut dyn Write, value: &Vec) -> Result<(), Error> { writer.write_i32::(value.len() as i32)?; match first_tag { Tag::Int(_) => { - // assert all items are the same variant - // assert_eq!(value.iter().all(|tag| tag.id() == 1), true); - for i in value { - assert!(matches!(i, Tag::Int(_))); + for tag in value { writer.write_i32::( - *i.as_int().expect("List of Int should only contains Int"), + *tag.as_int().expect("List of Int should only contains Int"), )?; } } Tag::String(_) => { - for i in value { - assert!(matches!(i, Tag::String(_))); + for tag in value { write_string( writer, - i.as_string() + tag.as_string() .expect("List of String should only contain String"), )?; } } Tag::Compound(_) => { - for i in value { - assert!(matches!(i, Tag::Compound(_))); + for tag in value { write_compound( writer, - i.as_compound() + tag.as_compound() .expect("List of Compound should only contain Compound"), true, )?; } } _ => { - for i in value { - i.write_without_end(writer)?; + for tag in value { + tag.write_without_end(writer)?; } } } -- cgit v1.2.3 From 7a272c216bb69e6e720ba420372cdffce13413e1 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sat, 23 Apr 2022 04:28:19 +0000 Subject: Vec to [Tag] --- azalea-nbt/src/encode.rs | 2 +- azalea-nbt/src/tag.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 08d41e89..c765d4a7 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -102,7 +102,7 @@ fn write_compound( } #[inline] -fn write_list(writer: &mut dyn Write, value: &Vec) -> Result<(), Error> { +fn write_list(writer: &mut dyn Write, value: &[Tag]) -> Result<(), Error> { // we just get the type from the first item, or default the type to END if value.is_empty() { writer.write_all(&[0; 5])?; diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs index f8960aa6..61bd15ba 100755 --- a/azalea-nbt/src/tag.rs +++ b/azalea-nbt/src/tag.rs @@ -137,7 +137,7 @@ impl Tag { } #[inline] - pub fn as_list(&self) -> Option<&Vec> { + pub fn as_list(&self) -> Option<&[Tag]> { if let Tag::List(v) = self { Some(v) } else { -- cgit v1.2.3 From b43ad19636aa197f145d696f2bb01adf833ddec8 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 24 Apr 2022 14:44:02 -0500 Subject: remove an unused thing from dependencies --- Cargo.lock | 10 ---------- azalea-nbt/src/encode.rs | 4 ++-- azalea-protocol/packet-macros/Cargo.toml | 1 - 3 files changed, 2 insertions(+), 13 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/Cargo.lock b/Cargo.lock index 8d94c26c..a0d60170 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -180,15 +180,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" -[[package]] -name = "casey" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabe85130dda9cf267715582ce6cf1ab581c8dfe3cb33f7065fee0f14e3fea14" -dependencies = [ - "syn", -] - [[package]] name = "cast" version = "0.2.7" @@ -697,7 +688,6 @@ checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" name = "packet-macros" version = "0.1.0" dependencies = [ - "casey", "proc-macro2", "quote", "syn", diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index c765d4a7..0a0237d3 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -5,6 +5,8 @@ use flate2::write::{GzEncoder, ZlibEncoder}; use std::collections::HashMap; use std::io::Write; +// who needs friends when you've got code that runs in nanoseconds? + #[inline] fn write_string(writer: &mut dyn Write, string: &str) -> Result<(), Error> { writer.write_i16::(string.len() as i16)?; @@ -13,8 +15,6 @@ fn write_string(writer: &mut dyn Write, string: &str) -> Result<(), Error> { Ok(()) } -// who needs friends when you've got code that runs in nanoseconds? - #[inline] fn write_compound( writer: &mut dyn Write, diff --git a/azalea-protocol/packet-macros/Cargo.toml b/azalea-protocol/packet-macros/Cargo.toml index 5a301756..2c0f36d7 100755 --- a/azalea-protocol/packet-macros/Cargo.toml +++ b/azalea-protocol/packet-macros/Cargo.toml @@ -8,7 +8,6 @@ proc-macro = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -casey = "^0.3.3" proc-macro2 = "^1.0.36" quote = "^1.0.10" syn = "^1.0.82" -- cgit v1.2.3 From 59ef9450942b88cad14b5bfc93e0ed9f451d76be Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 24 Apr 2022 14:53:01 -0500 Subject: fix writing wrong ids in nbt --- azalea-nbt/src/encode.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'azalea-nbt/src/encode.rs') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 0a0237d3..20d13793 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -63,22 +63,22 @@ fn write_compound( } } Tag::String(value) => { - writer.write_u8(9)?; + writer.write_u8(8)?; write_string(writer, key)?; write_string(writer, value)? } Tag::List(value) => { - writer.write_u8(10)?; + writer.write_u8(9)?; write_string(writer, key)?; write_list(writer, value)? } Tag::Compound(value) => { - writer.write_u8(11)?; + writer.write_u8(10)?; write_string(writer, key)?; write_compound(writer, value, true)? } Tag::IntArray(value) => { - writer.write_u8(12)?; + writer.write_u8(11)?; write_string(writer, key)?; writer.write_i32::(value.len() as i32)?; for &int in value { @@ -86,7 +86,7 @@ fn write_compound( } } Tag::LongArray(value) => { - writer.write_u8(13)?; + writer.write_u8(12)?; write_string(writer, key)?; writer.write_i32::(value.len() as i32)?; for &long in value { -- cgit v1.2.3