From 99b0269ec25bfa77680f7a2b2f730adce832c1d3 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Fri, 15 Dec 2023 20:19:15 +0100 Subject: [PATCH] chore: extract jc2m into multiple files (#171) * chore: extract jc2m into multiple files * docs: add jc2m fields public --- CHANGELOG.md | 1 + crates/lib/src/games/jc2m/mod.rs | 8 +++ .../src/games/{jc2m.rs => jc2m/protocol.rs} | 66 ++----------------- crates/lib/src/games/jc2m/types.rs | 50 ++++++++++++++ 4 files changed, 65 insertions(+), 60 deletions(-) create mode 100644 crates/lib/src/games/jc2m/mod.rs rename crates/lib/src/games/{jc2m.rs => jc2m/protocol.rs} (52%) create mode 100644 crates/lib/src/games/jc2m/types.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 82d736d..48c92a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Games: - [Squad](https://store.steampowered.com/app/393380/Squad/) support. - [Savage 2](https://savage2.net/) support. - Added a valve protocol query example. +- Made all of Just Cause 2: Multiplayer Response and Player fields public. Protocols: - Added the unreal2 protocol and its associated games: Darkest Hour, Devastation, Killing Floor, Red Orchestra, Unreal Tournament 2003, Unreal Tournament 2004 (by @Douile). diff --git a/crates/lib/src/games/jc2m/mod.rs b/crates/lib/src/games/jc2m/mod.rs new file mode 100644 index 0000000..642b155 --- /dev/null +++ b/crates/lib/src/games/jc2m/mod.rs @@ -0,0 +1,8 @@ +/// The implementation. +/// Reference: [Node-GameGig](https://github.com/gamedig/node-gamedig/blob/master/protocols/jc2mp.js) +pub mod protocol; +/// All types used by the implementation. +pub mod types; + +pub use protocol::*; +pub use types::*; diff --git a/crates/lib/src/games/jc2m.rs b/crates/lib/src/games/jc2m/protocol.rs similarity index 52% rename from crates/lib/src/games/jc2m.rs rename to crates/lib/src/games/jc2m/protocol.rs index 6fe3505..b6e0461 100644 --- a/crates/lib/src/games/jc2m.rs +++ b/crates/lib/src/games/jc2m/protocol.rs @@ -1,61 +1,13 @@ use crate::buffer::{Buffer, Utf8Decoder}; +use crate::jc2m::{Player, Response}; use crate::protocols::gamespy::common::has_password; use crate::protocols::gamespy::three::{data_to_map, GameSpy3}; -use crate::protocols::types::{CommonPlayer, CommonResponse, GenericPlayer, TimeoutSettings}; -use crate::protocols::GenericResponse; +use crate::protocols::types::TimeoutSettings; use crate::GDErrorKind::{PacketBad, TypeParse}; -use crate::{GDErrorKind, GDResult}; +use crate::GDResult; use byteorder::BigEndian; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; use std::net::{IpAddr, SocketAddr}; -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct Player { - name: String, - steam_id: String, - ping: u16, -} - -impl CommonPlayer for Player { - fn as_original(&self) -> GenericPlayer { GenericPlayer::JCMP2(self) } - - fn name(&self) -> &str { &self.name } -} - -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct Response { - game_version: String, - description: String, - name: String, - has_password: bool, - players: Vec, - players_maximum: u32, - players_online: u32, -} - -impl CommonResponse for Response { - fn as_original(&self) -> GenericResponse { GenericResponse::JC2M(self) } - - fn game_version(&self) -> Option<&str> { Some(&self.game_version) } - fn description(&self) -> Option<&str> { Some(&self.description) } - fn name(&self) -> Option<&str> { Some(&self.name) } - fn has_password(&self) -> Option { Some(self.has_password) } - fn players_maximum(&self) -> u32 { self.players_maximum } - fn players_online(&self) -> u32 { self.players_online } - - fn players(&self) -> Option> { - Some( - self.players - .iter() - .map(|p| p as &dyn CommonPlayer) - .collect(), - ) - } -} - fn parse_players_and_teams(packet: &[u8]) -> GDResult> { let mut buf = Buffer::::new(packet); @@ -112,15 +64,9 @@ pub fn query_with_timeout( } as u32; Ok(Response { - game_version: server_vars - .remove("version") - .ok_or(GDErrorKind::PacketBad)?, - description: server_vars - .remove("description") - .ok_or(GDErrorKind::PacketBad)?, - name: server_vars - .remove("hostname") - .ok_or(GDErrorKind::PacketBad)?, + game_version: server_vars.remove("version").ok_or(PacketBad)?, + description: server_vars.remove("description").ok_or(PacketBad)?, + name: server_vars.remove("hostname").ok_or(PacketBad)?, has_password: has_password(&mut server_vars)?, players, players_maximum, diff --git a/crates/lib/src/games/jc2m/types.rs b/crates/lib/src/games/jc2m/types.rs new file mode 100644 index 0000000..0f14a58 --- /dev/null +++ b/crates/lib/src/games/jc2m/types.rs @@ -0,0 +1,50 @@ +use crate::protocols::types::{CommonPlayer, CommonResponse, GenericPlayer}; +use crate::protocols::GenericResponse; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct Player { + pub name: String, + pub steam_id: String, + pub ping: u16, +} + +impl CommonPlayer for Player { + fn as_original(&self) -> GenericPlayer { GenericPlayer::JCMP2(self) } + + fn name(&self) -> &str { &self.name } +} + +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct Response { + pub game_version: String, + pub description: String, + pub name: String, + pub has_password: bool, + pub players: Vec, + pub players_maximum: u32, + pub players_online: u32, +} + +impl CommonResponse for Response { + fn as_original(&self) -> GenericResponse { GenericResponse::JC2M(self) } + + fn game_version(&self) -> Option<&str> { Some(&self.game_version) } + fn description(&self) -> Option<&str> { Some(&self.description) } + fn name(&self) -> Option<&str> { Some(&self.name) } + fn has_password(&self) -> Option { Some(self.has_password) } + fn players_maximum(&self) -> u32 { self.players_maximum } + fn players_online(&self) -> u32 { self.players_online } + + fn players(&self) -> Option> { + Some( + self.players + .iter() + .map(|p| p as &dyn CommonPlayer) + .collect(), + ) + } +}