Skip to main content

ghast_protocol/
versions.rs

1//! The protocol version table: one row per protocol number this crate knows.
2//!
3//! A row is the protocol layer's view of a version: its number, the generated module, the
4//! game versions that speak it (e.g. 775 = 26.1, 26.1.1 and 26.1.2) and the few wire facts
5//! that differ between versions outside the packet definitions. A number whose wire layout
6//! equals another row's in every packet has a row of its own but that row's module (751 =
7//! 1.16.2 and 753 = 1.16.3 are served by `v1_16_5`, written with their own number in the
8//! handshake). Rows of versions whose Cargo feature is off stay in the table with
9//! `enabled == false`, so a caller can tell "unknown" from "not compiled in".
10//!
11//! Layers above (data sets, physics rules) keep their own columns keyed by the protocol
12//! number or the game version; [`ProtocolVersion`] is `#[non_exhaustive]`, so columns of
13//! this layer can be added without a breaking change.
14
15use crate::types::PositionLayout;
16
17/// One protocol number.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct ProtocolVersion {
21    /// Protocol number sent in the handshake.
22    pub protocol: i32,
23    /// Name of the generated module (`"v1_8"`, `"v26_1"`); for a number without a module of
24    /// its own, the module of the row with the identical layout (751 -> `"v1_16_5"`).
25    pub module: &'static str,
26    /// Cargo feature that compiles the module in.
27    pub feature: &'static str,
28    /// The version the row is named after and tested with (`"1.8.9"`, `"1.16.2"`).
29    pub name: &'static str,
30    /// Every release that speaks this protocol, oldest first (from minecraft-data's
31    /// `protocolVersions.json`).
32    pub game_versions: &'static [&'static str],
33    /// Bit layout of packed block positions.
34    pub position_layout: PositionLayout,
35    /// Whether the version has the configuration state (1.20.2+).
36    pub configuration: bool,
37    /// Whether the module is compiled in (its feature is on).
38    pub enabled: bool,
39}
40
41include!("generated/versions_table.rs");
42
43/// The row of a protocol number.
44#[must_use]
45pub fn by_protocol(protocol: i32) -> Option<&'static ProtocolVersion> {
46    TABLE.iter().find(|v| v.protocol == protocol)
47}
48
49/// The row of a game version name (`"1.8.8"`, `"26.1.2"`).
50#[must_use]
51pub fn by_game_version(name: &str) -> Option<&'static ProtocolVersion> {
52    TABLE.iter().find(|v| v.game_versions.contains(&name))
53}
54
55/// The rows that are compiled in.
56pub fn enabled() -> impl Iterator<Item = &'static ProtocolVersion> {
57    TABLE.iter().filter(|v| v.enabled)
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn table_is_sorted_and_unique() {
66        for w in TABLE.windows(2) {
67            assert!(w[0].protocol < w[1].protocol);
68        }
69        for v in TABLE {
70            assert!(!v.game_versions.is_empty());
71            assert_eq!(by_protocol(v.protocol), Some(v));
72            for g in v.game_versions {
73                assert_eq!(by_game_version(g), Some(v));
74            }
75        }
76        assert_eq!(by_game_version("1.8.8").map(|v| v.protocol), Some(47));
77        assert_eq!(by_game_version("1.16.4").map(|v| v.protocol), Some(754));
78        assert_eq!(
79            by_protocol(775).map(|v| v.game_versions),
80            Some(&["26.1", "26.1.1", "26.1.2"][..])
81        );
82        assert!(by_protocol(775).is_some_and(|v| v.configuration));
83        assert!(by_protocol(1).is_none());
84        // Pre-netty releases with a reused number are not listed.
85        assert!(by_game_version("1.4.2").is_none());
86        // 1.16.2 / 1.16.3 have their own numbers and the 1.16.5 module (identical layouts).
87        for (g, p) in [("1.16.2", 751), ("1.16.3", 753)] {
88            let row = by_game_version(g).unwrap();
89            assert_eq!((row.protocol, row.module, row.name), (p, "v1_16_5", g));
90        }
91        let row = by_game_version("1.12.1").unwrap();
92        assert_eq!(
93            (row.protocol, row.module, row.feature),
94            (338, "v1_12_1", "v1_12_2")
95        );
96    }
97}