Skip to content

Commit

Permalink
work on removing bytebuf serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kralverde committed Feb 15, 2025
1 parent f458072 commit 8473383
Show file tree
Hide file tree
Showing 18 changed files with 522 additions and 362 deletions.
4 changes: 2 additions & 2 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub fn client_packet(input: TokenStream, item: TokenStream) -> TokenStream {

let gen = quote! {
#item
impl #impl_generics crate::bytebuf::packet::Packet for #name #ty_generics {
impl #impl_generics crate::Packet for #name #ty_generics {
const PACKET_ID: i32 = #input;
}
};
Expand All @@ -182,7 +182,7 @@ pub fn server_packet(input: TokenStream, item: TokenStream) -> TokenStream {

let gen = quote! {
#item
impl #impl_generics crate::bytebuf::packet::Packet for #name #ty_generics {
impl #impl_generics crate::Packet for #name #ty_generics {
const PACKET_ID: i32 = #input;
}
};
Expand Down
4 changes: 4 additions & 0 deletions pumpkin-nbt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ version.workspace = true
edition.workspace = true

[dependencies]
pumpkin-util = { path = "../pumpkin-util" }

serde.workspace = true
thiserror.workspace = true
bytes.workspace = true


cesu8 = "1.1"

[dev-dependencies]
flate2 = "1.0"
pumpkin-world = { path = "../pumpkin-world" }
fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
28 changes: 12 additions & 16 deletions pumpkin-nbt/src/compound.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::deserializer::ReadAdaptor;
use crate::serializer::WriteAdaptor;
use pumpkin_util::{ReadHelper, WriteHelper};

use crate::tag::NbtTag;
use crate::{get_nbt_string, Error, Nbt, END_ID};
use std::io::{ErrorKind, Read, Write};
Expand All @@ -17,7 +17,7 @@ impl NbtCompound {
}
}

pub fn deserialize_content<R>(reader: &mut ReadAdaptor<R>) -> Result<NbtCompound, Error>
pub fn deserialize_content<R>(reader: &mut ReadHelper<R>) -> Result<NbtCompound, Error>
where
R: Read,
{
Expand All @@ -26,17 +26,12 @@ impl NbtCompound {
loop {
let tag_id = match reader.get_u8_be() {
Ok(id) => id,
Err(err) => match err {
Error::Incomplete(err) => match err.kind() {
ErrorKind::UnexpectedEof => {
break;
}
_ => {
return Err(Error::Incomplete(err));
}
},
Err(err) => match err.kind() {
ErrorKind::UnexpectedEof => {
break;
}
_ => {
return Err(err);
return Err(Error::Incomplete(err));
}
},
};
Expand All @@ -56,16 +51,17 @@ impl NbtCompound {
Ok(compound)
}

pub fn serialize_content<W>(&self, w: &mut WriteAdaptor<W>) -> Result<(), Error>
pub fn serialize_content<W>(&self, w: &mut WriteHelper<W>) -> Result<(), Error>
where
W: Write,
{
for (name, tag) in &self.child_tags {
w.write_u8_be(tag.get_type_id())?;
w.write_u8_be(tag.get_type_id())
.map_err(Error::Incomplete)?;
NbtTag::String(name.clone()).serialize_data(w)?;
tag.serialize_data(w)?;
}
w.write_u8_be(END_ID)?;
w.write_u8_be(END_ID).map_err(Error::Incomplete)?;
Ok(())
}

Expand Down
111 changes: 8 additions & 103 deletions pumpkin-nbt/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,9 @@ use serde::{forward_to_deserialize_any, Deserialize};

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
pub struct ReadAdaptor<R: Read> {
reader: R,
}

impl<R: Read> ReadAdaptor<R> {
pub fn new(r: R) -> Self {
Self { reader: r }
}
}

impl<R: Read> ReadAdaptor<R> {
//TODO: Macroize this
pub fn get_u8_be(&mut self) -> Result<u8> {
let mut buf = [0u8];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(u8::from_be_bytes(buf))
}

pub fn get_i8_be(&mut self) -> Result<i8> {
let mut buf = [0u8];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(i8::from_be_bytes(buf))
}

pub fn get_i16_be(&mut self) -> Result<i16> {
let mut buf = [0u8; 2];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(i16::from_be_bytes(buf))
}

pub fn get_u16_be(&mut self) -> Result<u16> {
let mut buf = [0u8; 2];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(u16::from_be_bytes(buf))
}

pub fn get_i32_be(&mut self) -> Result<i32> {
let mut buf = [0u8; 4];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(i32::from_be_bytes(buf))
}

pub fn get_i64_be(&mut self) -> Result<i64> {
let mut buf = [0u8; 8];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(i64::from_be_bytes(buf))
}

pub fn get_f32_be(&mut self) -> Result<f32> {
let mut buf = [0u8; 4];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(f32::from_be_bytes(buf))
}

pub fn get_f64_be(&mut self) -> Result<f64> {
let mut buf = [0u8; 8];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(f64::from_be_bytes(buf))
}

pub fn read_boxed_slice(&mut self, count: usize) -> Result<Box<[u8]>> {
let mut buf = vec![0u8; count];
self.reader
.read_exact(&mut buf)
.map_err(Error::Incomplete)?;

Ok(buf.into())
}
}

#[derive(Debug)]
pub struct Deserializer<R: Read> {
input: ReadAdaptor<R>,
input: ReadHelper<R>,
tag_to_deserialize: Option<u8>,
in_list: bool,
is_named: bool,
Expand All @@ -111,7 +16,7 @@ pub struct Deserializer<R: Read> {
impl<R: Read> Deserializer<R> {
pub fn new(input: R, is_named: bool) -> Self {
Deserializer {
input: ReadAdaptor { reader: input },
input: ReadHelper::new(input),
tag_to_deserialize: None,
in_list: false,
is_named,
Expand Down Expand Up @@ -152,15 +57,15 @@ impl<'de, R: Read> de::Deserializer<'de> for &mut Deserializer<R> {
let tag_to_deserialize = self.tag_to_deserialize.unwrap();

let list_type = match tag_to_deserialize {
LIST_ID => Some(self.input.get_u8_be()?),
LIST_ID => Some(self.input.get_u8_be().map_err(Error::Incomplete)?),
INT_ARRAY_ID => Some(INT_ID),
LONG_ARRAY_ID => Some(LONG_ID),
BYTE_ARRAY_ID => Some(BYTE_ID),
_ => None,
};

if let Some(list_type) = list_type {
let remaining_values = self.input.get_i32_be()?;
let remaining_values = self.input.get_i32_be().map_err(Error::Incomplete)?;
if remaining_values < 0 {
return Err(Error::NegativeLength(remaining_values));
}
Expand Down Expand Up @@ -197,7 +102,7 @@ impl<'de, R: Read> de::Deserializer<'de> for &mut Deserializer<R> {
V: Visitor<'de>,
{
if self.in_list {
let value = self.input.get_u8_be()?;
let value = self.input.get_u8_be().map_err(Error::Incomplete)?;
visitor.visit_u8::<Error>(value)
} else {
Err(Error::UnsupportedType(
Expand Down Expand Up @@ -238,7 +143,7 @@ impl<'de, R: Read> de::Deserializer<'de> for &mut Deserializer<R> {
V: Visitor<'de>,
{
if self.tag_to_deserialize.unwrap() == BYTE_ID {
let value = self.input.get_u8_be()?;
let value = self.input.get_u8_be().map_err(Error::Incomplete)?;
if value != 0 {
visitor.visit_bool(true)
} else {
Expand Down Expand Up @@ -275,7 +180,7 @@ impl<'de, R: Read> de::Deserializer<'de> for &mut Deserializer<R> {
V: Visitor<'de>,
{
if self.tag_to_deserialize.is_none() {
let next_byte = self.input.get_u8_be()?;
let next_byte = self.input.get_u8_be().map_err(Error::Incomplete)?;
if next_byte != COMPOUND_ID {
return Err(Error::NoRootCompound(next_byte));
}
Expand Down Expand Up @@ -326,7 +231,7 @@ impl<'de, R: Read> MapAccess<'de> for CompoundAccess<'_, R> {
where
K: DeserializeSeed<'de>,
{
let tag = self.de.input.get_u8_be()?;
let tag = self.de.input.get_u8_be().map_err(Error::Incomplete)?;
self.de.tag_to_deserialize = Some(tag);

if tag == END_ID {
Expand Down
Loading

0 comments on commit 8473383

Please sign in to comment.