Skip to main content

ghast_protocol/
lib.rs

1//! Minecraft Java Edition network protocol for the ghast client library.
2//!
3//! Layers, bottom to top:
4//!
5//! - [`codec`]: bounds-checked [`Reader`] over [`bytes::Bytes`] (zero-copy byte arrays
6//!   and strings), [`McWrite`] for encoding, VarInt/VarLong.
7//! - [`types`], [`nbt`], [`chat`]: hand-written shared types — [`BlockPos`] with the
8//!   per-version bit layout, slots, lazily parsed NBT ([`nbt::RawNbt`]), chat JSON.
9//! - Generated packet modules per protocol version (`v1_8` = protocol 47, `v1_12_1` = 338,
10//!   `v1_12_2` = 340, `v1_16_5` = 754, `v26_1` = 775, `v26_2` = 776, `v26_3` = 777), each with typed
11//!   packets, one enum per state
12//!   and direction, and id constants. The definitions themselves live once in [`defs`]
13//!   and are shared by every version whose code for them is identical. A [`Version`]
14//!   marker type ties a version's packet sets together so a version-agnostic layer can be
15//!   written generically; [`versions`] is the table of known protocol numbers.
16//! - [`frame`]: length framing, zlib compression with partial inflation, the
17//!   AES-128-CFB8 hook ([`crypto`]), and the per-id [`PacketFilter`] that drops unwanted
18//!   packets before they are decoded.
19//! - [`io`] (feature `tokio`): async framed reader/writer.
20//!
21//! Decoding never panics on untrusted input: every failure is a [`DecodeError`] or
22//! [`FrameError`], and allocations are bounded by the input size.
23//!
24//! ```
25//! use ghast_protocol::{FrameDecoder, FrameEncoder, PacketSet, RawPacket};
26//! use ghast_protocol::v1_8::play::serverbound::{KeepAlive, ServerboundPlayPacket};
27//!
28//! let mut enc = FrameEncoder::new();
29//! enc.write_packet(&KeepAlive { keep_alive_id: 42 }).unwrap();
30//! let mut dec = FrameDecoder::new();
31//! dec.push(&enc.take());
32//! let raw: RawPacket = dec.next_packet().unwrap().unwrap();
33//! let p = ServerboundPlayPacket::decode_raw(&raw).unwrap();
34//! assert_eq!(p, ServerboundPlayPacket::KeepAlive(KeepAlive { keep_alive_id: 42 }));
35//! ```
36
37#![forbid(unsafe_code)]
38#![warn(missing_docs, unnameable_types)]
39
40pub mod chat;
41pub mod codec;
42pub mod crypto;
43pub mod error;
44pub mod frame;
45pub mod intern;
46#[cfg(feature = "tokio")]
47pub mod io;
48pub mod nbt;
49pub mod packet;
50#[cfg(feature = "testgen")]
51pub mod testgen;
52pub mod types;
53pub mod versions;
54
55#[path = "generated/defs/mod.rs"]
56pub mod defs;
57
58#[cfg(feature = "v1_8")]
59#[path = "generated/v1_8/mod.rs"]
60pub mod v1_8;
61
62// 1.12.1 rides on the 1.12.2 feature: its module is aliases of 1.12.2's definitions except the
63// keep-alive packets (VarInt ids, as in 1.8).
64#[cfg(feature = "v1_12_2")]
65#[path = "generated/v1_12_1/mod.rs"]
66pub mod v1_12_1;
67
68#[cfg(feature = "v1_12_2")]
69#[path = "generated/v1_12_2/mod.rs"]
70pub mod v1_12_2;
71
72#[cfg(feature = "v1_16_5")]
73#[path = "generated/v1_16_5/mod.rs"]
74pub mod v1_16_5;
75
76#[cfg(feature = "v26_1")]
77#[path = "generated/v26_1/mod.rs"]
78pub mod v26_1;
79
80#[cfg(feature = "v26_2")]
81#[path = "generated/v26_2/mod.rs"]
82pub mod v26_2;
83#[cfg(feature = "v26_3")]
84#[path = "generated/v26_3/mod.rs"]
85pub mod v26_3;
86
87pub use codec::{Decode, Encode, McWrite, Reader};
88pub use error::{DecodeError, EncodeError, FrameError};
89pub use frame::{FrameDecoder, FrameEncoder, FrameLimits};
90#[cfg(feature = "tokio")]
91pub use io::{FramedReader, FramedWriter};
92pub use packet::{
93    Direction, NoPackets, Packet, PacketFilter, PacketSet, RawPacket, State, Version,
94};
95pub use types::BlockPos;
96pub use versions::ProtocolVersion;