chore: Tidy up some out of place types (#160)

* tidy: Move TimeoutSettings out of protocol types

* tidy: Move game type to a types file

* tidy: Move generic query functions to own file

* tidy: Move ExtraRequestSettings to game types

* tidy: Move generic query functions into games module

* Revert "tidy: Move ExtraRequestSettings to game types"

This reverts commit aa0d23fc2acc12d68f03265a9caff6f98bf25054.

* Revert "tidy: Move TimeoutSettings out of protocol types"

This reverts commit 7ee10711ed11f17cc06565d4cc9102040d53a319.

* tidy: Re-export types needed for query at the root

* Add/Update badge

---------

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tom 2024-01-01 22:17:43 +00:00 committed by GitHub
parent 483d728ac8
commit bd3727d7fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 139 deletions

View file

@ -1,5 +1,5 @@
<svg width="181.6" height="20" viewBox="0 0 1816 200" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Node game coverage: 14.02%">
<title>Node game coverage: 14.02%</title>
<svg width="181.6" height="20" viewBox="0 0 1816 200" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Node game coverage: 13.98%">
<title>Node game coverage: 13.98%</title>
<linearGradient id="a" x2="0" y2="100%">
<stop offset="0" stop-opacity=".1" stop-color="#EEE"/>
<stop offset="1" stop-opacity=".1"/>
@ -13,8 +13,8 @@
<g aria-hidden="true" fill="#fff" text-anchor="start" font-family="Verdana,DejaVu Sans,sans-serif" font-size="110">
<text x="60" y="148" textLength="1176" fill="#000" opacity="0.25">Node game coverage</text>
<text x="50" y="138" textLength="1176">Node game coverage</text>
<text x="1331" y="148" textLength="440" fill="#000" opacity="0.25">14.02%</text>
<text x="1321" y="138" textLength="440">14.02%</text>
<text x="1331" y="148" textLength="440" fill="#000" opacity="0.25">13.98%</text>
<text x="1321" y="138" textLength="440">13.98%</text>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1 KiB

After

Width:  |  Height:  |  Size: 1 KiB

Before After
Before After

View file

@ -1,10 +1,7 @@
use std::net::{IpAddr, ToSocketAddrs};
use clap::{Parser, ValueEnum};
use gamedig::{
games::*,
protocols::types::{CommonResponse, ExtraRequestSettings, TimeoutSettings},
};
use gamedig::{games::*, protocols::types::CommonResponse, ExtraRequestSettings, TimeoutSettings};
mod error;

View file

@ -1,8 +1,10 @@
use gamedig::{
protocols::types::{CommonResponse, ExtraRequestSettings, TimeoutSettings},
protocols::types::CommonResponse,
query_with_timeout_and_extra_settings,
ExtraRequestSettings,
GDResult,
Game,
TimeoutSettings,
GAMES,
};

View file

@ -1,6 +1,6 @@
use gamedig::protocols::types::TimeoutSettings;
use gamedig::protocols::valve;
use gamedig::protocols::valve::{Engine, GatheringSettings};
use gamedig::TimeoutSettings;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::time::Duration;

View file

@ -1,8 +1,5 @@
//! Currently supported games.
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub mod gamespy;
pub mod quake;
pub mod unreal2;
@ -26,135 +23,14 @@ pub mod savage2;
/// The Ship
pub mod theship;
use crate::protocols::gamespy::GameSpyVersion;
use crate::protocols::quake::QuakeVersion;
use crate::protocols::types::{CommonResponse, ExtraRequestSettings, ProprietaryProtocol, TimeoutSettings};
use crate::protocols::{self, Protocol};
use crate::GDResult;
use std::net::{IpAddr, SocketAddr};
pub mod types;
pub use types::*;
/// Definition of a game
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Game {
/// Full name of the game
pub name: &'static str,
/// Default port used by game
pub default_port: u16,
/// The protocol the game's query uses
pub protocol: Protocol,
/// Request settings.
pub request_settings: ExtraRequestSettings,
}
pub mod query;
pub use query::*;
#[cfg(feature = "game_defs")]
mod definitions;
#[cfg(feature = "game_defs")]
pub use definitions::GAMES;
/// Make a query given a game definition
#[inline]
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<Box<dyn CommonResponse>> {
query_with_timeout_and_extra_settings(game, address, port, None, None)
}
/// Make a query given a game definition and timeout settings
#[inline]
pub fn query_with_timeout(
game: &Game,
address: &IpAddr,
port: Option<u16>,
timeout_settings: Option<TimeoutSettings>,
) -> GDResult<Box<dyn CommonResponse>> {
query_with_timeout_and_extra_settings(game, address, port, timeout_settings, None)
}
/// Make a query given a game definition, timeout settings, and extra settings
pub fn query_with_timeout_and_extra_settings(
game: &Game,
address: &IpAddr,
port: Option<u16>,
timeout_settings: Option<TimeoutSettings>,
extra_settings: Option<ExtraRequestSettings>,
) -> GDResult<Box<dyn CommonResponse>> {
let socket_addr = SocketAddr::new(*address, port.unwrap_or(game.default_port));
Ok(match &game.protocol {
Protocol::Valve(engine) => {
protocols::valve::query(
&socket_addr,
*engine,
extra_settings
.or_else(|| Option::from(game.request_settings.clone()))
.map(ExtraRequestSettings::into),
timeout_settings,
)
.map(Box::new)?
}
Protocol::Gamespy(version) => {
match version {
GameSpyVersion::One => protocols::gamespy::one::query(&socket_addr, timeout_settings).map(Box::new)?,
GameSpyVersion::Two => protocols::gamespy::two::query(&socket_addr, timeout_settings).map(Box::new)?,
GameSpyVersion::Three => {
protocols::gamespy::three::query(&socket_addr, timeout_settings).map(Box::new)?
}
}
}
Protocol::Quake(version) => {
match version {
QuakeVersion::One => protocols::quake::one::query(&socket_addr, timeout_settings).map(Box::new)?,
QuakeVersion::Two => protocols::quake::two::query(&socket_addr, timeout_settings).map(Box::new)?,
QuakeVersion::Three => protocols::quake::three::query(&socket_addr, timeout_settings).map(Box::new)?,
}
}
Protocol::Unreal2 => {
protocols::unreal2::query(
&socket_addr,
&extra_settings
.map(ExtraRequestSettings::into)
.unwrap_or_default(),
timeout_settings,
)
.map(Box::new)?
}
Protocol::PROPRIETARY(protocol) => {
match protocol {
ProprietaryProtocol::Savage2 => {
savage2::query_with_timeout(address, port, timeout_settings).map(Box::new)?
}
ProprietaryProtocol::TheShip => {
theship::query_with_timeout(address, port, timeout_settings).map(Box::new)?
}
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)?
}
}
}
}
}
})
}

View file

@ -0,0 +1,117 @@
//! Generic query functions
use std::net::{IpAddr, SocketAddr};
use crate::games::types::Game;
use crate::games::{ffow, jc2m, minecraft, savage2, theship};
use crate::protocols;
use crate::protocols::gamespy::GameSpyVersion;
use crate::protocols::quake::QuakeVersion;
use crate::protocols::types::{CommonResponse, ExtraRequestSettings, ProprietaryProtocol, Protocol, TimeoutSettings};
use crate::GDResult;
/// Make a query given a game definition
#[inline]
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<Box<dyn CommonResponse>> {
query_with_timeout_and_extra_settings(game, address, port, None, None)
}
/// Make a query given a game definition and timeout settings
#[inline]
pub fn query_with_timeout(
game: &Game,
address: &IpAddr,
port: Option<u16>,
timeout_settings: Option<TimeoutSettings>,
) -> GDResult<Box<dyn CommonResponse>> {
query_with_timeout_and_extra_settings(game, address, port, timeout_settings, None)
}
/// Make a query given a game definition, timeout settings, and extra settings
pub fn query_with_timeout_and_extra_settings(
game: &Game,
address: &IpAddr,
port: Option<u16>,
timeout_settings: Option<TimeoutSettings>,
extra_settings: Option<ExtraRequestSettings>,
) -> GDResult<Box<dyn CommonResponse>> {
let socket_addr = SocketAddr::new(*address, port.unwrap_or(game.default_port));
Ok(match &game.protocol {
Protocol::Valve(engine) => {
protocols::valve::query(
&socket_addr,
*engine,
extra_settings
.or_else(|| Option::from(game.request_settings.clone()))
.map(ExtraRequestSettings::into),
timeout_settings,
)
.map(Box::new)?
}
Protocol::Gamespy(version) => {
match version {
GameSpyVersion::One => protocols::gamespy::one::query(&socket_addr, timeout_settings).map(Box::new)?,
GameSpyVersion::Two => protocols::gamespy::two::query(&socket_addr, timeout_settings).map(Box::new)?,
GameSpyVersion::Three => {
protocols::gamespy::three::query(&socket_addr, timeout_settings).map(Box::new)?
}
}
}
Protocol::Quake(version) => {
match version {
QuakeVersion::One => protocols::quake::one::query(&socket_addr, timeout_settings).map(Box::new)?,
QuakeVersion::Two => protocols::quake::two::query(&socket_addr, timeout_settings).map(Box::new)?,
QuakeVersion::Three => protocols::quake::three::query(&socket_addr, timeout_settings).map(Box::new)?,
}
}
Protocol::Unreal2 => {
protocols::unreal2::query(
&socket_addr,
&extra_settings
.map(ExtraRequestSettings::into)
.unwrap_or_default(),
timeout_settings,
)
.map(Box::new)?
}
Protocol::PROPRIETARY(protocol) => {
match protocol {
ProprietaryProtocol::Savage2 => {
savage2::query_with_timeout(address, port, timeout_settings).map(Box::new)?
}
ProprietaryProtocol::TheShip => {
theship::query_with_timeout(address, port, timeout_settings).map(Box::new)?
}
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)?
}
}
}
}
}
})
}

View file

@ -0,0 +1,20 @@
//! Game related types
use crate::protocols::types::{ExtraRequestSettings, Protocol};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Definition of a game
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Game {
/// Full name of the game
pub name: &'static str,
/// Default port used by game
pub default_port: u16,
/// The protocol the game's query uses
pub protocol: Protocol,
/// Request settings.
pub request_settings: ExtraRequestSettings,
}

View file

@ -15,7 +15,7 @@
//!
//! ## Using a game definition
//! ```
//! use gamedig::games::{GAMES, query};
//! use gamedig::{GAMES, query};
//!
//! let game = GAMES.get("teamfortress2").unwrap(); // Get a game definition, the full list can be found in src/games/mod.rs
//! let response = query(game, &"127.0.0.1".parse().unwrap(), None); // None will use the default port
@ -48,5 +48,10 @@ mod utils;
pub use errors::*;
#[cfg(feature = "games")]
pub use games::*;
#[cfg(feature = "games")]
pub use query::*;
#[cfg(feature = "services")]
pub use services::*;
// Re-export types needed to call games::query::query in the root
pub use protocols::types::{ExtraRequestSettings, TimeoutSettings};