From 99a331c6abbf1a468118c5da1432b7f579019e41 Mon Sep 17 00:00:00 2001 From: Vladislavs Golubs Date: Sun, 7 Feb 2021 16:09:48 +0300 Subject: [PATCH] Fix --- generate.sh | 4 ++ protocol-generator/templates/packet_enum.hbs | 2 +- .../templates/packet_imports.hbs | 4 +- protocol/src/lib.rs | 14 ++++ protocol/src/packet/handshake.rs | 3 + protocol/src/packet/login.rs | 71 +++++++++---------- protocol/src/packet/status.rs | 43 +++++------ 7 files changed, 75 insertions(+), 66 deletions(-) create mode 100755 generate.sh diff --git a/generate.sh b/generate.sh new file mode 100755 index 0000000..8a3dd85 --- /dev/null +++ b/generate.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +cargo run +cargo fmt diff --git a/protocol-generator/templates/packet_enum.hbs b/protocol-generator/templates/packet_enum.hbs index 5cc3ed9..0b81503 100644 --- a/protocol-generator/templates/packet_enum.hbs +++ b/protocol-generator/templates/packet_enum.hbs @@ -48,4 +48,4 @@ impl {{packet_enum_name}} { } {{/each~}} } -{{~/if}} \ No newline at end of file +{{~/if}} diff --git a/protocol-generator/templates/packet_imports.hbs b/protocol-generator/templates/packet_imports.hbs index 03e74a9..a97ad24 100644 --- a/protocol-generator/templates/packet_imports.hbs +++ b/protocol-generator/templates/packet_imports.hbs @@ -1,3 +1,5 @@ +// This file is automatically generated. +// It is not intended for manual editing. {{#each imports as |i|~}} use {{i}}; -{{/each}} +{{/each~}} diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 6c92a9c..e7eee2b 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -239,6 +239,20 @@ impl Decoder for u8 { } } +impl Encoder for u16 { + fn encode(&self, writer: &mut W) -> Result<(), EncodeError> { + Ok(writer.write_u16::(*self)?) + } +} + +impl Decoder for u16 { + type Output = Self; + + fn decode(reader: &mut R) -> Result { + Ok(reader.read_u16::()?) + } +} + impl Encoder for i32 { fn encode(&self, writer: &mut W) -> Result<(), EncodeError> { Ok(writer.write_i32::(*self)?) diff --git a/protocol/src/packet/handshake.rs b/protocol/src/packet/handshake.rs index 77d3687..90d4683 100644 --- a/protocol/src/packet/handshake.rs +++ b/protocol/src/packet/handshake.rs @@ -1,3 +1,5 @@ +// This file is automatically generated. +// It is not intended for manual editing. use crate::DecodeError; use crate::Decoder; use minecraft_protocol_derive::Packet; @@ -41,6 +43,7 @@ impl HandshakeServerBoundPacket { Self::SetProtocol(set_protocol) } } + #[derive(Packet, Debug)] pub struct SetProtocol { #[packet(with = "var_int")] diff --git a/protocol/src/packet/login.rs b/protocol/src/packet/login.rs index 00c489e..4414da5 100644 --- a/protocol/src/packet/login.rs +++ b/protocol/src/packet/login.rs @@ -1,13 +1,14 @@ +// This file is automatically generated. +// It is not intended for manual editing. use crate::DecodeError; use crate::Decoder; -use std::io::Read; use minecraft_protocol_derive::Packet; - +use std::io::Read; pub enum LoginServerBoundPacket { LoginStart(LoginStart), EncryptionResponse(EncryptionResponse), - LoginPluginResponse(LoginPluginResponse) + LoginPluginResponse(LoginPluginResponse), } impl LoginServerBoundPacket { @@ -15,7 +16,7 @@ impl LoginServerBoundPacket { match self { Self::LoginStart(_) => 0x00, Self::EncryptionResponse(_) => 0x01, - Self::LoginPluginResponse(_) => 0x02 + Self::LoginPluginResponse(_) => 0x02, } } @@ -36,14 +37,12 @@ impl LoginServerBoundPacket { Ok(Self::LoginPluginResponse(login_plugin_response)) } - _ => Err(DecodeError::UnknownPacketType { type_id }) + _ => Err(DecodeError::UnknownPacketType { type_id }), } } pub fn login_start(username: String) -> Self { - let login_start = LoginStart { - username - }; + let login_start = LoginStart { username }; Self::LoginStart(login_start) } @@ -51,27 +50,25 @@ impl LoginServerBoundPacket { pub fn encryption_response(shared_secret: Vec, verify_token: Vec) -> Self { let encryption_response = EncryptionResponse { shared_secret, - verify_token + verify_token, }; Self::EncryptionResponse(encryption_response) } pub fn login_plugin_response(message_id: i32, data: Vec) -> Self { - let login_plugin_response = LoginPluginResponse { - message_id, - data - }; + let login_plugin_response = LoginPluginResponse { message_id, data }; Self::LoginPluginResponse(login_plugin_response) } } + pub enum LoginClientBoundPacket { Disconnect(Disconnect), EncryptionRequest(EncryptionRequest), Success(Success), Compress(Compress), - LoginPluginRequest(LoginPluginRequest) + LoginPluginRequest(LoginPluginRequest), } impl LoginClientBoundPacket { @@ -81,7 +78,7 @@ impl LoginClientBoundPacket { Self::EncryptionRequest(_) => 0x01, Self::Success(_) => 0x02, Self::Compress(_) => 0x03, - Self::LoginPluginRequest(_) => 0x04 + Self::LoginPluginRequest(_) => 0x04, } } @@ -112,41 +109,38 @@ impl LoginClientBoundPacket { Ok(Self::LoginPluginRequest(login_plugin_request)) } - _ => Err(DecodeError::UnknownPacketType { type_id }) + _ => Err(DecodeError::UnknownPacketType { type_id }), } } pub fn disconnect(reason: String) -> Self { - let disconnect = Disconnect { - reason - }; + let disconnect = Disconnect { reason }; Self::Disconnect(disconnect) } - pub fn encryption_request(server_id: String, public_key: Vec, verify_token: Vec) -> Self { + pub fn encryption_request( + server_id: String, + public_key: Vec, + verify_token: Vec, + ) -> Self { let encryption_request = EncryptionRequest { server_id, public_key, - verify_token + verify_token, }; Self::EncryptionRequest(encryption_request) } pub fn success(uuid: String, username: String) -> Self { - let success = Success { - uuid, - username - }; + let success = Success { uuid, username }; Self::Success(success) } pub fn compress(threshold: i32) -> Self { - let compress = Compress { - threshold - }; + let compress = Compress { threshold }; Self::Compress(compress) } @@ -155,21 +149,22 @@ impl LoginClientBoundPacket { let login_plugin_request = LoginPluginRequest { message_id, channel, - data + data, }; Self::LoginPluginRequest(login_plugin_request) } } + #[derive(Packet, Debug)] pub struct LoginStart { - pub username: String + pub username: String, } #[derive(Packet, Debug)] pub struct EncryptionResponse { pub shared_secret: Vec, - pub verify_token: Vec + pub verify_token: Vec, } #[derive(Packet, Debug)] @@ -177,32 +172,31 @@ pub struct LoginPluginResponse { #[packet(with = "var_int")] pub message_id: i32, #[packet(with = "rest")] - pub data: Vec + pub data: Vec, } - #[derive(Packet, Debug)] pub struct Disconnect { - pub reason: String + pub reason: String, } #[derive(Packet, Debug)] pub struct EncryptionRequest { pub server_id: String, pub public_key: Vec, - pub verify_token: Vec + pub verify_token: Vec, } #[derive(Packet, Debug)] pub struct Success { pub uuid: String, - pub username: String + pub username: String, } #[derive(Packet, Debug)] pub struct Compress { #[packet(with = "var_int")] - pub threshold: i32 + pub threshold: i32, } #[derive(Packet, Debug)] @@ -211,6 +205,5 @@ pub struct LoginPluginRequest { pub message_id: i32, pub channel: String, #[packet(with = "rest")] - pub data: Vec + pub data: Vec, } - diff --git a/protocol/src/packet/status.rs b/protocol/src/packet/status.rs index a754a9c..4beb5be 100644 --- a/protocol/src/packet/status.rs +++ b/protocol/src/packet/status.rs @@ -1,33 +1,32 @@ +// This file is automatically generated. +// It is not intended for manual editing. use crate::DecodeError; use crate::Decoder; -use std::io::Read; use minecraft_protocol_derive::Packet; - +use std::io::Read; pub enum StatusServerBoundPacket { StatusRequest, - PingRequest(PingRequest) + PingRequest(PingRequest), } impl StatusServerBoundPacket { pub fn get_type_id(&self) -> u8 { match self { Self::StatusRequest => 0x00, - Self::PingRequest(_) => 0x01 + Self::PingRequest(_) => 0x01, } } pub fn decode(type_id: u8, reader: &mut R) -> Result { match type_id { - 0x00 => { - Ok(Self::StatusRequest) - } + 0x00 => Ok(Self::StatusRequest), 0x01 => { let ping_request = PingRequest::decode(reader)?; Ok(Self::PingRequest(ping_request)) } - _ => Err(DecodeError::UnknownPacketType { type_id }) + _ => Err(DecodeError::UnknownPacketType { type_id }), } } @@ -36,23 +35,22 @@ impl StatusServerBoundPacket { } pub fn ping_request(time: i64) -> Self { - let ping_request = PingRequest { - time - }; + let ping_request = PingRequest { time }; Self::PingRequest(ping_request) } } + pub enum StatusClientBoundPacket { StatusResponse(StatusResponse), - PingResponse(PingResponse) + PingResponse(PingResponse), } impl StatusClientBoundPacket { pub fn get_type_id(&self) -> u8 { match self { Self::StatusResponse(_) => 0x00, - Self::PingResponse(_) => 0x01 + Self::PingResponse(_) => 0x01, } } @@ -68,39 +66,34 @@ impl StatusClientBoundPacket { Ok(Self::PingResponse(ping_response)) } - _ => Err(DecodeError::UnknownPacketType { type_id }) + _ => Err(DecodeError::UnknownPacketType { type_id }), } } pub fn status_response(response: String) -> Self { - let status_response = StatusResponse { - response - }; + let status_response = StatusResponse { response }; Self::StatusResponse(status_response) } pub fn ping_response(time: i64) -> Self { - let ping_response = PingResponse { - time - }; + let ping_response = PingResponse { time }; Self::PingResponse(ping_response) } } + #[derive(Packet, Debug)] pub struct PingRequest { - pub time: i64 + pub time: i64, } - #[derive(Packet, Debug)] pub struct StatusResponse { - pub response: String + pub response: String, } #[derive(Packet, Debug)] pub struct PingResponse { - pub time: i64 + pub time: i64, } -