From 7416d54b144812b63a691335b951843e916c701a Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Sat, 18 Nov 2023 19:44:09 +0200 Subject: [PATCH] chore: Move minecraft protocol to games. (#153) * chore: initial move and refactor of minecraft * fix: glob re-exports * fix: failing example sample * docs: update changelog to note the mc protocol implementation to games * docs: add back the reference query standard reference --- CHANGELOG.md | 4 +- crates/lib/examples/minecraft.rs | 4 +- crates/lib/src/games/definitions.rs | 23 ++++---- .../games/{minecraft.rs => minecraft/mod.rs} | 24 +++++---- .../minecraft/protocol/bedrock.rs | 6 +-- .../minecraft/protocol/java.rs | 10 ++-- .../minecraft/protocol/legacy_v1_4.rs | 7 ++- .../minecraft/protocol/legacy_v1_6.rs | 6 +-- .../minecraft/protocol/legacy_vb1_8.rs | 6 +-- .../minecraft/protocol/mod.rs | 4 +- .../{protocols => games}/minecraft/types.rs | 10 ++++ crates/lib/src/games/mod.rs | 53 ++++++++++--------- crates/lib/src/protocols/minecraft/mod.rs | 7 --- crates/lib/src/protocols/mod.rs | 2 - crates/lib/src/protocols/types.rs | 16 +++--- 15 files changed, 88 insertions(+), 94 deletions(-) rename crates/lib/src/games/{minecraft.rs => minecraft/mod.rs} (79%) rename crates/lib/src/{protocols => games}/minecraft/protocol/bedrock.rs (97%) rename crates/lib/src/{protocols => games}/minecraft/protocol/java.rs (96%) rename crates/lib/src/{protocols => games}/minecraft/protocol/legacy_v1_4.rs (94%) rename crates/lib/src/{protocols => games}/minecraft/protocol/legacy_v1_6.rs (97%) rename crates/lib/src/{protocols => games}/minecraft/protocol/legacy_vb1_8.rs (95%) rename crates/lib/src/{protocols => games}/minecraft/protocol/mod.rs (96%) rename crates/lib/src/{protocols => games}/minecraft/types.rs (97%) delete mode 100644 crates/lib/src/protocols/minecraft/mod.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce4b4e7..7d09f966 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,9 @@ Game: - - Left 4 Dead: `left4dead` -> `l4d`. - - 7 Days to Die: `7d2d` in definitions and `sd2d` in game declaration -> `sdtd`. - - Quake 3 Arena: `quake3arena` -> `q3a`. -- Minecraft Legacy 1.5 and 1.3 were renamed to 1.4 and beta 1.8 respectively to show the lowest version they support, this change includes Structs, Enum and game id renames, also removed the "v" from the game definition name. +- Minecraft: +- - Legacy 1.5 and 1.3 were renamed to 1.4 and beta 1.8 respectively to show the lowest version they support, this change includes Structs, Enum and game id renames, also removed the "v" from the game definition name. +- - Moved the Minecraft protocol implementation in the games folder as its proprietary. Protocols: - Valve: Removed `SteamApp` due to it not being really useful at all, replaced all instances with `Engine`. diff --git a/crates/lib/examples/minecraft.rs b/crates/lib/examples/minecraft.rs index 00ffb14c..ca49a5e4 100644 --- a/crates/lib/examples/minecraft.rs +++ b/crates/lib/examples/minecraft.rs @@ -1,5 +1,5 @@ -use gamedig::games::minecraft; -use gamedig::protocols::minecraft::RequestSettings; +use gamedig::minecraft; +use gamedig::minecraft::types::RequestSettings; fn main() { // or Some(), None is the default protocol port (which is 25565 for java diff --git a/crates/lib/src/games/definitions.rs b/crates/lib/src/games/definitions.rs index bcefc0df..7f55a652 100644 --- a/crates/lib/src/games/definitions.rs +++ b/crates/lib/src/games/definitions.rs @@ -1,12 +1,7 @@ //! Static definitions of currently supported games -use crate::protocols::{ - gamespy::GameSpyVersion, - minecraft::{LegacyGroup, Server}, - quake::QuakeVersion, - valve::Engine, - Protocol, -}; +use crate::games::minecraft::types::{LegacyGroup, Server}; +use crate::protocols::{gamespy::GameSpyVersion, quake::QuakeVersion, valve::Engine, Protocol}; use crate::Game; use crate::protocols::types::ProprietaryProtocol; @@ -36,14 +31,14 @@ macro_rules! game { /// Map of all currently supported games pub static GAMES: Map<&'static str, Game> = phf_map! { // Query with all minecraft protocols - "minecraft" => game!("Minecraft", 25565, Protocol::Minecraft(None)), + "minecraft" => game!("Minecraft", 25565, Protocol::PROPRIETARY(ProprietaryProtocol::Minecraft(None))), // Query with specific minecraft protocols - "minecraftbedrock" => game!("Minecraft (bedrock)", 19132, Protocol::Minecraft(Some(Server::Bedrock))), - "minecraftpocket" => game!("Minecraft (pocket)", 19132, Protocol::Minecraft(Some(Server::Bedrock))), - "minecraftjava" => game!("Minecraft (java)", 25565, Protocol::Minecraft(Some(Server::Java))), - "minecraftlegacy16" => game!("Minecraft (legacy 1.6)", 25565, Protocol::Minecraft(Some(Server::Legacy(LegacyGroup::V1_6)))), - "minecraftlegacy14" => game!("Minecraft (legacy 1.4)", 25565, Protocol::Minecraft(Some(Server::Legacy(LegacyGroup::V1_4)))), - "minecraftlegacyb18" => game!("Minecraft (legacy b1.8)", 25565, Protocol::Minecraft(Some(Server::Legacy(LegacyGroup::VB1_8)))), + "minecraftbedrock" => game!("Minecraft (bedrock)", 19132, Protocol::PROPRIETARY(ProprietaryProtocol::Minecraft(Some(Server::Bedrock)))), + "minecraftpocket" => game!("Minecraft (pocket)", 19132, Protocol::PROPRIETARY(ProprietaryProtocol::Minecraft(Some(Server::Bedrock)))), + "minecraftjava" => game!("Minecraft (java)", 25565, Protocol::PROPRIETARY(ProprietaryProtocol::Minecraft(Some(Server::Java)))), + "minecraftlegacy16" => game!("Minecraft (legacy 1.6)", 25565, Protocol::PROPRIETARY(ProprietaryProtocol::Minecraft(Some(Server::Legacy(LegacyGroup::V1_6))))), + "minecraftlegacy14" => game!("Minecraft (legacy 1.4)", 25565, Protocol::PROPRIETARY(ProprietaryProtocol::Minecraft(Some(Server::Legacy(LegacyGroup::V1_4))))), + "minecraftlegacyb18" => game!("Minecraft (legacy b1.8)", 25565, Protocol::PROPRIETARY(ProprietaryProtocol::Minecraft(Some(Server::Legacy(LegacyGroup::VB1_8))))), "alienswarm" => game!("Alien Swarm", 27015, Protocol::Valve(Engine::new(630))), "aoc" => game!("Age of Chivalry", 27015, Protocol::Valve(Engine::new(17510))), "a2oa" => game!("ARMA 2: Operation Arrowhead", 2304, Protocol::Valve(Engine::new(33930))), diff --git a/crates/lib/src/games/minecraft.rs b/crates/lib/src/games/minecraft/mod.rs similarity index 79% rename from crates/lib/src/games/minecraft.rs rename to crates/lib/src/games/minecraft/mod.rs index 9fe2db20..aa8b28ea 100644 --- a/crates/lib/src/games/minecraft.rs +++ b/crates/lib/src/games/minecraft/mod.rs @@ -1,9 +1,13 @@ -use crate::protocols::minecraft::RequestSettings; -use crate::{ - protocols::minecraft::{self, BedrockResponse, JavaResponse, LegacyGroup}, - GDErrorKind, - GDResult, -}; +/// The implementation. +/// Reference: [Server List Ping](https://wiki.vg/Server_List_Ping) +pub mod protocol; +/// All types used by the implementation. +pub mod types; + +pub use protocol::*; +pub use types::*; + +use crate::{GDErrorKind, GDResult}; use std::net::{IpAddr, SocketAddr}; /// Query with all the protocol variants one by one (Java -> Bedrock -> Legacy @@ -30,7 +34,7 @@ pub fn query_java( port: Option, request_settings: Option, ) -> GDResult { - minecraft::query_java( + protocol::query_java( &SocketAddr::new(*address, port_or_java_default(port)), None, request_settings, @@ -39,12 +43,12 @@ pub fn query_java( /// Query a (Java) Legacy Server (1.6 -> 1.4 -> Beta 1.8). pub fn query_legacy(address: &IpAddr, port: Option) -> GDResult { - minecraft::query_legacy(&SocketAddr::new(*address, port_or_java_default(port)), None) + protocol::query_legacy(&SocketAddr::new(*address, port_or_java_default(port)), None) } /// Query a specific (Java) Legacy Server. pub fn query_legacy_specific(group: LegacyGroup, address: &IpAddr, port: Option) -> GDResult { - minecraft::query_legacy_specific( + protocol::query_legacy_specific( group, &SocketAddr::new(*address, port_or_java_default(port)), None, @@ -53,7 +57,7 @@ pub fn query_legacy_specific(group: LegacyGroup, address: &IpAddr, port: Option< /// Query a Bedrock Server. pub fn query_bedrock(address: &IpAddr, port: Option) -> GDResult { - minecraft::query_bedrock( + protocol::query_bedrock( &SocketAddr::new(*address, port_or_bedrock_default(port)), None, ) diff --git a/crates/lib/src/protocols/minecraft/protocol/bedrock.rs b/crates/lib/src/games/minecraft/protocol/bedrock.rs similarity index 97% rename from crates/lib/src/protocols/minecraft/protocol/bedrock.rs rename to crates/lib/src/games/minecraft/protocol/bedrock.rs index 60efc36c..119d8053 100644 --- a/crates/lib/src/protocols/minecraft/protocol/bedrock.rs +++ b/crates/lib/src/games/minecraft/protocol/bedrock.rs @@ -2,10 +2,8 @@ // (MIT) from https://github.com/gamedig/node-gamedig/blob/master/protocols/minecraftbedrock.js use crate::{ buffer::{Buffer, Utf8Decoder}, - protocols::{ - minecraft::{BedrockResponse, GameMode, Server}, - types::TimeoutSettings, - }, + games::minecraft::{BedrockResponse, GameMode, Server}, + protocols::types::TimeoutSettings, socket::{Socket, UdpSocket}, utils::{error_by_expected_size, retry_on_timeout}, GDErrorKind::{PacketBad, TypeParse}, diff --git a/crates/lib/src/protocols/minecraft/protocol/java.rs b/crates/lib/src/games/minecraft/protocol/java.rs similarity index 96% rename from crates/lib/src/protocols/minecraft/protocol/java.rs rename to crates/lib/src/games/minecraft/protocol/java.rs index d3406c31..5edc8ca5 100644 --- a/crates/lib/src/protocols/minecraft/protocol/java.rs +++ b/crates/lib/src/games/minecraft/protocol/java.rs @@ -1,20 +1,16 @@ use crate::{ buffer::Buffer, - protocols::{ - minecraft::{as_varint, get_string, get_varint, JavaResponse, Player, Server}, - types::TimeoutSettings, - }, + games::minecraft::{as_string, as_varint, get_string, get_varint, JavaResponse, Player, RequestSettings, Server}, + protocols::types::TimeoutSettings, socket::{Socket, TcpSocket}, utils::retry_on_timeout, GDErrorKind::{JsonParse, PacketBad}, GDResult, }; -use std::net::SocketAddr; - -use crate::protocols::minecraft::{as_string, RequestSettings}; use byteorder::LittleEndian; use serde_json::Value; +use std::net::SocketAddr; pub struct Java { socket: TcpSocket, diff --git a/crates/lib/src/protocols/minecraft/protocol/legacy_v1_4.rs b/crates/lib/src/games/minecraft/protocol/legacy_v1_4.rs similarity index 94% rename from crates/lib/src/protocols/minecraft/protocol/legacy_v1_4.rs rename to crates/lib/src/games/minecraft/protocol/legacy_v1_4.rs index ba4733ec..0df738aa 100644 --- a/crates/lib/src/protocols/minecraft/protocol/legacy_v1_4.rs +++ b/crates/lib/src/games/minecraft/protocol/legacy_v1_4.rs @@ -1,11 +1,10 @@ use byteorder::BigEndian; +use crate::minecraft::protocol::legacy_v1_6::LegacyV1_6; use crate::{ buffer::{Buffer, Utf16Decoder}, - protocols::{ - minecraft::{protocol::legacy_v1_6::LegacyV1_6, JavaResponse, LegacyGroup, Server}, - types::TimeoutSettings, - }, + games::minecraft::{JavaResponse, LegacyGroup, Server}, + protocols::types::TimeoutSettings, socket::{Socket, TcpSocket}, utils::{error_by_expected_size, retry_on_timeout}, GDErrorKind::{PacketBad, ProtocolFormat}, diff --git a/crates/lib/src/protocols/minecraft/protocol/legacy_v1_6.rs b/crates/lib/src/games/minecraft/protocol/legacy_v1_6.rs similarity index 97% rename from crates/lib/src/protocols/minecraft/protocol/legacy_v1_6.rs rename to crates/lib/src/games/minecraft/protocol/legacy_v1_6.rs index 0def533b..3b53aac7 100644 --- a/crates/lib/src/protocols/minecraft/protocol/legacy_v1_6.rs +++ b/crates/lib/src/games/minecraft/protocol/legacy_v1_6.rs @@ -2,10 +2,8 @@ use byteorder::BigEndian; use crate::{ buffer::{Buffer, Utf16Decoder}, - protocols::{ - minecraft::{JavaResponse, LegacyGroup, Server}, - types::TimeoutSettings, - }, + games::minecraft::{JavaResponse, LegacyGroup, Server}, + protocols::types::TimeoutSettings, socket::{Socket, TcpSocket}, utils::{error_by_expected_size, retry_on_timeout}, GDErrorKind::{PacketBad, ProtocolFormat}, diff --git a/crates/lib/src/protocols/minecraft/protocol/legacy_vb1_8.rs b/crates/lib/src/games/minecraft/protocol/legacy_vb1_8.rs similarity index 95% rename from crates/lib/src/protocols/minecraft/protocol/legacy_vb1_8.rs rename to crates/lib/src/games/minecraft/protocol/legacy_vb1_8.rs index 426f42ee..3ad21bae 100644 --- a/crates/lib/src/protocols/minecraft/protocol/legacy_vb1_8.rs +++ b/crates/lib/src/games/minecraft/protocol/legacy_vb1_8.rs @@ -1,9 +1,7 @@ use crate::{ buffer::{Buffer, Utf16Decoder}, - protocols::{ - minecraft::{JavaResponse, LegacyGroup, Server}, - types::TimeoutSettings, - }, + games::minecraft::{JavaResponse, LegacyGroup, Server}, + protocols::types::TimeoutSettings, socket::{Socket, TcpSocket}, utils::{error_by_expected_size, retry_on_timeout}, GDErrorKind::{PacketBad, ProtocolFormat}, diff --git a/crates/lib/src/protocols/minecraft/protocol/mod.rs b/crates/lib/src/games/minecraft/protocol/mod.rs similarity index 96% rename from crates/lib/src/protocols/minecraft/protocol/mod.rs rename to crates/lib/src/games/minecraft/protocol/mod.rs index d2c7762a..5beaeba9 100644 --- a/crates/lib/src/protocols/minecraft/protocol/mod.rs +++ b/crates/lib/src/games/minecraft/protocol/mod.rs @@ -1,6 +1,6 @@ -use crate::protocols::minecraft::types::RequestSettings; +use crate::games::minecraft::types::RequestSettings; use crate::{ - protocols::minecraft::{ + games::minecraft::{ protocol::{ bedrock::Bedrock, java::Java, diff --git a/crates/lib/src/protocols/minecraft/types.rs b/crates/lib/src/games/minecraft/types.rs similarity index 97% rename from crates/lib/src/protocols/minecraft/types.rs rename to crates/lib/src/games/minecraft/types.rs index 82618eb3..3bb90ff0 100644 --- a/crates/lib/src/protocols/minecraft/types.rs +++ b/crates/lib/src/games/minecraft/types.rs @@ -293,3 +293,13 @@ pub(crate) fn as_string(value: &str) -> GDResult> { Ok(buf) } + +#[cfg(tests)] +mod tests { + #[test] + fn test_extra_request_settings() { + let settings = ExtraRequestSettings::default(); + + let _: minecraft::RequestSettings = settings.clone().into(); + } +} diff --git a/crates/lib/src/games/mod.rs b/crates/lib/src/games/mod.rs index 7749d5b2..e8e56415 100644 --- a/crates/lib/src/games/mod.rs +++ b/crates/lib/src/games/mod.rs @@ -89,32 +89,6 @@ pub fn query_with_timeout_and_extra_settings( ) .map(Box::new)? } - Protocol::Minecraft(version) => { - match version { - Some(protocols::minecraft::Server::Java) => { - protocols::minecraft::query_java( - &socket_addr, - timeout_settings, - extra_settings.map(ExtraRequestSettings::into), - ) - .map(Box::new)? - } - Some(protocols::minecraft::Server::Bedrock) => { - protocols::minecraft::query_bedrock(&socket_addr, timeout_settings).map(Box::new)? - } - Some(protocols::minecraft::Server::Legacy(group)) => { - protocols::minecraft::query_legacy_specific(*group, &socket_addr, timeout_settings).map(Box::new)? - } - None => { - protocols::minecraft::query( - &socket_addr, - timeout_settings, - extra_settings.map(ExtraRequestSettings::into), - ) - .map(Box::new)? - } - } - } Protocol::Gamespy(version) => { match version { GameSpyVersion::One => protocols::gamespy::one::query(&socket_addr, timeout_settings).map(Box::new)?, @@ -148,6 +122,33 @@ pub fn query_with_timeout_and_extra_settings( } ProprietaryProtocol::FFOW => ffow::query_with_timeout(address, port, timeout_settings).map(Box::new)?, ProprietaryProtocol::JC2M => jc2m::query_with_timeout(address, port, timeout_settings).map(Box::new)?, + ProprietaryProtocol::Minecraft(version) => { + match version { + Some(minecraft::Server::Java) => { + minecraft::protocol::query_java( + &socket_addr, + timeout_settings, + extra_settings.map(ExtraRequestSettings::into), + ) + .map(Box::new)? + } + Some(minecraft::Server::Bedrock) => { + minecraft::protocol::query_bedrock(&socket_addr, timeout_settings).map(Box::new)? + } + Some(minecraft::Server::Legacy(group)) => { + minecraft::protocol::query_legacy_specific(*group, &socket_addr, timeout_settings) + .map(Box::new)? + } + None => { + minecraft::protocol::query( + &socket_addr, + timeout_settings, + extra_settings.map(ExtraRequestSettings::into), + ) + .map(Box::new)? + } + } + } } } }) diff --git a/crates/lib/src/protocols/minecraft/mod.rs b/crates/lib/src/protocols/minecraft/mod.rs deleted file mode 100644 index 3d98ab90..00000000 --- a/crates/lib/src/protocols/minecraft/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -/// The implementation. -pub mod protocol; -/// All types used by the implementation. -pub mod types; - -pub use protocol::*; -pub use types::*; diff --git a/crates/lib/src/protocols/mod.rs b/crates/lib/src/protocols/mod.rs index 697d7e8c..639fbf80 100644 --- a/crates/lib/src/protocols/mod.rs +++ b/crates/lib/src/protocols/mod.rs @@ -6,8 +6,6 @@ /// Reference: [node-GameDig](https://github.com/gamedig/node-gamedig/blob/master/protocols/gamespy1.js) pub mod gamespy; -/// Reference: [Server List Ping](https://wiki.vg/Server_List_Ping) -pub mod minecraft; /// Reference: [node-GameDig](https://github.com/gamedig/node-gamedig/blob/master/protocols/quake1.js) pub mod quake; /// General types that are used by all protocols. diff --git a/crates/lib/src/protocols/types.rs b/crates/lib/src/protocols/types.rs index 67e7d13c..22ad776d 100644 --- a/crates/lib/src/protocols/types.rs +++ b/crates/lib/src/protocols/types.rs @@ -1,6 +1,6 @@ -use crate::protocols::{gamespy, minecraft, quake, unreal2, valve}; +use crate::protocols::{gamespy, quake, unreal2, valve}; use crate::GDErrorKind::InvalidInput; -use crate::GDResult; +use crate::{minecraft, GDResult}; use std::time::Duration; @@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum ProprietaryProtocol { TheShip, + Minecraft(Option), FFOW, JC2M, } @@ -21,7 +22,6 @@ pub enum ProprietaryProtocol { #[derive(Debug, Clone, PartialEq, Eq)] pub enum Protocol { Gamespy(gamespy::GameSpyVersion), - Minecraft(Option), Quake(quake::QuakeVersion), Valve(valve::Engine), Unreal2, @@ -33,11 +33,12 @@ pub enum Protocol { #[derive(Debug, Clone, PartialEq)] pub enum GenericResponse<'a> { GameSpy(gamespy::VersionedResponse<'a>), - Minecraft(minecraft::VersionedResponse<'a>), Quake(quake::VersionedResponse<'a>), Valve(&'a valve::Response), Unreal2(&'a unreal2::Response), #[cfg(feature = "games")] + Minecraft(minecraft::VersionedResponse<'a>), + #[cfg(feature = "games")] TheShip(&'a crate::games::theship::Response), #[cfg(feature = "games")] FFOW(&'a crate::games::ffow::Response), @@ -51,10 +52,11 @@ pub enum GenericPlayer<'a> { Valve(&'a valve::ServerPlayer), QuakeOne(&'a quake::one::Player), QuakeTwo(&'a quake::two::Player), - Minecraft(&'a minecraft::Player), Gamespy(gamespy::VersionedPlayer<'a>), Unreal2(&'a unreal2::Player), #[cfg(feature = "games")] + Minecraft(&'a minecraft::Player), + #[cfg(feature = "games")] TheShip(&'a crate::games::theship::TheShipPlayer), #[cfg(feature = "games")] JCMP2(&'a crate::games::jc2m::Player), @@ -238,7 +240,8 @@ impl Default for TimeoutSettings { /// ## Examples /// Create minecraft settings with builder: /// ``` -/// use gamedig::protocols::{minecraft, ExtraRequestSettings}; +/// use gamedig::games::minecraft; +/// use gamedig::protocols::ExtraRequestSettings; /// let mc_settings: minecraft::RequestSettings = ExtraRequestSettings::default().set_hostname("mc.hypixel.net".to_string()).into(); /// ``` /// @@ -362,7 +365,6 @@ mod tests { fn test_extra_request_settings() { let settings = ExtraRequestSettings::default(); - let _: minecraft::RequestSettings = settings.clone().into(); let _: valve::GatheringSettings = settings.into(); } }