GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — feather/feather/protocol/src/packets.rs at develop · aheart/feather Expand file tree/ Copy pathpackets.rsMore file actions 294 lines (271 loc) · 8.83 KB / Copy pathpackets.rsFile metadata and controls294 lines (271 loc) · 8.83 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 macro_rules! user_type { (VarInt) => { i32 }; (VarIntPrefixedVec <$inner:ident>) => { Vec<$inner> }; (ShortPrefixedVec <$inner:ident>) => { Vec<$inner> }; (LengthInferredVecU8) => { Vec<u8> }; (Angle) => { f32 }; ($typ:ty) => { $typ }; } macro_rules! user_type_convert_to_writeable { (VarInt, $e:expr) => { VarInt(*$e as i32) }; (VarIntPrefixedVec <$inner:ident>, $e:expr) => { VarIntPrefixedVec::from($e.as_slice()) }; (ShortPrefixedVec <$inner:ident>, $e:expr) => { ShortPrefixedVec::from($e.as_slice()) }; (LengthInferredVecU8, $e:expr) => { LengthInferredVecU8::from($e.as_slice()) }; (Angle, $e:expr) => { Angle(*$e) }; ($typ:ty, $e:expr) => { $e }; } macro_rules! packets { ( $( $packet:ident { $( $field:ident $typ:ident $(<$generics:ident>)? );* $(;)? } $(,)? )* ) => { $( #[derive(Debug, Clone)] pub struct $packet { $( pub $field: user_type!($typ $(<$generics>)?), )* } #[allow(unused_imports, unused_variables)] impl crate::Readable for $packet { fn read(buffer: &mut ::std::io::Cursor<&[u8]>, version: crate::ProtocolVersion) -> anyhow::Result<Self> where Self: Sized { use anyhow::Context as _; $( let $field = <$typ $(<$generics>)?>::read(buffer, version) .context(concat!("failed to read field `", stringify!($field), "` of packet `", stringify!($packet), "`"))? .into(); )* Ok(Self { $( $field, )* }) } } #[allow(unused_variables)] impl crate::Writeable for $packet { fn write(&self, buffer: &mut Vec<u8>, version: crate::ProtocolVersion) -> anyhow::Result<()> { $( user_type_convert_to_writeable!($typ $(<$generics>)?, &self.$field).write(buffer, version)?; )* Ok(()) } } )* }; } macro_rules! discriminant_to_literal { (String, $discriminant:expr) => { &*$discriminant }; ($discriminant_type:ident, $discriminant:expr) => { $discriminant.into() }; } macro_rules! def_enum { ( $ident:ident ($discriminant_type:ident) { $( $discriminant:literal = $variant:ident $( { $( $field:ident $typ:ident $(<$generics:ident>)? );* $(;)? } )? ),* $(,)? } ) => { #[derive(Debug, Clone)] pub enum $ident { $( $variant $( { $( $field: user_type!($typ $(<$generics>)?), )* } )?, )* } impl crate::Readable for $ident { fn read(buffer: &mut ::std::io::Cursor<&[u8]>, version: crate::ProtocolVersion) -> anyhow::Result<Self> where Self: Sized { use anyhow::Context as _; let discriminant = <$discriminant_type>::read(buffer, version) .context(concat!("failed to read discriminant for enum type ", stringify!($ident)))?; match discriminant_to_literal!($discriminant_type, discriminant) { $( $discriminant => { $( $( let $field = <$typ $(<$generics>)?>::read(buffer, version) .context(concat!("failed to read field `", stringify!($field), "` of enum `", stringify!($ident), "::", stringify!($variant), "`"))? .into(); )* )? Ok($ident::$variant $( { $( $field, )* } )?) }, )* _ => Err(anyhow::anyhow!( concat!( "no discriminant for enum `", stringify!($ident), "` matched value {:?}" ), discriminant )) } } } impl crate::Writeable for $ident { fn write(&self, buffer: &mut Vec<u8>, version: crate::ProtocolVersion) -> anyhow::Result<()> { match self { $( $ident::$variant $( { $($field,)* } )? => { let discriminant = <$discriminant_type>::from($discriminant); discriminant.write(buffer, version)?; $( $( user_type_convert_to_writeable!($typ $(<$generics>)?, $field).write(buffer, version)?; )* )? } )* } Ok(()) } } }; } macro_rules! packet_enum { ( $ident:ident { $($id:literal = $packet:ident),* $(,)? } ) => { #[derive(Debug, Clone)] pub enum $ident { $( $packet($packet), )* } impl $ident { /// Returns the packet ID of this packet. pub fn id(&self) -> u32 { match self { $( $ident::$packet(_) => $id, )* } } } impl crate::Readable for $ident { fn read(buffer: &mut ::std::io::Cursor<&[u8]>, version: crate::ProtocolVersion) -> anyhow::Result<Self> where Self: Sized { let packet_id = VarInt::read(buffer, version)?.0; match packet_id { $( id if id == $id => Ok($ident::$packet($packet::read(buffer, version)?)), )* _ => Err(anyhow::anyhow!("unknown packet ID {}", packet_id)), } } } impl crate::Writeable for $ident { fn write(&self, buffer: &mut Vec<u8>, version: crate::ProtocolVersion) -> anyhow::Result<()> { VarInt(self.id() as i32).write(buffer, version)?; match self { $( $ident::$packet(packet) => { packet.write(buffer, version)?; } )* } Ok(()) } } $( impl VariantOf<$ident> for $packet { fn discriminant_id() -> u32 { $id } #[allow(unreachable_patterns)] fn destructure(e: $ident) -> Option<Self> { match e { $ident::$packet(p) => Some(p), _ => None, } } } impl From<$packet> for $ident { fn from(packet: $packet) -> Self { $ident::$packet(packet) } } )* } } /// Trait implemented for packets which can be converted from a packet /// enum. For example, `SpawnEntity` implements `VariantOf<ServerPlayPacket>`. pub trait VariantOf<Enum> { /// Returns the unique ID used to determine whether /// an enum variant matches this variant. fn discriminant_id() -> u32; /// Attempts to destructure the `Enum` into this type. /// Returns `None` if `enum` is not the correct variant. fn destructure(e: Enum) -> Option<Self> where Self: Sized; } use crate::io::{Angle, LengthInferredVecU8, Nbt, ShortPrefixedVec, VarInt, VarIntPrefixedVec}; use crate::Slot; use base::{BlockId, BlockPosition}; use nbt::Blob; use uuid::Uuid; pub mod client; pub mod server; |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |