1use borrowme::borrowme;
4
5use crate::{cursor::{self, Cursor}, the_ship};
6
7pub(crate) const REQUEST_HEADER: &[u8] = b"TSource Engine Query\0";
9pub(crate) const RESPONSE_HEADER: u8 = b'I';
10
11const EDF_PORT: u8 = 0x80;
12const EDF_STEAM_ID: u8 = 0x10;
13const EDF_SOURCE_TV: u8 = 0x40;
14const EDF_KEYWORDS: u8 = 0x20;
15const EDF_GAME_ID: u8 = 0x01;
16
17#[derive(thiserror::Error, Debug)]
18pub enum Error {
20 #[error(transparent)]
22 Read(#[from] cursor::Error),
23 #[error("invalid server type: {0}")]
25 InvalidServerType(u8),
26 #[error("invalid server platform: {0}")]
28 InvalidPlatform(u8)
29}
30
31#[derive(Debug, Clone)]
32pub enum ServerType {
34 Dedicated,
36 Listen,
38 Proxy
40}
41
42#[derive(Debug, Clone)]
43pub enum Environment {
45 Linux,
47 Windows,
49 Mac
51}
52
53impl TryFrom<u8> for ServerType {
54 type Error = Error;
55
56 fn try_from(byte: u8) -> Result<Self, Self::Error> {
57 Ok(match byte {
58 b'd' => Self::Dedicated,
59 b'l' => Self::Listen,
60 b'p' => Self::Proxy,
61 other => return Err(Error::InvalidServerType(other)),
62 })
63 }
64}
65
66#[borrowme]
67#[derive(Debug)]
68#[owned_attr(derive(Clone))]
69#[owned_attr(doc(hidden))]
70pub struct ExtraData<'a> {
72 pub port: Option<u16>,
74 pub steam_id: Option<u64>,
76 pub source_tv: Option<SourceTv<'a>>,
78 pub keywords: Option<&'a str>,
80 pub game_id: Option<u64>
82}
83
84#[borrowme]
85#[derive(Debug)]
86#[owned_attr(derive(Clone))]
87#[owned_attr(doc(hidden))]
88pub struct SourceTv<'a> {
90 pub port: u16,
92 pub name: &'a str
94}
95
96#[borrowme]
97#[derive(Debug)]
98#[owned_attr(derive(Clone))]
99#[owned_attr(doc(hidden))]
100pub struct Info<'a> {
105 pub protocol: u8,
107 pub name: &'a str,
109 pub map: &'a str,
111 pub folder: &'a str,
113 pub game: &'a str,
115 pub id: u16,
117 pub players: u8,
119 pub max_players: u8,
121 pub bots: u8,
123 pub server_type: ServerType,
125 pub environment: Environment,
127 pub is_private: bool,
129 pub vac: bool,
131 pub the_ship: Option<the_ship::Info>,
133 pub version: &'a str,
135 pub extra_data: Option<ExtraData<'a>>
137}
138
139impl Info<'_> {
140 pub fn to_owned(self) -> OwnedInfo {
144 borrowme::to_owned(self)
145 }
146}
147
148pub(crate) fn parse(raw: &[u8]) -> Result<Info<'_>, Error> {
149 let mut cur = Cursor::new(raw);
150
151 let protocol = cur.read_byte()?;
152 let name = cur.read_str()?;
153 let map = cur.read_str()?;
154 let folder = cur.read_str()?;
155 let game = cur.read_str()?;
156 let id = cur.read_short()?;
157 let players = cur.read_byte()?;
158 let max_players = cur.read_byte()?;
159 let bots = cur.read_byte()?;
160 let server_type = ServerType::try_from(cur.read_byte()?)?;
161 let environment = match cur.read_byte()? {
162 b'l' => Environment::Linux,
163 b'w' => Environment::Windows,
164 b'm' | b'o' => Environment::Mac,
165 other => return Err(Error::InvalidPlatform(other)),
166 };
167 let is_private = cur.read_byte()? != 0;
168 let vac = cur.read_byte()? != 0;
169
170 let the_ship = if id == the_ship::APP_ID {
171 Some(the_ship::Info {
172 mode: the_ship::Mode::from(cur.read_byte()?),
173 witnesses: cur.read_byte()?,
174 duration: cur.read_byte()?
175 })
176 } else {
177 None
178 };
179
180 let version = cur.read_str()?;
181
182 let extra_data = if cur.is_end() {
183 None
184 } else {
185 let edf = cur.read_byte()?;
186 Some(ExtraData {
187 port: match edf & EDF_PORT {
188 0 => None,
189 _ => Some(cur.read_short()?)
190 },
191 steam_id: match edf & EDF_STEAM_ID {
192 0 => None,
193 _ => Some(cur.read_longlong()?)
194 },
195 source_tv: match edf & EDF_SOURCE_TV {
196 0 => None,
197 _ => Some(SourceTv{
198 port: cur.read_short()?,
199 name: cur.read_str()?
200 })
201 },
202 keywords: match edf & EDF_KEYWORDS {
203 0 => None,
204 _ => Some(cur.read_str()?)
205 },
206 game_id: match edf & EDF_GAME_ID {
207 0 => None,
208 _ => Some(cur.read_longlong()?)
209 }
210 })
211 };
212
213 Ok(Info { protocol, name, map, folder, game, id, players, max_players, bots, server_type, environment, is_private, vac, the_ship, version, extra_data })
214}