mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
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
This commit is contained in:
parent
bd73b657c7
commit
7416d54b14
15 changed files with 88 additions and 94 deletions
|
|
@ -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))),
|
||||
|
|
|
|||
|
|
@ -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<u16>,
|
||||
request_settings: Option<RequestSettings>,
|
||||
) -> GDResult<JavaResponse> {
|
||||
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<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,
|
||||
|
|
@ -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,
|
||||
)
|
||||
|
|
@ -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},
|
||||
|
|
@ -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,
|
||||
|
|
@ -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},
|
||||
|
|
@ -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},
|
||||
|
|
@ -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},
|
||||
|
|
@ -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,
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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)?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
/// The implementation.
|
||||
pub mod protocol;
|
||||
/// All types used by the implementation.
|
||||
pub mod types;
|
||||
|
||||
pub use protocol::*;
|
||||
pub use types::*;
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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<minecraft::types::Server>),
|
||||
FFOW,
|
||||
JC2M,
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue