mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
feat: Add GatheringSettings on Valve macros (#128)
* Initial macro modification for gathering settings * Initial Valheim support * Remove unused use * Fix macro Thanks bunch @Douile * docs: Add Valheim to CHANGELOG and GAMES * Add commentary regarding gathering settings comment generation * Add GatheringSettings to game! * Remove unused stuff * Fix tests and add comment regarding the game argument
This commit is contained in:
parent
1ca6e6e85c
commit
501524b0da
8 changed files with 89 additions and 21 deletions
|
|
@ -3,7 +3,7 @@ Who knows what the future holds...
|
||||||
|
|
||||||
# 0.X.Y - DD/MM/YYYY
|
# 0.X.Y - DD/MM/YYYY
|
||||||
### Changes:
|
### Changes:
|
||||||
Nothing yet.
|
- Added [Valheim](https://store.steampowered.com/app/892970/Valheim/) support.
|
||||||
|
|
||||||
### Breaking:
|
### Breaking:
|
||||||
None, yaay!
|
None, yaay!
|
||||||
|
|
|
||||||
1
GAMES.md
1
GAMES.md
|
|
@ -62,6 +62,7 @@ Beware of the `Notes` column, as it contains information about query port offset
|
||||||
| Creativerse | CREATIVERSE | Valve | Query Port offset: 1. |
|
| Creativerse | CREATIVERSE | Valve | Query Port offset: 1. |
|
||||||
| Garry's Mod | GARRYSMOD | Valve | |
|
| Garry's Mod | GARRYSMOD | Valve | |
|
||||||
| Barotrauma | BAROTRAUMA | Valve | Query Port offset: 1. |
|
| Barotrauma | BAROTRAUMA | Valve | Query Port offset: 1. |
|
||||||
|
| Valheim | VALHEIM | Valve | Query Port offset: 1. Does not respond to the A2S rules. |
|
||||||
|
|
||||||
## Planned to add support:
|
## Planned to add support:
|
||||||
_
|
_
|
||||||
|
|
|
||||||
|
|
@ -2,23 +2,21 @@ use gamedig::{
|
||||||
protocols::types::{CommonResponse, ExtraRequestSettings, TimeoutSettings},
|
protocols::types::{CommonResponse, ExtraRequestSettings, TimeoutSettings},
|
||||||
query_with_timeout_and_extra_settings,
|
query_with_timeout_and_extra_settings,
|
||||||
GDResult,
|
GDResult,
|
||||||
|
Game,
|
||||||
GAMES,
|
GAMES,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
|
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
|
||||||
|
|
||||||
/// Make a query given the name of a game
|
/// Make a query given the name of a game
|
||||||
|
/// The `game` argument is taken from the [GAMES](gamedig::GAMES) map.
|
||||||
fn generic_query(
|
fn generic_query(
|
||||||
game_name: &str,
|
game: &Game,
|
||||||
addr: &IpAddr,
|
addr: &IpAddr,
|
||||||
port: Option<u16>,
|
port: Option<u16>,
|
||||||
timeout_settings: Option<TimeoutSettings>,
|
timeout_settings: Option<TimeoutSettings>,
|
||||||
extra_settings: Option<ExtraRequestSettings>,
|
extra_settings: Option<ExtraRequestSettings>,
|
||||||
) -> GDResult<Box<dyn CommonResponse>> {
|
) -> GDResult<Box<dyn CommonResponse>> {
|
||||||
let game = GAMES
|
|
||||||
.get(game_name)
|
|
||||||
.expect("Game doesn't exist, run without arguments to see a list of games");
|
|
||||||
|
|
||||||
println!("Querying {:#?} with game {:#?}.", addr, game);
|
println!("Querying {:#?} with game {:#?}.", addr, game);
|
||||||
|
|
||||||
let response = query_with_timeout_and_extra_settings(game, addr, port, timeout_settings, extra_settings)?;
|
let response = query_with_timeout_and_extra_settings(game, addr, port, timeout_settings, extra_settings)?;
|
||||||
|
|
@ -51,14 +49,18 @@ fn main() {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let extra_settings = ExtraRequestSettings::default()
|
let game = GAMES
|
||||||
|
.get(&game_name)
|
||||||
|
.expect("Game doesn't exist, run without arguments to see a list of games");
|
||||||
|
|
||||||
|
let extra_settings = game
|
||||||
|
.request_settings
|
||||||
|
.clone()
|
||||||
.set_hostname(hostname.to_string())
|
.set_hostname(hostname.to_string())
|
||||||
.set_gather_rules(true)
|
|
||||||
.set_gather_players(true)
|
|
||||||
.set_check_app_id(false);
|
.set_check_app_id(false);
|
||||||
|
|
||||||
generic_query(
|
generic_query(
|
||||||
&game_name,
|
game,
|
||||||
&addr.ip(),
|
&addr.ip(),
|
||||||
port,
|
port,
|
||||||
Some(timeout_settings),
|
Some(timeout_settings),
|
||||||
|
|
@ -67,8 +69,7 @@ fn main() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
// Without arguments print a list of games
|
// Without arguments print a list of games
|
||||||
|
for (name, game) in GAMES.entries() {
|
||||||
for (name, game) in gamedig::games::GAMES.entries() {
|
|
||||||
println!("{}\t{}", name, game.name);
|
println!("{}\t{}", name, game.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -95,7 +96,12 @@ mod test {
|
||||||
)
|
)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
assert!(generic_query(game_name, &ADDR, None, timeout_settings, None).is_err());
|
|
||||||
|
let game = GAMES
|
||||||
|
.get(game_name)
|
||||||
|
.expect("Game doesn't exist, run without arguments to see a list of games");
|
||||||
|
|
||||||
|
assert!(generic_query(game, &ADDR, None, timeout_settings, None).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,25 @@ use crate::protocols::{
|
||||||
use crate::Game;
|
use crate::Game;
|
||||||
|
|
||||||
use crate::protocols::types::ProprietaryProtocol;
|
use crate::protocols::types::ProprietaryProtocol;
|
||||||
|
use crate::protocols::valve::GatheringSettings;
|
||||||
use phf::{phf_map, Map};
|
use phf::{phf_map, Map};
|
||||||
|
|
||||||
macro_rules! game {
|
macro_rules! game {
|
||||||
($name: literal, $default_port: literal, $protocol: expr) => {
|
($name: literal, $default_port: literal, $protocol: expr) => {
|
||||||
|
game!(
|
||||||
|
$name,
|
||||||
|
$default_port,
|
||||||
|
$protocol,
|
||||||
|
GatheringSettings::default().into_extra()
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
($name: literal, $default_port: literal, $protocol: expr, $extra_request_settings: expr) => {
|
||||||
Game {
|
Game {
|
||||||
name: $name,
|
name: $name,
|
||||||
default_port: $default_port,
|
default_port: $default_port,
|
||||||
protocol: $protocol,
|
protocol: $protocol,
|
||||||
|
request_settings: $extra_request_settings,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -86,6 +97,11 @@ pub static GAMES: Map<&'static str, Game> = phf_map! {
|
||||||
"theship" => game!("The Ship", 27015, Protocol::PROPRIETARY(ProprietaryProtocol::TheShip)),
|
"theship" => game!("The Ship", 27015, Protocol::PROPRIETARY(ProprietaryProtocol::TheShip)),
|
||||||
"unturned" => game!("Unturned", 27015, Protocol::Valve(SteamApp::UNTURNED)),
|
"unturned" => game!("Unturned", 27015, Protocol::Valve(SteamApp::UNTURNED)),
|
||||||
"unrealtournament" => game!("Unreal Tournament", 7778, Protocol::Gamespy(GameSpyVersion::One)),
|
"unrealtournament" => game!("Unreal Tournament", 7778, Protocol::Gamespy(GameSpyVersion::One)),
|
||||||
|
"valheim" => game!("Valheim", 2457, Protocol::Valve(SteamApp::VALHEIM), GatheringSettings {
|
||||||
|
players: true,
|
||||||
|
rules: false,
|
||||||
|
check_app_id: true,
|
||||||
|
}.into_extra()),
|
||||||
"vrising" => game!("V Rising", 27016, Protocol::Valve(SteamApp::VRISING)),
|
"vrising" => game!("V Rising", 27016, Protocol::Valve(SteamApp::VRISING)),
|
||||||
"jc2m" => game!("Just Cause 2: Multiplayer", 7777, Protocol::PROPRIETARY(ProprietaryProtocol::JC2M)),
|
"jc2m" => game!("Just Cause 2: Multiplayer", 7777, Protocol::PROPRIETARY(ProprietaryProtocol::JC2M)),
|
||||||
"warsow" => game!("Warsow", 44400, Protocol::Quake(QuakeVersion::Three)),
|
"warsow" => game!("Warsow", 44400, Protocol::Quake(QuakeVersion::Three)),
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@ pub struct Game {
|
||||||
pub default_port: u16,
|
pub default_port: u16,
|
||||||
/// The protocol the game's query uses
|
/// The protocol the game's query uses
|
||||||
pub protocol: Protocol,
|
pub protocol: Protocol,
|
||||||
|
/// Request settings.
|
||||||
|
pub request_settings: ExtraRequestSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "game_defs")]
|
#[cfg(feature = "game_defs")]
|
||||||
|
|
@ -78,7 +80,9 @@ pub fn query_with_timeout_and_extra_settings(
|
||||||
protocols::valve::query(
|
protocols::valve::query(
|
||||||
&socket_addr,
|
&socket_addr,
|
||||||
steam_app.as_engine(),
|
steam_app.as_engine(),
|
||||||
extra_settings.map(ExtraRequestSettings::into),
|
extra_settings
|
||||||
|
.or(Option::from(game.request_settings.clone()))
|
||||||
|
.map(ExtraRequestSettings::into),
|
||||||
timeout_settings,
|
timeout_settings,
|
||||||
)
|
)
|
||||||
.map(Box::new)?
|
.map(Box::new)?
|
||||||
|
|
|
||||||
|
|
@ -53,4 +53,15 @@ game_query_mod!(teamfortress2, "Team Fortress 2", TEAMFORTRESS2, 27015);
|
||||||
game_query_mod!(tfc, "Team Fortress Classic", TFC, 27015);
|
game_query_mod!(tfc, "Team Fortress Classic", TFC, 27015);
|
||||||
game_query_mod!(theforest, "The Forest", THEFOREST, 27016);
|
game_query_mod!(theforest, "The Forest", THEFOREST, 27016);
|
||||||
game_query_mod!(unturned, "Unturned", UNTURNED, 27015);
|
game_query_mod!(unturned, "Unturned", UNTURNED, 27015);
|
||||||
|
game_query_mod!(
|
||||||
|
valheim,
|
||||||
|
"Valheim",
|
||||||
|
VALHEIM,
|
||||||
|
2457,
|
||||||
|
GatheringSettings {
|
||||||
|
players: true,
|
||||||
|
rules: false,
|
||||||
|
check_app_id: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
game_query_mod!(vrising, "V Rising", VRISING, 27016);
|
game_query_mod!(vrising, "V Rising", VRISING, 27016);
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,21 @@ pub use types::*;
|
||||||
/// * `steam_app`, `default_port` - Passed through to [game_query_fn].
|
/// * `steam_app`, `default_port` - Passed through to [game_query_fn].
|
||||||
macro_rules! game_query_mod {
|
macro_rules! game_query_mod {
|
||||||
($mod_name: ident, $pretty_name: expr, $steam_app: ident, $default_port: literal) => {
|
($mod_name: ident, $pretty_name: expr, $steam_app: ident, $default_port: literal) => {
|
||||||
|
crate::protocols::valve::game_query_mod!(
|
||||||
|
$mod_name,
|
||||||
|
$pretty_name,
|
||||||
|
$steam_app,
|
||||||
|
$default_port,
|
||||||
|
GatheringSettings::default()
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
($mod_name: ident, $pretty_name: expr, $steam_app: ident, $default_port: literal, $gathering_settings: expr) => {
|
||||||
#[doc = $pretty_name]
|
#[doc = $pretty_name]
|
||||||
pub mod $mod_name {
|
pub mod $mod_name {
|
||||||
crate::protocols::valve::game_query_fn!($steam_app, $default_port);
|
use crate::protocols::valve::GatheringSettings;
|
||||||
|
|
||||||
|
crate::protocols::valve::game_query_fn!($steam_app, $default_port, $gathering_settings);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -36,19 +48,20 @@ pub(crate) use game_query_mod;
|
||||||
/// game_query_fn!(TEAMFORTRESS2, 27015);
|
/// game_query_fn!(TEAMFORTRESS2, 27015);
|
||||||
/// ```
|
/// ```
|
||||||
macro_rules! game_query_fn {
|
macro_rules! game_query_fn {
|
||||||
($steam_app: ident, $default_port: literal) => {
|
($steam_app: ident, $default_port: literal, $gathering_settings: expr) => {
|
||||||
|
// TODO: By using $gathering_settings, also add to doc if a game doesnt respond to certain gathering settings
|
||||||
crate::protocols::valve::game_query_fn!{@gen $steam_app, $default_port, concat!(
|
crate::protocols::valve::game_query_fn!{@gen $steam_app, $default_port, concat!(
|
||||||
"Make a valve query for ", stringify!($steam_app), " with default timeout settings and default extra request settings.\n\n",
|
"Make a valve query for ", stringify!($steam_app), " with default timeout settings and default extra request settings.\n\n",
|
||||||
"If port is `None`, then the default port (", stringify!($default_port), ") will be used.")}
|
"If port is `None`, then the default port (", stringify!($default_port), ") will be used."), $gathering_settings}
|
||||||
};
|
};
|
||||||
|
|
||||||
(@gen $steam_app: ident, $default_port: literal, $doc: expr) => {
|
(@gen $steam_app: ident, $default_port: literal, $doc: expr, $gathering_settings: expr) => {
|
||||||
#[doc = $doc]
|
#[doc = $doc]
|
||||||
pub fn query(address: &std::net::IpAddr, port: Option<u16>) -> crate::GDResult<crate::protocols::valve::game::Response> {
|
pub fn query(address: &std::net::IpAddr, port: Option<u16>) -> crate::GDResult<crate::protocols::valve::game::Response> {
|
||||||
let valve_response = crate::protocols::valve::query(
|
let valve_response = crate::protocols::valve::query(
|
||||||
&std::net::SocketAddr::new(*address, port.unwrap_or($default_port)),
|
&std::net::SocketAddr::new(*address, port.unwrap_or($default_port)),
|
||||||
crate::protocols::valve::SteamApp::$steam_app.as_engine(),
|
crate::protocols::valve::SteamApp::$steam_app.as_engine(),
|
||||||
None,
|
Some($gathering_settings),
|
||||||
None,
|
None,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -341,6 +341,8 @@ pub enum SteamApp {
|
||||||
HLL,
|
HLL,
|
||||||
/// Barotrauma
|
/// Barotrauma
|
||||||
BAROTRAUMA,
|
BAROTRAUMA,
|
||||||
|
/// Valheim
|
||||||
|
VALHEIM,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SteamApp {
|
impl SteamApp {
|
||||||
|
|
@ -383,6 +385,7 @@ impl SteamApp {
|
||||||
Self::BAROTRAUMA => Engine::new_source(602960),
|
Self::BAROTRAUMA => Engine::new_source(602960),
|
||||||
Self::ROR2 => Engine::new_source(632_360),
|
Self::ROR2 => Engine::new_source(632_360),
|
||||||
Self::OHD => Engine::new_source_with_dedicated(736_590, 950_900),
|
Self::OHD => Engine::new_source_with_dedicated(736_590, 950_900),
|
||||||
|
Self::VALHEIM => Engine::new_source(892_970),
|
||||||
Self::ONSET => Engine::new_source(1_105_810),
|
Self::ONSET => Engine::new_source(1_105_810),
|
||||||
Self::VRISING => Engine::new_source(1_604_030),
|
Self::VRISING => Engine::new_source(1_604_030),
|
||||||
Self::HLL => Engine::new_source(686_810),
|
Self::HLL => Engine::new_source(686_810),
|
||||||
|
|
@ -422,15 +425,29 @@ pub struct GatheringSettings {
|
||||||
pub check_app_id: bool,
|
pub check_app_id: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for GatheringSettings {
|
impl GatheringSettings {
|
||||||
/// Default values are true for both the players and the rules.
|
/// Default values are true for both the players and the rules.
|
||||||
fn default() -> Self {
|
pub const fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
players: true,
|
players: true,
|
||||||
rules: true,
|
rules: true,
|
||||||
check_app_id: true,
|
check_app_id: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn into_extra(self) -> ExtraRequestSettings {
|
||||||
|
ExtraRequestSettings {
|
||||||
|
hostname: None,
|
||||||
|
protocol_version: None,
|
||||||
|
gather_players: Some(self.players),
|
||||||
|
gather_rules: Some(self.rules),
|
||||||
|
check_app_id: Some(self.check_app_id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for GatheringSettings {
|
||||||
|
fn default() -> Self { GatheringSettings::default() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ExtraRequestSettings> for GatheringSettings {
|
impl From<ExtraRequestSettings> for GatheringSettings {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue