use borrowme::borrowme;
use crate::{cursor::{self, Cursor}, the_ship};
pub(crate) const REQUEST_HEADER: &[u8] = b"TSource Engine Query\0";
pub(crate) const RESPONSE_HEADER: u8 = b'I';
const EDF_PORT: u8 = 0x80;
const EDF_STEAM_ID: u8 = 0x10;
const EDF_SOURCE_TV: u8 = 0x40;
const EDF_KEYWORDS: u8 = 0x20;
const EDF_GAME_ID: u8 = 0x01;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Read(#[from] cursor::Error),
#[error("invalid server type: {0}")]
InvalidServerType(u8),
#[error("invalid server platform: {0}")]
InvalidPlatform(u8)
}
#[derive(Debug, Clone)]
pub enum ServerType {
Dedicated,
Listen,
Proxy
}
#[derive(Debug, Clone)]
pub enum Environment {
Linux,
Windows,
Mac
}
impl TryFrom<u8> for ServerType {
type Error = Error;
fn try_from(byte: u8) -> Result<Self, Self::Error> {
Ok(match byte {
b'd' => Self::Dedicated,
b'l' => Self::Listen,
b'p' => Self::Proxy,
other => return Err(Error::InvalidServerType(other)),
})
}
}
#[borrowme]
#[derive(Debug)]
#[owned_attr(derive(Clone))]
#[owned_attr(doc(hidden))]
pub struct ExtraData<'a> {
pub port: Option<u16>,
pub steam_id: Option<u64>,
pub source_tv: Option<SourceTv<'a>>,
pub keywords: Option<&'a str>,
pub game_id: Option<u64>
}
#[borrowme]
#[derive(Debug)]
#[owned_attr(derive(Clone))]
#[owned_attr(doc(hidden))]
pub struct SourceTv<'a> {
pub port: u16,
pub name: &'a str
}
#[borrowme]
#[derive(Debug)]
#[owned_attr(derive(Clone))]
#[owned_attr(doc(hidden))]
pub struct Info<'a> {
pub protocol: u8,
pub name: &'a str,
pub map: &'a str,
pub folder: &'a str,
pub game: &'a str,
pub id: u16,
pub players: u8,
pub max_players: u8,
pub bots: u8,
pub server_type: ServerType,
pub environment: Environment,
pub is_private: bool,
pub vac: bool,
pub the_ship: Option<the_ship::Info>,
pub version: &'a str,
pub extra_data: Option<ExtraData<'a>>
}
impl Info<'_> {
pub fn to_owned(self) -> OwnedInfo {
borrowme::to_owned(self)
}
}
pub(crate) fn parse(raw: &[u8]) -> Result<Info<'_>, Error> {
let mut cur = Cursor::new(raw);
let protocol = cur.read_byte()?;
let name = cur.read_str()?;
let map = cur.read_str()?;
let folder = cur.read_str()?;
let game = cur.read_str()?;
let id = cur.read_short()?;
let players = cur.read_byte()?;
let max_players = cur.read_byte()?;
let bots = cur.read_byte()?;
let server_type = ServerType::try_from(cur.read_byte()?)?;
let environment = match cur.read_byte()? {
b'l' => Environment::Linux,
b'w' => Environment::Windows,
b'm' | b'o' => Environment::Mac,
other => return Err(Error::InvalidPlatform(other)),
};
let is_private = cur.read_byte()? != 0;
let vac = cur.read_byte()? != 0;
let the_ship = if id == the_ship::APP_ID {
Some(the_ship::Info {
mode: the_ship::Mode::from(cur.read_byte()?),
witnesses: cur.read_byte()?,
duration: cur.read_byte()?
})
} else {
None
};
let version = cur.read_str()?;
let extra_data = if cur.is_end() {
None
} else {
let edf = cur.read_byte()?;
Some(ExtraData {
port: match edf & EDF_PORT {
0 => None,
_ => Some(cur.read_short()?)
},
steam_id: match edf & EDF_STEAM_ID {
0 => None,
_ => Some(cur.read_longlong()?)
},
source_tv: match edf & EDF_SOURCE_TV {
0 => None,
_ => Some(SourceTv{
port: cur.read_short()?,
name: cur.read_str()?
})
},
keywords: match edf & EDF_KEYWORDS {
0 => None,
_ => Some(cur.read_str()?)
},
game_id: match edf & EDF_GAME_ID {
0 => None,
_ => Some(cur.read_longlong()?)
}
})
};
Ok(Info { protocol, name, map, folder, game, id, players, max_players, bots, server_type, environment, is_private, vac, the_ship, version, extra_data })
}