Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VarInt api rewrite #237

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pumpkin-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "pumpkin-protocol"
version.workspace = true
edition.workspace = true

[dev-dependencies]
rayon = "1"

Comment on lines +6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this getting added.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

varint and varlong tests, I tested the full range of numbers to be sure and without rayon it is very slow to say the least

[dependencies]
pumpkin-config = { path = "../pumpkin-config" }
pumpkin-macros = { path = "../pumpkin-macros" }
Expand Down
20 changes: 16 additions & 4 deletions pumpkin-protocol/src/bytebuf/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::borrow::Cow;
use std::fmt::Display;

use super::ByteBuffer;
use crate::VarIntDecodeError;
use serde::de::{self, DeserializeSeed, SeqAccess};
use thiserror::Error;

use super::ByteBuffer;

pub struct Deserializer<'a> {
inner: &'a mut ByteBuffer,
}
Expand All @@ -14,14 +15,25 @@ pub enum DeserializerError {
#[error("Unknown Packet")]
UnknownPacket,
#[error("serializer error {0}")]
Message(String),
Message(Cow<'static, str>),
#[error("Stdio error {0}")]
Stdio(std::io::Error),
}

impl From<VarIntDecodeError> for DeserializerError {
fn from(value: VarIntDecodeError) -> Self {
match value {
VarIntDecodeError::Incomplete => {
Self::Message("Not enough bytes to read VarInt".into())
}
VarIntDecodeError::TooLarge => Self::Message("VarInt is too big".into()),
}
}
}

impl de::Error for DeserializerError {
fn custom<T: Display>(msg: T) -> Self {
Self::Message(msg.to_string())
Self::Message(msg.to_string().into())
}
}

Expand Down
117 changes: 32 additions & 85 deletions pumpkin-protocol/src/bytebuf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BitSet, FixedBitSet, VarInt, VarLongType};
use crate::{BitSet, FixedBitSet, VarEncodedInteger, VarInt, VarLong};
use bytes::{Buf, BufMut, BytesMut};
use core::str;

Expand All @@ -7,9 +7,6 @@ pub use deserializer::DeserializerError;
pub mod packet_id;
mod serializer;

const SEGMENT_BITS: u8 = 0x7F;
const CONTINUE_BIT: u8 = 0x80;

#[derive(Debug)]
pub struct ByteBuffer {
buffer: BytesMut,
Expand All @@ -26,72 +23,36 @@ impl ByteBuffer {
}

pub fn get_var_int(&mut self) -> Result<VarInt, DeserializerError> {
let mut value: i32 = 0;
let mut position: i32 = 0;

loop {
let read = self.get_u8()?;

value |= ((read & SEGMENT_BITS) as i32) << position;

if read & CONTINUE_BIT == 0 {
break;
}

position += 7;

if position >= 32 {
return Err(DeserializerError::Message("VarInt is too big".to_string()));
}
}

Ok(VarInt(value))
VarInt::decode(|| self.buffer.has_remaining().then(|| self.buffer.get_u8()))
.map_err(Into::into)
}

pub fn get_var_long(&mut self) -> Result<VarLongType, DeserializerError> {
let mut value: i64 = 0;
let mut position: i64 = 0;

loop {
let read = self.get_u8()?;

value |= ((read & SEGMENT_BITS) as i64) << position;

if read & CONTINUE_BIT == 0 {
break;
}

position += 7;

if position >= 64 {
return Err(DeserializerError::Message("VarLong is too big".to_string()));
}
}

Ok(value)
pub fn get_var_long(&mut self) -> Result<VarLong, DeserializerError> {
VarLong::decode(|| self.buffer.has_remaining().then(|| self.buffer.get_u8()))
.map_err(Into::into)
}

pub fn get_string(&mut self) -> Result<String, DeserializerError> {
self.get_string_len(i16::MAX as i32)
}

pub fn get_string_len(&mut self, max_size: i32) -> Result<String, DeserializerError> {
let size = self.get_var_int()?.0;
let size = self.get_var_int()?.get();
if size > max_size {
return Err(DeserializerError::Message(
"String length is bigger than max size".to_string(),
"String length is bigger than max size".into(),
));
}

let data = self.copy_to_bytes(size as usize)?;
if data.len() as i32 > max_size {
return Err(DeserializerError::Message(
"String is bigger than max size".to_string(),
"String is bigger than max size".into(),
));
}
match str::from_utf8(&data) {
Ok(string_result) => Ok(string_result.to_string()),
Err(e) => Err(DeserializerError::Message(e.to_string())),
Ok(string_result) => Ok(string_result.into()),
Err(e) => Err(DeserializerError::Message(e.to_string().into())),
}
}

Expand Down Expand Up @@ -133,7 +94,7 @@ impl ByteBuffer {
// Should be panic?, I mean its our fault
panic!("String is too big");
}
self.put_var_int(&val.len().into());
self.put_var_int(val.len().try_into().unwrap());
self.buffer.put(val.as_bytes());
}

Expand All @@ -143,23 +104,12 @@ impl ByteBuffer {
}
}

pub fn put_var_int(&mut self, value: &VarInt) {
let mut val = value.0;
for _ in 0..5 {
let mut b: u8 = val as u8 & 0b01111111;
val >>= 7;
if val != 0 {
b |= 0b10000000;
}
self.buffer.put_u8(b);
if val == 0 {
break;
}
}
pub fn put_var_int(&mut self, value: VarInt) {
value.encode(|buff| self.put_slice(buff))
}

pub fn put_bit_set(&mut self, set: &BitSet) {
self.put_var_int(&set.0);
pub fn put_bit_set(&mut self, set: BitSet) {
self.put_var_int(set.0);
for b in set.1 {
self.put_i64(*b);
}
Expand Down Expand Up @@ -190,7 +140,8 @@ impl ByteBuffer {
&mut self,
val: impl Fn(&mut Self) -> Result<T, DeserializerError>,
) -> Result<Vec<T>, DeserializerError> {
let len = self.get_var_int()?.0 as usize;
let len = usize::try_from(self.get_var_int()?.get())
.map_err(|_| DeserializerError::Message("invalid length given from list".into()))?;
let mut list = Vec::with_capacity(len);
for _ in 0..len {
list.push(val(self)?);
Expand All @@ -199,14 +150,14 @@ impl ByteBuffer {
}
/// Writes a list to the buffer.
pub fn put_list<T>(&mut self, list: &[T], write: impl Fn(&mut Self, &T)) {
self.put_var_int(&list.len().into());
self.put_var_int(list.len().try_into().unwrap());
for v in list {
write(self, v);
}
}

pub fn put_varint_arr(&mut self, v: &[i32]) {
self.put_list(v, |p, &v| p.put_var_int(&v.into()))
self.put_list(v, |p, &v| p.put_var_int(v.into()))
}

/* pub fn get_nbt(&mut self) -> Option<fastnbt::value::Value> {
Expand All @@ -230,7 +181,7 @@ impl ByteBuffer {
Ok(self.buffer.get_u8())
} else {
Err(DeserializerError::Message(
"No bytes left to consume".to_string(),
"No bytes left to consume".into(),
))
}
}
Expand All @@ -240,7 +191,7 @@ impl ByteBuffer {
Ok(self.buffer.get_i8())
} else {
Err(DeserializerError::Message(
"No bytes left to consume".to_string(),
"No bytes left to consume".into(),
))
}
}
Expand All @@ -250,7 +201,7 @@ impl ByteBuffer {
Ok(self.buffer.get_u16())
} else {
Err(DeserializerError::Message(
"Less than 2 bytes left to consume".to_string(),
"Less than 2 bytes left to consume".into(),
))
}
}
Expand All @@ -260,7 +211,7 @@ impl ByteBuffer {
Ok(self.buffer.get_i16())
} else {
Err(DeserializerError::Message(
"Less than 2 bytes left to consume".to_string(),
"Less than 2 bytes left to consume".into(),
))
}
}
Expand All @@ -270,7 +221,7 @@ impl ByteBuffer {
Ok(self.buffer.get_u32())
} else {
Err(DeserializerError::Message(
"Less than 4 bytes left to consume".to_string(),
"Less than 4 bytes left to consume".into(),
))
}
}
Expand All @@ -280,7 +231,7 @@ impl ByteBuffer {
Ok(self.buffer.get_i32())
} else {
Err(DeserializerError::Message(
"Less than 4 bytes left to consume".to_string(),
"Less than 4 bytes left to consume".into(),
))
}
}
Expand All @@ -290,7 +241,7 @@ impl ByteBuffer {
Ok(self.buffer.get_u64())
} else {
Err(DeserializerError::Message(
"Less than 8 bytes left to consume".to_string(),
"Less than 8 bytes left to consume".into(),
))
}
}
Expand All @@ -300,7 +251,7 @@ impl ByteBuffer {
Ok(self.buffer.get_i64())
} else {
Err(DeserializerError::Message(
"Less than 8 bytes left to consume".to_string(),
"Less than 8 bytes left to consume".into(),
))
}
}
Expand All @@ -310,7 +261,7 @@ impl ByteBuffer {
Ok(self.buffer.get_f32())
} else {
Err(DeserializerError::Message(
"Less than 4 bytes left to consume".to_string(),
"Less than 4 bytes left to consume".into(),
))
}
}
Expand All @@ -320,7 +271,7 @@ impl ByteBuffer {
Ok(self.buffer.get_f64())
} else {
Err(DeserializerError::Message(
"Less than 8 bytes left to consume".to_string(),
"Less than 8 bytes left to consume".into(),
))
}
}
Expand Down Expand Up @@ -370,9 +321,7 @@ impl ByteBuffer {
if self.buffer.len() >= len {
Ok(self.buffer.copy_to_bytes(len))
} else {
Err(DeserializerError::Message(
"Unable to copy bytes".to_string(),
))
Err(DeserializerError::Message("Unable to copy bytes".into()))
}
}

Expand All @@ -381,9 +330,7 @@ impl ByteBuffer {
self.buffer.copy_to_slice(dst);
Ok(())
} else {
Err(DeserializerError::Message(
"Unable to copy slice".to_string(),
))
Err(DeserializerError::Message("Unable to copy slice".into()))
}
}

Expand Down
Loading