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

vanilla Component implementation #39

Merged
merged 6 commits into from
May 15, 2024
Merged
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
27 changes: 27 additions & 0 deletions crates/core/component/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "rimecraft-component"
version = "0.1.0"
edition = "2021"
authors = ["JieningYu <[email protected]>"]
description = "Minecraft Component implementation"
repository = "https://github.com/rimecraft-rs/rimecraft/"
license = "AGPL-3.0-or-later"
categories = []

[badges]
maintenance = { status = "passively-maintained" }

[dependencies]
serde = "1.0"
erased-serde = "0.4"
bytes = "1.6"
ahash = "0.8"
rimecraft-edcode = { path = "../../util/edcode" }
rimecraft-registry = { path = "../../util/registry", features = ["serde"] }
rimecraft-global-cx = { path = "../global-cx", features = ["nbt", "std"] }
rimecraft-maybe = { path = "../../util/maybe" }

[features]

[lints]
workspace = true
2 changes: 2 additions & 0 deletions crates/core/component/rime-target.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[java_edition]
version = "1.20.6"
128 changes: 128 additions & 0 deletions crates/core/component/src/changes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//! `ComponentChanges` implementation.

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};
use serde::{Deserialize, Serialize};

use crate::{map::CompTyCell, ErasedComponentType, Object, RawErasedComponentType};

/// Changes of components.
pub struct ComponentChanges<'a, 'cow, Cx>
where
Cx: ProvideIdTy,
{
pub(crate) changes: Maybe<'cow, AHashMap<CompTyCell<'a, Cx>, Option<Box<Object>>>>,
}

const REMOVED_PREFIX: char = '!';

struct Type<'a, Cx>
where
Cx: ProvideIdTy,
{
ty: ErasedComponentType<'a, Cx>,
rm: bool,
}

impl<Cx> Serialize for Type<'_, Cx>
where
Cx: ProvideIdTy,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let id = Reg::id(self.ty);
serializer.serialize_str(&if self.rm {
format!("{}{}", REMOVED_PREFIX, id)
} else {
id.to_string()
})
}
}

impl<'a, 'de, Cx> Deserialize<'de> for Type<'a, Cx>
where
Cx: ProvideIdTy + ProvideRegistry<'a, Cx::Id, RawErasedComponentType<Cx>>,
Cx::Id: FromStr,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor<'a, Cx>
where
Cx: ProvideIdTy,
{
_marker: PhantomData<&'a Cx>,
}

impl<'a, Cx> serde::de::Visitor<'_> for Visitor<'a, Cx>
where
Cx: ProvideIdTy + ProvideRegistry<'a, Cx::Id, RawErasedComponentType<Cx>>,
Cx::Id: FromStr,
{
type Value = Type<'a, Cx>;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "a string")
}

#[inline]
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let stripped = value.strip_prefix(REMOVED_PREFIX);
let any = stripped.unwrap_or(value);
let id: Cx::Id = any.parse().ok().ok_or_else(|| {
E::custom(format!("unable to deserialize the identifier {}", any))
})?;

Ok(Type {
ty: Cx::registry().get(&id).ok_or_else(|| {
E::custom(format!("unable to find the component type {}", id))
})?,
rm: stripped.is_some(),
})
}
}

deserializer.deserialize_str(Visitor {
_marker: PhantomData,
})
}
}

//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,
Cx::Id: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.changes, f)
}
}
Loading