Merge branch 'main' into feat/cli

This commit is contained in:
Cain 2023-11-08 20:42:07 +00:00
commit 963040fb84
28 changed files with 1071 additions and 348 deletions

View file

@ -13,42 +13,58 @@ pub use types::*;
/// * `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].
#[cfg(feature = "games")]
macro_rules! game_query_mod {
($mod_name: ident, $pretty_name: expr, $steam_app: ident, $default_port: literal) => {
($mod_name: ident, $pretty_name: expr, $engine: expr, $default_port: literal) => {
crate::protocols::valve::game_query_mod!(
$mod_name,
$pretty_name,
$engine,
$default_port,
GatheringSettings::default()
);
};
($mod_name: ident, $pretty_name: expr, $engine: expr, $default_port: literal, $gathering_settings: expr) => {
#[doc = $pretty_name]
pub mod $mod_name {
crate::protocols::valve::game_query_fn!($steam_app, $default_port);
use crate::protocols::valve::{Engine, GatheringSettings};
crate::protocols::valve::game_query_fn!($pretty_name, $engine, $default_port, $gathering_settings);
}
};
}
#[cfg(feature = "games")]
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.
/// * `engine` - The [Engine] 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);
/// ```
#[cfg(feature = "games")]
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.")}
($pretty_name: expr, $engine: expr, $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 $engine, $default_port, concat!(
"Make a valve query for ", $pretty_name, " 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."), $gathering_settings}
};
(@gen $steam_app: ident, $default_port: literal, $doc: expr) => {
(@gen $engine: expr, $default_port: literal, $doc: expr, $gathering_settings: 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,
$engine,
Some($gathering_settings),
None,
)?;
@ -57,4 +73,5 @@ macro_rules! game_query_fn {
};
}
#[cfg(feature = "games")]
pub(crate) use game_query_fn;

View file

@ -16,7 +16,6 @@ use crate::{
},
Engine,
ModData,
SteamApp,
},
},
socket::{Socket, UdpSocket},
@ -59,8 +58,8 @@ impl SplitPacket {
Engine::Source(_) => {
let total = buffer.read()?;
let number = buffer.read()?;
let size = match protocol == 7 && (*engine == SteamApp::CSS.as_engine()) {
// certain apps with protocol = 7 dont have this field
let size = match protocol == 7 && (*engine == Engine::new(240)) {
// certain apps with protocol = 7 dont have this field, such as CSS
false => buffer.read()?,
true => 1248,
};
@ -304,7 +303,7 @@ impl ValveProtocol {
let environment_type = Environment::from_gldsrc(buffer.read()?)?;
let has_password = buffer.read::<u8>()? == 1;
let vac_secured = buffer.read::<u8>()? == 1;
let the_ship = match *engine == SteamApp::THESHIP.as_engine() {
let the_ship = match *engine == Engine::new(2400) {
false => None,
true => {
Some(TheShip {
@ -389,11 +388,11 @@ impl ValveProtocol {
name: buffer.read_string::<Utf8Decoder>(None)?,
score: buffer.read()?,
duration: buffer.read()?,
deaths: match *engine == SteamApp::THESHIP.as_engine() {
deaths: match *engine == Engine::new(2400) {
false => None,
true => Some(buffer.read()?),
},
money: match *engine == SteamApp::THESHIP.as_engine() {
money: match *engine == Engine::new(2400) {
false => None,
true => Some(buffer.read()?),
},
@ -418,7 +417,8 @@ impl ValveProtocol {
rules.insert(name, value);
}
if *engine == SteamApp::ROR2.as_engine() {
if *engine == Engine::new(632_360) {
// ROR2
rules.remove("Test");
}

View file

@ -249,156 +249,20 @@ impl Request {
}
}
/// Supported steam apps
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SteamApp {
/// Counter-Strike
COUNTERSTRIKE,
/// Creativerse
CREATIVERSE,
/// Team Fortress Classic
TFC,
/// Day of Defeat
DOD,
/// Counter-Strike: Condition Zero
CSCZ,
/// Counter-Strike: Source
CSS,
/// Day of Defeat: Source
DODS,
/// Half-Life 2 Deathmatch
HL2D,
/// Half-Life Deathmatch: Source
HLDS,
/// Team Fortress 2
TEAMFORTRESS2,
/// Left 4 Dead
LEFT4DEAD,
/// Left 4 Dead
LEFT4DEAD2,
/// Alien Swarm
ALIENSWARM,
/// Counter-Strike: Global Offensive
CSGO,
/// The Ship
THESHIP,
/// Garry's Mod
GARRYSMOD,
/// Age of Chivalry
AOC,
/// Insurgency: Modern Infantry Combat
IMIC,
/// ARMA 2: Operation Arrowhead
A2OA,
/// Project Zomboid
PROJECTZOMBOID,
/// Insurgency
INSURGENCY,
/// Sven Co-op
SCO,
/// 7 Days To Die
SD2D,
/// Rust
RUST,
/// Ballistic Overkill
BALLISTICOVERKILL,
/// Don't Starve Together
DST,
/// BrainBread 2
BRAINBREAD2,
/// Codename CURE
CODENAMECURE,
/// Black Mesa
BLACKMESA,
/// Colony Survival
COLONYSURVIVAL,
/// Avorion
AVORION,
/// Day of Infamy
DOI,
/// The Forest
THEFOREST,
/// Unturned
UNTURNED,
/// ARK: Survival Evolved
ASE,
/// Battalion 1944
BATTALION1944,
/// Insurgency: Sandstorm
INSURGENCYSANDSTORM,
/// Alien Swarm: Reactive Drop
ASRD,
/// Risk of Rain 2
ROR2,
/// Operation: Harsh Doorstop
OHD,
/// Onset
ONSET,
/// V Rising
VRISING,
/// Hell Let Loose
HLL,
/// Barotrauma
BAROTRAUMA,
}
impl SteamApp {
/// Get the specified app as engine.
pub const fn as_engine(&self) -> Engine {
match self {
Self::CSS => Engine::new_source(240),
Self::DODS => Engine::new_source(300),
Self::HL2D => Engine::new_source(320),
Self::HLDS => Engine::new_source(360),
Self::TEAMFORTRESS2 => Engine::new_source(440),
Self::LEFT4DEAD => Engine::new_source(500),
Self::LEFT4DEAD2 => Engine::new_source(550),
Self::ALIENSWARM => Engine::new_source(630),
Self::CSGO => Engine::new_source(730),
Self::THESHIP => Engine::new_source(2400),
Self::GARRYSMOD => Engine::new_source(4000),
Self::AOC => Engine::new_source(17510),
Self::IMIC => Engine::new_source(17700),
Self::A2OA => Engine::new_source(33930),
Self::PROJECTZOMBOID => Engine::new_source(108_600),
Self::INSURGENCY => Engine::new_source(222_880),
Self::SD2D => Engine::new_source(251_570),
Self::RUST => Engine::new_source(252_490),
Self::CREATIVERSE => Engine::new_source(280_790),
Self::BALLISTICOVERKILL => Engine::new_source(296_300),
Self::DST => Engine::new_source(322_320),
Self::BRAINBREAD2 => Engine::new_source(346_330),
Self::CODENAMECURE => Engine::new_source(355_180),
Self::BLACKMESA => Engine::new_source(362_890),
Self::COLONYSURVIVAL => Engine::new_source(366_090),
Self::AVORION => Engine::new_source(445_220),
Self::DOI => Engine::new_source(447_820),
Self::THEFOREST => Engine::new_source(556_450),
Self::UNTURNED => Engine::new_source(304_930),
Self::ASE => Engine::new_source(346_110),
Self::BATTALION1944 => Engine::new_source(489_940),
Self::INSURGENCYSANDSTORM => Engine::new_source(581_320),
Self::ASRD => Engine::new_source(563_560),
Self::BAROTRAUMA => Engine::new_source(602960),
Self::ROR2 => Engine::new_source(632_360),
Self::OHD => Engine::new_source_with_dedicated(736_590, 950_900),
Self::ONSET => Engine::new_source(1_105_810),
Self::VRISING => Engine::new_source(1_604_030),
Self::HLL => Engine::new_source(686_810),
_ => Engine::GoldSrc(false), // CS - 10, TFC - 20, DOD - 30, CSCZ - 80, SC - 225840
}
}
}
/// Engine type.
/// Every supported Valve game references this enum, represents the behaviour
/// of server requests and responses.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Engine {
/// A Source game, the argument represents the possible steam app ids, if
/// its **None**, let the query find it, if its **Some**, the query
/// fails if the response id is not the first one, which is the game app
/// id, or the other one, which is the dedicated server app id.
/// A Source game, the argument represents the possible steam app ids.
/// If its **None**, let the query find it (could come with some drawbacks,
/// some games do not respond on certain protocol versions (CSS on 7),
/// some have additional data (The Ship).
/// If its **Some**, the first value is the main steam app id, the second
/// could be a secondly used id, as some games use a different one for
/// dedicated servers. Beware if **check_app_id** is set to true in
/// [GatheringSettings], as the query will fail if the server doesnt respond
/// with the expected ids.
Source(Option<(u32, Option<u32>)>),
/// A GoldSrc game, the argument indicates whether to enforce
/// requesting the obsolete A2S_INFO response or not.
@ -406,9 +270,11 @@ pub enum Engine {
}
impl Engine {
pub const fn new_source(appid: u32) -> Self { Self::Source(Some((appid, None))) }
pub const fn new(appid: u32) -> Self { Self::Source(Some((appid, None))) }
pub const fn new_source_with_dedicated(appid: u32, dedicated_appid: u32) -> Self {
pub const fn new_gold_src(force: bool) -> Self { Self::GoldSrc(force) }
pub const fn new_with_dedicated(appid: u32, dedicated_appid: u32) -> Self {
Self::Source(Some((appid, Some(dedicated_appid))))
}
}
@ -422,15 +288,29 @@ pub struct GatheringSettings {
pub check_app_id: bool,
}
impl Default for GatheringSettings {
impl GatheringSettings {
/// Default values are true for both the players and the rules.
fn default() -> Self {
pub const fn default() -> Self {
Self {
players: true,
rules: 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 {