Skip to content

Commit 835c664

Browse files
authored
hash the remaining item components and styled text without the fork (#994)
* hash the item components pomme reads with its own hashes, checked against the game's for every item's defaults * hash every other item component and styled text through pomme, each from its saved-data form as a value tree * hash a player sprite's hat as a flag, leave a click's inline dialog unhashed and tell negative zero from zero * add fixtures for a banner pattern layer and a sulfur cube's stack * unwrap a mixed nbt list's elements before hashing them, tell negative zero from zero and drop the unused empty hash
1 parent dc5afbf commit 835c664

12 files changed

Lines changed: 1164 additions & 377 deletions

File tree

‎pomme-client/src/net/item_codec.rs‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! The bridge to the inventory fork, which still writes the JSON a stack's
2-
//! tooltip is built from and hashes the components pomme doesn't yet.
2+
//! tooltip is built from.
33
//!
44
//! A stack (`pomme_item`) holds each component as its vanilla bytes, and the
55
//! client reads them through pomme's own values (`pomme_item::components`).
@@ -106,25 +106,14 @@ impl pomme_item::hash::Names for SessionNames<'_> {
106106
}
107107

108108
/// `HashedStack.create`: the stack with each component value replaced by
109-
/// its hash; `None` when empty. The hash is pomme's own for a value it
110-
/// reads, and the fork's for the rest.
111-
// TODO: a component neither can hash goes unhashed, so the server resyncs
112-
// a slot holding one.
109+
/// the hash of its `CODEC` form; `None` when empty.
110+
// TODO: a value with no form yet goes unhashed, so the server resyncs a slot
111+
// holding one.
113112
pub fn hashed_stack(
114113
stack: &ItemStack,
115114
registries: &azalea_core::registry_holder::RegistryHolder,
116115
) -> Option<pomme_protocol::serverbound::HashedStack> {
117116
let data = stack.as_present()?;
118-
let fork = std::cell::OnceCell::new();
119-
let fork_hash = |kind| {
120-
let fork: &ForkStack = fork.get_or_init(|| {
121-
stack
122-
.to_wire()
123-
.map_or(ForkStack::Empty, |wire| to_fork(&wire))
124-
});
125-
let value = fork.as_present()?.component_patch.get_kind(kind)?;
126-
Some(value.crc_hash(registries).0 as i32)
127-
};
128117
let names = SessionNames(registries);
129118
let mut hashed = pomme_protocol::serverbound::HashedStack {
130119
item: data.kind.to_u32(),
@@ -137,11 +126,8 @@ pub fn hashed_stack(
137126
hashed.removed.push(kind.to_u32());
138127
continue;
139128
};
140-
let hash = pomme_item::components::hash_value(kind, bytes, &names)
141-
.map(pomme_item::hash::Hash::as_i32)
142-
.or_else(|| fork_hash(kind));
143-
if let Some(hash) = hash {
144-
hashed.added.push((kind.to_u32(), hash));
129+
if let Some(value) = pomme_item::components::value_of(kind, bytes, &names) {
130+
hashed.added.push((kind.to_u32(), value.hash().as_i32()));
145131
}
146132
}
147133
Some(hashed)

‎pomme-item/src/components/basic.rs‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use pomme_registry::builtin::DataComponentKind;
66

77
use super::Component;
88
use super::codec::{
9-
Nbt, Text, hash_compound, hash_entry, id_enum, registry, string, write_bool, write_f32,
9+
Nbt, Text, compound_value, entry_value, id_enum, registry, string, write_bool, write_f32,
1010
write_i32, write_list, write_registry, write_var_i32,
1111
};
12-
use crate::hash::{Hash, Names, Record};
12+
use crate::hash::{Names, Record, Value};
1313

1414
/// Declares a component that is one var-int, a `ByteBufCodecs.VAR_INT`.
1515
macro_rules! var_int_component {
@@ -32,8 +32,8 @@ macro_rules! var_int_component {
3232
write_var_i32(buf, self.$field);
3333
}
3434

35-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
36-
Some(Hash::int(self.$field))
35+
fn value(&self, _: &dyn Names) -> Option<Value> {
36+
Some(Value::Int(self.$field))
3737
}
3838
}
3939
};
@@ -70,7 +70,7 @@ impl Component for Unbreakable {
7070
fn write(&self, _: &mut Vec<u8>) {}
7171

7272
/// `Unit.CODEC`: an empty map.
73-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
73+
fn value(&self, _: &dyn Names) -> Option<Value> {
7474
Some(Record::new().build())
7575
}
7676
}
@@ -96,8 +96,8 @@ macro_rules! color_component {
9696
write_i32(buf, self.$field);
9797
}
9898

99-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
100-
Some(Hash::int(self.$field))
99+
fn value(&self, _: &dyn Names) -> Option<Value> {
100+
Some(Value::Int(self.$field))
101101
}
102102
}
103103
};
@@ -135,8 +135,8 @@ impl Component for Rarity {
135135
self.write_id(buf);
136136
}
137137

138-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
139-
Some((*self).hash_name())
138+
fn value(&self, _: &dyn Names) -> Option<Value> {
139+
Some((*self).name_value())
140140
}
141141
}
142142

@@ -180,8 +180,8 @@ impl Component for Dye {
180180
self.color.write_id(buf);
181181
}
182182

183-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
184-
Some(self.color.hash_name())
183+
fn value(&self, _: &dyn Names) -> Option<Value> {
184+
Some(self.color.name_value())
185185
}
186186
}
187187

@@ -206,8 +206,8 @@ macro_rules! text_component {
206206
self.name.write(buf);
207207
}
208208

209-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
210-
self.name.hash()
209+
fn value(&self, _: &dyn Names) -> Option<Value> {
210+
self.name.value()
211211
}
212212
}
213213
};
@@ -246,9 +246,9 @@ impl Component for Lore {
246246
write_list(buf, &self.lines, |buf, line| line.write(buf));
247247
}
248248

249-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
250-
let lines = self.lines.iter().map(Text::hash);
251-
Hash::try_list(lines)
249+
fn value(&self, _: &dyn Names) -> Option<Value> {
250+
let lines = self.lines.iter().map(Text::value);
251+
Value::try_list(lines)
252252
}
253253
}
254254

@@ -270,8 +270,8 @@ impl Component for CustomData {
270270
self.nbt.write(buf);
271271
}
272272

273-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
274-
Some(hash_compound(&self.nbt.compound()?, None))
273+
fn value(&self, _: &dyn Names) -> Option<Value> {
274+
Some(compound_value(&self.nbt.compound()?, None))
275275
}
276276
}
277277

@@ -298,13 +298,13 @@ impl Component for TooltipDisplay {
298298
write_list(buf, &self.hidden_components, write_registry);
299299
}
300300

301-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
301+
fn value(&self, _: &dyn Names) -> Option<Value> {
302302
let hidden = &self.hidden_components;
303303
Some(
304304
Record::new()
305305
.flag("hide_tooltip", self.hide_tooltip, false)
306306
.unless("hidden_components", hidden.is_empty(), || {
307-
Hash::list(hidden.iter().map(hash_entry))
307+
Value::list(hidden.iter().map(entry_value))
308308
})
309309
.build(),
310310
)
@@ -341,11 +341,11 @@ impl Component for CustomModelData {
341341
write_list(buf, &self.colors, |buf, v| write_i32(buf, *v));
342342
}
343343

344-
fn hash(&self, _: &dyn Names) -> Option<Hash> {
345-
let floats = || Hash::list(self.floats.iter().map(|v| Hash::float(*v)));
346-
let flags = || Hash::list(self.flags.iter().map(|v| Hash::bool(*v)));
347-
let strings = || Hash::list(self.strings.iter().map(|v| Hash::string(v)));
348-
let colors = || Hash::list(self.colors.iter().map(|v| Hash::int(*v)));
344+
fn value(&self, _: &dyn Names) -> Option<Value> {
345+
let floats = || Value::list(self.floats.iter().map(|v| Value::Float(*v)));
346+
let flags = || Value::list(self.flags.iter().map(|v| Value::Bool(*v)));
347+
let strings = || Value::list(self.strings.iter().map(|v| Value::string(v)));
348+
let colors = || Value::list(self.colors.iter().map(|v| Value::Int(*v)));
349349
Some(
350350
Record::new()
351351
.unless("floats", self.floats.is_empty(), floats)

0 commit comments

Comments
 (0)