From 63c87377e521e5c5ae2590ffd0ec82e4757b2800 Mon Sep 17 00:00:00 2001 From: Corey Shupe Date: Fri, 12 Aug 2022 01:03:27 -0400 Subject: [PATCH 1/4] Update nbt stuff. --- src/de.rs | 10 ++++++++++ src/ser.rs | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/de.rs b/src/de.rs index 7f0c857..22fd693 100644 --- a/src/de.rs +++ b/src/de.rs @@ -114,6 +114,7 @@ impl<'de: 'a, 'a, R: io::Read> de::Deserializer<'de> for &'a mut Decoder { match tag { 0x0a => visitor.visit_map(MapDecoder::new(self)), + 0x00 => visitor.visit_map(MapDecoder::new_seeded(self, 0x00)), _ => Err(Error::NoRootCompound), } } @@ -137,6 +138,10 @@ where fn new(outer: &'a mut Decoder) -> Self { MapDecoder { outer, tag: None } } + + fn new_seeded(outer: &'a mut Decoder, seed: u8) -> Self { + MapDecoder { outer, tag: Some(seed) } + } } impl<'de: 'a, 'a, R: io::Read + 'a> de::MapAccess<'de> for MapDecoder<'a, R> { @@ -146,6 +151,11 @@ impl<'de: 'a, 'a, R: io::Read + 'a> de::MapAccess<'de> for MapDecoder<'a, R> { where K: de::DeserializeSeed<'de>, { + if matches!(self.tag, Some(0x00)) { + // we exit early here - we know that the map contains nothing from a 0x00 headed object + return Ok(None); + } + let tag = raw::read_bare_byte(&mut self.outer.reader)?; // NBT indicates the end of a compound type with a 0x00 tag. diff --git a/src/ser.rs b/src/ser.rs index fe8c0f0..ff7803a 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -269,7 +269,12 @@ where /// Serialize maps as `Tag_Compound` data. #[inline] - fn serialize_map(self, _len: Option) -> Result { + fn serialize_map(self, len: Option) -> Result { + if matches!(len, Some(0)) { + self.write_header(0, None)?; + return Ok(Compound::from_outer(self)); + } + let header = self.header; // Circumvent strange borrowing errors. self.write_header(0x0a, header)?; Ok(Compound::from_outer(self)) @@ -277,7 +282,12 @@ where /// Serialize structs as `Tag_Compound` data. #[inline] - fn serialize_struct(self, _name: &'static str, _len: usize) -> Result { + fn serialize_struct(self, _name: &'static str, len: usize) -> Result { + if len == 0 { + self.write_header(0, None)?; + return Ok(Compound::from_outer(self)); + } + let header = self.header; // Circumvent strange borrowing errors. self.write_header(0x0a, header)?; Ok(Compound::from_outer(self)) From 4cf91c815634ed5be27630398d0c2bce16f8cd3f Mon Sep 17 00:00:00 2001 From: Corey Shupe Date: Fri, 12 Aug 2022 01:16:45 -0400 Subject: [PATCH 2/4] Debug changes. --- src/de.rs | 1 + src/ser.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/de.rs b/src/de.rs index 22fd693..bc794c3 100644 --- a/src/de.rs +++ b/src/de.rs @@ -152,6 +152,7 @@ impl<'de: 'a, 'a, R: io::Read + 'a> de::MapAccess<'de> for MapDecoder<'a, R> { K: de::DeserializeSeed<'de>, { if matches!(self.tag, Some(0x00)) { + println!("Reading quick exit header."); // we exit early here - we know that the map contains nothing from a 0x00 headed object return Ok(None); } diff --git a/src/ser.rs b/src/ser.rs index ff7803a..5215b35 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -271,6 +271,7 @@ where #[inline] fn serialize_map(self, len: Option) -> Result { if matches!(len, Some(0)) { + println!("Writing quick exit header."); self.write_header(0, None)?; return Ok(Compound::from_outer(self)); } @@ -284,6 +285,7 @@ where #[inline] fn serialize_struct(self, _name: &'static str, len: usize) -> Result { if len == 0 { + println!("Writing quick exit header."); self.write_header(0, None)?; return Ok(Compound::from_outer(self)); } From 42d8338fe751e6d491dcf72ee82ae89e9e62df0f Mon Sep 17 00:00:00 2001 From: Corey Shupe Date: Fri, 12 Aug 2022 01:22:13 -0400 Subject: [PATCH 3/4] Debug changes. --- src/raw.rs | 2 ++ src/ser.rs | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/raw.rs b/src/raw.rs index 5200232..356adc0 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -107,8 +107,10 @@ pub fn write_bare_string(dst: &mut W, value: &str) -> Result<()> where W: io::Write, { + println!("Writing string: {}", value); let encoded = to_java_cesu8(value); dst.write_u16::(encoded.len() as u16)?; + println!("Writing {} as len", encoded.len()); dst.write_all(&encoded).map_err(From::from) } diff --git a/src/ser.rs b/src/ser.rs index 5215b35..d5c989d 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -20,6 +20,7 @@ where W: ?Sized + io::Write, T: ?Sized + ser::Serialize, { + println!("To writer call, let's follow the logic here."); let mut encoder = Encoder::new(dst, header); value.serialize(&mut encoder) } @@ -69,6 +70,7 @@ where /// Write the NBT tag and an optional header to the underlying writer. #[inline] fn write_header(&mut self, tag: i8, header: Option<&str>) -> Result<()> { + println!("Writing bare byte {}", tag); raw::write_bare_byte(&mut self.writer, tag)?; match header { None => raw::write_bare_short(&mut self.writer, 0).map_err(From::from), @@ -283,7 +285,9 @@ where /// Serialize structs as `Tag_Compound` data. #[inline] - fn serialize_struct(self, _name: &'static str, len: usize) -> Result { + fn serialize_struct(self, name: &'static str, len: usize) -> Result { + println!("Serializing struct {} with length {}", name, len); + if len == 0 { println!("Writing quick exit header."); self.write_header(0, None)?; @@ -291,6 +295,7 @@ where } let header = self.header; // Circumvent strange borrowing errors. + println!("Writing 0x0a header..."); self.write_header(0x0a, header)?; Ok(Compound::from_outer(self)) } From 475d71454c64221918338aa501c07d57f1b1087d Mon Sep 17 00:00:00 2001 From: Corey Shupe Date: Fri, 12 Aug 2022 01:52:39 -0400 Subject: [PATCH 4/4] Revert debug changes --- src/de.rs | 1 - src/raw.rs | 2 -- src/ser.rs | 9 +-------- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/src/de.rs b/src/de.rs index bc794c3..22fd693 100644 --- a/src/de.rs +++ b/src/de.rs @@ -152,7 +152,6 @@ impl<'de: 'a, 'a, R: io::Read + 'a> de::MapAccess<'de> for MapDecoder<'a, R> { K: de::DeserializeSeed<'de>, { if matches!(self.tag, Some(0x00)) { - println!("Reading quick exit header."); // we exit early here - we know that the map contains nothing from a 0x00 headed object return Ok(None); } diff --git a/src/raw.rs b/src/raw.rs index 356adc0..5200232 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -107,10 +107,8 @@ pub fn write_bare_string(dst: &mut W, value: &str) -> Result<()> where W: io::Write, { - println!("Writing string: {}", value); let encoded = to_java_cesu8(value); dst.write_u16::(encoded.len() as u16)?; - println!("Writing {} as len", encoded.len()); dst.write_all(&encoded).map_err(From::from) } diff --git a/src/ser.rs b/src/ser.rs index d5c989d..ff7803a 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -20,7 +20,6 @@ where W: ?Sized + io::Write, T: ?Sized + ser::Serialize, { - println!("To writer call, let's follow the logic here."); let mut encoder = Encoder::new(dst, header); value.serialize(&mut encoder) } @@ -70,7 +69,6 @@ where /// Write the NBT tag and an optional header to the underlying writer. #[inline] fn write_header(&mut self, tag: i8, header: Option<&str>) -> Result<()> { - println!("Writing bare byte {}", tag); raw::write_bare_byte(&mut self.writer, tag)?; match header { None => raw::write_bare_short(&mut self.writer, 0).map_err(From::from), @@ -273,7 +271,6 @@ where #[inline] fn serialize_map(self, len: Option) -> Result { if matches!(len, Some(0)) { - println!("Writing quick exit header."); self.write_header(0, None)?; return Ok(Compound::from_outer(self)); } @@ -285,17 +282,13 @@ where /// Serialize structs as `Tag_Compound` data. #[inline] - fn serialize_struct(self, name: &'static str, len: usize) -> Result { - println!("Serializing struct {} with length {}", name, len); - + fn serialize_struct(self, _name: &'static str, len: usize) -> Result { if len == 0 { - println!("Writing quick exit header."); self.write_header(0, None)?; return Ok(Compound::from_outer(self)); } let header = self.header; // Circumvent strange borrowing errors. - println!("Writing 0x0a header..."); self.write_header(0x0a, header)?; Ok(Compound::from_outer(self)) }