Skip to content

Commit

Permalink
Merge pull request 'migrate to edcode2 (or packet-codec)' (#43) f…
Browse files Browse the repository at this point in the history
…rom edcode2 into main

Reviewed-on: https://codeberg.org/DM-Earth/rimecraft/pulls/43
  • Loading branch information
JieningYu committed Aug 12, 2024
2 parents b31931b + d177b36 commit f382cd6
Show file tree
Hide file tree
Showing 29 changed files with 290 additions and 873 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
[workspace]
resolver = "2"
members = [
"crates/util/*",
"crates/client/*",
"crates/core/*",
]
members = ["crates/util/*", "crates/client/*", "crates/core/*"]

[workspace.lints.rust]
missing-docs = "warn"
Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ serde = "1.0"
erased-serde = "0.4"
bytes = "1.6"
ahash = "0.8"
rimecraft-edcode = { path = "../../util/edcode" }
edcode2 = { path = "../../util/edcode2", package = "rimecraft-edcode2" }
rimecraft-registry = { path = "../registry", features = ["serde"] }
rimecraft-global-cx = { path = "../global-cx", features = ["nbt", "std"] }
rimecraft-maybe = { path = "../../util/maybe" }
Expand Down
19 changes: 0 additions & 19 deletions crates/core/component/src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::{fmt::Debug, marker::PhantomData, str::FromStr};

use ahash::AHashMap;
use rimecraft_edcode::{Encode, VarI32};
use rimecraft_global_cx::ProvideIdTy;
use rimecraft_maybe::Maybe;
use rimecraft_registry::{ProvideRegistry, Reg};
Expand Down Expand Up @@ -99,24 +98,6 @@ where
}
}

//TODO: implement encode and decode
/*
impl<Cx> Encode for ComponentChanges<'_, '_, Cx>
where
Cx: ProvideIdTy,
{
fn encode<B>(&self, buf: B) -> Result<(), std::io::Error>
where
B: bytes::BufMut,
{
let c_valid = self.changes.iter().filter(|(_, v)| v.is_some()).count();
let c_rm = self.changes.len() - c_valid;
VarI32(c_valid as i32).encode(buf)?;
VarI32(c_rm as i32).encode(buf)?;
}
}
*/

impl<Cx> Debug for ComponentChanges<'_, '_, Cx>
where
Cx: ProvideIdTy + Debug,
Expand Down
25 changes: 17 additions & 8 deletions crates/core/component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::{
marker::PhantomData,
};

use rimecraft_edcode::{Decode, Encode};
use bytes::{Buf, BufMut};
use edcode2::{Decode, Encode};
use rimecraft_global_cx::ProvideIdTy;
use rimecraft_registry::{ProvideRegistry, Reg};
use serde::{de::DeserializeOwned, Serialize};
Expand Down Expand Up @@ -49,7 +50,11 @@ where

impl<T> ComponentType<T>
where
T: Encode + Decode + Send + Sync + 'static,
T: for<'a> Encode<&'a dyn BufMut>
+ for<'a> Decode<'static, &'a dyn Buf>
+ Send
+ Sync
+ 'static,
{
/// Codec for packet encoding and decoding.
pub const PACKET_CODEC: PacketCodec = PacketCodec {
Expand All @@ -58,7 +63,13 @@ where
.expect("mismatched type")
.encode(buf)
},
decode: |buf| Ok(Box::new(T::decode(buf)?)),
decode: {
assert!(
<T as Decode<'static, &dyn Buf>>::SUPPORT_NON_IN_PLACE,
"non-in-place decoding is not supported for this type",
);
|buf| Ok(Box::new(T::decode(buf)?))
},
upd: |obj, buf| {
obj.downcast_mut::<T>()
.expect("mismatched type")
Expand Down Expand Up @@ -145,8 +156,6 @@ impl<T> PartialEq for ComponentType<T> {
}
}

impl<T> ComponentType<T> where T: Encode + Decode {}

impl<T> Default for ComponentType<T>
where
T: Clone + Eq + Send + Sync + 'static,
Expand Down Expand Up @@ -188,9 +197,9 @@ struct SerdeCodec {
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub struct PacketCodec {
encode: fn(&Object, &mut dyn bytes::BufMut) -> Result<(), std::io::Error>,
decode: fn(&mut dyn bytes::Buf) -> Result<Box<Object>, std::io::Error>,
upd: fn(&mut Object, &mut dyn bytes::Buf) -> Result<(), std::io::Error>,
encode: fn(&Object, &mut dyn BufMut) -> Result<(), edcode2::BoxedError<'static>>,
decode: fn(&mut dyn Buf) -> Result<Box<Object>, edcode2::BoxedError<'static>>,
upd: fn(&mut Object, &mut dyn Buf) -> Result<(), edcode2::BoxedError<'static>>,
}

#[derive(Debug, Clone, Copy)]
Expand Down
4 changes: 2 additions & 2 deletions crates/core/item/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ rimecraft-global-cx = { path = "../global-cx", features = ["nbt"] }
rimecraft-registry = { path = "../registry" }
rimecraft-fmt = { path = "../../util/fmt" }
serde = { version = "1.0", optional = true }
rimecraft-edcode = { path = "../../util/edcode", optional = true }
edcode2 = { path = "../../util/edcode2", package = "rimecraft-edcode2", optional = true }

[features]
default = ["serde", "edcode"]
serde = ["dep:serde", "rimecraft-registry/serde"]
edcode = ["dep:rimecraft-edcode", "rimecraft-registry/edcode"]
edcode = ["dep:edcode2", "rimecraft-registry/edcode"]

[lints]
workspace = true
26 changes: 11 additions & 15 deletions crates/core/item/src/edcode.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use rimecraft_edcode::{Decode, Encode};
use edcode2::{Buf, BufExt, BufMut, BufMutExt, Decode, Encode};
use rimecraft_global_cx::nbt_edcode::{ReadNbt, WriteNbt};
use rimecraft_registry::{ProvideRegistry, Reg};

use crate::{stack::ItemStackCx, ItemStack, RawItem};

impl<Cx> Encode for ItemStack<'_, Cx>
impl<Cx, B> Encode<B> for ItemStack<'_, Cx>
where
Cx: ItemStackCx + for<'a> WriteNbt<Option<&'a Cx::Compound>>,
B: BufMut,
{
fn encode<B>(&self, mut buf: B) -> Result<(), std::io::Error>
where
B: rimecraft_edcode::bytes::BufMut,
{
fn encode(&self, mut buf: B) -> Result<(), edcode2::BoxedError<'static>> {
if self.count() == 0 {
false.encode(buf)?;
buf.put_bool(false);
} else {
true.encode(&mut buf)?;
buf.put_bool(true);
let item = self.item();
item.encode(&mut buf)?;
self.count().encode(&mut buf)?;
Expand All @@ -27,17 +25,15 @@ where
}
}

impl<'r, Cx> Decode for ItemStack<'r, Cx>
impl<'r, 'de, Cx, B> Decode<'de, B> for ItemStack<'r, Cx>
where
Cx: ItemStackCx + ReadNbt<Option<Cx::Compound>> + ProvideRegistry<'r, Cx::Id, RawItem<Cx>>,
B: Buf,
{
fn decode<B>(mut buf: B) -> Result<Self, std::io::Error>
where
B: rimecraft_edcode::bytes::Buf,
{
if bool::decode(&mut buf)? {
fn decode(mut buf: B) -> Result<Self, edcode2::BoxedError<'de>> {
if buf.get_bool() {
let item = Reg::<'r, Cx::Id, RawItem<Cx>>::decode(&mut buf)?;
let count = u8::decode(&mut buf)?;
let count = buf.get_u8();
let nbt = Cx::read_nbt(buf.reader())?;
Ok(ItemStack::with_nbt(item, count, nbt))
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/core/palette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ maintenance = { status = "passively-maintained" }
[dependencies]
rimecraft-packed-int-array = { path = "../../util/packed-int-array" }
rimecraft-maybe = { path = "../../util/maybe" }
rimecraft-edcode = { path = "../../util/edcode", optional = true }
edcode2 = { path = "../../util/edcode2", package = "rimecraft-edcode2", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
rimecraft-serde-update = { path = "../../util/serde-update", optional = true }
ahash = "0.8"

[features]
edcode = ["dep:rimecraft-edcode"]
edcode = ["dep:edcode2"]
serde = ["dep:serde", "dep:rimecraft-serde-update"]

[lints]
Expand Down
70 changes: 16 additions & 54 deletions crates/core/palette/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,16 @@ where

#[cfg(feature = "edcode")]
mod _edcode {
use rimecraft_edcode::{Encode, Update, VarI32};
use edcode2::{Buf, BufMut, Decode, Encode};

use super::*;

impl<L, T> Encode for Data<L, T>
impl<L, T, B> Encode<B> for Data<L, T>
where
L: for<'a> IndexToRaw<&'a T>,
B: BufMut,
{
fn encode<B>(&self, mut buf: B) -> Result<(), std::io::Error>
where
B: rimecraft_edcode::bytes::BufMut,
{
fn encode(&self, mut buf: B) -> Result<(), edcode2::BoxedError<'static>> {
buf.put_u8(
self.storage
.as_array()
Expand All @@ -341,53 +339,25 @@ mod _edcode {
}
}

impl<L, T> Data<L, T>
where
L: for<'a> IndexToRaw<&'a T>,
{
/// Returns the encoded length of this data.
///
/// # Panics
///
/// See errors in [`Palette::encoded_len`].
pub fn encoded_len(&self) -> usize {
let len = self
.storage
.as_array()
.map(|array| array.data().len())
.unwrap_or_default();
1 + self
.palette
.encoded_len()
.expect("palette is not encodable")
+ VarI32(len as i32).encoded_len()
+ len * 8
}
}

impl<L, T, Cx> Encode for PalettedContainer<L, T, Cx>
impl<L, T, Cx, B> Encode<B> for PalettedContainer<L, T, Cx>
where
L: for<'a> IndexToRaw<&'a T>,
B: BufMut,
{
#[inline]
fn encode<B>(&self, buf: B) -> Result<(), std::io::Error>
where
B: rimecraft_edcode::bytes::BufMut,
{
fn encode(&self, buf: B) -> Result<(), edcode2::BoxedError<'static>> {
self.data.encode(buf)
}
}

impl<L, T, Cx> Update for PalettedContainer<L, T, Cx>
impl<'de, L, T, Cx, B> Decode<'de, B> for PalettedContainer<L, T, Cx>
where
L: for<'s> IndexFromRaw<'s, T> + Clone,
T: Clone + Hash + Eq,
Cx: ProvidePalette<L, T>,
B: Buf,
{
fn update<B>(&mut self, mut buf: B) -> Result<(), std::io::Error>
where
B: rimecraft_edcode::bytes::Buf,
{
fn decode_in_place(&mut self, mut buf: B) -> Result<(), edcode2::BoxedError<'de>> {
let data = compatible_data::<L, T, Cx>(
self.list.clone(),
Some(&self.data),
Expand All @@ -397,28 +367,20 @@ mod _edcode {
self.data = data
}

self.data.palette.update(&mut buf)?;
self.data.palette.decode_in_place(&mut buf)?;
if let Some(array) = self.data.storage.as_array_mut() {
array.data_mut().update(&mut buf)?;
array.data_mut().decode_in_place(&mut buf)?;
}

Ok(())
}
}

impl<L, T, Cx> PalettedContainer<L, T, Cx>
where
L: for<'a> IndexToRaw<&'a T>,
{
/// Returns the encoded length of this container.
///
/// # Panics
///
/// See errors in [`Palette::encoded_len`].
#[inline]
pub fn encoded_len(&self) -> usize {
self.data.encoded_len()
fn decode(_buf: B) -> Result<Self, edcode2::BoxedError<'de>> {
Err("paletted containers does not support non-in-place decoding".into())
}

const SUPPORT_NON_IN_PLACE: bool = false;
}
}

Expand Down
Loading

0 comments on commit f382cd6

Please sign in to comment.