Skip to main content

a2s_protocol/
obsolete.rs

1//! Legacy GoldSrc response types and request builders.
2//!
3//! A2S_INFO, A2S_PLAYER, and A2S_RULES are supported for both Source and GoldSrc
4//! servers. This module also exposes the older GoldSrc ping and challenge request
5//! formats.
6
7use borrowme::borrowme;
8
9use crate::{cursor::Cursor, info::{ServerType}};
10use crate::{request, RequestType};
11use crate::info::Error;
12
13pub(crate) const INFO_RESPONSE_HEADER: u8 = b'm';
14pub(crate) const CHALLENGE_RESPONSE_HEADER: u8 = b'A';
15pub(crate) const PING_RESPONSE_HEADER: u8 = b'j';
16pub(crate) const PING_REQUEST_HEADER: &[u8] = b"i";
17pub(crate) const CHALLENGE_REQUEST_HEADER: &[u8] = b"W";
18
19#[derive(Debug, Clone)]
20/// Operating system reported by a GoldSrc server.
21pub enum Environment {
22    /// Linux.
23    Linux,
24    /// Windows.
25    Windows
26}
27
28#[borrowme]
29#[derive(Debug)]
30#[owned_attr(derive(Clone))]
31#[owned_attr(doc(hidden))]
32/// Optional GoldSrc mod metadata included in a server information response.
33pub struct Mod<'a> {
34    /// Mod website URL.
35    pub link: &'a str,
36    /// Mod download URL.
37    pub download_link: &'a str,
38    /// Mod version number.
39    pub version: i32,
40    /// Mod size in bytes.
41    pub size: i32,
42    /// Whether the mod is multiplayer-only.
43    pub multiplayer_only: bool,
44    /// Whether the mod provides its own client DLL.
45    pub own_dll: bool,
46}
47
48#[borrowme]
49#[derive(Debug)]
50#[owned_attr(derive(Clone))]
51#[owned_attr(doc(hidden))]
52/// Parsed legacy GoldSrc server information.
53///
54/// Text fields borrow from the response packet, which must remain alive while
55/// those fields are used.
56pub struct GoldSrcInfo<'a> {
57    /// Server address string as reported in the response.
58    pub address: &'a str,
59    /// Human-readable server name.
60    pub name: &'a str,
61    /// Current map name.
62    pub map: &'a str,
63    /// Game directory name.
64    pub folder: &'a str,
65    /// Human-readable game description.
66    pub game: &'a str,
67    /// Current number of players.
68    pub players: u8,
69    /// Maximum number of players.
70    pub max_players: u8,
71    /// Protocol version reported by the server.
72    pub protocol: u8,
73    /// Server role (dedicated, listen, or proxy).
74    pub server_type: ServerType,
75    /// Operating system reported by the server.
76    pub environment: Environment,
77    /// Whether the server is password-protected.
78    pub is_private: bool,
79    /// Optional metadata about the installed game mod.
80    pub mod_data: Option<Mod<'a>>,
81    /// Whether Valve Anti-Cheat is enabled.
82    pub vac: bool,
83    /// Number of bots reported by the server.
84    pub bots: u8,
85}
86
87pub(crate) fn parse_info(raw: &[u8]) -> Result<GoldSrcInfo<'_>, Error> {
88    let mut cur = Cursor::new(raw);
89
90    let address = cur.read_str()?;
91    let name = cur.read_str()?;
92    let map = cur.read_str()?;
93    let folder = cur.read_str()?;
94    let game = cur.read_str()?;
95    let players = cur.read_byte()?;
96    let max_players = cur.read_byte()?;
97    let protocol = cur.read_byte()?;
98    let server_type = ServerType::try_from(cur.read_byte()?)?;
99    let environment = match cur.read_byte()? {
100        b'l' => Environment::Linux,
101        b'w' => Environment::Windows,
102        other => return Err(Error::InvalidPlatform(other)),
103    };
104    let is_private = cur.read_byte()? != 0;
105    let mod_data = if cur.read_byte()? != 0 {
106        Some(Mod {
107            link: cur.read_str()?,
108            download_link: cur.read_str()?,
109            version: cur.read_long()?,
110            size: cur.read_long()?,
111            multiplayer_only: cur.read_byte()? != 0,
112            own_dll: cur.read_byte()? != 0,
113        })
114    } else {
115        None
116    };
117    let vac = cur.read_byte()? != 0;
118    let bots = cur.read_byte()?;
119
120    Ok(GoldSrcInfo { address, name, map, folder, game, players, max_players, protocol, server_type, environment, is_private, mod_data, vac, bots })
121}
122
123/// Builds a legacy GoldSrc ping request packet.
124///
125/// Use this with a UDP socket and parse the reply using [`crate::parse_payload`].
126pub fn ping_request() -> Vec<u8> {
127    request(RequestType::Ping, None)
128}
129
130/// Builds a legacy GoldSrc challenge request packet.
131///
132/// Use this when a server requires a challenge before accepting another request.
133pub fn challenge_request() -> Vec<u8> {
134    request(crate::RequestType::Challenge, None)
135}