From b038d90032c38bb244d58a5fad26b62d714353d1 Mon Sep 17 00:00:00 2001 From: ya7on Date: Thu, 30 Nov 2023 22:18:19 +0300 Subject: [PATCH] Added configuration state logic for player login #6 - Send Registry Data (Configuration) - Send Finish Configuration (Clientbound) - Receive Finish Configuration (Serverbound) --- Cargo.lock | 45 +++++++++++++++++++ Cargo.toml | 4 ++ mclib/main/src/lib.rs | 3 ++ mclib/protocol/src/packets/client.rs | 1 + .../packets/client/finish_configuration.rs | 8 ++++ .../src/packets/client/registry_data.rs | 2 +- mclib/protocol/src/packets/server.rs | 1 + .../packets/server/finish_configuration.rs | 8 ++++ src/server/thread/tcp_listener.rs | 38 ++++++++++++++-- 9 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 mclib/protocol/src/packets/client/finish_configuration.rs create mode 100644 mclib/protocol/src/packets/server/finish_configuration.rs diff --git a/Cargo.lock b/Cargo.lock index 0ff48fb..e1469ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -200,6 +200,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "lazy_static" version = "1.4.0" @@ -265,6 +271,8 @@ dependencies = [ "clap", "lazy_static", "mclib", + "serde", + "serde_json", "tracing", "tracing-subscriber", "uuid", @@ -405,6 +413,43 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "serde" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "sharded-slab" version = "0.1.7" diff --git a/Cargo.toml b/Cargo.toml index 9764afa..205e5f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,8 @@ clap = { version = "4.4.8", features = ["derive", "env"] } darling = { version = "0.20.3" } flate2 = { version = "1.0.28" } lazy_static = { version = "1.4.0" } +serde = { version = "1.0.193", features = ["derive"] } +serde_json = { version = "1.0.108" } syn = { version = "2.0.39" } tracing = { version = "0.1.40" } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } @@ -33,6 +35,8 @@ mclib = { workspace = true } clap = { workspace = true } lazy_static = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true } uuid = { workspace = true } diff --git a/mclib/main/src/lib.rs b/mclib/main/src/lib.rs index e592ae8..dd3a9a2 100644 --- a/mclib/main/src/lib.rs +++ b/mclib/main/src/lib.rs @@ -4,6 +4,7 @@ pub use mclib_protocol::packets::base::MCPacket; pub use mclib_protocol::types::base::MCType; pub mod packets { pub mod server { + pub use mclib_protocol::packets::server::finish_configuration::FinishConfigurationServerbound; pub use mclib_protocol::packets::server::handshake::{Handshake, HandshakeNextState}; pub use mclib_protocol::packets::server::login_acknowledged::LoginAcknowledged; pub use mclib_protocol::packets::server::login_start::LoginStart; @@ -11,9 +12,11 @@ pub mod packets { pub use mclib_protocol::packets::server::status_request::StatusRequest; } pub mod client { + pub use mclib_protocol::packets::client::finish_configuration::FinishConfigurationClientbound; pub use mclib_protocol::packets::client::login_success::{ LoginSuccess, LoginSuccessProperty, }; + pub use mclib_protocol::packets::client::registry_data::RegistryData; pub use mclib_protocol::packets::client::status_response::StatusResponse; } } diff --git a/mclib/protocol/src/packets/client.rs b/mclib/protocol/src/packets/client.rs index 5838101..fce101e 100644 --- a/mclib/protocol/src/packets/client.rs +++ b/mclib/protocol/src/packets/client.rs @@ -1,3 +1,4 @@ +pub mod finish_configuration; pub mod login_success; pub mod registry_data; pub mod status_response; diff --git a/mclib/protocol/src/packets/client/finish_configuration.rs b/mclib/protocol/src/packets/client/finish_configuration.rs new file mode 100644 index 0000000..ea10c10 --- /dev/null +++ b/mclib/protocol/src/packets/client/finish_configuration.rs @@ -0,0 +1,8 @@ +use crate::packets::base::MCPacket; +use crate::types::base::MCType; +use crate::types::varint::MCVarInt; +use mclib_macros::MCPacket; + +#[derive(MCPacket, Debug, Clone)] +#[packet(packet_id = 0x02)] +pub struct FinishConfigurationClientbound {} diff --git a/mclib/protocol/src/packets/client/registry_data.rs b/mclib/protocol/src/packets/client/registry_data.rs index a7b4fee..b704e6b 100644 --- a/mclib/protocol/src/packets/client/registry_data.rs +++ b/mclib/protocol/src/packets/client/registry_data.rs @@ -7,5 +7,5 @@ use mclib_macros::MCPacket; #[derive(MCPacket, Debug, Clone)] #[packet(packet_id = 0x05)] pub struct RegistryData { - registry_codec: MCNBT, + pub registry_data: MCNBT, } diff --git a/mclib/protocol/src/packets/server.rs b/mclib/protocol/src/packets/server.rs index a45c24d..cfa60f1 100644 --- a/mclib/protocol/src/packets/server.rs +++ b/mclib/protocol/src/packets/server.rs @@ -1,3 +1,4 @@ +pub mod finish_configuration; pub mod handshake; pub mod login_acknowledged; pub mod login_start; diff --git a/mclib/protocol/src/packets/server/finish_configuration.rs b/mclib/protocol/src/packets/server/finish_configuration.rs new file mode 100644 index 0000000..db39307 --- /dev/null +++ b/mclib/protocol/src/packets/server/finish_configuration.rs @@ -0,0 +1,8 @@ +use crate::packets::base::MCPacket; +use crate::types::base::MCType; +use crate::types::varint::MCVarInt; +use mclib_macros::MCPacket; + +#[derive(MCPacket, Debug, Clone)] +#[packet(packet_id = 0x02)] +pub struct FinishConfigurationServerbound {} diff --git a/src/server/thread/tcp_listener.rs b/src/server/thread/tcp_listener.rs index b553fbd..b2db96d 100644 --- a/src/server/thread/tcp_listener.rs +++ b/src/server/thread/tcp_listener.rs @@ -1,12 +1,17 @@ use crate::conf::conf; +use crate::registry; use crate::server::communicator::WriteCommunicator; use crate::server::net::tcp::{NativeRead, TCPRead}; use crate::server::thread::tcp_writer::TCPWriterAPI; -use mclib::packets::client::{LoginSuccess, StatusResponse}; +use mclib::nbt::NBT; +use mclib::packets::client::{ + FinishConfigurationClientbound, LoginSuccess, RegistryData, StatusResponse, +}; use mclib::packets::server::{ - Handshake, HandshakeNextState, LoginAcknowledged, LoginStart, PingRequest, StatusRequest, + FinishConfigurationServerbound, Handshake, HandshakeNextState, LoginAcknowledged, LoginStart, + PingRequest, StatusRequest, }; -use mclib::types::MCVarInt; +use mclib::types::{MCVarInt, MCNBT}; use mclib::MCPacket; pub struct TCPListenerThread { @@ -80,9 +85,33 @@ impl TCPListenerThread { } pub fn handle_configuration(&mut self) { - todo!() + let registry_data = serde_json::from_str::(include_str!( + "../../../assets/registry_data_1.20.2.json" + )) + .unwrap(); + let registry_data_nbt = NBT::from(registry_data); + let registry_data_packet = RegistryData { + registry_data: MCNBT::from(registry_data_nbt), + }; + self.tcp_writer_api.send(TCPWriterAPI::SendMessageRaw { + uid: self.uid, + body: registry_data_packet.pack(), + }); + + let finish_configuration = FinishConfigurationClientbound {}; + self.tcp_writer_api.send(TCPWriterAPI::SendMessageRaw { + uid: self.uid, + body: finish_configuration.pack(), + }); + + let _ = self + .tcp_read + .read_packet() + .parse_packet::(); } + pub fn handle_play(&mut self) {} + pub fn execute(&mut self) { match self.handle_handshake() { HandshakeNextState::Status => { @@ -91,6 +120,7 @@ impl TCPListenerThread { HandshakeNextState::Login => { self.handle_login(); self.handle_configuration(); + self.handle_play(); } HandshakeNextState::Unknown => { error!("Unknown next state for handshake");