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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
use super::{UnsizedByteArray, MAX_STRING_LENGTH};
use async_trait::async_trait;
use azalea_chat::component::Component;
use azalea_core::{
difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
serializable_uuid::SerializableUuid, Slot,
};
use byteorder::{BigEndian, WriteBytesExt};
use std::io::Write;
use uuid::Uuid;
#[async_trait]
pub trait Writable {
fn write_list<F, T>(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error>
where
F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy,
T: Sized,
Self: Sized;
fn write_int_id_list(&mut self, list: &Vec<i32>) -> Result<(), std::io::Error>;
fn write_map<KF, VF, KT, VT>(
&mut self,
map: Vec<(KT, VT)>,
key_writer: KF,
value_writer: VF,
) -> Result<(), std::io::Error>
where
KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy,
VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy,
Self: Sized;
fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error>;
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>;
fn write_varint(&mut self, value: i32) -> Result<(), std::io::Error>;
fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error>;
fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>;
fn write_short(&mut self, n: i16) -> Result<(), std::io::Error>;
fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>;
fn write_int(&mut self, n: i32) -> Result<(), std::io::Error>;
fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error>;
fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error>;
fn write_long(&mut self, n: i64) -> Result<(), std::io::Error>;
fn write_resource_location(
&mut self,
location: &ResourceLocation,
) -> Result<(), std::io::Error>;
fn write_float(&mut self, n: f32) -> Result<(), std::io::Error>;
fn write_double(&mut self, n: f64) -> Result<(), std::io::Error>;
fn write_uuid(&mut self, uuid: &Uuid) -> Result<(), std::io::Error>;
}
#[async_trait]
impl Writable for Vec<u8> {
fn write_list<F, T>(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error>
where
F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy,
Self: Sized,
{
self.write_varint(list.len() as i32)?;
for item in list {
writer(self, item)?;
}
Ok(())
}
fn write_int_id_list(&mut self, list: &Vec<i32>) -> Result<(), std::io::Error> {
self.write_list(&list, |buf, n| buf.write_varint(*n))
}
fn write_map<KF, VF, KT, VT>(
&mut self,
map: Vec<(KT, VT)>,
key_writer: KF,
value_writer: VF,
) -> Result<(), std::io::Error>
where
KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy,
VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy,
Self: Sized,
{
self.write_varint(map.len() as i32)?;
for (key, value) in map {
key_writer(self, key)?;
value_writer(self, value)?;
}
Ok(())
}
fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error> {
WriteBytesExt::write_u8(self, n)
}
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> {
self.extend_from_slice(bytes);
Ok(())
}
fn write_varint(&mut self, mut value: i32) -> Result<(), std::io::Error> {
let mut buffer = [0];
if value == 0 {
self.write_all(&buffer).unwrap();
}
while value != 0 {
buffer[0] = (value & 0b0111_1111) as u8;
value = (value >> 7) & (i32::max_value() >> 6);
if value != 0 {
buffer[0] |= 0b1000_0000;
}
self.write_all(&buffer)?;
}
Ok(())
}
fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error> {
if string.len() > len {
panic!(
"String too big (was {} bytes encoded, max {})",
string.len(),
len
);
}
self.write_varint(string.len() as i32)?;
self.write_bytes(string.as_bytes())
}
fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error> {
self.write_utf_with_len(string, MAX_STRING_LENGTH.into())
}
fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> {
WriteBytesExt::write_i16::<BigEndian>(self, n)
}
fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> {
self.write_varint(bytes.len() as i32)?;
self.write_bytes(bytes)
}
fn write_int(&mut self, n: i32) -> Result<(), std::io::Error> {
WriteBytesExt::write_i32::<BigEndian>(self, n)
}
fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error> {
self.write_byte(if b { 1 } else { 0 })
}
fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error> {
nbt.write(self)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
}
fn write_long(&mut self, n: i64) -> Result<(), std::io::Error> {
WriteBytesExt::write_i64::<BigEndian>(self, n)
}
fn write_float(&mut self, n: f32) -> Result<(), std::io::Error> {
WriteBytesExt::write_f32::<BigEndian>(self, n)
}
fn write_double(&mut self, n: f64) -> Result<(), std::io::Error> {
WriteBytesExt::write_f64::<BigEndian>(self, n)
}
fn write_resource_location(
&mut self,
location: &ResourceLocation,
) -> Result<(), std::io::Error> {
self.write_utf(&location.to_string())
}
fn write_uuid(&mut self, uuid: &Uuid) -> Result<(), std::io::Error> {
let [a, b, c, d] = uuid.to_int_array();
a.write_into(self)?;
b.write_into(self)?;
c.write_into(self)?;
d.write_into(self)?;
Ok(())
}
}
pub trait McBufWritable
where
Self: Sized,
{
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error>;
}
pub trait McBufVarintWritable
where
Self: Sized,
{
fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error>;
}
impl McBufWritable for i32 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
Writable::write_int(buf, *self)
}
}
impl McBufVarintWritable for i32 {
fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_varint(*self)
}
}
impl McBufWritable for UnsizedByteArray {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_bytes(self)
}
}
// TODO: use specialization when that gets stabilized into rust
// to optimize for Vec<u8> byte arrays
impl<T: McBufWritable> McBufWritable for Vec<T> {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_list(self, |buf, i| T::write_into(i, buf))
}
}
// string
impl McBufWritable for String {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_utf(self)
}
}
// ResourceLocation
impl McBufWritable for ResourceLocation {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_resource_location(self)
}
}
// u32
impl McBufWritable for u32 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
i16::write_into(&(*self as i16), buf)
}
}
// u32 varint
impl McBufVarintWritable for u32 {
fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
i32::varint_write_into(&(*self as i32), buf)
}
}
// u16
impl McBufWritable for u16 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
i16::write_into(&(*self as i16), buf)
}
}
// u16 varint
impl McBufVarintWritable for u16 {
fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
i32::varint_write_into(&(*self as i32), buf)
}
}
// u8
impl McBufWritable for u8 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_byte(*self)
}
}
// i16
impl McBufWritable for i16 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
Writable::write_short(buf, *self)
}
}
// i64
impl McBufWritable for i64 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
Writable::write_long(buf, *self)
}
}
// u64
impl McBufWritable for u64 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
i64::write_into(&(*self as i64), buf)
}
}
// bool
impl McBufWritable for bool {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_boolean(*self)
}
}
// i8
impl McBufWritable for i8 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_byte(*self as u8)
}
}
// f32
impl McBufWritable for f32 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_float(*self)
}
}
// f64
impl McBufWritable for f64 {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_double(*self)
}
}
// GameType
impl McBufWritable for GameType {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
u8::write_into(&self.to_id(), buf)
}
}
// Option<GameType>
impl McBufWritable for Option<GameType> {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_byte(GameType::to_optional_id(self) as u8)
}
}
// Option<String>
impl McBufWritable for Option<String> {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
if let Some(s) = self {
buf.write_boolean(true)?;
buf.write_utf(s)?;
} else {
buf.write_boolean(false)?;
};
Ok(())
}
}
// Option<Component>
impl McBufWritable for Option<Component> {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
if let Some(s) = self {
buf.write_boolean(true)?;
s.write_into(buf)?;
} else {
buf.write_boolean(false)?;
};
Ok(())
}
}
// azalea_nbt::Tag
impl McBufWritable for azalea_nbt::Tag {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_nbt(self)
}
}
// Difficulty
impl McBufWritable for Difficulty {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
u8::write_into(&self.id(), buf)
}
}
// Component
#[async_trait]
impl McBufWritable for Component {
// async fn read_into<R>(buf: &mut R) -> Result<Self, String>
// where
// R: AsyncRead + std::marker::Unpin + std::marker::Send,
// {
// let string = buf.read_utf().await?;
// let json: serde_json::Value = serde_json::from_str(string.as_str())
// .map_err(|e| "Component isn't valid JSON".to_string())?;
// let component = Component::deserialize(json).map_err(|e| e.to_string())?;
// Ok(component)
// }
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
// component doesn't have serialize implemented yet
todo!()
}
}
// Slot
impl McBufWritable for Slot {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
match self {
Slot::Empty => buf.write_byte(0)?,
Slot::Present(i) => {
buf.write_varint(i.id)?;
buf.write_byte(i.count)?;
buf.write_nbt(&i.nbt)?;
}
}
Ok(())
}
}
// Slot
impl McBufWritable for Uuid {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_uuid(self)?;
Ok(())
}
}
|