GeezerVison is showing github.com — ... · Defman/feather-protocol@aa27287
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codegen/src/generation.rs +29-37Lines changed: 29 additions & 37 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,17 +15,9 @@ pub struct ProtocolGenerator; | 15 | 15 | impl ProtocolGenerator { | 16 | 16 | pub fn generate(protocol: Protocol) -> TokenStream { | 17 | 17 | let mut packets = protocol.packets; | 18 | | - let server_bound_packets = packets.split_off(&PacketIdentifier { | 19 | | - direction: PacketDirection::ServerBound, | 20 | | - stage: PacketStage::Handshaking, | 21 | | - id: 0.into(), | 22 | | - }); | 23 | | - | 24 | | - let client_bound_packets = packets.split_off(&PacketIdentifier { | 25 | | - direction: PacketDirection::ClientBound, | 26 | | - stage: PacketStage::Handshaking, | 27 | | - id: 0.into(), | 28 | | - }); | | 18 | + let server_bound_packets = packets.split_off(&PacketIdentifier(PacketDirection::ServerBound, PacketStage::Handshaking, 0.into())); | | 19 | + | | 20 | + let client_bound_packets = packets.split_off(&PacketIdentifier(PacketDirection::ClientBound, PacketStage::Handshaking, 0.into())); | 29 | 21 |
| 30 | 22 | let (client, _, _) = DirectionGenerator::generate( | 31 | 23 | PacketDirection::ServerBound, | @@ -54,29 +46,29 @@ impl DirectionGenerator { | 54 | 46 | ) -> (TokenStream, (Ident, Ident, Ident, Ident), Ident) { | 55 | 47 | let direction_ident = Self::ident(direction); | 56 | 48 |
| 57 | | - let play_packets = packets.split_off(&PacketIdentifier { | 58 | | - direction: direction, | 59 | | - stage: PacketStage::Play, | 60 | | - id: 0.into(), | 61 | | - }); | 62 | | - | 63 | | - let login_packets = packets.split_off(&PacketIdentifier { | 64 | | - direction: direction, | 65 | | - stage: PacketStage::Login, | 66 | | - id: 0.into(), | 67 | | - }); | 68 | | - | 69 | | - let status_packets = packets.split_off(&PacketIdentifier { | 70 | | - direction: direction, | 71 | | - stage: PacketStage::Status, | 72 | | - id: 0.into(), | 73 | | - }); | 74 | | - | 75 | | - let handshaking_packets = packets.split_off(&PacketIdentifier { | 76 | | - direction: direction, | 77 | | - stage: PacketStage::Handshaking, | 78 | | - id: 0.into(), | 79 | | - }); | | 49 | + let play_packets = packets.split_off(&PacketIdentifier( | | 50 | + direction, | | 51 | + PacketStage::Play, | | 52 | + 0.into(), | | 53 | + )); | | 54 | + | | 55 | + let login_packets = packets.split_off(&PacketIdentifier( | | 56 | + direction, | | 57 | + PacketStage::Login, | | 58 | + 0.into(), | | 59 | + )); | | 60 | + | | 61 | + let status_packets = packets.split_off(&PacketIdentifier( | | 62 | + direction, | | 63 | + PacketStage::Status, | | 64 | + 0.into(), | | 65 | + )); | | 66 | + | | 67 | + let handshaking_packets = packets.split_off(&PacketIdentifier( | | 68 | + direction, | | 69 | + PacketStage::Handshaking, | | 70 | + 0.into(), | | 71 | + )); | 80 | 72 |
| 81 | 73 | let (handshaking, handshaking_ident) = StageGenerator::generate( | 82 | 74 | direction, | @@ -186,10 +178,10 @@ impl PacketGenerator { | 186 | 178 | let ident = Ident::new(&format!("{}_packet", *packet.name), Span::call_site()); | 187 | 179 | let (custom_type, custom_type_ident) = CustomTypeGenerator::generate("packet", &packet.custom_type); | 188 | 180 |
| 189 | | - let id_lit = LitInt::new(&identifier.id.to_string(), Span::call_site()); | | 181 | + let id_lit = LitInt::new(&identifier.id().to_string(), Span::call_site()); | 190 | 182 | let name_lit = LitStr::new(&*packet.name, Span::call_site()); | 191 | 183 |
| 192 | | - let direction = match identifier.direction { | | 184 | + let direction = match identifier.direction() { | 193 | 185 | PacketDirection::ClientBound => { | 194 | 186 | quote! { feather_protocol::PacketDirection::ClientBound } | 195 | 187 | } | @@ -198,7 +190,7 @@ impl PacketGenerator { | 198 | 190 | } | 199 | 191 | }; | 200 | 192 |
| 201 | | - let stage = match identifier.stage { | | 193 | + let stage = match identifier.stage() { | 202 | 194 | PacketStage::Handshaking => quote! { feather_protocol::PacketStage::Handshaking }, | 203 | 195 | PacketStage::Status => quote! { feather_protocol::PacketStage::Status }, | 204 | 196 | PacketStage::Login => quote! { feather_protocol::PacketStage::Login }, | |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +( | | 2 | + version: 578, | | 3 | + packets: { | | 4 | + (ServerBound, Handshaking, 0): ( | | 5 | + name: "handshake", | | 6 | + custom_type: Struct({ | | 7 | + "protocol_version": VarInt, | | 8 | + "server_address": String(255), | | 9 | + "server_port": U16, | | 10 | + "next_state": CustomType("state", Enum( | | 11 | + variant: Prefixed(VarInt), | | 12 | + variants: { | | 13 | + (1, "status"): Unit, | | 14 | + (2, "login"): Unit, | | 15 | + } | | 16 | + )) | | 17 | + }) | | 18 | + ) | | 19 | + }, | | 20 | + shared_types: {} | | 21 | +) |
Collapse file Copy file name to clipboard +24-2Lines changed: 24 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -fn main() { | 2 | | - // Prettyfie protocol files | | 1 | +use std::fs; | | 2 | +use std::env; | | 3 | +use std::io::Read; | | 4 | +use anyhow::{Result, anyhow}; | | 5 | +use feather_protocol_spec::Protocol; | | 6 | + | | 7 | +fn main() -> Result<()> { | | 8 | + verify()?; | | 9 | + Ok(()) | | 10 | +} | | 11 | + | | 12 | +fn verify() -> Result<()> { | | 13 | + let path = env::args() | | 14 | + .skip(1) | | 15 | + .next() | | 16 | + .ok_or(anyhow!("Specify a file path to verify."))?; | | 17 | + let file = fs::File::open(&path)?; | | 18 | + | | 19 | + let _: Protocol = ron::de::from_reader(file) | | 20 | + .map_err(|e| anyhow!("{}", e))?; | | 21 | + | | 22 | + println!("Its all good"); | | 23 | + | | 24 | + Ok(()) | 3 | 25 | } |
Collapse file Copy file name to clipboardExpand all lines: spec/src/primitives.rs +17-7Lines changed: 17 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,7 @@ macro_rules! wrap { | 32 | 32 | }; | 33 | 33 | } | 34 | 34 |
| 35 | | -#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] | | 35 | +#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] | 36 | 36 | #[serde(transparent)] | 37 | 37 | pub struct PacketId(u64); | 38 | 38 |
| @@ -44,7 +44,7 @@ pub struct PacketName(String); | 44 | 44 | #[serde(transparent)] | 45 | 45 | pub struct MinecraftVersion(String); | 46 | 46 |
| 47 | | -#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] | | 47 | +#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] | 48 | 48 | #[serde(transparent)] | 49 | 49 | pub struct ProtocolVersion(u64); | 50 | 50 |
| @@ -71,8 +71,18 @@ wrap!(FieldName, String); | 71 | 71 |
| 72 | 72 |
| 73 | 73 | #[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] | 74 | | -pub struct PacketIdentifier { | 75 | | - pub direction: PacketDirection, | 76 | | - pub stage: PacketStage, | 77 | | - pub id: PacketId, | 78 | | -} | | 74 | +pub struct PacketIdentifier(pub PacketDirection, pub PacketStage, pub PacketId); | | 75 | + | | 76 | +impl PacketIdentifier { | | 77 | + pub fn direction(&self) -> PacketDirection { | | 78 | + self.0 | | 79 | + } | | 80 | + | | 81 | + pub fn stage(&self) -> PacketStage { | | 82 | + self.1 | | 83 | + } | | 84 | + | | 85 | + pub fn id(&self) -> PacketId { | | 86 | + self.2 | | 87 | + } | | 88 | +} |
|