1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
//! The data sent to the client in the `ClientboundRegistryDataPacket`.
//!
//! This module contains the structures used to represent the registry
//! sent to the client upon login. This contains a lot of information about
//! the game, including the types of chat messages, dimensions, and
//! biomes.
pub mod block_predicate;
pub mod block_state_provider;
pub mod components;
pub mod dimension_type;
pub mod enchantment;
pub mod entity_effect;
pub mod float_provider;
pub mod value;
use std::{collections::HashMap, io::Cursor};
use azalea_registry::identifier::Identifier;
use indexmap::IndexMap;
use simdnbt::{DeserializeError, FromNbtTag, borrow, owned::NbtCompound};
use thiserror::Error;
use tracing::error;
/// The base of the registry.
///
/// This is the registry that is sent to the client upon login.
///
/// Note that `azalea-client` stores registries per-world rather than
/// per-client like you might expect. This is an optimization for swarms to
/// reduce memory usage, since registries are expected to be the same for every
/// client in a world.
#[derive(Clone, Debug, Default)]
pub struct RegistryHolder {
// if you add new fields here, don't forget to also update `RegistryHolder::append`,
// `protocol_id_to_identifier`, and `define_default_deserializes_to!` in
// `data_registry.rs`.
#[rustfmt::skip] // allow empty line
/// Attributes about the dimension.
pub dimension_type: RegistryType<dimension_type::DimensionKindElement>,
pub enchantment: RegistryType<enchantment::EnchantmentData>,
/// Registries that we haven't implemented deserializable types for.
///
/// You can still access these just fine, but they'll be NBT instead of
/// nicer structs.
pub extra: HashMap<Identifier, RegistryType<NbtCompound>>,
}
macro_rules! registry_holder {
($($registry:ident),* $(,)?) => {
impl RegistryHolder {
pub fn append(
&mut self,
id: Identifier,
entries: Vec<(Identifier, Option<NbtCompound>)>,
) {
if id.namespace() == "minecraft" {
match id.path() {
$(
stringify!($registry) => {
return self.$registry.append_nbt(id, entries);
}
)*
_ => {}
}
}
self.extra
.entry(id.clone())
.or_default()
.append_nbt(id, entries);
}
pub fn extend(&mut self, other: RegistryHolder) {
$(
self.$registry = other.$registry;
)*
self.extra.extend(other.extra);
}
/// Convert a protocol ID for a registry key (like the protocol_id for
/// something that implements `DataRegistry`) and convert it to its string
/// name.
pub fn protocol_id_to_identifier(
&self,
registry: Identifier,
protocol_id: u32,
) -> Option<&Identifier> {
let index = protocol_id as usize;
if registry.namespace() == "minecraft" {
match registry.path() {
$(
stringify!($registry) => {
return self.$registry.map.get_index(index).map(|(k, _)| k);
}
)*
_ => {}
}
}
self.extra
.get(®istry)
.and_then(|r| r.map.get_index(index))
.map(|(k, _)| k)
}
}
};
}
registry_holder!(dimension_type, enchantment);
fn nbt_to_serializable_type<T: simdnbt::Deserialize>(
value: &NbtCompound,
) -> Result<T, NbtToSerializableTypeError> {
// convert the value to T
let mut nbt_bytes = Vec::new();
value.write(&mut nbt_bytes);
let nbt_borrow_compound = simdnbt::borrow::read_compound(&mut Cursor::new(&nbt_bytes))?;
T::from_compound((&nbt_borrow_compound).into()).map_err(Into::into)
}
#[derive(Debug, Error)]
enum NbtToSerializableTypeError {
#[error(transparent)]
NbtError(#[from] simdnbt::Error),
#[error(transparent)]
DeserializeError(#[from] simdnbt::DeserializeError),
}
/// A collection of values for a certain type of registry data.
#[derive(Clone, Debug)]
pub struct RegistryType<T: simdnbt::Deserialize> {
pub map: IndexMap<Identifier, T>,
}
impl<T: simdnbt::Deserialize> Default for RegistryType<T> {
fn default() -> Self {
Self {
map: IndexMap::new(),
}
}
}
impl<T: simdnbt::Deserialize> RegistryType<T> {
fn append_nbt(&mut self, id: Identifier, entries: Vec<(Identifier, Option<NbtCompound>)>) {
let map = &mut self.map;
for (key, value) in entries {
if let Some(value) = value {
match nbt_to_serializable_type(&value) {
Ok(value) => {
map.insert(key, value);
}
Err(err) => {
error!("Error deserializing {id} entry {key}: {err:?}\n{value:?}");
}
}
} else {
map.shift_remove(&key);
}
}
}
}
pub trait RegistryDeserializesTo: simdnbt::Deserialize {
fn get_for_registry<'a>(
registries: &'a RegistryHolder,
registry_name: &'static str,
protocol_id: u32,
) -> Option<(&'a Identifier, &'a Self)>;
}
impl RegistryDeserializesTo for NbtCompound {
fn get_for_registry<'a>(
registries: &'a RegistryHolder,
registry_name: &'static str,
protocol_id: u32,
) -> Option<(&'a Identifier, &'a Self)> {
registries
.extra
.get(&Identifier::new(registry_name))?
.map
.get_index(protocol_id as usize)
}
}
impl RegistryDeserializesTo for dimension_type::DimensionKindElement {
fn get_for_registry<'a>(
registries: &'a RegistryHolder,
registry_name: &'static str,
protocol_id: u32,
) -> Option<(&'a Identifier, &'a Self)> {
if registry_name != "dimension_type" {
error!(
"called RegistryDeserializesTo::get_for_registry with the wrong registry: {registry_name}"
);
}
registries
.dimension_type
.map
.get_index(protocol_id as usize)
}
}
impl RegistryDeserializesTo for enchantment::EnchantmentData {
fn get_for_registry<'a>(
registries: &'a RegistryHolder,
registry_name: &'static str,
protocol_id: u32,
) -> Option<(&'a Identifier, &'a Self)> {
if registry_name != "enchantment" {
error!(
"called RegistryDeserializesTo::get_for_registry with the wrong registry: {registry_name}"
);
}
registries.enchantment.map.get_index(protocol_id as usize)
}
}
pub fn get_in_compound<T: FromNbtTag>(
compound: &borrow::NbtCompound,
key: &str,
) -> Result<T, DeserializeError> {
let value = compound.get(key).ok_or(DeserializeError::MissingField)?;
T::from_nbt_tag(value).ok_or(DeserializeError::MissingField)
}
|