azalea_registry/item/
checksum.rs1use azalea_buf::AzBuf;
2use azalea_codec::Codec;
3use thiserror::Error;
4
5use crate::item::components::DataComponent;
6
7#[derive(AzBuf, Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
8pub struct Checksum(pub u32);
9impl Checksum {
10 #[must_use]
11 fn write(mut self, bytes: &[u8]) -> Checksum {
12 self.0 = crc32c::crc32c_append(self.0, bytes);
13 self
14 }
15 fn update(&mut self, bytes: &[u8]) -> &mut Self {
16 *self = self.clone().write(bytes);
17 self
18 }
19}
20
21#[derive(Clone)]
22pub struct ChecksumOps {
23 checksum: Checksum,
24}
25impl ChecksumOps {
26 pub fn new(checksum: Checksum) -> Self {
27 Self { checksum }
28 }
29}
30impl azalea_codec::Ops for ChecksumOps {
31 type List = Vec<ChecksumOps>;
32 type Map = Vec<(ChecksumOps, ChecksumOps)>;
33
34 fn empty() -> azalea_codec::error::WriteResult<Self> {
35 let checksum = Checksum(0).write(&[1]);
36 Ok(Self::new(checksum))
37 }
38 fn create_number(n: azalea_codec::number::Number) -> azalea_codec::error::WriteResult<Self> {
39 use azalea_codec::number::Number as N;
40 let checksum = match n {
41 N::Byte(n) => Checksum(0).write(&[6]).write(&n.to_le_bytes()),
42 N::Short(n) => Checksum(0).write(&[7]).write(&n.to_le_bytes()),
43 N::Int(n) => Checksum(0).write(&[8]).write(&n.to_le_bytes()),
44 N::Long(n) => Checksum(0).write(&[9]).write(&n.to_le_bytes()),
45 N::Float(n) => Checksum(0).write(&[10]).write(&n.to_le_bytes()),
46 N::Double(n) => Checksum(0).write(&[11]).write(&n.to_le_bytes()),
47 };
48 Ok(Self::new(checksum))
49 }
50 fn create_boolean(v: bool) -> azalea_codec::error::WriteResult<Self> {
51 let checksum = Checksum(0).write(&[13, v as u8]);
52 Ok(Self::new(checksum))
53 }
54 fn create_map(map: &[(Self, Self)]) -> azalea_codec::error::WriteResult<Self> {
55 let mut checksum = Checksum(0).write(&[2]);
56
57 let mut map = map.to_owned();
58 map.sort_by_key(|(k, v)| (k.checksum.0, v.checksum.0));
59
60 for (k, v) in map {
61 checksum
62 .update(&k.checksum.0.to_le_bytes())
63 .update(&v.checksum.0.to_le_bytes());
64 }
65 checksum.update(&[3]);
66 Ok(Self::new(checksum))
67 }
68 fn create_list(list: &[Self]) -> azalea_codec::error::WriteResult<Self> {
69 let mut checksum = Checksum(0).write(&[4]);
70 for i in list {
71 checksum.update(&i.checksum.0.to_le_bytes());
72 }
73 checksum = checksum.write(&[5]);
74 Ok(Self::new(checksum))
75 }
76 fn create_string(v: &str) -> azalea_codec::error::WriteResult<Self> {
77 let mut checksum = Checksum(0).write(&[12]);
78 let utf16 = v.encode_utf16().collect::<Box<[u16]>>();
79 checksum.update(&(utf16.len() as u32).to_le_bytes());
80 for c in utf16 {
81 checksum.update(&c.to_le_bytes());
82 }
83 Ok(Self::new(checksum))
84 }
85 fn create_byte_list(v: &[u8]) -> azalea_codec::error::WriteResult<Self> {
86 let checksum = Checksum(0).write(&[14]).write(v).write(&[15]);
87 Ok(Self::new(checksum))
88 }
89 fn create_int_list(v: &[i32]) -> azalea_codec::error::WriteResult<Self> {
90 let mut checksum = Checksum(0).write(&[16]);
91 for i in v {
92 checksum.update(&i.to_le_bytes());
93 }
94 checksum.update(&[17]);
95 Ok(Self::new(checksum))
96 }
97 fn create_long_list(v: &[i64]) -> azalea_codec::error::WriteResult<Self> {
98 let mut checksum = Checksum(0).write(&[18]);
99 for i in v {
100 checksum.update(&i.to_le_bytes());
101 }
102 checksum.update(&[19]);
103 Ok(Self::new(checksum))
104 }
105}
106
107impl<O: azalea_codec::Ops> Codec<O> for ChecksumOps {
108 fn codec_read(_: &O) -> azalea_codec::error::ReadResult<Self> {
109 unimplemented!()
110 }
111 fn codec_write(&self) -> azalea_codec::error::WriteResult<O> {
112 unimplemented!()
113 }
114}
115
116#[derive(Debug, Error)]
117#[error("Checksum serialization error")]
118pub struct ChecksumError;
119
120impl DataComponent {
121 pub fn checksum(&self) -> Checksum {
122 self.codec_write_value::<ChecksumOps>()
123 .expect("writing a component shouldn't fail")
124 .checksum
125 }
126}