use borrowme::borrowme;
use crate::{cursor::Cursor, info::{ServerType}};
use crate::{request, RequestType};
use crate::info::Error;
pub(crate) const INFO_RESPONSE_HEADER: u8 = b'm';
pub(crate) const CHALLENGE_RESPONSE_HEADER: u8 = b'A';
pub(crate) const PING_RESPONSE_HEADER: u8 = b'j';
pub(crate) const PING_REQUEST_HEADER: &[u8] = b"i";
pub(crate) const CHALLENGE_REQUEST_HEADER: &[u8] = b"W";
#[derive(Debug, Clone)]
pub enum Environment {
Linux,
Windows
}
#[borrowme]
#[derive(Debug)]
#[owned_attr(derive(Clone))]
#[owned_attr(doc(hidden))]
pub struct Mod<'a> {
pub link: &'a str,
pub download_link: &'a str,
pub version: i32,
pub size: i32,
pub multiplayer_only: bool,
pub own_dll: bool,
}
#[borrowme]
#[derive(Debug)]
#[owned_attr(derive(Clone))]
#[owned_attr(doc(hidden))]
pub struct GoldSrcInfo<'a> {
pub address: &'a str,
pub name: &'a str,
pub map: &'a str,
pub folder: &'a str,
pub game: &'a str,
pub players: u8,
pub max_players: u8,
pub protocol: u8,
pub server_type: ServerType,
pub environment: Environment,
pub is_private: bool,
pub mod_data: Option<Mod<'a>>,
pub vac: bool,
pub bots: u8,
}
pub(crate) fn parse_info(raw: &[u8]) -> Result<GoldSrcInfo<'_>, Error> {
let mut cur = Cursor::new(raw);
let address = cur.read_str()?;
let name = cur.read_str()?;
let map = cur.read_str()?;
let folder = cur.read_str()?;
let game = cur.read_str()?;
let players = cur.read_byte()?;
let max_players = cur.read_byte()?;
let protocol = 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,
other => return Err(Error::InvalidPlatform(other)),
};
let is_private = cur.read_byte()? != 0;
let mod_data = if cur.read_byte()? != 0 {
Some(Mod {
link: cur.read_str()?,
download_link: cur.read_str()?,
version: cur.read_long()?,
size: cur.read_long()?,
multiplayer_only: cur.read_byte()? != 0,
own_dll: cur.read_byte()? != 0,
})
} else {
None
};
let vac = cur.read_byte()? != 0;
let bots = cur.read_byte()?;
Ok(GoldSrcInfo { address, name, map, folder, game, players, max_players, protocol, server_type, environment, is_private, mod_data, vac, bots })
}
pub fn ping_request() -> Vec<u8> {
request(RequestType::Ping, None)
}
pub fn challenge_request() -> Vec<u8> {
request(crate::RequestType::Challenge, None)
}