aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--azalea-nbt/src/decode.rs2
-rw-r--r--azalea-nbt/src/encode.rs42
2 files changed, 28 insertions, 16 deletions
diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs
index 90bc25f9..fb1b78b6 100644
--- a/azalea-nbt/src/decode.rs
+++ b/azalea-nbt/src/decode.rs
@@ -76,7 +76,7 @@ impl Tag {
break;
}
let name = read_string(stream)?;
- let tag = Tag::read_known(stream, tag_id).map_err(|_| Error::InvalidTag)?;
+ let tag = Tag::read_known(stream, tag_id)?;
map.insert(name, tag);
}
Tag::Compound(map)
diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs
index 2347bbf3..8bed2681 100644
--- a/azalea-nbt/src/encode.rs
+++ b/azalea-nbt/src/encode.rs
@@ -4,6 +4,18 @@ use byteorder::{WriteBytesExt, BE};
use flate2::write::{GzEncoder, ZlibEncoder};
use std::io::Write;
+#[inline]
+fn write_string(writer: &mut dyn Write, string: &str) -> Result<(), Error> {
+ writer
+ .write_i16::<BE>(string.len() as i16)
+ .map_err(|_| Error::WriteError)?;
+ writer
+ .write_all(string.as_bytes())
+ .map_err(|_| Error::WriteError)?;
+
+ Ok(())
+}
+
impl Tag {
pub fn write_without_end(&self, writer: &mut dyn Write) -> Result<(), Error> {
match self {
@@ -33,28 +45,28 @@ impl Tag {
}
}
Tag::String(value) => {
- writer
- .write_i16::<BE>(value.len() as i16)
- .map_err(|_| Error::WriteError)?;
- writer
- .write_all(value.as_bytes())
- .map_err(|_| Error::WriteError)?;
+ write_string(writer, value)?;
}
Tag::List(value) => {
// we just get the type from the first item, or default the type to END
- let type_id = value.first().map(|f| f.id()).unwrap_or(0);
- writer.write_u8(type_id).map_err(|_| Error::WriteError)?;
- writer
- .write_i32::<BE>(value.len() as i32)
- .map_err(|_| Error::WriteError)?;
- for tag in value {
- tag.write_without_end(writer)?;
+ if value.is_empty() {
+ writer.write_i8(0).map_err(|_| Error::WriteError)?;
+ writer.write_i32::<BE>(0).map_err(|_| Error::WriteError)?;
+ } else {
+ let type_id = value[0].id();
+ writer.write_u8(type_id).map_err(|_| Error::WriteError)?;
+ writer
+ .write_i32::<BE>(value.len() as i32)
+ .map_err(|_| Error::WriteError)?;
+ for tag in value {
+ tag.write_without_end(writer)?;
+ }
}
}
Tag::Compound(value) => {
for (key, tag) in value {
writer.write_u8(tag.id()).map_err(|_| Error::WriteError)?;
- Tag::String(key.clone()).write_without_end(writer)?;
+ write_string(writer, key)?;
tag.write_without_end(writer)?;
}
writer
@@ -91,7 +103,7 @@ impl Tag {
Tag::Compound(value) => {
for (key, tag) in value {
writer.write_u8(tag.id()).map_err(|_| Error::WriteError)?;
- Tag::String(key.clone()).write_without_end(writer)?;
+ write_string(writer, key)?;
tag.write_without_end(writer)?;
}
Ok(())