Skip to content

Commit

Permalink
Merge branch 'master' into plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
vyPal authored Dec 25, 2024
2 parents 6d7ec38 + e8deff5 commit 198f0f5
Show file tree
Hide file tree
Showing 144 changed files with 1,394 additions and 921 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ members = [
version = "0.1.0"
edition = "2021"

[profile.dev.package."*"]
opt-level = 3

[profile.dev]
opt-level = 1
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1-alpine3.20 AS builder
FROM rust:1-alpine3.21 AS builder
ARG GIT_VERSION=Docker
ENV GIT_VERSION=$GIT_VERSION
ENV RUSTFLAGS="-C target-feature=-crt-static -C target-cpu=native"
Expand All @@ -16,7 +16,7 @@ RUN --mount=type=cache,sharing=private,target=/pumpkin/target \
# strip debug symbols from binary
RUN strip pumpkin.release

FROM alpine:3.20
FROM alpine:3.21

# Identifying information for registries like ghcr.io
LABEL org.opencontainers.image.source=https://github.com/Snowiiii/Pumpkin
Expand Down
22 changes: 15 additions & 7 deletions pumpkin-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use query::QueryConfig;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use std::{
fs,
env, fs,
net::{Ipv4Addr, SocketAddr},
num::NonZeroU8,
path::Path,
Expand Down Expand Up @@ -36,6 +36,8 @@ mod server_links;
use proxy::ProxyConfig;
use resource_pack::ResourcePackConfig;

const CONFIG_ROOT_FOLDER: &str = "config/";

pub static ADVANCED_CONFIG: LazyLock<AdvancedConfiguration> =
LazyLock::new(AdvancedConfiguration::load);

Expand Down Expand Up @@ -126,26 +128,32 @@ trait LoadConfiguration {
where
Self: Sized + Default + Serialize + DeserializeOwned,
{
let path = Self::get_path();
let exe_dir = env::current_dir().unwrap();
let config_dir = exe_dir.join(CONFIG_ROOT_FOLDER);
if !config_dir.exists() {
log::debug!("creating new config root folder");
fs::create_dir(&config_dir).expect("Failed to create Config root folder");
}
let path = config_dir.join(Self::get_path());

let config = if path.exists() {
let file_content = fs::read_to_string(path)
.unwrap_or_else(|_| panic!("Couldn't read configuration file at {:?}", path));
let file_content = fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Couldn't read configuration file at {:?}", &path));

toml::from_str(&file_content).unwrap_or_else(|err| {
panic!(
"Couldn't parse config at {:?}. Reason: {}. This is is proberbly caused by an Config update, Just delete the old Config and start Pumpkin again",
path,
&path,
err.message()
)
})
} else {
let content = Self::default();

if let Err(err) = fs::write(path, toml::to_string(&content).unwrap()) {
if let Err(err) = fs::write(&path, toml::to_string(&content).unwrap()) {
warn!(
"Couldn't write default config to {:?}. Reason: {}. This is is proberbly caused by an Config update, Just delete the old Config and start Pumpkin again",
path, err
&path, err
);
}

Expand Down
2 changes: 1 addition & 1 deletion pumpkin-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use gamemode::GameMode;

use serde::{Deserialize, Serialize};

#[derive(PartialEq, Serialize, Deserialize)]
#[derive(PartialEq, Serialize, Deserialize, Clone)]
pub enum Difficulty {
Peaceful,
Easy,
Expand Down
8 changes: 6 additions & 2 deletions pumpkin-inventory/src/drag_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ impl DragHandler {
let changing_slots =
drag.possibly_changing_slots(&slots_cloned, carried_item.item_id);
let amount_of_slots = changing_slots.clone().count();
let (amount_per_slot, remainder) =
(carried_item.item_count as usize).div_rem_euclid(&amount_of_slots);
let (amount_per_slot, remainder) = if amount_of_slots == 0 {
// TODO: please work lol
(1, 0)
} else {
(carried_item.item_count as usize).div_rem_euclid(&amount_of_slots)
};
let mut item_in_each_slot = *carried_item;
item_in_each_slot.item_count = amount_per_slot as u8;
changing_slots.for_each(|slot| *slots[slot] = Some(item_in_each_slot));
Expand Down
7 changes: 7 additions & 0 deletions pumpkin-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ name = "pumpkin-protocol"
version.workspace = true
edition.workspace = true

[features]
default = ["packets", "query"]
packets = ["serverbound", "clientbound"]
serverbound = []
clientbound = []
query = []

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

use super::{ByteBuf, ReadingError};
use bytes::Bytes;
use bytes::Buf;
use serde::de::{self, DeserializeSeed, SeqAccess};

pub struct Deserializer<'a> {
inner: &'a mut Bytes,
pub struct Deserializer<'a, B: Buf> {
inner: &'a mut B,
}

impl de::Error for ReadingError {
Expand All @@ -14,13 +14,13 @@ impl de::Error for ReadingError {
}
}

impl<'a> Deserializer<'a> {
pub fn new(buf: &'a mut Bytes) -> Self {
impl<'a, B: Buf> Deserializer<'a, B> {
pub fn new(buf: &'a mut B) -> Self {
Self { inner: buf }
}
}

impl<'de> de::Deserializer<'de> for Deserializer<'_> {
impl<'de, B: Buf> de::Deserializer<'de> for Deserializer<'_, B> {
type Error = ReadingError;

fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
Expand Down Expand Up @@ -184,11 +184,11 @@ impl<'de> de::Deserializer<'de> for Deserializer<'_> {
where
V: de::Visitor<'de>,
{
struct Access<'a, 'b> {
deserializer: &'a mut Deserializer<'b>,
struct Access<'a, 'b, B: Buf> {
deserializer: &'a mut Deserializer<'b, B>,
}

impl<'de, 'a, 'b: 'a> SeqAccess<'de> for Access<'a, 'b> {
impl<'de, 'a, 'b: 'a, B: Buf> SeqAccess<'de> for Access<'a, 'b, B> {
type Error = ReadingError;

fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
Expand Down Expand Up @@ -216,12 +216,12 @@ impl<'de> de::Deserializer<'de> for Deserializer<'_> {
where
V: de::Visitor<'de>,
{
struct Access<'a, 'b> {
deserializer: &'a mut Deserializer<'b>,
struct Access<'a, 'b, B: Buf> {
deserializer: &'a mut Deserializer<'b, B>,
len: usize,
}

impl<'de, 'a, 'b: 'a> SeqAccess<'de> for Access<'a, 'b> {
impl<'de, 'a, 'b: 'a, B: Buf> SeqAccess<'de> for Access<'a, 'b, B> {
type Error = ReadingError;

fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
Expand Down
91 changes: 50 additions & 41 deletions pumpkin-protocol/src/bytebuf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use core::str;

use crate::{BitSet, FixedBitSet, VarInt, VarLong};
use crate::{
codec::{
bit_set::BitSet, identifier::Identifier, var_int::VarInt, var_long::VarLong, Codec,
DecodeError,
},
FixedBitSet,
};
use bytes::{Buf, BufMut};

mod deserializer;
Expand Down Expand Up @@ -54,9 +60,11 @@ pub trait ByteBuf: Buf {

fn try_get_var_long(&mut self) -> Result<VarLong, ReadingError>;

fn try_get_identifer(&mut self) -> Result<Identifier, ReadingError>;

fn try_get_string(&mut self) -> Result<String, ReadingError>;

fn try_get_string_len(&mut self, max_size: u32) -> Result<String, ReadingError>;
fn try_get_string_len(&mut self, max_size: usize) -> Result<String, ReadingError>;

/// Reads a boolean. If true, the closure is called, and the returned value is
/// wrapped in Some. Otherwise, this returns None.
Expand Down Expand Up @@ -181,41 +189,33 @@ impl<T: Buf> ByteBuf for T {
match VarInt::decode(self) {
Ok(var_int) => Ok(var_int),
Err(error) => match error {
crate::VarIntDecodeError::Incomplete => {
Err(ReadingError::Incomplete("varint".to_string()))
}
crate::VarIntDecodeError::TooLarge => {
Err(ReadingError::TooLarge("varint".to_string()))
}
DecodeError::Incomplete => Err(ReadingError::Incomplete("varint".to_string())),
DecodeError::TooLarge => Err(ReadingError::TooLarge("varint".to_string())),
},
}
}
fn try_get_var_long(&mut self) -> Result<VarLong, ReadingError> {
match VarLong::decode(self) {
Ok(var_long) => Ok(var_long),
Err(error) => match error {
crate::VarLongDecodeError::Incomplete => {
Err(ReadingError::Incomplete("varint".to_string()))
}
crate::VarLongDecodeError::TooLarge => {
Err(ReadingError::TooLarge("varlong".to_string()))
}
DecodeError::Incomplete => Err(ReadingError::Incomplete("varint".to_string())),
DecodeError::TooLarge => Err(ReadingError::TooLarge("varlong".to_string())),
},
}
}

fn try_get_string(&mut self) -> Result<String, ReadingError> {
self.try_get_string_len(i16::MAX as u32)
self.try_get_string_len(i16::MAX as usize)
}

fn try_get_string_len(&mut self, max_size: u32) -> Result<String, ReadingError> {
fn try_get_string_len(&mut self, max_size: usize) -> Result<String, ReadingError> {
let size = self.try_get_var_int()?.0;
if size as u32 > max_size {
if size as usize > max_size {
return Err(ReadingError::TooLarge("string".to_string()));
}

let data = self.try_copy_to_bytes(size as usize)?;
if data.len() as u32 > max_size {
if data.len() > max_size {
return Err(ReadingError::TooLarge("string".to_string()));
}
match str::from_utf8(&data) {
Expand Down Expand Up @@ -256,6 +256,16 @@ impl<T: Buf> ByteBuf for T {
fn try_get_fixed_bitset(&mut self, bits: usize) -> Result<FixedBitSet, ReadingError> {
self.try_copy_to_bytes(bits.div_ceil(8))
}

fn try_get_identifer(&mut self) -> Result<Identifier, ReadingError> {
match Identifier::decode(self) {
Ok(identifer) => Ok(identifer),
Err(error) => match error {
DecodeError::Incomplete => Err(ReadingError::Incomplete("identifer".to_string())),
DecodeError::TooLarge => Err(ReadingError::TooLarge("identifer".to_string())),
},
}
}
}

pub trait ByteBufMut {
Expand All @@ -265,7 +275,7 @@ pub trait ByteBufMut {

fn put_string(&mut self, val: &str);

fn put_string_len(&mut self, val: &str, max_size: u32);
fn put_string_len(&mut self, val: &str, max_size: usize);

fn put_string_array(&mut self, array: &[String]);

Expand All @@ -277,6 +287,8 @@ pub trait ByteBufMut {

fn put_list<G>(&mut self, list: &[G], write: impl Fn(&mut Self, &G));

fn put_identifier(&mut self, val: &Identifier);

fn put_var_int(&mut self, value: &VarInt);

fn put_varint_arr(&mut self, v: &[i32]);
Expand All @@ -299,11 +311,11 @@ impl<T: BufMut> ByteBufMut for T {
}

fn put_string(&mut self, val: &str) {
self.put_string_len(val, i16::MAX as u32);
self.put_string_len(val, i16::MAX as usize);
}

fn put_string_len(&mut self, val: &str, max_size: u32) {
if val.len() as u32 > max_size {
fn put_string_len(&mut self, val: &str, max_size: usize) {
if val.len() > max_size {
// Should be panic?, I mean its our fault
panic!("String is too big");
}
Expand All @@ -317,15 +329,12 @@ impl<T: BufMut> ByteBufMut for T {
}
}

fn put_var_int(&mut self, value: &VarInt) {
value.encode(self);
fn put_var_int(&mut self, var_int: &VarInt) {
var_int.encode(self);
}

fn put_bit_set(&mut self, set: &BitSet) {
self.put_var_int(&set.0);
for b in set.1 {
self.put_i64(*b);
}
fn put_bit_set(&mut self, bit_set: &BitSet) {
bit_set.encode(self);
}

fn put_option<G>(&mut self, val: &Option<G>, write: impl FnOnce(&mut Self, &G)) {
Expand All @@ -345,6 +354,10 @@ impl<T: BufMut> ByteBufMut for T {
fn put_varint_arr(&mut self, v: &[i32]) {
self.put_list(v, |p, &v| p.put_var_int(&v.into()))
}

fn put_identifier(&mut self, val: &Identifier) {
val.encode(self);
}
}

#[cfg(test)]
Expand All @@ -364,14 +377,12 @@ mod test {
bar: i32,
}
let foo = Foo { bar: 69 };
let mut serializer = serializer::Serializer::new(BytesMut::new());
let mut bytes = BytesMut::new();
let mut serializer = serializer::Serializer::new(&mut bytes);
foo.serialize(&mut serializer).unwrap();

let serialized: BytesMut = serializer.into();
let deserialized: Foo = Foo::deserialize(deserializer::Deserializer::new(
&mut Bytes::from(serialized),
))
.unwrap();
let deserialized: Foo =
Foo::deserialize(deserializer::Deserializer::new(&mut Bytes::from(bytes))).unwrap();

assert_eq!(foo, deserialized);
}
Expand All @@ -383,14 +394,12 @@ mod test {
bar: VarInt,
}
let foo = Foo { bar: 69.into() };
let mut serializer = serializer::Serializer::new(BytesMut::new());
let mut bytes = BytesMut::new();
let mut serializer = serializer::Serializer::new(&mut bytes);
foo.serialize(&mut serializer).unwrap();

let serialized: BytesMut = serializer.into();
let deserialized: Foo = Foo::deserialize(deserializer::Deserializer::new(
&mut Bytes::from(serialized),
))
.unwrap();
let deserialized: Foo =
Foo::deserialize(deserializer::Deserializer::new(&mut Bytes::from(bytes))).unwrap();

assert_eq!(foo, deserialized);
}
Expand Down
Loading

0 comments on commit 198f0f5

Please sign in to comment.