1use bytes::{Bytes, BytesMut};
4
5use crate::codec::{Decode, DecodeResult, Encode, EncodeResult, McWrite, decode_exact};
6use crate::error::DecodeError;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[non_exhaustive]
11pub enum State {
12 Handshaking,
14 Status,
16 Login,
18 Configuration,
21 Play,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27pub enum Direction {
28 Clientbound,
30 Serverbound,
32}
33
34pub trait Packet: Encode + Decode {
36 const ID: i32;
38 const NAME: &'static str;
40 const STATE: State;
42 const DIRECTION: Direction;
44}
45
46pub trait PacketSet: Sized {
48 const STATE: State;
50 const DIRECTION: Direction;
52
53 fn decode_raw(raw: &RawPacket) -> DecodeResult<Self>;
55 fn id(&self) -> i32;
57 fn name(&self) -> &'static str;
59 fn encode_body(&self, w: &mut BytesMut) -> EncodeResult;
61 fn name_of(id: i32) -> Option<&'static str>;
63}
64
65#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct RawPacket {
70 pub id: i32,
72 pub body: Bytes,
74}
75
76impl RawPacket {
77 #[inline]
79 pub fn new(id: i32, body: Bytes) -> Self {
80 RawPacket { id, body }
81 }
82
83 #[inline]
85 pub fn decode_as<P: Packet>(&self) -> DecodeResult<P> {
86 if self.id != P::ID {
87 return Err(DecodeError::IdMismatch {
88 expected: P::ID,
89 actual: self.id,
90 });
91 }
92 decode_exact(&self.body)
93 }
94
95 pub fn from_packet<P: Packet>(p: &P) -> Result<Self, crate::error::EncodeError> {
97 let mut w = BytesMut::new();
98 p.encode(&mut w)?;
99 Ok(RawPacket::new(P::ID, w.freeze()))
100 }
101}
102
103#[inline]
105pub fn write_packet<P: Packet>(p: &P, w: &mut BytesMut) -> EncodeResult {
106 w.put_varint(P::ID);
107 p.encode(w)
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
116pub struct PacketFilter {
117 bits: [u64; 4],
118}
119
120impl Default for PacketFilter {
121 fn default() -> Self {
122 Self::all()
123 }
124}
125
126impl PacketFilter {
127 pub const fn all() -> Self {
129 PacketFilter {
130 bits: [u64::MAX; 4],
131 }
132 }
133
134 pub const fn none() -> Self {
136 PacketFilter { bits: [0; 4] }
137 }
138
139 #[must_use]
141 pub const fn with(mut self, id: i32) -> Self {
142 if id >= 0 && id < 256 {
143 self.bits[(id as usize) >> 6] |= 1 << (id & 63);
144 }
145 self
146 }
147
148 #[must_use]
150 pub const fn without(mut self, id: i32) -> Self {
151 if id >= 0 && id < 256 {
152 self.bits[(id as usize) >> 6] &= !(1 << (id & 63));
153 }
154 self
155 }
156
157 pub fn insert(&mut self, id: i32) {
159 *self = self.with(id);
160 }
161
162 pub fn remove(&mut self, id: i32) {
164 *self = self.without(id);
165 }
166
167 #[inline]
169 pub const fn contains(&self, id: i32) -> bool {
170 if id < 0 || id >= 256 {
171 return true;
172 }
173 self.bits[(id as usize) >> 6] & (1 << (id & 63)) != 0
174 }
175
176 #[inline]
178 pub const fn is_all(&self) -> bool {
179 self.bits[0] == u64::MAX
180 && self.bits[1] == u64::MAX
181 && self.bits[2] == u64::MAX
182 && self.bits[3] == u64::MAX
183 }
184}
185
186#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
189pub enum NoPackets {}
190
191impl PacketSet for NoPackets {
192 const STATE: State = State::Configuration;
193 const DIRECTION: Direction = Direction::Clientbound;
194
195 fn decode_raw(raw: &RawPacket) -> DecodeResult<Self> {
196 Err(DecodeError::UnknownPacketId(raw.id))
197 }
198 fn id(&self) -> i32 {
199 match *self {}
200 }
201 fn name(&self) -> &'static str {
202 match *self {}
203 }
204 fn encode_body(&self, _: &mut BytesMut) -> EncodeResult {
205 match *self {}
206 }
207 fn name_of(_: i32) -> Option<&'static str> {
208 None
209 }
210}
211
212pub(crate) mod sealed {
213 #[allow(unnameable_types)] pub trait Sealed {}
216}
217
218pub trait Version: sealed::Sealed + Copy + Send + Sync + 'static {
225 const PROTOCOL: i32;
227 const NAME: &'static str;
229 const POSITION_LAYOUT: crate::types::PositionLayout;
231 const INFO: &'static crate::versions::ProtocolVersion;
233
234 type HandshakingServerbound: PacketSet;
236 type StatusClientbound: PacketSet;
238 type StatusServerbound: PacketSet;
240 type LoginClientbound: PacketSet;
242 type LoginServerbound: PacketSet;
244 type ConfigurationClientbound: PacketSet;
246 type ConfigurationServerbound: PacketSet;
248 type PlayClientbound: PacketSet;
250 type PlayServerbound: PacketSet;
252}
253
254#[cfg(test)]
255mod tests {
256 use super::*;
257
258 #[test]
259 fn filter() {
260 let f = PacketFilter::none().with(0x21).with(200);
261 assert!(f.contains(0x21) && f.contains(200) && !f.contains(0x22));
262 assert!(f.contains(300) && f.contains(-1));
263 assert!(PacketFilter::all().is_all() && !f.is_all());
264 assert!(!PacketFilter::all().without(5).contains(5));
265 }
266}