Add game packets

This commit is contained in:
Vladislavs Golubs 2021-02-07 18:40:11 +03:00
parent 3be31e8b81
commit 0a0835fd2d
5 changed files with 3458 additions and 133 deletions

View File

@ -7,27 +7,3 @@ rust:
matrix: matrix:
allow_failures: allow_failures:
- rust: nightly - rust: nightly
addons:
apt:
packages:
- libcurl4-openssl-dev
- libelf-dev
- libdw-dev
- cmake
- gcc
- binutils-dev
- libiberty-dev
after_success: |
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
tar xzf master.tar.gz &&
cd kcov-master &&
mkdir build &&
cd build &&
cmake .. &&
make &&
make install DESTDIR=../../kcov-build &&
cd ../.. &&
rm -rf kcov-master &&
for file in target/debug/minecraft_protocol*; do [ -x "${file}" ] || continue; mkdir -p "target/cov/$(basename $file)"; ./kcov-build/usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
bash <(curl -s https://codecov.io/bash) &&
echo "Uploaded code coverage"

View File

@ -127,6 +127,7 @@ pub enum DataType {
RefType { RefType {
ref_name: String, ref_name: String,
}, },
#[serde(rename(serialize = "Message"))]
Chat, Chat,
} }
@ -136,7 +137,7 @@ impl DataType {
DataType::Uuid { .. } => Some("uuid::Uuid"), DataType::Uuid { .. } => Some("uuid::Uuid"),
DataType::CompoundTag => Some("nbt::CompoundTag"), DataType::CompoundTag => Some("nbt::CompoundTag"),
DataType::RefType { .. } => Some(state.data_import()), DataType::RefType { .. } => Some(state.data_import()),
DataType::Chat => Some("crate::chat::Message"), DataType::Chat => Some("crate::data::chat::Message"),
_ => None, _ => None,
} }
} }

View File

@ -6,7 +6,6 @@ use heck::{CamelCase, SnakeCase};
use crate::data::input::{Container, Data, ProtocolData, ProtocolState}; use crate::data::input::{Container, Data, ProtocolData, ProtocolState};
use crate::data::output; use crate::data::output;
use crate::data::output::Field;
use serde::Serialize; use serde::Serialize;
use serde_json::json; use serde_json::json;
use std::collections::HashMap; use std::collections::HashMap;
@ -49,10 +48,10 @@ pub fn main() {
transform_protocol_state(output::State::Login, &protocol_input.login), transform_protocol_state(output::State::Login, &protocol_input.login),
output::State::Login, output::State::Login,
), ),
// ( (
// transform_protocol_state(output::State::Game, &protocol_input.game), transform_protocol_state(output::State::Game, &protocol_input.game),
// output::State::Game, output::State::Game,
// ), ),
]; ];
for (protocol, state) in protocols { for (protocol, state) in protocols {
@ -102,10 +101,16 @@ fn transform_protocol_state(
state: output::State, state: output::State,
protocol_state: &ProtocolState, protocol_state: &ProtocolState,
) -> output::Protocol { ) -> output::Protocol {
let server_bound_packets = let server_bound_packets = transform_protocol_data(
transform_protocol_data(&protocol_state.to_server, output::Bound::Server); protocol_state,
let client_bound_packets = &protocol_state.to_server,
transform_protocol_data(&protocol_state.to_client, output::Bound::Client); output::Bound::Server,
);
let client_bound_packets = transform_protocol_data(
protocol_state,
&protocol_state.to_client,
output::Bound::Client,
);
output::Protocol { output::Protocol {
state, state,
@ -115,6 +120,7 @@ fn transform_protocol_state(
} }
fn transform_protocol_data( fn transform_protocol_data(
protocol_state: &ProtocolState,
protocol_data: &ProtocolData, protocol_data: &ProtocolData,
bound: output::Bound, bound: output::Bound,
) -> Vec<output::Packet> { ) -> Vec<output::Packet> {
@ -128,12 +134,18 @@ fn transform_protocol_data(
continue; continue;
} }
let no_prefix_name = unformatted_name.trim_start_matches("packet_"); let no_prefix_unformatted = unformatted_name.trim_start_matches("packet_");
let id = *packet_ids let id = *packet_ids
.get(no_prefix_name) .get(no_prefix_unformatted)
.expect("Failed to get packet id"); .expect("Failed to get packet id");
let packet_name = rename_packet(&no_prefix_name.to_camel_case(), &bound);
let packet_name = rename_packet(
unformatted_name,
&no_prefix_unformatted.to_camel_case(),
&bound,
protocol_state,
);
let mut fields = vec![]; let mut fields = vec![];
@ -248,8 +260,13 @@ fn transform_data_type(name: &str) -> Option<output::DataType> {
} }
} }
fn rename_packet(name: &str, bound: &output::Bound) -> String { fn rename_packet(
match (name, bound) { unformatted_name: &str,
name: &str,
bound: &output::Bound,
protocol_state: &ProtocolState,
) -> String {
let new_name = match (name, bound) {
("EncryptionBegin", output::Bound::Server) => "EncryptionResponse", ("EncryptionBegin", output::Bound::Server) => "EncryptionResponse",
("EncryptionBegin", output::Bound::Client) => "EncryptionRequest", ("EncryptionBegin", output::Bound::Client) => "EncryptionRequest",
("PingStart", output::Bound::Server) => "StatusRequest", ("PingStart", output::Bound::Server) => "StatusRequest",
@ -258,7 +275,29 @@ fn rename_packet(name: &str, bound: &output::Bound) -> String {
("Ping", output::Bound::Client) => "PingResponse", ("Ping", output::Bound::Client) => "PingResponse",
_ => name, _ => name,
} }
.to_owned() .to_owned();
if new_name == name
&& protocol_state
.to_client
.types
.contains_key(unformatted_name)
&& protocol_state
.to_server
.types
.contains_key(unformatted_name)
{
bidirectional(&new_name, bound)
} else {
new_name.to_owned()
}
}
fn bidirectional(name: &str, bound: &output::Bound) -> String {
match bound {
output::Bound::Server => format!("ServerBound{}", name),
output::Bound::Client => format!("ClientBound{}", name),
}
} }
fn modify_field(packet_name: &str, field: output::Field) -> output::Field { fn modify_field(packet_name: &str, field: output::Field) -> output::Field {
@ -268,6 +307,10 @@ fn modify_field(packet_name: &str, field: output::Field) -> output::Field {
}), }),
("Success", "uuid") => field.change_type(output::DataType::Uuid { hyphenated: true }), ("Success", "uuid") => field.change_type(output::DataType::Uuid { hyphenated: true }),
("Disconnect", "reason") => field.change_type(output::DataType::Chat), ("Disconnect", "reason") => field.change_type(output::DataType::Chat),
("ClientBoundChatMessage", "message") => field.change_type(output::DataType::Chat),
("ClientBoundChatMessage", "position") => field.change_type(output::DataType::RefType {
ref_name: "MessagePosition".to_owned(),
}),
_ => field, _ => field,
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,16 @@
// This file is automatically generated. // This file is automatically generated.
// It is not intended for manual editing. // It is not intended for manual editing.
use crate::chat::Message;
use crate::DecodeError; use crate::DecodeError;
use crate::Decoder; use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read; use std::io::Read;
use minecraft_protocol_derive::Packet;
use uuid::Uuid; use uuid::Uuid;
use crate::data::chat::Message;
pub enum LoginServerBoundPacket { pub enum LoginServerBoundPacket {
LoginStart(LoginStart), LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse), EncryptionResponse(EncryptionResponse),
LoginPluginResponse(LoginPluginResponse), LoginPluginResponse(LoginPluginResponse)
} }
impl LoginServerBoundPacket { impl LoginServerBoundPacket {
@ -18,7 +18,7 @@ impl LoginServerBoundPacket {
match self { match self {
Self::LoginStart(_) => 0x00, Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01, Self::EncryptionResponse(_) => 0x01,
Self::LoginPluginResponse(_) => 0x02, Self::LoginPluginResponse(_) => 0x02
} }
} }
@ -39,12 +39,14 @@ impl LoginServerBoundPacket {
Ok(Self::LoginPluginResponse(login_plugin_response)) Ok(Self::LoginPluginResponse(login_plugin_response))
} }
_ => Err(DecodeError::UnknownPacketType { type_id }), _ => Err(DecodeError::UnknownPacketType { type_id })
} }
} }
pub fn login_start(username: String) -> Self { pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username }; let login_start = LoginStart {
username
};
Self::LoginStart(login_start) Self::LoginStart(login_start)
} }
@ -52,14 +54,17 @@ impl LoginServerBoundPacket {
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self { pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse { let encryption_response = EncryptionResponse {
shared_secret, shared_secret,
verify_token, verify_token
}; };
Self::EncryptionResponse(encryption_response) Self::EncryptionResponse(encryption_response)
} }
pub fn login_plugin_response(message_id: i32, data: Vec<u8>) -> Self { pub fn login_plugin_response(message_id: i32, data: Vec<u8>) -> Self {
let login_plugin_response = LoginPluginResponse { message_id, data }; let login_plugin_response = LoginPluginResponse {
message_id,
data
};
Self::LoginPluginResponse(login_plugin_response) Self::LoginPluginResponse(login_plugin_response)
} }
@ -70,7 +75,7 @@ pub enum LoginClientBoundPacket {
EncryptionRequest(EncryptionRequest), EncryptionRequest(EncryptionRequest),
Success(Success), Success(Success),
Compress(Compress), Compress(Compress),
LoginPluginRequest(LoginPluginRequest), LoginPluginRequest(LoginPluginRequest)
} }
impl LoginClientBoundPacket { impl LoginClientBoundPacket {
@ -80,7 +85,7 @@ impl LoginClientBoundPacket {
Self::EncryptionRequest(_) => 0x01, Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02, Self::Success(_) => 0x02,
Self::Compress(_) => 0x03, Self::Compress(_) => 0x03,
Self::LoginPluginRequest(_) => 0x04, Self::LoginPluginRequest(_) => 0x04
} }
} }
@ -111,38 +116,41 @@ impl LoginClientBoundPacket {
Ok(Self::LoginPluginRequest(login_plugin_request)) Ok(Self::LoginPluginRequest(login_plugin_request))
} }
_ => Err(DecodeError::UnknownPacketType { type_id }), _ => Err(DecodeError::UnknownPacketType { type_id })
} }
} }
pub fn disconnect(reason: Chat) -> Self { pub fn disconnect(reason: Message) -> Self {
let disconnect = Disconnect { reason }; let disconnect = Disconnect {
reason
};
Self::Disconnect(disconnect) Self::Disconnect(disconnect)
} }
pub fn encryption_request( pub fn encryption_request(server_id: String, public_key: Vec<u8>, verify_token: Vec<u8>) -> Self {
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest { let encryption_request = EncryptionRequest {
server_id, server_id,
public_key, public_key,
verify_token, verify_token
}; };
Self::EncryptionRequest(encryption_request) Self::EncryptionRequest(encryption_request)
} }
pub fn success(uuid: Uuid, username: String) -> Self { pub fn success(uuid: Uuid, username: String) -> Self {
let success = Success { uuid, username }; let success = Success {
uuid,
username
};
Self::Success(success) Self::Success(success)
} }
pub fn compress(threshold: i32) -> Self { pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold }; let compress = Compress {
threshold
};
Self::Compress(compress) Self::Compress(compress)
} }
@ -151,7 +159,7 @@ impl LoginClientBoundPacket {
let login_plugin_request = LoginPluginRequest { let login_plugin_request = LoginPluginRequest {
message_id, message_id,
channel, channel,
data, data
}; };
Self::LoginPluginRequest(login_plugin_request) Self::LoginPluginRequest(login_plugin_request)
@ -160,13 +168,13 @@ impl LoginClientBoundPacket {
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
pub struct LoginStart { pub struct LoginStart {
pub username: String, pub username: String
} }
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
pub struct EncryptionResponse { pub struct EncryptionResponse {
pub shared_secret: Vec<u8>, pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>, pub verify_token: Vec<u8>
} }
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
@ -174,32 +182,33 @@ pub struct LoginPluginResponse {
#[packet(with = "var_int")] #[packet(with = "var_int")]
pub message_id: i32, pub message_id: i32,
#[packet(with = "rest")] #[packet(with = "rest")]
pub data: Vec<u8>, pub data: Vec<u8>
} }
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
pub struct Disconnect { pub struct Disconnect {
pub reason: Chat, pub reason: Message
} }
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
pub struct EncryptionRequest { pub struct EncryptionRequest {
pub server_id: String, pub server_id: String,
pub public_key: Vec<u8>, pub public_key: Vec<u8>,
pub verify_token: Vec<u8>, pub verify_token: Vec<u8>
} }
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
pub struct Success { pub struct Success {
#[packet(with = "uuid_hyp_str")] #[packet(with = "uuid_hyp_str")]
pub uuid: Uuid, pub uuid: Uuid,
pub username: String, pub username: String
} }
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
pub struct Compress { pub struct Compress {
#[packet(with = "var_int")] #[packet(with = "var_int")]
pub threshold: i32, pub threshold: i32
} }
#[derive(Packet, Debug)] #[derive(Packet, Debug)]
@ -208,5 +217,6 @@ pub struct LoginPluginRequest {
pub message_id: i32, pub message_id: i32,
pub channel: String, pub channel: String,
#[packet(with = "rest")] #[packet(with = "rest")]
pub data: Vec<u8>, pub data: Vec<u8>
} }