Skip to content

Commit

Permalink
chore: Move minecraft protocol to games. (gamedig#153)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
CosminPerRam authored Nov 18, 2023
1 parent bd73b65 commit 7416d54
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 94 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/examples/minecraft.rs
Original file line number Diff line number Diff line change
@@ -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(<port>), None is the default protocol port (which is 25565 for java
Expand Down
23 changes: 9 additions & 14 deletions crates/lib/src/games/definitions.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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))),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -30,7 +34,7 @@ pub fn query_java(
port: Option<u16>,
request_settings: Option<RequestSettings>,
) -> GDResult<JavaResponse> {
minecraft::query_java(
protocol::query_java(
&SocketAddr::new(*address, port_or_java_default(port)),
None,
request_settings,
Expand All @@ -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<u16>) -> GDResult<JavaResponse> {
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<u16>) -> GDResult<JavaResponse> {
minecraft::query_legacy_specific(
protocol::query_legacy_specific(
group,
&SocketAddr::new(*address, port_or_java_default(port)),
None,
Expand All @@ -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<u16>) -> GDResult<BedrockResponse> {
minecraft::query_bedrock(
protocol::query_bedrock(
&SocketAddr::new(*address, port_or_bedrock_default(port)),
None,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,13 @@ pub(crate) fn as_string(value: &str) -> GDResult<Vec<u8>> {

Ok(buf)
}

#[cfg(tests)]
mod tests {
#[test]
fn test_extra_request_settings() {
let settings = ExtraRequestSettings::default();

let _: minecraft::RequestSettings = settings.clone().into();
}
}
53 changes: 27 additions & 26 deletions crates/lib/src/games/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,
Expand Down Expand Up @@ -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)?
}
}
}
}
}
})
Expand Down
7 changes: 0 additions & 7 deletions crates/lib/src/protocols/minecraft/mod.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/lib/src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 9 additions & 7 deletions crates/lib/src/protocols/types.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ProprietaryProtocol {
TheShip,
Minecraft(Option<minecraft::types::Server>),
FFOW,
JC2M,
}
Expand All @@ -21,7 +22,6 @@ pub enum ProprietaryProtocol {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Protocol {
Gamespy(gamespy::GameSpyVersion),
Minecraft(Option<minecraft::types::Server>),
Quake(quake::QuakeVersion),
Valve(valve::Engine),
Unreal2,
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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();
/// ```
///
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 7416d54

Please sign in to comment.