WIP: procmacro for automatic packet generation

This commit is contained in:
vagola 2019-12-30 03:16:38 +03:00
parent 5c4721a2bc
commit d78175d685
19 changed files with 70 additions and 34 deletions

View File

@ -1,21 +1,6 @@
[package]
name = "minecraft-protocol"
version = "0.1.0"
authors = ["vagola <vladislavs.golubs@yandex.ru>"]
edition = "2018"
description = "Library for decoding and encoding Minecraft packets"
license = "MIT"
homepage = "https://github.com/eihwaz/minecraft-protocol"
repository = "https://github.com/eihwaz/minecraft-protocol"
keywords = ["minecraft", "protocol", "packet", "io"]
readme = "README.md"
[workspace]
[dependencies]
byteorder = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "0.7", features = ["v4", "serde"] }
mc-varint = { git = "https://github.com/luojia65/mc-varint" }
num-traits = "0.2"
num-derive = "0.2"
named-binary-tag = "0.2"
members = [
"protocol",
"protocol-derive",
]

View File

@ -0,0 +1,14 @@
[package]
name = "minecraft-protocol-derive"
version = "0.0.0"
authors = ["vagola <vladislavs.golubs@yandex.ru>"]
edition = "2018"
description = "Derive macro for Minecraft packet read and write"
publish = false
[lib]
proc-macro = true
[dependencies]
syn = "1.0"
quote = "1.0"

View File

@ -0,0 +1,28 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(MinecraftPacket)]
pub fn derive_packet(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let output = quote! {
impl minecraft_protocol::Packet for #name {
type Output = Self;
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
}
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
}
}
};
TokenStream::from(output)
}

22
protocol/Cargo.toml Normal file
View File

@ -0,0 +1,22 @@
[package]
name = "minecraft-protocol"
version = "0.1.0"
authors = ["vagola <vladislavs.golubs@yandex.ru>"]
edition = "2018"
description = "Library for decoding and encoding Minecraft packets"
license = "MIT"
homepage = "https://github.com/eihwaz/minecraft-protocol"
repository = "https://github.com/eihwaz/minecraft-protocol"
keywords = ["minecraft", "protocol", "packet", "io"]
readme = "README.md"
[dependencies]
minecraft-protocol-derive = { path = "../protocol-derive" }
byteorder = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "0.7", features = ["v4", "serde"] }
mc-varint = { git = "https://github.com/luojia65/mc-varint" }
num-traits = "0.2"
num-derive = "0.2"
named-binary-tag = "0.2"

View File

@ -106,6 +106,7 @@ impl LoginClientBoundPacket {
}
}
#[derive(minecraft_protocol_derive::MinecraftPacket)]
pub struct LoginStart {
pub name: String,
}
@ -118,20 +119,6 @@ impl LoginStart {
}
}
impl Packet for LoginStart {
type Output = Self;
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
writer.write_string(&self.name, LOGIN_MAX_LENGTH)
}
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
let name = reader.read_string(LOGIN_MAX_LENGTH)?;
Ok(LoginStart { name })
}
}
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,