mcproto-types 0.5.0

Minecraft protocol types.
Documentation
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Entity metadata entries, value types, and terminated list encoding.

use std::{fmt, io::Read};

use mcproto_codec::error::{CodecError, CodecKind, CodecOperation, InvalidEncodingReason};

use crate::{
    Boolean, Byte, Float, IdOr, PaintingVariant, Position, PrefixedString, ProtocolEnum,
    ResolvableProfile, Slot, TextComponent, TypeCodec, UnsignedByte, VarInt, VarLong,
};

use super::{
    ArmadilloState, BlockStateId, CatSoundVariantId, CatVariantId, ChickenSoundVariantId,
    ChickenVariantId, CopperGolemState, CowSoundVariantId, CowVariantId, Direction, FrogVariantId,
    HumanoidArm, OptionalBlockState, OptionalGlobalPosition, OptionalLivingEntityReference,
    OptionalPosition, OptionalTextComponent, OptionalVarInt, Particle, Particles,
    PigSoundVariantId, PigVariantId, Pose, Quaternion, Rotations, SnifferState, Vector3,
    VillagerData, WeatheringCopperState, WolfSoundVariantId, WolfVariantId,
    ZombieNautilusVariantId,
};

/// Error returned when `0xff` is used as an entity metadata entry index.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InvalidEntityMetadataIndex {
    index: u8,
}

impl InvalidEntityMetadataIndex {
    /// Returns the rejected index.
    #[must_use]
    pub const fn index(self) -> u8 {
        self.index
    }
}

impl fmt::Display for InvalidEntityMetadataIndex {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "entity metadata index 0x{:02X} is reserved as the terminator",
            self.index
        )
    }
}

impl std::error::Error for InvalidEntityMetadataIndex {}

/// A valid entity metadata index in the inclusive range `0..=254`.
///
/// Byte value `0xff` cannot be constructed because it terminates the complete
/// [`EntityMetadata`] sequence.
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EntityMetadataIndex(u8);

impl EntityMetadataIndex {
    /// Largest index available to an entry.
    pub const MAX: u8 = 0xfe;
    /// Reserved byte that terminates an entity metadata sequence.
    pub const TERMINATOR: u8 = 0xff;

    /// Creates a valid metadata index.
    pub const fn new(index: u8) -> Result<Self, InvalidEntityMetadataIndex> {
        if index == Self::TERMINATOR {
            Err(InvalidEntityMetadataIndex { index })
        } else {
            Ok(Self(index))
        }
    }

    /// Returns the raw unsigned-byte index.
    #[must_use]
    pub const fn get(self) -> u8 {
        self.0
    }
}

impl TryFrom<u8> for EntityMetadataIndex {
    type Error = InvalidEntityMetadataIndex;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<EntityMetadataIndex> for u8 {
    fn from(value: EntityMetadataIndex) -> Self {
        value.get()
    }
}

impl TypeCodec for EntityMetadataIndex {
    fn encode(&self, writer: &mut impl std::io::Write) -> Result<(), CodecError> {
        UnsignedByte(self.0)
            .encode(writer)
            .map_err(|error| error.with_context(CodecKind::EntityMetadataEntry))
    }

    fn decode(reader: &mut impl Read) -> Result<Self, CodecError> {
        let index = UnsignedByte::decode(reader)
            .map_err(|error| error.with_context(CodecKind::EntityMetadataEntry))?
            .0;
        Self::new(index).map_err(|_| {
            CodecError::invalid_encoding(
                CodecKind::EntityMetadataEntry,
                1,
                InvalidEncodingReason::InvalidEntityMetadataIndex { index },
            )
        })
    }
}

macro_rules! define_metadata_values {
    ($($id:literal => $variant:ident($payload:ty) = $name:literal,)*) => {
        /// Numeric value-type selector used by an entity metadata entry.
        ///
        /// Storing this selector separately from a payload is unnecessary and
        /// could permit mismatches. [`EntityMetadataValue`] is therefore the
        /// primary wire value; this enum is exposed for inspection and registry
        /// mapping, and is returned by [`EntityMetadataValue::value_type`].
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ProtocolEnum)]
        #[protocol_enum(repr = VarInt)]
        pub enum EntityMetadataValueType {
            $(
                #[doc = concat!("The `", $name, "` value type (ID `", stringify!($id), "`).")]
                $variant = $id,
            )*
        }

        /// A metadata type ID bound to exactly the payload required by that type.
        ///
        /// Every variant fixes both the VarInt type selector and its payload,
        /// so an ID/value-layout mismatch cannot be represented in memory.
        #[derive(Debug, Clone, PartialEq)]
        pub enum EntityMetadataValue {
            $(
                #[doc = concat!("A `", $name, "` metadata value.")]
                $variant($payload),
            )*
        }

        impl EntityMetadataValue {
            /// Returns this value's protocol type selector.
            #[must_use]
            pub const fn value_type(&self) -> EntityMetadataValueType {
                match self {
                    $(Self::$variant(_) => EntityMetadataValueType::$variant,)*
                }
            }

            /// Returns the numeric VarInt type ID.
            #[must_use]
            pub fn type_id(&self) -> i32 {
                self.value_type().discriminant() as i32
            }
        }

        impl TypeCodec for EntityMetadataValue {
            fn encode(
                &self,
                writer: &mut impl std::io::Write,
            ) -> Result<(), CodecError> {
                self.value_type()
                    .encode(writer)
                    .map_err(with_value_context)?;
                match self {
                    $(Self::$variant(value) => value.encode(writer).map_err(with_value_context),)*
                }
            }

            fn decode(reader: &mut impl Read) -> Result<Self, CodecError> {
                let value_type = EntityMetadataValueType::decode(reader)
                    .map_err(with_value_context)?;
                match value_type {
                    $(
                        EntityMetadataValueType::$variant => <$payload>::decode(reader)
                            .map(Self::$variant)
                            .map_err(with_value_context),
                    )*
                }
            }
        }
    };
}

define_metadata_values! {
    0 => Byte(Byte) = "Byte",
    1 => VarInt(VarInt) = "VarInt",
    2 => VarLong(VarLong) = "VarLong",
    3 => Float(Float) = "Float",
    4 => String(PrefixedString) = "String",
    5 => TextComponent(TextComponent) = "Text Component",
    6 => OptionalTextComponent(OptionalTextComponent) = "Optional Text Component",
    7 => Slot(Slot) = "Slot",
    8 => Boolean(Boolean) = "Boolean",
    9 => Rotations(Rotations) = "Rotations",
    10 => Position(Position) = "Position",
    11 => OptionalPosition(OptionalPosition) = "Optional Position",
    12 => Direction(Direction) = "Direction",
    13 => OptionalLivingEntityReference(OptionalLivingEntityReference) = "Optional Living Entity Reference",
    14 => BlockState(BlockStateId) = "Block State",
    15 => OptionalBlockState(OptionalBlockState) = "Optional Block State",
    16 => Particle(Particle) = "Particle",
    17 => Particles(Particles) = "Particles",
    18 => VillagerData(VillagerData) = "Villager Data",
    19 => OptionalVarInt(OptionalVarInt) = "Optional VarInt",
    20 => Pose(Pose) = "Pose",
    21 => CatVariant(CatVariantId) = "Cat Variant",
    22 => CatSoundVariant(CatSoundVariantId) = "Cat Sound Variant",
    23 => CowVariant(CowVariantId) = "Cow Variant",
    24 => CowSoundVariant(CowSoundVariantId) = "Cow Sound Variant",
    25 => WolfVariant(WolfVariantId) = "Wolf Variant",
    26 => WolfSoundVariant(WolfSoundVariantId) = "Wolf Sound Variant",
    27 => FrogVariant(FrogVariantId) = "Frog Variant",
    28 => PigVariant(PigVariantId) = "Pig Variant",
    29 => PigSoundVariant(PigSoundVariantId) = "Pig Sound Variant",
    30 => ChickenVariant(ChickenVariantId) = "Chicken Variant",
    31 => ChickenSoundVariant(ChickenSoundVariantId) = "Chicken Sound Variant",
    32 => ZombieNautilusVariant(ZombieNautilusVariantId) = "Zombie Nautilus Variant",
    33 => OptionalGlobalPosition(OptionalGlobalPosition) = "Optional Global Position",
    34 => PaintingVariant(IdOr<PaintingVariant>) = "Painting Variant",
    35 => SnifferState(SnifferState) = "Sniffer State",
    36 => ArmadilloState(ArmadilloState) = "Armadillo State",
    37 => CopperGolemState(CopperGolemState) = "Copper Golem State",
    38 => WeatheringCopperState(WeatheringCopperState) = "Weathering Copper State",
    39 => Vector3(Vector3) = "Vector3",
    40 => Quaternion(Quaternion) = "Quaternion",
    41 => ResolvableProfile(ResolvableProfile) = "Resolvable Profile",
    42 => HumanoidArm(HumanoidArm) = "Humanoid Arm",
}

fn with_value_context(error: CodecError) -> CodecError {
    if error.context() == Some(CodecKind::EntityMetadataValue) {
        error
    } else {
        error.with_context(CodecKind::EntityMetadataValue)
    }
}

/// One index and one type-safe value in an entity metadata sequence.
#[derive(Debug, Clone, PartialEq)]
pub struct EntityMetadataEntry {
    index: EntityMetadataIndex,
    value: EntityMetadataValue,
}

impl EntityMetadataEntry {
    /// Creates an entry from an already validated index.
    #[must_use]
    pub const fn new(index: EntityMetadataIndex, value: EntityMetadataValue) -> Self {
        Self { index, value }
    }

    /// Creates an entry from a raw index, rejecting the `0xff` terminator.
    pub fn try_new(
        index: u8,
        value: EntityMetadataValue,
    ) -> Result<Self, InvalidEntityMetadataIndex> {
        Ok(Self::new(EntityMetadataIndex::new(index)?, value))
    }

    /// Returns this entry's unique index key.
    #[must_use]
    pub const fn index(&self) -> EntityMetadataIndex {
        self.index
    }

    /// Returns this entry's typed value.
    #[must_use]
    pub const fn value(&self) -> &EntityMetadataValue {
        &self.value
    }

    /// Extracts the typed value.
    #[must_use]
    pub fn into_value(self) -> EntityMetadataValue {
        self.value
    }
}

impl TypeCodec for EntityMetadataEntry {
    fn encode(&self, writer: &mut impl std::io::Write) -> Result<(), CodecError> {
        self.index.encode(writer)?;
        self.value
            .encode(writer)
            .map_err(|error| error.with_context(CodecKind::EntityMetadataEntry))
    }

    fn decode(reader: &mut impl Read) -> Result<Self, CodecError> {
        let index = EntityMetadataIndex::decode(reader)?;
        let value = EntityMetadataValue::decode(reader)
            .map_err(|error| error.with_context(CodecKind::EntityMetadataEntry))?;
        Ok(Self { index, value })
    }
}

/// Error returned when an entity metadata sequence repeats an index.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DuplicateEntityMetadataIndex {
    index: EntityMetadataIndex,
}

impl DuplicateEntityMetadataIndex {
    /// Returns the repeated index.
    #[must_use]
    pub const fn index(self) -> EntityMetadataIndex {
        self.index
    }
}

impl fmt::Display for DuplicateEntityMetadataIndex {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "duplicate entity metadata index {}",
            self.index.get()
        )
    }
}

impl std::error::Error for DuplicateEntityMetadataIndex {}

/// A complete `0xff`-terminated entity metadata sequence.
///
/// Entry indices are unique and can only be in `0..=254`; the list codec always
/// appends `0xff` and consumes no bytes after that terminator. Metadata fields
/// may be omitted, so an empty value is encoded as the terminator alone.
///
/// # Examples
///
/// ```
/// use mcproto_types::{
///     Byte, EntityMetadata, EntityMetadataEntry, EntityMetadataValue, TypeCodec,
/// };
///
/// let metadata = EntityMetadata::new(vec![EntityMetadataEntry::try_new(
///     0,
///     EntityMetadataValue::Byte(Byte(0x20)),
/// )?])?;
///
/// let mut encoded = Vec::new();
/// metadata.encode(&mut encoded)?;
/// assert_eq!(encoded, [0x00, 0x00, 0x20, 0xff]);
///
/// let mut input = encoded.as_slice();
/// assert_eq!(EntityMetadata::decode(&mut input)?, metadata);
/// assert!(input.is_empty());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// See the official [Entity Metadata Format] documentation.
///
/// [Entity Metadata Format]: https://minecraft.wiki/w/Java_Edition_protocol/Entity_metadata#Entity_Metadata_Format
#[derive(Debug, Clone, PartialEq, Default)]
pub struct EntityMetadata {
    entries: Vec<EntityMetadataEntry>,
}

impl EntityMetadata {
    /// Creates a metadata sequence after checking that all indices are unique.
    pub fn new(entries: Vec<EntityMetadataEntry>) -> Result<Self, DuplicateEntityMetadataIndex> {
        validate_unique_indices(&entries)?;
        Ok(Self { entries })
    }

    /// Returns the entries in wire order.
    #[must_use]
    pub fn entries(&self) -> &[EntityMetadataEntry] {
        &self.entries
    }

    /// Extracts the entries in wire order.
    #[must_use]
    pub fn into_entries(self) -> Vec<EntityMetadataEntry> {
        self.entries
    }

    /// Returns the number of metadata entries, excluding the terminator.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns whether this sequence contains no entries.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Returns the entry at `index`, if present.
    #[must_use]
    pub fn get(&self, index: EntityMetadataIndex) -> Option<&EntityMetadataEntry> {
        self.entries.iter().find(|entry| entry.index == index)
    }

    /// Appends an entry if its index is not already present.
    pub fn push(&mut self, entry: EntityMetadataEntry) -> Result<(), DuplicateEntityMetadataIndex> {
        if self.get(entry.index).is_some() {
            return Err(DuplicateEntityMetadataIndex { index: entry.index });
        }
        self.entries.push(entry);
        Ok(())
    }
}

impl TypeCodec for EntityMetadata {
    fn encode(&self, writer: &mut impl std::io::Write) -> Result<(), CodecError> {
        if let Err(error) = validate_unique_indices(&self.entries) {
            return Err(CodecError::invalid_encoding_for_operation(
                CodecKind::EntityMetadata,
                CodecOperation::Write,
                0,
                InvalidEncodingReason::DuplicateEntityMetadataIndex {
                    index: error.index.get(),
                },
            ));
        }
        for entry in &self.entries {
            entry
                .encode(writer)
                .map_err(|error| error.with_context(CodecKind::EntityMetadata))?;
        }
        UnsignedByte(EntityMetadataIndex::TERMINATOR)
            .encode(writer)
            .map_err(|error| error.with_context(CodecKind::EntityMetadata))
    }

    fn decode(reader: &mut impl Read) -> Result<Self, CodecError> {
        let mut entries = Vec::new();
        let mut seen = [false; EntityMetadataIndex::TERMINATOR as usize];
        loop {
            let index = UnsignedByte::decode(reader)
                .map_err(|error| error.with_context(CodecKind::EntityMetadata))?
                .0;
            if index == EntityMetadataIndex::TERMINATOR {
                return Ok(Self { entries });
            }
            if seen[index as usize] {
                return Err(CodecError::invalid_encoding(
                    CodecKind::EntityMetadata,
                    0,
                    InvalidEncodingReason::DuplicateEntityMetadataIndex { index },
                ));
            }
            seen[index as usize] = true;
            let value = EntityMetadataValue::decode(reader)
                .map_err(|error| error.with_context(CodecKind::EntityMetadataEntry))
                .map_err(|error| error.with_context(CodecKind::EntityMetadata))?;
            entries.push(EntityMetadataEntry {
                index: EntityMetadataIndex(index),
                value,
            });
        }
    }
}

fn validate_unique_indices(
    entries: &[EntityMetadataEntry],
) -> Result<(), DuplicateEntityMetadataIndex> {
    let mut seen = [false; EntityMetadataIndex::TERMINATOR as usize];
    for entry in entries {
        let index = entry.index.get() as usize;
        if seen[index] {
            return Err(DuplicateEntityMetadataIndex { index: entry.index });
        }
        seen[index] = true;
    }
    Ok(())
}