-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathmctypes.rs
More file actions
386 lines (333 loc) · 11.3 KB
/
Copy pathmctypes.rs
File metadata and controls
386 lines (333 loc) · 11.3 KB
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
use crate::bytes_ext::{BytesExt, BytesMutExt, TryGetError};
use bytes::{Buf, BytesMut};
use feather_entity_metadata::{EntityMetadata, MetaEntry};
use feather_items::{Item, ItemStack};
use feather_util::BlockPosition;
use feather_util::Direction;
use num_traits::FromPrimitive;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::BTreeMap;
use std::io::Read;
use uuid::Uuid;
/// Identifies a type to which Minecraft-specific
/// types (`VarInt`, `VarLong`, etc.) can be written.
pub trait McTypeWrite {
/// Writes a `VarInt` to the object. See wiki.vg for
/// details on `VarInt`s and related types.
///
/// Returns the number of bytes used to encode this integer.
fn push_var_int(&mut self, x: i32) -> usize;
/// Writes a string to the object. This method
/// will first write the length of the string in bytes
/// encodes as a `VarInt` and will then write
/// the UTF-8 bytes of the string.
fn push_string(&mut self, x: &str);
fn push_position(&mut self, x: &BlockPosition);
fn push_bool(&mut self, x: bool);
fn push_uuid(&mut self, x: &Uuid);
fn push_nbt<T: Serialize>(&mut self, x: &T);
fn push_slot(&mut self, slot: Option<ItemStack>);
}
/// Identifies a type from which Minecraft-specified
/// types can be read.
pub trait McTypeRead {
/// Reads a `VarInt` from this object, returning
/// `Some(x)` if successful or `None` if the object
/// does not contain a valid `VarInt`.
fn try_get_var_int(&mut self) -> Result<i32, TryGetError>;
/// Reads a string from the object.
fn try_get_string(&mut self) -> Result<String, TryGetError>;
fn try_get_position(&mut self) -> Result<BlockPosition, TryGetError>;
fn try_get_bool(&mut self) -> Result<bool, TryGetError>;
fn try_get_uuid(&mut self) -> anyhow::Result<Uuid>;
fn try_get_nbt<T: DeserializeOwned>(&mut self) -> Result<T, nbt::Error>;
fn try_get_slot(&mut self) -> Result<Option<ItemStack>, TryGetError>;
}
impl McTypeWrite for BytesMut {
fn push_var_int(&mut self, mut x: i32) -> usize {
let mut bytes_written = 0;
loop {
let mut temp = (x & 0b0111_1111) as u8;
x >>= 7;
if x != 0 {
temp |= 0b1000_0000;
}
self.push_u8(temp);
bytes_written += 1;
if x == 0 {
break;
}
}
bytes_written
}
/// Writes a string to the object. This method
/// will first write the length of the string in bytes
/// encodes as a `VarInt` and will then write
/// the UTF-8 bytes of the string.
fn push_string(&mut self, x: &str) {
let bytes = x.as_bytes();
self.push_var_int(bytes.len() as i32);
self.extend_from_slice(bytes);
}
fn push_position(&mut self, x: &BlockPosition) {
let result: u64 = ((x.x as u64 & 0x03FF_FFFF) << 38)
| ((x.y as u64 & 0xFFF) << 26)
| (x.z as u64 & 0x03FF_FFFF);
self.push_u64(result);
}
fn push_bool(&mut self, x: bool) {
if x {
self.push_u8(1);
} else {
self.push_u8(0);
}
}
fn push_uuid(&mut self, x: &Uuid) {
self.extend_from_slice(&x.as_bytes()[..]);
}
fn push_nbt<T: Serialize>(&mut self, val: &T) {
// TODO: fix inefficient use of temp buf.
let mut temp = vec![];
nbt::to_writer(&mut temp, val, None).unwrap(); // Unwrap is safe because writing would only fail if a struct couldn't be written
self.extend_from_slice(&temp);
}
fn push_slot(&mut self, slot: Option<ItemStack>) {
self.push_bool(slot.is_some());
if let Some(slot) = slot.as_ref() {
self.push_var_int(slot.ty.native_protocol_id());
self.push_i8(slot.amount as i8);
self.push_i8(0x00); // TAG_End - TODO item NBT support
}
}
}
impl<B: Buf + Read> McTypeRead for B {
/// Reads a `VarInt` from this object, returning
/// `Some(x)` if successful or `None` if the object
/// does not contain a valid `VarInt`.
fn try_get_var_int(&mut self) -> Result<i32, TryGetError> {
let mut num_read = 0;
let mut result = 0;
loop {
if self.remaining() == 0 {
return Err(TryGetError::NotEnoughBytes);
}
let read = self.try_get_u8()?;
let value = i32::from(read & 0b0111_1111);
result |= value.overflowing_shl(7u32 * num_read).0;
num_read += 1;
if num_read > 5 {
return Err(TryGetError::NotEnoughBytes);
}
if read & 0b1000_0000 == 0 {
break;
}
}
Ok(result)
}
/// Reads a string from the object.
fn try_get_string(&mut self) -> Result<String, TryGetError> {
let len = self.try_get_var_int();
if let Ok(len) = len {
// Check that the client isn't trying
// to make the server allocate ridiculous
// amounts of memory
if len > 32767 {
return Err(TryGetError::ValueTooLarge);
}
if self.remaining() < len as usize {
return Err(TryGetError::NotEnoughBytes);
}
let mut result = String::with_capacity(len as usize);
self.take(len as u64)
.read_to_string(&mut result)
.map_err(|_| TryGetError::NotEnoughBytes)?;
return Ok(result);
}
Err(TryGetError::NotEnoughBytes)
}
fn try_get_position(&mut self) -> Result<BlockPosition, TryGetError> {
let val = self.try_get_i64()?;
let x = val >> 38;
let y = (val >> 26) & 0xFFF;
let z = val << 38 >> 38;
Ok(BlockPosition::new(x as i32, y as i32, z as i32))
}
fn try_get_bool(&mut self) -> Result<bool, TryGetError> {
let byte = self.try_get_i8()?;
match byte {
0 => Ok(false),
1 => Ok(true),
x => Err(TryGetError::InvalidValue(i32::from(x))),
}
}
fn try_get_uuid(&mut self) -> anyhow::Result<Uuid> {
let mut bytes = [0u8; 16];
self.read_exact(&mut bytes)?;
Ok(Uuid::from_bytes(bytes))
}
fn try_get_nbt<D: DeserializeOwned>(&mut self) -> Result<D, nbt::Error> {
nbt::from_reader(self)
}
fn try_get_slot(&mut self) -> Result<Option<ItemStack>, TryGetError> {
let present = self.try_get_bool()?;
if !present {
return Ok(None);
}
let id = self.try_get_var_int()?;
let ty = Item::from_native_protocol_id(id).ok_or(TryGetError::InvalidValue(id))?;
let amount = self.try_get_i8()? as u8;
// TODO NBT support
Ok(Some(ItemStack::new(ty, amount)))
}
}
pub trait EntityMetaWrite {
fn push_metadata(&mut self, meta: &EntityMetadata);
}
pub trait EntityMetaRead {
fn try_get_metadata(&mut self) -> anyhow::Result<EntityMetadata>;
}
impl<B> EntityMetaWrite for B
where
B: BytesMutExt + McTypeWrite,
{
fn push_metadata(&mut self, meta: &EntityMetadata) {
for (index, entry) in meta.iter() {
self.push_u8(index);
self.push_var_int(entry.id());
write_entry_to_buf(entry, self);
}
self.push_u8(0xff); // End of metadata
}
}
impl<B> EntityMetaRead for B
where
B: Buf + std::io::Read,
{
fn try_get_metadata(&mut self) -> anyhow::Result<EntityMetadata> {
let mut values = BTreeMap::new();
while self.has_remaining() {
let index = self.try_get_u8()?;
if index == 0xFF {
break;
}
let entry = try_get_entry(self)?;
values.insert(index, entry);
}
Ok(EntityMetadata { values })
}
}
fn write_entry_to_buf<B>(entry: &MetaEntry, buf: &mut B)
where
B: BytesMutExt + McTypeWrite,
{
match entry {
MetaEntry::Byte(x) => buf.push_i8(*x),
MetaEntry::VarInt(x) => {
buf.push_var_int(*x);
}
MetaEntry::Float(x) => buf.push_f32(*x),
MetaEntry::String(x) => buf.push_string(x),
MetaEntry::Chat(x) => buf.push_string(x),
MetaEntry::OptChat(ox) => {
if let Some(x) = ox {
buf.push_bool(true);
buf.push_string(x);
} else {
buf.push_bool(false);
}
}
MetaEntry::Slot(slot) => {
buf.push_slot(*slot);
}
MetaEntry::Boolean(x) => buf.push_bool(*x),
MetaEntry::Rotation(x, y, z) => {
buf.push_f32(*x);
buf.push_f32(*y);
buf.push_f32(*z);
}
MetaEntry::Position(x) => buf.push_position(x),
MetaEntry::OptPosition(ox) => {
if let Some(x) = ox {
buf.push_bool(true);
buf.push_position(x);
} else {
buf.push_bool(false);
}
}
MetaEntry::Direction(x) => {
buf.push_var_int(x.id());
}
MetaEntry::OptUuid(ox) => {
if let Some(x) = ox {
buf.push_bool(true);
buf.push_uuid(x);
} else {
buf.push_bool(false);
}
}
MetaEntry::OptBlockId(ox) => {
if let Some(x) = ox {
buf.push_var_int(*x);
} else {
buf.push_var_int(0); // No value implies air
}
}
MetaEntry::Nbt(val) => buf.push_nbt(val),
MetaEntry::Particle => unimplemented!(),
}
}
fn try_get_entry<B>(buf: &mut B) -> anyhow::Result<MetaEntry>
where
B: Buf + McTypeRead,
{
let id = buf.try_get_var_int()?;
Ok(match id {
0 => MetaEntry::Byte(buf.try_get_i8()?),
1 => MetaEntry::VarInt(buf.try_get_var_int()?),
2 => MetaEntry::Float(buf.try_get_f32()?),
3 => MetaEntry::String(buf.try_get_string()?),
4 => MetaEntry::Chat(buf.try_get_string()?),
5 => MetaEntry::OptChat(if buf.try_get_bool()? {
Some(buf.try_get_string()?)
} else {
None
}),
6 => MetaEntry::Slot(buf.try_get_slot()?),
7 => MetaEntry::Boolean(buf.try_get_bool()?),
8 => MetaEntry::Rotation(buf.try_get_f32()?, buf.try_get_f32()?, buf.try_get_f32()?),
9 => MetaEntry::Position(buf.try_get_position()?),
10 => MetaEntry::OptPosition(if buf.try_get_bool()? {
Some(buf.try_get_position()?)
} else {
None
}),
11 => MetaEntry::Direction(
Direction::from_i32(buf.try_get_var_int()?).ok_or(TryGetError::InvalidValue(0))?,
),
12 => MetaEntry::OptUuid(if buf.try_get_bool()? {
Some(buf.try_get_uuid()?)
} else {
None
}),
13 => MetaEntry::OptBlockId(if buf.try_get_bool()? {
Some(buf.try_get_var_int()?)
} else {
None
}),
14 => MetaEntry::Nbt(buf.try_get_nbt()?),
x => return Err(TryGetError::InvalidValue(x).into()),
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn test_read_var_int() {
// Examples from wiki.vg
let mut buf = BytesMut::new();
buf.extend_from_slice(&[0xff, 0x01]);
assert_eq!(Cursor::new(&buf).try_get_var_int(), Ok(255));
}
}