mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Crate] Add formatting (#22)
* chore: add standard for formatting * chore: manually tidy up imports and format * chore: remove vscode and add to gitignore * chore: alphabetically order and fix * chore: format * chore: fix format issue with payload * chore: format as merge had unformatted code * [format] Fix comments, change max width and binop operator --------- Co-authored-by: CosminPerRam <cosmin.p@live.com>
This commit is contained in:
parent
e023e13236
commit
1b13d39856
71 changed files with 3165 additions and 2593 deletions
|
|
@ -1,8 +1,7 @@
|
|||
|
||||
/// The implementation.
|
||||
pub mod protocol;
|
||||
/// All types used by the implementation.
|
||||
pub mod types;
|
||||
|
||||
pub use protocol::*;
|
||||
pub use types::*;
|
||||
/// The implementation.
|
||||
pub mod protocol;
|
||||
/// All types used by the implementation.
|
||||
pub mod types;
|
||||
|
||||
pub use protocol::*;
|
||||
pub use types::*;
|
||||
|
|
|
|||
|
|
@ -1,455 +1,512 @@
|
|||
use std::collections::HashMap;
|
||||
use bzip2_rs::decoder::Decoder;
|
||||
use crate::GDResult;
|
||||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::GDError::{BadGame, Decompress, UnknownEnumCast};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::protocols::valve::{Engine, ModData, SteamApp};
|
||||
use crate::protocols::valve::types::{Environment, ExtraData, GatheringSettings, Request, Response, Server, ServerInfo, ServerPlayer, TheShip};
|
||||
use crate::socket::{Socket, UdpSocket};
|
||||
use crate::utils::u8_lower_upper;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Packet {
|
||||
pub header: u32,
|
||||
pub kind: u8,
|
||||
pub payload: Vec<u8>
|
||||
}
|
||||
|
||||
impl Packet {
|
||||
fn new(buffer: &mut Bufferer) -> GDResult<Self> {
|
||||
Ok(Self {
|
||||
header: buffer.get_u32()?,
|
||||
kind: buffer.get_u8()?,
|
||||
payload: buffer.remaining_data_vec()
|
||||
})
|
||||
}
|
||||
|
||||
fn challenge(kind: Request, challenge: Vec<u8>) -> Self {
|
||||
let mut initial = Packet::initial(kind);
|
||||
|
||||
Self {
|
||||
header: initial.header,
|
||||
kind: initial.kind,
|
||||
payload: match kind {
|
||||
Request::Info => {
|
||||
initial.payload.extend(challenge);
|
||||
initial.payload
|
||||
},
|
||||
_ => challenge
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn initial(kind: Request) -> Self {
|
||||
Self {
|
||||
header: 4294967295, //FF FF FF FF
|
||||
kind: kind as u8,
|
||||
payload: match kind {
|
||||
Request::Info => String::from("Source Engine Query\0").into_bytes(),
|
||||
_ => vec![0xFF, 0xFF, 0xFF, 0xFF]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut buf = Vec::from(self.header.to_be_bytes());
|
||||
|
||||
buf.push(self.kind);
|
||||
buf.extend(&self.payload);
|
||||
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)] //remove this later on
|
||||
struct SplitPacket {
|
||||
pub header: u32,
|
||||
pub id: u32,
|
||||
pub total: u8,
|
||||
pub number: u8,
|
||||
pub size: u16,
|
||||
pub compressed: bool,
|
||||
pub decompressed_size: Option<u32>,
|
||||
pub uncompressed_crc32: Option<u32>,
|
||||
payload: Vec<u8>
|
||||
}
|
||||
|
||||
impl SplitPacket {
|
||||
fn new(engine: &Engine, protocol: u8, buffer: &mut Bufferer) -> GDResult<Self> {
|
||||
let header = buffer.get_u32()?;
|
||||
let id = buffer.get_u32()?;
|
||||
let (total, number, size, compressed, decompressed_size, uncompressed_crc32) = match engine {
|
||||
Engine::GoldSrc(_) => {
|
||||
let (lower, upper) = u8_lower_upper(buffer.get_u8()?);
|
||||
(lower, upper, 0, false, None, None)
|
||||
}
|
||||
Engine::Source(_) => {
|
||||
let total = buffer.get_u8()?;
|
||||
let number = buffer.get_u8()?;
|
||||
let size = match protocol == 7 && (*engine == SteamApp::CSS.as_engine()) { //certain apps with protocol = 7 dont have this field
|
||||
false => buffer.get_u16()?,
|
||||
true => 1248
|
||||
};
|
||||
let compressed = ((id >> 31) & 1) == 1;
|
||||
let (decompressed_size, uncompressed_crc32) = match compressed {
|
||||
false => (None, None),
|
||||
true => (Some(buffer.get_u32()?), Some(buffer.get_u32()?))
|
||||
};
|
||||
(total, number, size, compressed, decompressed_size, uncompressed_crc32)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
header,
|
||||
id,
|
||||
total,
|
||||
number,
|
||||
size,
|
||||
compressed,
|
||||
decompressed_size,
|
||||
uncompressed_crc32,
|
||||
payload: buffer.remaining_data_vec()
|
||||
})
|
||||
}
|
||||
|
||||
fn get_payload(&self) -> GDResult<Vec<u8>> {
|
||||
if self.compressed {
|
||||
let mut decoder = Decoder::new();
|
||||
decoder.write(&self.payload).map_err(|_| Decompress)?;
|
||||
|
||||
let decompressed_size = self.decompressed_size.unwrap() as usize;
|
||||
|
||||
let mut decompressed_payload = vec![0; decompressed_size];
|
||||
|
||||
decoder.read(&mut decompressed_payload).map_err(|_| Decompress)?;
|
||||
|
||||
if decompressed_payload.len() != decompressed_size
|
||||
|| crc32fast::hash(&decompressed_payload) != self.uncompressed_crc32.unwrap() {
|
||||
Err(Decompress)
|
||||
}
|
||||
else {
|
||||
Ok(decompressed_payload)
|
||||
}
|
||||
} else {
|
||||
Ok(self.payload.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ValveProtocol {
|
||||
socket: UdpSocket
|
||||
}
|
||||
|
||||
static PACKET_SIZE: usize = 6144;
|
||||
|
||||
impl ValveProtocol {
|
||||
fn new(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = UdpSocket::new(address, port)?;
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(Self {
|
||||
socket
|
||||
})
|
||||
}
|
||||
|
||||
fn receive(&mut self, engine: &Engine, protocol: u8, buffer_size: usize) -> GDResult<Packet> {
|
||||
let data = self.socket.receive(Some(buffer_size))?;
|
||||
let mut buffer = Bufferer::new_with_data(Endianess::Little, &data);
|
||||
|
||||
let header = buffer.get_u8()?;
|
||||
buffer.move_position_backward(1);
|
||||
if header == 0xFE { //the packet is split
|
||||
let mut main_packet = SplitPacket::new(engine, protocol, &mut buffer)?;
|
||||
let mut chunk_packets = Vec::with_capacity((main_packet.total - 1) as usize);
|
||||
|
||||
for _ in 1..main_packet.total {
|
||||
let new_data = self.socket.receive(Some(buffer_size))?;
|
||||
buffer = Bufferer::new_with_data(Endianess::Little, &new_data);
|
||||
let chunk_packet = SplitPacket::new(engine, protocol, &mut buffer)?;
|
||||
chunk_packets.push(chunk_packet);
|
||||
}
|
||||
|
||||
chunk_packets.sort_by(|a, b| a.number.cmp(&b.number));
|
||||
|
||||
for chunk_packet in chunk_packets {
|
||||
main_packet.payload.extend(chunk_packet.payload);
|
||||
}
|
||||
|
||||
let mut new_packet_buffer = Bufferer::new_with_data(Endianess::Little, &main_packet.get_payload()?);
|
||||
Ok(Packet::new(&mut new_packet_buffer)?)
|
||||
}
|
||||
else {
|
||||
Packet::new(&mut buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Ask for a specific request only.
|
||||
fn get_request_data(&mut self, engine: &Engine, protocol: u8, kind: Request) -> GDResult<Bufferer> {
|
||||
let request_initial_packet = Packet::initial(kind).to_bytes();
|
||||
self.socket.send(&request_initial_packet)?;
|
||||
|
||||
let mut packet = self.receive(engine, protocol, PACKET_SIZE)?;
|
||||
while packet.kind == 0x41 {// 'A'
|
||||
let challenge = packet.payload.clone();
|
||||
let challenge_packet = Packet::challenge(kind, challenge).to_bytes();
|
||||
|
||||
self.socket.send(&challenge_packet)?;
|
||||
|
||||
packet = self.receive(engine, protocol, PACKET_SIZE)?;
|
||||
}
|
||||
|
||||
let data = packet.payload;
|
||||
Ok(Bufferer::new_with_data(Endianess::Little, &data))
|
||||
}
|
||||
|
||||
fn get_goldsrc_server_info(buffer: &mut Bufferer) -> GDResult<ServerInfo> {
|
||||
buffer.get_u8()?; //get the header (useless info)
|
||||
buffer.get_string_utf8()?; //get the server address (useless info)
|
||||
let name = buffer.get_string_utf8()?;
|
||||
let map = buffer.get_string_utf8()?;
|
||||
let folder = buffer.get_string_utf8()?;
|
||||
let game = buffer.get_string_utf8()?;
|
||||
let players = buffer.get_u8()?;
|
||||
let max_players = buffer.get_u8()?;
|
||||
let protocol = buffer.get_u8()?;
|
||||
let server_type = match buffer.get_u8()? {
|
||||
68 => Server::Dedicated, //'D'
|
||||
76 => Server::NonDedicated, //'L'
|
||||
80 => Server::TV, //'P'
|
||||
_ => Err(UnknownEnumCast)?
|
||||
};
|
||||
let environment_type = match buffer.get_u8()? {
|
||||
76 => Environment::Linux, //'L'
|
||||
87 => Environment::Windows, //'W'
|
||||
_ => Err(UnknownEnumCast)?
|
||||
};
|
||||
let has_password = buffer.get_u8()? == 1;
|
||||
let is_mod = buffer.get_u8()? == 1;
|
||||
let mod_data = match is_mod {
|
||||
false => None,
|
||||
true => Some(ModData {
|
||||
link: buffer.get_string_utf8()?,
|
||||
download_link: buffer.get_string_utf8()?,
|
||||
version: buffer.get_u32()?,
|
||||
size: buffer.get_u32()?,
|
||||
multiplayer_only: buffer.get_u8()? == 1,
|
||||
has_own_dll: buffer.get_u8()? == 1
|
||||
})
|
||||
};
|
||||
let vac_secured = buffer.get_u8()? == 1;
|
||||
let bots = buffer.get_u8()?;
|
||||
|
||||
Ok(ServerInfo {
|
||||
protocol,
|
||||
name,
|
||||
map,
|
||||
folder,
|
||||
game,
|
||||
appid: 0, //not present in the obsolete response
|
||||
players_online: players,
|
||||
players_maximum: max_players,
|
||||
players_bots: bots,
|
||||
server_type,
|
||||
environment_type,
|
||||
has_password,
|
||||
vac_secured,
|
||||
the_ship: None,
|
||||
version: "".to_string(), //a version field only for the mod
|
||||
extra_data: None,
|
||||
is_mod,
|
||||
mod_data
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the server information's.
|
||||
fn get_server_info(&mut self, engine: &Engine) -> GDResult<ServerInfo> {
|
||||
let mut buffer = self.get_request_data(engine, 0, Request::Info)?;
|
||||
|
||||
if let Engine::GoldSrc(force) = engine {
|
||||
if *force {
|
||||
return ValveProtocol::get_goldsrc_server_info(&mut buffer);
|
||||
}
|
||||
}
|
||||
|
||||
let protocol = buffer.get_u8()?;
|
||||
let name = buffer.get_string_utf8()?;
|
||||
let map = buffer.get_string_utf8()?;
|
||||
let folder = buffer.get_string_utf8()?;
|
||||
let game = buffer.get_string_utf8()?;
|
||||
let mut appid = buffer.get_u16()? as u32;
|
||||
let players = buffer.get_u8()?;
|
||||
let max_players = buffer.get_u8()?;
|
||||
let bots = buffer.get_u8()?;
|
||||
let server_type = match buffer.get_u8()? {
|
||||
100 => Server::Dedicated, //'d'
|
||||
108 => Server::NonDedicated, //'l'
|
||||
112 => Server::TV, //'p'
|
||||
_ => Err(UnknownEnumCast)?
|
||||
};
|
||||
let environment_type = match buffer.get_u8()? {
|
||||
108 => Environment::Linux, //'l'
|
||||
119 => Environment::Windows, //'w'
|
||||
109 | 111 => Environment::Mac, //'m' or 'o'
|
||||
_ => Err(UnknownEnumCast)?
|
||||
};
|
||||
let has_password = buffer.get_u8()? == 1;
|
||||
let vac_secured = buffer.get_u8()? == 1;
|
||||
let the_ship = match *engine == SteamApp::TS.as_engine() {
|
||||
false => None,
|
||||
true => Some(TheShip {
|
||||
mode: buffer.get_u8()?,
|
||||
witnesses: buffer.get_u8()?,
|
||||
duration: buffer.get_u8()?
|
||||
})
|
||||
};
|
||||
let version = buffer.get_string_utf8()?;
|
||||
let extra_data = match buffer.get_u8() {
|
||||
Err(_) => None,
|
||||
Ok(value) => Some(ExtraData {
|
||||
port: match (value & 0x80) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_u16()?)
|
||||
},
|
||||
steam_id: match (value & 0x10) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_u64()?)
|
||||
},
|
||||
tv_port: match (value & 0x40) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_u16()?)
|
||||
},
|
||||
tv_name: match (value & 0x40) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_string_utf8()?)
|
||||
},
|
||||
keywords: match (value & 0x20) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_string_utf8()?)
|
||||
},
|
||||
game_id: match (value & 0x01) > 0 {
|
||||
false => None,
|
||||
true => {
|
||||
let gid = buffer.get_u64()?;
|
||||
appid = (gid & ((1 << 24) - 1)) as u32;
|
||||
|
||||
Some(gid)
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
Ok(ServerInfo {
|
||||
protocol,
|
||||
name,
|
||||
map,
|
||||
folder,
|
||||
game,
|
||||
appid,
|
||||
players_online: players,
|
||||
players_maximum: max_players,
|
||||
players_bots: bots,
|
||||
server_type,
|
||||
environment_type,
|
||||
has_password,
|
||||
vac_secured,
|
||||
the_ship,
|
||||
version,
|
||||
extra_data,
|
||||
is_mod: false,
|
||||
mod_data: None
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the server player's.
|
||||
fn get_server_players(&mut self, engine: &Engine, protocol: u8) -> GDResult<Vec<ServerPlayer>> {
|
||||
let mut buffer = self.get_request_data(engine, protocol, Request::Players)?;
|
||||
|
||||
let count = buffer.get_u8()? as usize;
|
||||
let mut players: Vec<ServerPlayer> = Vec::with_capacity(count);
|
||||
|
||||
for _ in 0..count {
|
||||
buffer.move_position_ahead(1); //skip the index byte
|
||||
|
||||
players.push(ServerPlayer {
|
||||
name: buffer.get_string_utf8()?,
|
||||
score: buffer.get_u32()?,
|
||||
duration: buffer.get_f32()?,
|
||||
deaths: match *engine == SteamApp::TS.as_engine() {
|
||||
false => None,
|
||||
true => Some(buffer.get_u32()?)
|
||||
},
|
||||
money: match *engine == SteamApp::TS.as_engine() {
|
||||
false => None,
|
||||
true => Some(buffer.get_u32()?)
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Ok(players)
|
||||
}
|
||||
|
||||
/// Get the server's rules.
|
||||
fn get_server_rules(&mut self, engine: &Engine, protocol: u8) -> GDResult<HashMap<String, String>> {
|
||||
let mut buffer = self.get_request_data(engine, protocol, Request::Rules)?;
|
||||
|
||||
let count = buffer.get_u16()? as usize;
|
||||
let mut rules: HashMap<String, String> = HashMap::with_capacity(count);
|
||||
|
||||
for _ in 0..count {
|
||||
let name = buffer.get_string_utf8()?;
|
||||
let value = buffer.get_string_utf8()?;
|
||||
|
||||
rules.insert(name, value);
|
||||
}
|
||||
|
||||
if *engine == SteamApp::ROR2.as_engine() {
|
||||
rules.remove("Test");
|
||||
}
|
||||
|
||||
Ok(rules)
|
||||
}
|
||||
}
|
||||
|
||||
/// Query a server by providing the address, the port, the app, gather and timeout settings.
|
||||
/// Providing None to the settings results in using the default values for them (GatherSettings::[default](GatheringSettings::default), TimeoutSettings::[default](TimeoutSettings::default)).
|
||||
pub fn query(address: &str, port: u16, engine: Engine, gather_settings: Option<GatheringSettings>, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
let response_gather_settings = gather_settings.unwrap_or_default();
|
||||
get_response(address, port, engine, response_gather_settings, timeout_settings)
|
||||
}
|
||||
|
||||
fn get_response(address: &str, port: u16, engine: Engine, gather_settings: GatheringSettings, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
let mut client = ValveProtocol::new(address, port, timeout_settings)?;
|
||||
|
||||
let info = client.get_server_info(&engine)?;
|
||||
let protocol = info.protocol;
|
||||
|
||||
if let Engine::Source(Some(appids)) = &engine {
|
||||
let mut is_specified_id = false;
|
||||
|
||||
if appids.0 == info.appid {
|
||||
is_specified_id = true;
|
||||
} else if let Some(dedicated_appid) = appids.1 {
|
||||
if dedicated_appid == info.appid {
|
||||
is_specified_id = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !is_specified_id {
|
||||
return Err(BadGame(format!("AppId: {}", info.appid)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Response {
|
||||
info,
|
||||
players: match gather_settings.players {
|
||||
false => None,
|
||||
true => Some(client.get_server_players(&engine, protocol)?)
|
||||
},
|
||||
rules: match gather_settings.rules {
|
||||
false => None,
|
||||
true => Some(client.get_server_rules(&engine, protocol)?)
|
||||
}
|
||||
})
|
||||
}
|
||||
use crate::{
|
||||
bufferer::{Bufferer, Endianess},
|
||||
protocols::{
|
||||
types::TimeoutSettings,
|
||||
valve::{
|
||||
types::{
|
||||
Environment,
|
||||
ExtraData,
|
||||
GatheringSettings,
|
||||
Request,
|
||||
Response,
|
||||
Server,
|
||||
ServerInfo,
|
||||
ServerPlayer,
|
||||
TheShip,
|
||||
},
|
||||
Engine,
|
||||
ModData,
|
||||
SteamApp,
|
||||
},
|
||||
},
|
||||
|
||||
socket::{Socket, UdpSocket},
|
||||
utils::u8_lower_upper,
|
||||
GDError::{BadGame, Decompress, UnknownEnumCast},
|
||||
GDResult,
|
||||
};
|
||||
|
||||
use bzip2_rs::decoder::Decoder;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Packet {
|
||||
pub header: u32,
|
||||
pub kind: u8,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Packet {
|
||||
fn new(buffer: &mut Bufferer) -> GDResult<Self> {
|
||||
Ok(Self {
|
||||
header: buffer.get_u32()?,
|
||||
kind: buffer.get_u8()?,
|
||||
payload: buffer.remaining_data_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
fn challenge(kind: Request, challenge: Vec<u8>) -> Self {
|
||||
let mut initial = Packet::initial(kind);
|
||||
|
||||
Self {
|
||||
header: initial.header,
|
||||
kind: initial.kind,
|
||||
payload: match kind {
|
||||
Request::Info => {
|
||||
initial.payload.extend(challenge);
|
||||
initial.payload
|
||||
}
|
||||
_ => challenge,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn initial(kind: Request) -> Self {
|
||||
Self {
|
||||
header: 4294967295, // FF FF FF FF
|
||||
kind: kind as u8,
|
||||
payload: match kind {
|
||||
Request::Info => String::from("Source Engine Query\0").into_bytes(),
|
||||
_ => vec![0xFF, 0xFF, 0xFF, 0xFF],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut buf = Vec::from(self.header.to_be_bytes());
|
||||
|
||||
buf.push(self.kind);
|
||||
buf.extend(&self.payload);
|
||||
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)] //remove this later on
|
||||
struct SplitPacket {
|
||||
pub header: u32,
|
||||
pub id: u32,
|
||||
pub total: u8,
|
||||
pub number: u8,
|
||||
pub size: u16,
|
||||
pub compressed: bool,
|
||||
pub decompressed_size: Option<u32>,
|
||||
pub uncompressed_crc32: Option<u32>,
|
||||
payload: Vec<u8>,
|
||||
}
|
||||
|
||||
impl SplitPacket {
|
||||
fn new(engine: &Engine, protocol: u8, buffer: &mut Bufferer) -> GDResult<Self> {
|
||||
let header = buffer.get_u32()?;
|
||||
let id = buffer.get_u32()?;
|
||||
let (total, number, size, compressed, decompressed_size, uncompressed_crc32) = match engine {
|
||||
Engine::GoldSrc(_) => {
|
||||
let (lower, upper) = u8_lower_upper(buffer.get_u8()?);
|
||||
(lower, upper, 0, false, None, None)
|
||||
}
|
||||
Engine::Source(_) => {
|
||||
let total = buffer.get_u8()?;
|
||||
let number = buffer.get_u8()?;
|
||||
let size = match protocol == 7 && (*engine == SteamApp::CSS.as_engine()) {
|
||||
// certain apps with protocol = 7 dont have this field
|
||||
false => buffer.get_u16()?,
|
||||
true => 1248,
|
||||
};
|
||||
let compressed = ((id >> 31) & 1) == 1;
|
||||
let (decompressed_size, uncompressed_crc32) = match compressed {
|
||||
false => (None, None),
|
||||
true => (Some(buffer.get_u32()?), Some(buffer.get_u32()?)),
|
||||
};
|
||||
(
|
||||
total,
|
||||
number,
|
||||
size,
|
||||
compressed,
|
||||
decompressed_size,
|
||||
uncompressed_crc32,
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
header,
|
||||
id,
|
||||
total,
|
||||
number,
|
||||
size,
|
||||
compressed,
|
||||
decompressed_size,
|
||||
uncompressed_crc32,
|
||||
payload: buffer.remaining_data_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
fn get_payload(&self) -> GDResult<Vec<u8>> {
|
||||
if self.compressed {
|
||||
let mut decoder = Decoder::new();
|
||||
decoder.write(&self.payload).map_err(|_| Decompress)?;
|
||||
|
||||
let decompressed_size = self.decompressed_size.unwrap() as usize;
|
||||
|
||||
let mut decompressed_payload = vec![0; decompressed_size];
|
||||
|
||||
decoder
|
||||
.read(&mut decompressed_payload)
|
||||
.map_err(|_| Decompress)?;
|
||||
|
||||
if decompressed_payload.len() != decompressed_size
|
||||
|| crc32fast::hash(&decompressed_payload) != self.uncompressed_crc32.unwrap()
|
||||
{
|
||||
Err(Decompress)
|
||||
} else {
|
||||
Ok(decompressed_payload)
|
||||
}
|
||||
} else {
|
||||
Ok(self.payload.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ValveProtocol {
|
||||
socket: UdpSocket,
|
||||
}
|
||||
|
||||
static PACKET_SIZE: usize = 6144;
|
||||
|
||||
impl ValveProtocol {
|
||||
fn new(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = UdpSocket::new(address, port)?;
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(Self { socket })
|
||||
}
|
||||
|
||||
fn receive(&mut self, engine: &Engine, protocol: u8, buffer_size: usize) -> GDResult<Packet> {
|
||||
let data = self.socket.receive(Some(buffer_size))?;
|
||||
let mut buffer = Bufferer::new_with_data(Endianess::Little, &data);
|
||||
|
||||
let header = buffer.get_u8()?;
|
||||
buffer.move_position_backward(1);
|
||||
if header == 0xFE {
|
||||
// the packet is split
|
||||
let mut main_packet = SplitPacket::new(engine, protocol, &mut buffer)?;
|
||||
let mut chunk_packets = Vec::with_capacity((main_packet.total - 1) as usize);
|
||||
|
||||
for _ in 1 .. main_packet.total {
|
||||
let new_data = self.socket.receive(Some(buffer_size))?;
|
||||
buffer = Bufferer::new_with_data(Endianess::Little, &new_data);
|
||||
let chunk_packet = SplitPacket::new(engine, protocol, &mut buffer)?;
|
||||
chunk_packets.push(chunk_packet);
|
||||
}
|
||||
|
||||
chunk_packets.sort_by(|a, b| a.number.cmp(&b.number));
|
||||
|
||||
for chunk_packet in chunk_packets {
|
||||
main_packet.payload.extend(chunk_packet.payload);
|
||||
}
|
||||
|
||||
let mut new_packet_buffer = Bufferer::new_with_data(Endianess::Little, &main_packet.get_payload()?);
|
||||
Ok(Packet::new(&mut new_packet_buffer)?)
|
||||
} else {
|
||||
Packet::new(&mut buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Ask for a specific request only.
|
||||
fn get_request_data(&mut self, engine: &Engine, protocol: u8, kind: Request) -> GDResult<Bufferer> {
|
||||
let request_initial_packet = Packet::initial(kind).to_bytes();
|
||||
self.socket.send(&request_initial_packet)?;
|
||||
|
||||
let mut packet = self.receive(engine, protocol, PACKET_SIZE)?;
|
||||
while packet.kind == 0x41 {
|
||||
// 'A'
|
||||
let challenge = packet.payload.clone();
|
||||
let challenge_packet = Packet::challenge(kind, challenge).to_bytes();
|
||||
|
||||
self.socket.send(&challenge_packet)?;
|
||||
|
||||
packet = self.receive(engine, protocol, PACKET_SIZE)?;
|
||||
}
|
||||
|
||||
let data = packet.payload;
|
||||
Ok(Bufferer::new_with_data(Endianess::Little, &data))
|
||||
}
|
||||
|
||||
fn get_goldsrc_server_info(buffer: &mut Bufferer) -> GDResult<ServerInfo> {
|
||||
buffer.get_u8()?; //get the header (useless info)
|
||||
buffer.get_string_utf8()?; //get the server address (useless info)
|
||||
let name = buffer.get_string_utf8()?;
|
||||
let map = buffer.get_string_utf8()?;
|
||||
let folder = buffer.get_string_utf8()?;
|
||||
let game = buffer.get_string_utf8()?;
|
||||
let players = buffer.get_u8()?;
|
||||
let max_players = buffer.get_u8()?;
|
||||
let protocol = buffer.get_u8()?;
|
||||
let server_type = match buffer.get_u8()? {
|
||||
68 => Server::Dedicated, //'D'
|
||||
76 => Server::NonDedicated, //'L'
|
||||
80 => Server::TV, //'P'
|
||||
_ => Err(UnknownEnumCast)?,
|
||||
};
|
||||
let environment_type = match buffer.get_u8()? {
|
||||
76 => Environment::Linux, //'L'
|
||||
87 => Environment::Windows, //'W'
|
||||
_ => Err(UnknownEnumCast)?,
|
||||
};
|
||||
let has_password = buffer.get_u8()? == 1;
|
||||
let is_mod = buffer.get_u8()? == 1;
|
||||
let mod_data = match is_mod {
|
||||
false => None,
|
||||
true => {
|
||||
Some(ModData {
|
||||
link: buffer.get_string_utf8()?,
|
||||
download_link: buffer.get_string_utf8()?,
|
||||
version: buffer.get_u32()?,
|
||||
size: buffer.get_u32()?,
|
||||
multiplayer_only: buffer.get_u8()? == 1,
|
||||
has_own_dll: buffer.get_u8()? == 1,
|
||||
})
|
||||
}
|
||||
};
|
||||
let vac_secured = buffer.get_u8()? == 1;
|
||||
let bots = buffer.get_u8()?;
|
||||
|
||||
Ok(ServerInfo {
|
||||
protocol,
|
||||
name,
|
||||
map,
|
||||
folder,
|
||||
game,
|
||||
appid: 0, // not present in the obsolete response
|
||||
players_online: players,
|
||||
players_maximum: max_players,
|
||||
players_bots: bots,
|
||||
server_type,
|
||||
environment_type,
|
||||
has_password,
|
||||
vac_secured,
|
||||
the_ship: None,
|
||||
version: "".to_string(), // a version field only for the mod
|
||||
extra_data: None,
|
||||
is_mod,
|
||||
mod_data,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the server information's.
|
||||
fn get_server_info(&mut self, engine: &Engine) -> GDResult<ServerInfo> {
|
||||
let mut buffer = self.get_request_data(engine, 0, Request::Info)?;
|
||||
|
||||
if let Engine::GoldSrc(force) = engine {
|
||||
if *force {
|
||||
return ValveProtocol::get_goldsrc_server_info(&mut buffer);
|
||||
}
|
||||
}
|
||||
|
||||
let protocol = buffer.get_u8()?;
|
||||
let name = buffer.get_string_utf8()?;
|
||||
let map = buffer.get_string_utf8()?;
|
||||
let folder = buffer.get_string_utf8()?;
|
||||
let game = buffer.get_string_utf8()?;
|
||||
let mut appid = buffer.get_u16()? as u32;
|
||||
let players = buffer.get_u8()?;
|
||||
let max_players = buffer.get_u8()?;
|
||||
let bots = buffer.get_u8()?;
|
||||
let server_type = match buffer.get_u8()? {
|
||||
100 => Server::Dedicated, //'d'
|
||||
108 => Server::NonDedicated, //'l'
|
||||
112 => Server::TV, //'p'
|
||||
_ => Err(UnknownEnumCast)?,
|
||||
};
|
||||
let environment_type = match buffer.get_u8()? {
|
||||
108 => Environment::Linux, //'l'
|
||||
119 => Environment::Windows, //'w'
|
||||
109 | 111 => Environment::Mac, //'m' or 'o'
|
||||
_ => Err(UnknownEnumCast)?,
|
||||
};
|
||||
let has_password = buffer.get_u8()? == 1;
|
||||
let vac_secured = buffer.get_u8()? == 1;
|
||||
let the_ship = match *engine == SteamApp::TS.as_engine() {
|
||||
false => None,
|
||||
true => {
|
||||
Some(TheShip {
|
||||
mode: buffer.get_u8()?,
|
||||
witnesses: buffer.get_u8()?,
|
||||
duration: buffer.get_u8()?,
|
||||
})
|
||||
}
|
||||
};
|
||||
let version = buffer.get_string_utf8()?;
|
||||
let extra_data = match buffer.get_u8() {
|
||||
Err(_) => None,
|
||||
Ok(value) => {
|
||||
Some(ExtraData {
|
||||
port: match (value & 0x80) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_u16()?),
|
||||
},
|
||||
steam_id: match (value & 0x10) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_u64()?),
|
||||
},
|
||||
tv_port: match (value & 0x40) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_u16()?),
|
||||
},
|
||||
tv_name: match (value & 0x40) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_string_utf8()?),
|
||||
},
|
||||
keywords: match (value & 0x20) > 0 {
|
||||
false => None,
|
||||
true => Some(buffer.get_string_utf8()?),
|
||||
},
|
||||
game_id: match (value & 0x01) > 0 {
|
||||
false => None,
|
||||
true => {
|
||||
let gid = buffer.get_u64()?;
|
||||
appid = (gid & ((1 << 24) - 1)) as u32;
|
||||
|
||||
Some(gid)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
Ok(ServerInfo {
|
||||
protocol,
|
||||
name,
|
||||
map,
|
||||
folder,
|
||||
game,
|
||||
appid,
|
||||
players_online: players,
|
||||
players_maximum: max_players,
|
||||
players_bots: bots,
|
||||
server_type,
|
||||
environment_type,
|
||||
has_password,
|
||||
vac_secured,
|
||||
the_ship,
|
||||
version,
|
||||
extra_data,
|
||||
is_mod: false,
|
||||
mod_data: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the server player's.
|
||||
fn get_server_players(&mut self, engine: &Engine, protocol: u8) -> GDResult<Vec<ServerPlayer>> {
|
||||
let mut buffer = self.get_request_data(engine, protocol, Request::Players)?;
|
||||
|
||||
let count = buffer.get_u8()? as usize;
|
||||
let mut players: Vec<ServerPlayer> = Vec::with_capacity(count);
|
||||
|
||||
for _ in 0 .. count {
|
||||
buffer.move_position_ahead(1); //skip the index byte
|
||||
|
||||
players.push(ServerPlayer {
|
||||
name: buffer.get_string_utf8()?,
|
||||
score: buffer.get_u32()?,
|
||||
duration: buffer.get_f32()?,
|
||||
deaths: match *engine == SteamApp::TS.as_engine() {
|
||||
false => None,
|
||||
true => Some(buffer.get_u32()?),
|
||||
},
|
||||
money: match *engine == SteamApp::TS.as_engine() {
|
||||
false => None,
|
||||
true => Some(buffer.get_u32()?),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Ok(players)
|
||||
}
|
||||
|
||||
/// Get the server's rules.
|
||||
fn get_server_rules(&mut self, engine: &Engine, protocol: u8) -> GDResult<HashMap<String, String>> {
|
||||
let mut buffer = self.get_request_data(engine, protocol, Request::Rules)?;
|
||||
|
||||
let count = buffer.get_u16()? as usize;
|
||||
let mut rules: HashMap<String, String> = HashMap::with_capacity(count);
|
||||
|
||||
for _ in 0 .. count {
|
||||
let name = buffer.get_string_utf8()?;
|
||||
let value = buffer.get_string_utf8()?;
|
||||
|
||||
rules.insert(name, value);
|
||||
}
|
||||
|
||||
if *engine == SteamApp::ROR2.as_engine() {
|
||||
rules.remove("Test");
|
||||
}
|
||||
|
||||
Ok(rules)
|
||||
}
|
||||
}
|
||||
|
||||
/// Query a server by providing the address, the port, the app, gather and
|
||||
/// timeout settings. Providing None to the settings results in using the
|
||||
/// default values for them
|
||||
/// (GatherSettings::[default](GatheringSettings::default),
|
||||
/// TimeoutSettings::[default](TimeoutSettings::default)).
|
||||
pub fn query(
|
||||
address: &str,
|
||||
port: u16,
|
||||
engine: Engine,
|
||||
gather_settings: Option<GatheringSettings>,
|
||||
timeout_settings: Option<TimeoutSettings>,
|
||||
) -> GDResult<Response> {
|
||||
let response_gather_settings = gather_settings.unwrap_or_default();
|
||||
get_response(
|
||||
address,
|
||||
port,
|
||||
engine,
|
||||
response_gather_settings,
|
||||
timeout_settings,
|
||||
)
|
||||
}
|
||||
|
||||
fn get_response(
|
||||
address: &str,
|
||||
port: u16,
|
||||
engine: Engine,
|
||||
gather_settings: GatheringSettings,
|
||||
timeout_settings: Option<TimeoutSettings>,
|
||||
) -> GDResult<Response> {
|
||||
let mut client = ValveProtocol::new(address, port, timeout_settings)?;
|
||||
|
||||
let info = client.get_server_info(&engine)?;
|
||||
let protocol = info.protocol;
|
||||
|
||||
if let Engine::Source(Some(appids)) = &engine {
|
||||
let mut is_specified_id = false;
|
||||
|
||||
if appids.0 == info.appid {
|
||||
is_specified_id = true;
|
||||
} else if let Some(dedicated_appid) = appids.1 {
|
||||
if dedicated_appid == info.appid {
|
||||
is_specified_id = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !is_specified_id {
|
||||
return Err(BadGame(format!("AppId: {}", info.appid)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Response {
|
||||
info,
|
||||
players: match gather_settings.players {
|
||||
false => None,
|
||||
true => Some(client.get_server_players(&engine, protocol)?),
|
||||
},
|
||||
rules: match gather_settings.rules {
|
||||
false => None,
|
||||
true => Some(client.get_server_rules(&engine, protocol)?),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,444 +1,441 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The type of the server.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Server {
|
||||
Dedicated,
|
||||
NonDedicated,
|
||||
TV,
|
||||
}
|
||||
|
||||
/// The Operating System that the server is on.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Environment {
|
||||
Linux,
|
||||
Windows,
|
||||
Mac,
|
||||
}
|
||||
|
||||
/// A query response.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Response {
|
||||
pub info: ServerInfo,
|
||||
pub players: Option<Vec<ServerPlayer>>,
|
||||
pub rules: Option<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
/// General server information's.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ServerInfo {
|
||||
/// Protocol used by the server.
|
||||
pub protocol: u8,
|
||||
/// Name of the server.
|
||||
pub name: String,
|
||||
/// Map name.
|
||||
pub map: String,
|
||||
/// Name of the folder containing the game files.
|
||||
pub folder: String,
|
||||
/// The name of the game.
|
||||
pub game: String,
|
||||
/// [Steam Application ID](https://developer.valvesoftware.com/wiki/Steam_Application_ID) of game.
|
||||
pub appid: u32,
|
||||
/// Number of players on the server.
|
||||
pub players_online: u8,
|
||||
/// Maximum number of players the server reports it can hold.
|
||||
pub players_maximum: u8,
|
||||
/// Number of bots on the server.
|
||||
pub players_bots: u8,
|
||||
/// Dedicated, NonDedicated or SourceTV
|
||||
pub server_type: Server,
|
||||
/// The Operating System that the server is on.
|
||||
pub environment_type: Environment,
|
||||
/// Indicates whether the server requires a password.
|
||||
pub has_password: bool,
|
||||
/// Indicates whether the server uses VAC.
|
||||
pub vac_secured: bool,
|
||||
/// [The ship](https://developer.valvesoftware.com/wiki/The_Ship) extra data
|
||||
pub the_ship: Option<TheShip>,
|
||||
/// Version of the game installed on the server.
|
||||
pub version: String,
|
||||
/// Some extra data that the server might provide or not.
|
||||
pub extra_data: Option<ExtraData>,
|
||||
/// GoldSrc only: Indicates whether the hosted game is a mod.
|
||||
pub is_mod: bool,
|
||||
/// GoldSrc only: If the game is a mod, provide additional data.
|
||||
pub mod_data: Option<ModData>,
|
||||
}
|
||||
|
||||
/// A server player.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
||||
pub struct ServerPlayer {
|
||||
/// Player's name.
|
||||
pub name: String,
|
||||
/// General score.
|
||||
pub score: u32,
|
||||
/// How long a player has been in the server (seconds).
|
||||
pub duration: f32,
|
||||
/// Only for [the ship](https://developer.valvesoftware.com/wiki/The_Ship): deaths count
|
||||
pub deaths: Option<u32>, //the_ship
|
||||
/// Only for [the ship](https://developer.valvesoftware.com/wiki/The_Ship): money amount
|
||||
pub money: Option<u32>, //the_ship
|
||||
}
|
||||
|
||||
/// Only present for [the ship](https://developer.valvesoftware.com/wiki/The_Ship).
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct TheShip {
|
||||
pub mode: u8,
|
||||
pub witnesses: u8,
|
||||
pub duration: u8,
|
||||
}
|
||||
|
||||
/// Some extra data that the server might provide or not.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ExtraData {
|
||||
/// The server's game port number.
|
||||
pub port: Option<u16>,
|
||||
/// Server's SteamID.
|
||||
pub steam_id: Option<u64>,
|
||||
/// SourceTV's port.
|
||||
pub tv_port: Option<u16>,
|
||||
/// SourceTV's name.
|
||||
pub tv_name: Option<String>,
|
||||
/// Keywords that describe the server according to it.
|
||||
pub keywords: Option<String>,
|
||||
/// The server's 64-bit GameID.
|
||||
pub game_id: Option<u64>,
|
||||
}
|
||||
|
||||
/// Data related to GoldSrc Mod response.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ModData {
|
||||
pub link: String,
|
||||
pub download_link: String,
|
||||
pub version: u32,
|
||||
pub size: u32,
|
||||
pub multiplayer_only: bool,
|
||||
pub has_own_dll: bool,
|
||||
}
|
||||
|
||||
pub(crate) type ExtractedData = (
|
||||
Option<u16>,
|
||||
Option<u64>,
|
||||
Option<u16>,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
);
|
||||
|
||||
pub(crate) fn get_optional_extracted_data(
|
||||
data: Option<ExtraData>,
|
||||
) -> ExtractedData {
|
||||
match data {
|
||||
None => (None, None, None, None, None),
|
||||
Some(ed) => (ed.port, ed.steam_id, ed.tv_port, ed.tv_name, ed.keywords),
|
||||
}
|
||||
}
|
||||
|
||||
/// The type of the request, see the [protocol](https://developer.valvesoftware.com/wiki/Server_queries).
|
||||
#[derive(Eq, PartialEq, Copy, Clone)]
|
||||
#[repr(u8)]
|
||||
pub(crate) enum Request {
|
||||
/// Known as `A2S_INFO`
|
||||
Info = 0x54,
|
||||
/// Known as `A2S_PLAYERS`
|
||||
Players = 0x55,
|
||||
/// Known as `A2S_RULES`
|
||||
Rules = 0x56,
|
||||
}
|
||||
|
||||
/// Supported steam apps
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum SteamApp {
|
||||
/// Counter-Strike
|
||||
CS,
|
||||
/// Team Fortress Classic
|
||||
TFC,
|
||||
/// Day of Defeat
|
||||
DOD,
|
||||
/// Counter-Strike: Condition Zero
|
||||
CSCZ,
|
||||
/// Counter-Strike: Source
|
||||
CSS,
|
||||
/// Day of Defeat: Source
|
||||
DODS,
|
||||
/// Half-Life 2 Deathmatch
|
||||
HL2DM,
|
||||
/// Half-Life Deathmatch: Source
|
||||
HLDMS,
|
||||
/// Team Fortress 2
|
||||
TF2,
|
||||
/// Left 4 Dead
|
||||
L4D,
|
||||
/// Left 4 Dead
|
||||
L4D2,
|
||||
/// Alien Swarm
|
||||
ALIENS,
|
||||
/// Counter-Strike: Global Offensive
|
||||
CSGO,
|
||||
/// The Ship
|
||||
TS,
|
||||
/// Garry's Mod
|
||||
GM,
|
||||
/// Age of Chivalry
|
||||
AOC,
|
||||
/// Insurgency: Modern Infantry Combat
|
||||
INSMIC,
|
||||
/// ARMA 2: Operation Arrowhead
|
||||
ARMA2OA,
|
||||
/// Project Zomboid
|
||||
PZ,
|
||||
/// Insurgency
|
||||
INS,
|
||||
/// Sven Co-op
|
||||
SC,
|
||||
/// 7 Days To Die
|
||||
SDTD,
|
||||
/// Rust
|
||||
RUST,
|
||||
/// Vallistic Overkill
|
||||
BO,
|
||||
/// Don't Starve Together
|
||||
DST,
|
||||
/// BrainBread 2
|
||||
BB2,
|
||||
/// Codename CURE
|
||||
CCURE,
|
||||
/// Black Mesa
|
||||
BM,
|
||||
/// Colony Survival
|
||||
COSU,
|
||||
/// Avorion
|
||||
AVORION,
|
||||
/// Day of Infamy
|
||||
DOI,
|
||||
/// The Forest
|
||||
TF,
|
||||
/// Unturned
|
||||
UNTURNED,
|
||||
/// ARK: Survival Evolved
|
||||
ASE,
|
||||
/// Battalion 1944
|
||||
BAT1944,
|
||||
/// Insurgency: Sandstorm
|
||||
INSS,
|
||||
/// Alien Swarm: Reactive Drop
|
||||
ASRD,
|
||||
/// Risk of Rain 2
|
||||
ROR2,
|
||||
/// Operation: Harsh Doorstop
|
||||
OHD,
|
||||
/// Onset
|
||||
ONSET,
|
||||
/// V Rising
|
||||
VR,
|
||||
}
|
||||
|
||||
impl SteamApp {
|
||||
/// Get the specified app as engine.
|
||||
pub fn as_engine(&self) -> Engine {
|
||||
match self {
|
||||
SteamApp::CS => Engine::GoldSrc(false), //10
|
||||
SteamApp::TFC => Engine::GoldSrc(false), //20
|
||||
SteamApp::DOD => Engine::GoldSrc(false), //30
|
||||
SteamApp::CSCZ => Engine::GoldSrc(false), //80
|
||||
SteamApp::CSS => Engine::new_source(240),
|
||||
SteamApp::DODS => Engine::new_source(300),
|
||||
SteamApp::HL2DM => Engine::new_source(320),
|
||||
SteamApp::HLDMS => Engine::new_source(360),
|
||||
SteamApp::TF2 => Engine::new_source(440),
|
||||
SteamApp::L4D => Engine::new_source(500),
|
||||
SteamApp::L4D2 => Engine::new_source(550),
|
||||
SteamApp::ALIENS => Engine::new_source(630),
|
||||
SteamApp::CSGO => Engine::new_source(730),
|
||||
SteamApp::TS => Engine::new_source(2400),
|
||||
SteamApp::GM => Engine::new_source(4000),
|
||||
SteamApp::AOC => Engine::new_source(17510),
|
||||
SteamApp::INSMIC => Engine::new_source(17700),
|
||||
SteamApp::ARMA2OA => Engine::new_source(33930),
|
||||
SteamApp::PZ => Engine::new_source(108600),
|
||||
SteamApp::INS => Engine::new_source(222880),
|
||||
SteamApp::SC => Engine::GoldSrc(false), //225840
|
||||
SteamApp::SDTD => Engine::new_source(251570),
|
||||
SteamApp::RUST => Engine::new_source(252490),
|
||||
SteamApp::BO => Engine::new_source(296300),
|
||||
SteamApp::DST => Engine::new_source(322320),
|
||||
SteamApp::BB2 => Engine::new_source(346330),
|
||||
SteamApp::CCURE => Engine::new_source(355180),
|
||||
SteamApp::BM => Engine::new_source(362890),
|
||||
SteamApp::COSU => Engine::new_source(366090),
|
||||
SteamApp::AVORION => Engine::new_source(445220),
|
||||
SteamApp::DOI => Engine::new_source(447820),
|
||||
SteamApp::TF => Engine::new_source(556450),
|
||||
SteamApp::UNTURNED => Engine::new_source(304930),
|
||||
SteamApp::ASE => Engine::new_source(346110),
|
||||
SteamApp::BAT1944 => Engine::new_source(489940),
|
||||
SteamApp::INSS => Engine::new_source(581320),
|
||||
SteamApp::ASRD => Engine::new_source(563560),
|
||||
SteamApp::ROR2 => Engine::new_source(632360),
|
||||
SteamApp::OHD => Engine::new_source_with_dedicated(736590, 950900),
|
||||
SteamApp::ONSET => Engine::new_source(1105810),
|
||||
SteamApp::VR => Engine::new_source(1604030),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Engine type.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Engine {
|
||||
/// A Source game, the argument represents the possible steam app ids, if its **None**, let
|
||||
/// the query find it, if its **Some**, the query fails if the response id is not the first
|
||||
/// one, which is the game app id, or the other one, which is the dedicated server app id.
|
||||
Source(Option<(u32, Option<u32>)>),
|
||||
/// A GoldSrc game, the argument indicates whether to enforce
|
||||
/// requesting the obsolete A2S_INFO response or not.
|
||||
GoldSrc(bool),
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new_source(appid: u32) -> Self {
|
||||
Engine::Source(Some((appid, None)))
|
||||
}
|
||||
|
||||
pub fn new_source_with_dedicated(appid: u32, dedicated_appid: u32) -> Self {
|
||||
Engine::Source(Some((appid, Some(dedicated_appid))))
|
||||
}
|
||||
}
|
||||
|
||||
/// What data to gather, purely used only with the query function.
|
||||
pub struct GatheringSettings {
|
||||
pub players: bool,
|
||||
pub rules: bool,
|
||||
}
|
||||
|
||||
impl Default for GatheringSettings {
|
||||
/// Default values are true for both the players and the rules.
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
players: true,
|
||||
rules: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic response types that are used by many games, they are the protocol ones, but without the
|
||||
/// unnecessary bits (example: the **The Ship**-only fields).
|
||||
pub mod game {
|
||||
use super::{Server, ServerPlayer};
|
||||
use crate::protocols::valve::types::get_optional_extracted_data;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A player's details.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
||||
pub struct Player {
|
||||
/// Player's name.
|
||||
pub name: String,
|
||||
/// Player's score.
|
||||
pub score: u32,
|
||||
/// How long a player has been in the server (seconds).
|
||||
pub duration: f32,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
pub fn from_valve_response(player: &ServerPlayer) -> Self {
|
||||
Self {
|
||||
name: player.name.clone(),
|
||||
score: player.score,
|
||||
duration: player.duration,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The query response.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Response {
|
||||
/// Protocol used by the server.
|
||||
pub protocol: u8,
|
||||
/// Name of the server.
|
||||
pub name: String,
|
||||
/// Map name.
|
||||
pub map: String,
|
||||
/// The name of the game.
|
||||
pub game: String,
|
||||
/// Server's app id.
|
||||
pub appid: u32,
|
||||
/// Number of players on the server.
|
||||
pub players_online: u8,
|
||||
/// Details about the server's players (not all players necessarily).
|
||||
pub players_details: Vec<Player>,
|
||||
/// Maximum number of players the server reports it can hold.
|
||||
pub players_maximum: u8,
|
||||
/// Number of bots on the server.
|
||||
pub players_bots: u8,
|
||||
/// Dedicated, NonDedicated or SourceTV
|
||||
pub server_type: Server,
|
||||
/// Indicates whether the server requires a password.
|
||||
pub has_password: bool,
|
||||
/// Indicated whether the server uses VAC.
|
||||
pub vac_secured: bool,
|
||||
/// Version of the game installed on the server.
|
||||
pub version: String,
|
||||
/// The server's reported connection port.
|
||||
pub port: Option<u16>,
|
||||
/// Server's SteamID.
|
||||
pub steam_id: Option<u64>,
|
||||
/// SourceTV's connection port.
|
||||
pub tv_port: Option<u16>,
|
||||
/// SourceTV's name.
|
||||
pub tv_name: Option<String>,
|
||||
/// Keywords that describe the server according to it.
|
||||
pub keywords: Option<String>,
|
||||
/// Server's rules.
|
||||
pub rules: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new_from_valve_response(response: super::Response) -> Self {
|
||||
let (port, steam_id, tv_port, tv_name, keywords) =
|
||||
get_optional_extracted_data(response.info.extra_data);
|
||||
|
||||
Self {
|
||||
protocol: response.info.protocol,
|
||||
name: response.info.name,
|
||||
map: response.info.map,
|
||||
game: response.info.game,
|
||||
appid: response.info.appid,
|
||||
players_online: response.info.players_online,
|
||||
players_details: response
|
||||
.players
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.map(Player::from_valve_response)
|
||||
.collect(),
|
||||
players_maximum: response.info.players_maximum,
|
||||
players_bots: response.info.players_bots,
|
||||
server_type: response.info.server_type,
|
||||
has_password: response.info.has_password,
|
||||
vac_secured: response.info.vac_secured,
|
||||
version: response.info.version,
|
||||
port,
|
||||
steam_id,
|
||||
tv_port,
|
||||
tv_name,
|
||||
keywords,
|
||||
rules: response.rules.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The type of the server.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Server {
|
||||
Dedicated,
|
||||
NonDedicated,
|
||||
TV,
|
||||
}
|
||||
|
||||
/// The Operating System that the server is on.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Environment {
|
||||
Linux,
|
||||
Windows,
|
||||
Mac,
|
||||
}
|
||||
|
||||
/// A query response.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Response {
|
||||
pub info: ServerInfo,
|
||||
pub players: Option<Vec<ServerPlayer>>,
|
||||
pub rules: Option<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
/// General server information's.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ServerInfo {
|
||||
/// Protocol used by the server.
|
||||
pub protocol: u8,
|
||||
/// Name of the server.
|
||||
pub name: String,
|
||||
/// Map name.
|
||||
pub map: String,
|
||||
/// Name of the folder containing the game files.
|
||||
pub folder: String,
|
||||
/// The name of the game.
|
||||
pub game: String,
|
||||
/// [Steam Application ID](https://developer.valvesoftware.com/wiki/Steam_Application_ID) of game.
|
||||
pub appid: u32,
|
||||
/// Number of players on the server.
|
||||
pub players_online: u8,
|
||||
/// Maximum number of players the server reports it can hold.
|
||||
pub players_maximum: u8,
|
||||
/// Number of bots on the server.
|
||||
pub players_bots: u8,
|
||||
/// Dedicated, NonDedicated or SourceTV
|
||||
pub server_type: Server,
|
||||
/// The Operating System that the server is on.
|
||||
pub environment_type: Environment,
|
||||
/// Indicates whether the server requires a password.
|
||||
pub has_password: bool,
|
||||
/// Indicates whether the server uses VAC.
|
||||
pub vac_secured: bool,
|
||||
/// [The ship](https://developer.valvesoftware.com/wiki/The_Ship) extra data
|
||||
pub the_ship: Option<TheShip>,
|
||||
/// Version of the game installed on the server.
|
||||
pub version: String,
|
||||
/// Some extra data that the server might provide or not.
|
||||
pub extra_data: Option<ExtraData>,
|
||||
/// GoldSrc only: Indicates whether the hosted game is a mod.
|
||||
pub is_mod: bool,
|
||||
/// GoldSrc only: If the game is a mod, provide additional data.
|
||||
pub mod_data: Option<ModData>,
|
||||
}
|
||||
|
||||
/// A server player.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
||||
pub struct ServerPlayer {
|
||||
/// Player's name.
|
||||
pub name: String,
|
||||
/// General score.
|
||||
pub score: u32,
|
||||
/// How long a player has been in the server (seconds).
|
||||
pub duration: f32,
|
||||
/// Only for [the ship](https://developer.valvesoftware.com/wiki/The_Ship): deaths count
|
||||
pub deaths: Option<u32>, // the_ship
|
||||
/// Only for [the ship](https://developer.valvesoftware.com/wiki/The_Ship): money amount
|
||||
pub money: Option<u32>, // the_ship
|
||||
}
|
||||
|
||||
/// Only present for [the ship](https://developer.valvesoftware.com/wiki/The_Ship).
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct TheShip {
|
||||
pub mode: u8,
|
||||
pub witnesses: u8,
|
||||
pub duration: u8,
|
||||
}
|
||||
|
||||
/// Some extra data that the server might provide or not.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ExtraData {
|
||||
/// The server's game port number.
|
||||
pub port: Option<u16>,
|
||||
/// Server's SteamID.
|
||||
pub steam_id: Option<u64>,
|
||||
/// SourceTV's port.
|
||||
pub tv_port: Option<u16>,
|
||||
/// SourceTV's name.
|
||||
pub tv_name: Option<String>,
|
||||
/// Keywords that describe the server according to it.
|
||||
pub keywords: Option<String>,
|
||||
/// The server's 64-bit GameID.
|
||||
pub game_id: Option<u64>,
|
||||
}
|
||||
|
||||
/// Data related to GoldSrc Mod response.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ModData {
|
||||
pub link: String,
|
||||
pub download_link: String,
|
||||
pub version: u32,
|
||||
pub size: u32,
|
||||
pub multiplayer_only: bool,
|
||||
pub has_own_dll: bool,
|
||||
}
|
||||
|
||||
pub(crate) type ExtractedData = (
|
||||
Option<u16>,
|
||||
Option<u64>,
|
||||
Option<u16>,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
);
|
||||
|
||||
pub(crate) fn get_optional_extracted_data(data: Option<ExtraData>) -> ExtractedData {
|
||||
match data {
|
||||
None => (None, None, None, None, None),
|
||||
Some(ed) => (ed.port, ed.steam_id, ed.tv_port, ed.tv_name, ed.keywords),
|
||||
}
|
||||
}
|
||||
|
||||
/// The type of the request, see the [protocol](https://developer.valvesoftware.com/wiki/Server_queries).
|
||||
#[derive(Eq, PartialEq, Copy, Clone)]
|
||||
#[repr(u8)]
|
||||
pub(crate) enum Request {
|
||||
/// Known as `A2S_INFO`
|
||||
Info = 0x54,
|
||||
/// Known as `A2S_PLAYERS`
|
||||
Players = 0x55,
|
||||
/// Known as `A2S_RULES`
|
||||
Rules = 0x56,
|
||||
}
|
||||
|
||||
/// Supported steam apps
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum SteamApp {
|
||||
/// Counter-Strike
|
||||
CS,
|
||||
/// Team Fortress Classic
|
||||
TFC,
|
||||
/// Day of Defeat
|
||||
DOD,
|
||||
/// Counter-Strike: Condition Zero
|
||||
CSCZ,
|
||||
/// Counter-Strike: Source
|
||||
CSS,
|
||||
/// Day of Defeat: Source
|
||||
DODS,
|
||||
/// Half-Life 2 Deathmatch
|
||||
HL2DM,
|
||||
/// Half-Life Deathmatch: Source
|
||||
HLDMS,
|
||||
/// Team Fortress 2
|
||||
TF2,
|
||||
/// Left 4 Dead
|
||||
L4D,
|
||||
/// Left 4 Dead
|
||||
L4D2,
|
||||
/// Alien Swarm
|
||||
ALIENS,
|
||||
/// Counter-Strike: Global Offensive
|
||||
CSGO,
|
||||
/// The Ship
|
||||
TS,
|
||||
/// Garry's Mod
|
||||
GM,
|
||||
/// Age of Chivalry
|
||||
AOC,
|
||||
/// Insurgency: Modern Infantry Combat
|
||||
INSMIC,
|
||||
/// ARMA 2: Operation Arrowhead
|
||||
ARMA2OA,
|
||||
/// Project Zomboid
|
||||
PZ,
|
||||
/// Insurgency
|
||||
INS,
|
||||
/// Sven Co-op
|
||||
SC,
|
||||
/// 7 Days To Die
|
||||
SDTD,
|
||||
/// Rust
|
||||
RUST,
|
||||
/// Vallistic Overkill
|
||||
BO,
|
||||
/// Don't Starve Together
|
||||
DST,
|
||||
/// BrainBread 2
|
||||
BB2,
|
||||
/// Codename CURE
|
||||
CCURE,
|
||||
/// Black Mesa
|
||||
BM,
|
||||
/// Colony Survival
|
||||
COSU,
|
||||
/// Avorion
|
||||
AVORION,
|
||||
/// Day of Infamy
|
||||
DOI,
|
||||
/// The Forest
|
||||
TF,
|
||||
/// Unturned
|
||||
UNTURNED,
|
||||
/// ARK: Survival Evolved
|
||||
ASE,
|
||||
/// Battalion 1944
|
||||
BAT1944,
|
||||
/// Insurgency: Sandstorm
|
||||
INSS,
|
||||
/// Alien Swarm: Reactive Drop
|
||||
ASRD,
|
||||
/// Risk of Rain 2
|
||||
ROR2,
|
||||
/// Operation: Harsh Doorstop
|
||||
OHD,
|
||||
/// Onset
|
||||
ONSET,
|
||||
/// V Rising
|
||||
VR,
|
||||
}
|
||||
|
||||
impl SteamApp {
|
||||
/// Get the specified app as engine.
|
||||
pub fn as_engine(&self) -> Engine {
|
||||
match self {
|
||||
SteamApp::CS => Engine::GoldSrc(false), // 10
|
||||
SteamApp::TFC => Engine::GoldSrc(false), // 20
|
||||
SteamApp::DOD => Engine::GoldSrc(false), // 30
|
||||
SteamApp::CSCZ => Engine::GoldSrc(false), // 80
|
||||
SteamApp::CSS => Engine::new_source(240),
|
||||
SteamApp::DODS => Engine::new_source(300),
|
||||
SteamApp::HL2DM => Engine::new_source(320),
|
||||
SteamApp::HLDMS => Engine::new_source(360),
|
||||
SteamApp::TF2 => Engine::new_source(440),
|
||||
SteamApp::L4D => Engine::new_source(500),
|
||||
SteamApp::L4D2 => Engine::new_source(550),
|
||||
SteamApp::ALIENS => Engine::new_source(630),
|
||||
SteamApp::CSGO => Engine::new_source(730),
|
||||
SteamApp::TS => Engine::new_source(2400),
|
||||
SteamApp::GM => Engine::new_source(4000),
|
||||
SteamApp::AOC => Engine::new_source(17510),
|
||||
SteamApp::INSMIC => Engine::new_source(17700),
|
||||
SteamApp::ARMA2OA => Engine::new_source(33930),
|
||||
SteamApp::PZ => Engine::new_source(108600),
|
||||
SteamApp::INS => Engine::new_source(222880),
|
||||
SteamApp::SC => Engine::GoldSrc(false), // 225840
|
||||
SteamApp::SDTD => Engine::new_source(251570),
|
||||
SteamApp::RUST => Engine::new_source(252490),
|
||||
SteamApp::BO => Engine::new_source(296300),
|
||||
SteamApp::DST => Engine::new_source(322320),
|
||||
SteamApp::BB2 => Engine::new_source(346330),
|
||||
SteamApp::CCURE => Engine::new_source(355180),
|
||||
SteamApp::BM => Engine::new_source(362890),
|
||||
SteamApp::COSU => Engine::new_source(366090),
|
||||
SteamApp::AVORION => Engine::new_source(445220),
|
||||
SteamApp::DOI => Engine::new_source(447820),
|
||||
SteamApp::TF => Engine::new_source(556450),
|
||||
SteamApp::UNTURNED => Engine::new_source(304930),
|
||||
SteamApp::ASE => Engine::new_source(346110),
|
||||
SteamApp::BAT1944 => Engine::new_source(489940),
|
||||
SteamApp::INSS => Engine::new_source(581320),
|
||||
SteamApp::ASRD => Engine::new_source(563560),
|
||||
SteamApp::ROR2 => Engine::new_source(632360),
|
||||
SteamApp::OHD => Engine::new_source_with_dedicated(736590, 950900),
|
||||
SteamApp::ONSET => Engine::new_source(1105810),
|
||||
SteamApp::VR => Engine::new_source(1604030),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Engine type.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub enum Engine {
|
||||
/// A Source game, the argument represents the possible steam app ids, if
|
||||
/// its **None**, let the query find it, if its **Some**, the query
|
||||
/// fails if the response id is not the first one, which is the game app
|
||||
/// id, or the other one, which is the dedicated server app id.
|
||||
Source(Option<(u32, Option<u32>)>),
|
||||
/// A GoldSrc game, the argument indicates whether to enforce
|
||||
/// requesting the obsolete A2S_INFO response or not.
|
||||
GoldSrc(bool),
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new_source(appid: u32) -> Self { Engine::Source(Some((appid, None))) }
|
||||
|
||||
pub fn new_source_with_dedicated(appid: u32, dedicated_appid: u32) -> Self {
|
||||
Engine::Source(Some((appid, Some(dedicated_appid))))
|
||||
}
|
||||
}
|
||||
|
||||
/// What data to gather, purely used only with the query function.
|
||||
pub struct GatheringSettings {
|
||||
pub players: bool,
|
||||
pub rules: bool,
|
||||
}
|
||||
|
||||
impl Default for GatheringSettings {
|
||||
/// Default values are true for both the players and the rules.
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
players: true,
|
||||
rules: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic response types that are used by many games, they are the protocol
|
||||
/// ones, but without the unnecessary bits (example: the **The Ship**-only
|
||||
/// fields).
|
||||
pub mod game {
|
||||
use super::{Server, ServerPlayer};
|
||||
use crate::protocols::valve::types::get_optional_extracted_data;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A player's details.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
||||
pub struct Player {
|
||||
/// Player's name.
|
||||
pub name: String,
|
||||
/// Player's score.
|
||||
pub score: u32,
|
||||
/// How long a player has been in the server (seconds).
|
||||
pub duration: f32,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
pub fn from_valve_response(player: &ServerPlayer) -> Self {
|
||||
Self {
|
||||
name: player.name.clone(),
|
||||
score: player.score,
|
||||
duration: player.duration,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The query response.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Response {
|
||||
/// Protocol used by the server.
|
||||
pub protocol: u8,
|
||||
/// Name of the server.
|
||||
pub name: String,
|
||||
/// Map name.
|
||||
pub map: String,
|
||||
/// The name of the game.
|
||||
pub game: String,
|
||||
/// Server's app id.
|
||||
pub appid: u32,
|
||||
/// Number of players on the server.
|
||||
pub players_online: u8,
|
||||
/// Details about the server's players (not all players necessarily).
|
||||
pub players_details: Vec<Player>,
|
||||
/// Maximum number of players the server reports it can hold.
|
||||
pub players_maximum: u8,
|
||||
/// Number of bots on the server.
|
||||
pub players_bots: u8,
|
||||
/// Dedicated, NonDedicated or SourceTV
|
||||
pub server_type: Server,
|
||||
/// Indicates whether the server requires a password.
|
||||
pub has_password: bool,
|
||||
/// Indicated whether the server uses VAC.
|
||||
pub vac_secured: bool,
|
||||
/// Version of the game installed on the server.
|
||||
pub version: String,
|
||||
/// The server's reported connection port.
|
||||
pub port: Option<u16>,
|
||||
/// Server's SteamID.
|
||||
pub steam_id: Option<u64>,
|
||||
/// SourceTV's connection port.
|
||||
pub tv_port: Option<u16>,
|
||||
/// SourceTV's name.
|
||||
pub tv_name: Option<String>,
|
||||
/// Keywords that describe the server according to it.
|
||||
pub keywords: Option<String>,
|
||||
/// Server's rules.
|
||||
pub rules: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new_from_valve_response(response: super::Response) -> Self {
|
||||
let (port, steam_id, tv_port, tv_name, keywords) = get_optional_extracted_data(response.info.extra_data);
|
||||
|
||||
Self {
|
||||
protocol: response.info.protocol,
|
||||
name: response.info.name,
|
||||
map: response.info.map,
|
||||
game: response.info.game,
|
||||
appid: response.info.appid,
|
||||
players_online: response.info.players_online,
|
||||
players_details: response
|
||||
.players
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.map(Player::from_valve_response)
|
||||
.collect(),
|
||||
players_maximum: response.info.players_maximum,
|
||||
players_bots: response.info.players_bots,
|
||||
server_type: response.info.server_type,
|
||||
has_password: response.info.has_password,
|
||||
vac_secured: response.info.vac_secured,
|
||||
version: response.info.version,
|
||||
port,
|
||||
steam_id,
|
||||
tv_port,
|
||||
tv_name,
|
||||
keywords,
|
||||
rules: response.rules.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue