mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
Reduce game implementation repetition (#122)
* [Games] Add macro to replace valve game query implementations This somewhat reduces repeated code (#120), and also adds auto-generated doc comments to all valve game query functions. * [Games] Add macro to replace gamespy game query implementations This somewhat reduces repeated code (#120), and also adds auto-generated doc comments to all gamespy game query functions. * [Games] Add macro to replace quake game query implementations This somewhat reduces repeated code (#120), and also adds auto-generated doc comments to all quake game query functions. * [Games] Move all valve game modules into a single file using macros Vastly reduces the number of files. However does break the game definition-per-file test, so that was removed. * [Games] Move all quake game modules into a single file using macros * [Games] Move all gamespy game modules into a single file using macros * [Docs] Update CHANGELOG * [Docs] Improve game query function generation macro documentation * [Games] Add missed Halo: Combat Evolved to gamespy
This commit is contained in:
parent
c8a93357cf
commit
3b9c784e70
61 changed files with 250 additions and 872 deletions
|
|
@ -5,3 +5,56 @@ pub mod types;
|
|||
|
||||
pub use protocol::*;
|
||||
pub use types::*;
|
||||
|
||||
/// Generate a module containing a query function for a valve game.
|
||||
///
|
||||
/// * `mod_name` - The name to be given to the game module (see ID naming
|
||||
/// conventions in CONTRIBUTING.md).
|
||||
/// * `pretty_name` - The full name of the game, will be used as the
|
||||
/// documentation for the created module.
|
||||
/// * `steam_app`, `default_port` - Passed through to [game_query_fn].
|
||||
macro_rules! game_query_mod {
|
||||
($mod_name: ident, $pretty_name: expr, $steam_app: ident, $default_port: literal) => {
|
||||
#[doc = $pretty_name]
|
||||
pub mod $mod_name {
|
||||
crate::protocols::valve::game_query_fn!($steam_app, $default_port);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use game_query_mod;
|
||||
|
||||
// Allow generating doc comments:
|
||||
// https://users.rust-lang.org/t/macros-filling-text-in-comments/20473
|
||||
/// Generate a query function for a valve game.
|
||||
///
|
||||
/// * `steam_app` - The entry in the [SteamApp] enum that the game uses.
|
||||
/// * `default_port` - The default port the game uses.
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use crate::protocols::valve::game_query_fn;
|
||||
/// game_query_fn!(TEAMFORTRESS2, 27015);
|
||||
/// ```
|
||||
macro_rules! game_query_fn {
|
||||
($steam_app: ident, $default_port: literal) => {
|
||||
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",
|
||||
"If port is `None`, then the default port (", stringify!($default_port), ") will be used.")}
|
||||
};
|
||||
|
||||
(@gen $steam_app: ident, $default_port: literal, $doc: expr) => {
|
||||
#[doc = $doc]
|
||||
pub fn query(address: &std::net::IpAddr, port: Option<u16>) -> crate::GDResult<crate::protocols::valve::game::Response> {
|
||||
let valve_response = crate::protocols::valve::query(
|
||||
&std::net::SocketAddr::new(*address, port.unwrap_or($default_port)),
|
||||
crate::protocols::valve::SteamApp::$steam_app.as_engine(),
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
|
||||
Ok(crate::protocols::valve::game::Response::new_from_valve_response(valve_response))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use game_query_fn;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue