Skip to content

Commit 99e61a0

Browse files
committed
finish serialize
1 parent 2fe17e0 commit 99e61a0

File tree

5 files changed

+28
-39
lines changed

5 files changed

+28
-39
lines changed

pumpkin-nbt/src/deserializer.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ where
137137
T::deserialize(&mut deserializer)
138138
}
139139

140-
/// Deserializes struct using Serde Deserializer from normal NBT
140+
/// Deserializes struct using Serde Deserializer from network NBT
141141
pub fn from_bytes_unnamed<'a, T>(r: impl Read) -> Result<T>
142142
where
143143
T: Deserialize<'a>,
@@ -205,13 +205,9 @@ impl<'de, R: Read> de::Deserializer<'de> for &mut Deserializer<R> {
205205
let value = self.input.get_u8_be()?;
206206
visitor.visit_u8::<Error>(value)
207207
} else {
208-
panic!("{:?}", self.tag_to_deserialize);
209-
210-
/*
211208
Err(Error::UnsupportedType(
212209
"u8; NBT only supports signed values".to_string(),
213210
))
214-
*/
215211
}
216212
}
217213

@@ -357,4 +353,3 @@ impl<'de, R: Read> SeqAccess<'de> for ListAccess<'_, R> {
357353
result
358354
}
359355
}
360-

pumpkin-nbt/src/lib.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ pub mod tag;
2020

2121
// This NBT crate is inspired from CrabNBT
2222

23-
pub const END_ID: u8 = 0;
24-
pub const BYTE_ID: u8 = 1;
25-
pub const SHORT_ID: u8 = 2;
26-
pub const INT_ID: u8 = 3;
27-
pub const LONG_ID: u8 = 4;
28-
pub const FLOAT_ID: u8 = 5;
29-
pub const DOUBLE_ID: u8 = 6;
30-
pub const BYTE_ARRAY_ID: u8 = 7;
31-
pub const STRING_ID: u8 = 8;
32-
pub const LIST_ID: u8 = 9;
33-
pub const COMPOUND_ID: u8 = 10;
34-
pub const INT_ARRAY_ID: u8 = 11;
35-
pub const LONG_ARRAY_ID: u8 = 12;
23+
pub const END_ID: u8 = 0x00;
24+
pub const BYTE_ID: u8 = 0x01;
25+
pub const SHORT_ID: u8 = 0x02;
26+
pub const INT_ID: u8 = 0x03;
27+
pub const LONG_ID: u8 = 0x04;
28+
pub const FLOAT_ID: u8 = 0x05;
29+
pub const DOUBLE_ID: u8 = 0x06;
30+
pub const BYTE_ARRAY_ID: u8 = 0x07;
31+
pub const STRING_ID: u8 = 0x08;
32+
pub const LIST_ID: u8 = 0x09;
33+
pub const COMPOUND_ID: u8 = 0x0A;
34+
pub const INT_ARRAY_ID: u8 = 0x0B;
35+
pub const LONG_ARRAY_ID: u8 = 0x0C;
3636

3737
#[derive(Error, Debug)]
3838
pub enum Error {
@@ -214,7 +214,6 @@ impl_array!(BytesArray, "byte");
214214

215215
#[cfg(test)]
216216
mod test {
217-
use std::io::Read;
218217
use std::sync::LazyLock;
219218

220219
use flate2::read::GzDecoder;
@@ -331,6 +330,8 @@ mod test {
331330
},
332331
});
333332

333+
// TODO: More robust tests
334+
334335
#[test]
335336
fn test_deserialize_level_dat() {
336337
let raw_compressed_nbt = include_bytes!("../assets/level.dat");
@@ -344,24 +345,8 @@ mod test {
344345

345346
#[test]
346347
fn test_serialize_level_dat() {
347-
let raw_compressed_nbt = include_bytes!("../assets/level.dat");
348-
assert!(!raw_compressed_nbt.is_empty());
349-
350-
let mut decoder = GzDecoder::new(&raw_compressed_nbt[..]);
351-
let mut raw_bytes = Vec::new();
352-
decoder.read_to_end(&mut raw_bytes).unwrap();
353-
354348
let mut serialized = Vec::new();
355-
to_bytes(&*LEVEL_DAT, "".to_string(), &mut serialized).expect("Failed to encode to bytes");
356-
raw_bytes
357-
.iter()
358-
.zip(serialized.iter())
359-
.enumerate()
360-
.for_each(|(index, (expected_byte, serialized_byte))| {
361-
if expected_byte != serialized_byte {
362-
panic!("{} vs {} ({})", expected_byte, serialized_byte, index);
363-
}
364-
});
349+
to_bytes(&*LEVEL_DAT, &mut serialized).expect("Failed to encode to bytes");
365350

366351
let level_dat_again: LevelDat =
367352
from_bytes(&serialized[..]).expect("Failed to decode from bytes");

pumpkin-nbt/src/serializer.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ where
154154
}
155155

156156
/// Serializes struct using Serde Serializer to normal NBT
157-
pub fn to_bytes<T>(value: &T, name: String, w: impl Write) -> Result<()>
157+
pub fn to_bytes_named<T>(value: &T, name: String, w: impl Write) -> Result<()>
158158
where
159159
T: Serialize,
160160
{
@@ -166,6 +166,13 @@ where
166166
Ok(())
167167
}
168168

169+
pub fn to_bytes<T>(value: &T, w: impl Write) -> Result<()>
170+
where
171+
T: Serialize,
172+
{
173+
to_bytes_named(value, String::new(), w)
174+
}
175+
169176
impl<W: Write> ser::Serializer for &mut Serializer<W> {
170177
type Ok = ();
171178
type Error = Error;

pumpkin-protocol/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ pub struct RawPacket {
8282
pub bytebuf: Bytes,
8383
}
8484

85+
// TODO: Have the input be `impl Write`
8586
pub trait ClientPacket: Packet {
8687
fn write(&self, bytebuf: &mut impl BufMut);
8788
}
8889

90+
// TODO: Have the input be `impl Read`
8991
pub trait ServerPacket: Packet + Sized {
9092
fn read(bytebuf: &mut impl Buf) -> Result<Self, ReadingError>;
9193
}

pumpkin-registry/src/jukebox_song.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub struct JukeboxSong {
55
sound_event: String,
66
description: Description,
77
length_in_seconds: f32,
8-
comparator_output: u32,
8+
comparator_output: i32,
99
}
1010

1111
#[derive(Debug, Clone, Serialize, Deserialize)]

0 commit comments

Comments
 (0)