mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
Specified MIT license and made the valve protocol check if the queried game is the right game
This commit is contained in:
parent
1cb00f826a
commit
192d50a11d
4 changed files with 34 additions and 18 deletions
|
|
@ -3,7 +3,7 @@ name = "gamedig"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["CosminPerRam [cosmin.p@live.com]", "node-GameDig [https://github.com/gamedig/node-gamedig]"]
|
authors = ["CosminPerRam [cosmin.p@live.com]", "node-GameDig [https://github.com/gamedig/node-gamedig]"]
|
||||||
license-file = "LICENSE.md"
|
license = "MIT"
|
||||||
description = "Check out servers with this."
|
description = "Check out servers with this."
|
||||||
homepage = "https://github.com/CosminPerRam/rust-gamedig"
|
homepage = "https://github.com/CosminPerRam/rust-gamedig"
|
||||||
documentation = "https://docs.rs/gamedig/latest/gamedig/"
|
documentation = "https://docs.rs/gamedig/latest/gamedig/"
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,9 @@ pub enum GDError {
|
||||||
PacketUnderflow(String),
|
PacketUnderflow(String),
|
||||||
PacketBad(String),
|
PacketBad(String),
|
||||||
PacketSend(String),
|
PacketSend(String),
|
||||||
PacketReceive(String)
|
PacketReceive(String),
|
||||||
|
UnknownEnumCast,
|
||||||
|
BadGame(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for GDError {
|
impl fmt::Display for GDError {
|
||||||
|
|
@ -17,7 +19,9 @@ impl fmt::Display for GDError {
|
||||||
GDError::PacketUnderflow(details) => write!(f, "Packet underflow: {details}"),
|
GDError::PacketUnderflow(details) => write!(f, "Packet underflow: {details}"),
|
||||||
GDError::PacketBad(details) => write!(f, "Packet bad: {details}"),
|
GDError::PacketBad(details) => write!(f, "Packet bad: {details}"),
|
||||||
GDError::PacketSend(details) => write!(f, "Couldn't send a packet: {details}"),
|
GDError::PacketSend(details) => write!(f, "Couldn't send a packet: {details}"),
|
||||||
GDError::PacketReceive(details) => write!(f, "Couldn't receive a packet: {details}")
|
GDError::PacketReceive(details) => write!(f, "Couldn't receive a packet: {details}"),
|
||||||
|
GDError::UnknownEnumCast => write!(f, "Unknown enum cast encountered."),
|
||||||
|
GDError::BadGame(details) => write!(f, "Queried another game that the supposed one: {details}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::errors::GDError;
|
use crate::errors::GDError;
|
||||||
use crate::valve::{Response, ValveProtocol, App, GatheringSettings};
|
use crate::valve::{ValveProtocol, App, GatheringSettings, Response};
|
||||||
|
|
||||||
pub struct TF2;
|
pub struct TF2;
|
||||||
|
|
||||||
|
|
@ -9,8 +9,7 @@ impl TF2 {
|
||||||
None => 27015,
|
None => 27015,
|
||||||
Some(port) => port
|
Some(port) => port
|
||||||
}, GatheringSettings {
|
}, GatheringSettings {
|
||||||
info: false,
|
players: true,
|
||||||
players: false,
|
|
||||||
rules: true
|
rules: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ pub enum Environment {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub info: Option<ServerInfo>,
|
pub info: ServerInfo,
|
||||||
pub players: Option<ServerPlayers>,
|
pub players: Option<ServerPlayers>,
|
||||||
pub rules: Option<ServerRules>
|
pub rules: Option<ServerRules>
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ pub struct Player {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ServerRules {
|
pub struct ServerRules {
|
||||||
pub count: u16,
|
pub count: u16,
|
||||||
pub rules: HashMap<String, String>
|
pub map: HashMap<String, String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -96,8 +96,20 @@ pub enum App {
|
||||||
TheShip = 2400
|
TheShip = 2400
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<u16> for App {
|
||||||
|
type Error = GDError;
|
||||||
|
|
||||||
|
fn try_from(value: u16) -> Result<Self, Self::Error> {
|
||||||
|
match value {
|
||||||
|
x if x == App::TF2 as u16 => Ok(App::TF2),
|
||||||
|
x if x == App::TF2 as u16 => Ok(App::CSGO),
|
||||||
|
x if x == App::TF2 as u16 => Ok(App::TheShip),
|
||||||
|
_ => Err(GDError::UnknownEnumCast),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct GatheringSettings {
|
pub struct GatheringSettings {
|
||||||
pub info: bool,
|
|
||||||
pub players: bool,
|
pub players: bool,
|
||||||
pub rules: bool
|
pub rules: bool
|
||||||
}
|
}
|
||||||
|
|
@ -171,11 +183,11 @@ impl ValveProtocol {
|
||||||
|
|
||||||
self.send(&challenge_packet)?;
|
self.send(&challenge_packet)?;
|
||||||
|
|
||||||
let packet = self.receive(DEFAULT_PACKET_SIZE)?;
|
let mut packet = self.receive(DEFAULT_PACKET_SIZE)?;
|
||||||
if packet[0] == 0xFE || (packet[0] == 0xFF && packet[4] == 0x45) { //'E'
|
if packet[0] == 0xFE || (packet[0] == 0xFF && packet[4] == 0x45) { //'E'
|
||||||
self.receive_truncated(&packet)
|
self.receive_truncated(&packet)
|
||||||
} else {
|
} else {
|
||||||
Ok(packet)
|
Ok(packet.drain(5..).collect::<Vec<u8>>())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -276,7 +288,7 @@ impl ValveProtocol {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_server_rules(&self, app: &App) -> Result<ServerRules, GDError> {
|
fn get_server_rules(&self) -> Result<ServerRules, GDError> {
|
||||||
let buf = self.get_request_data(Request::RULES)?;
|
let buf = self.get_request_data(Request::RULES)?;
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
|
|
||||||
|
|
@ -290,25 +302,26 @@ impl ValveProtocol {
|
||||||
|
|
||||||
Ok(ServerRules {
|
Ok(ServerRules {
|
||||||
count,
|
count,
|
||||||
rules
|
map: rules
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn query(app: App, address: &str, port: u16, gather: GatheringSettings) -> Result<Response, GDError> {
|
pub(crate) fn query(app: App, address: &str, port: u16, gather: GatheringSettings) -> Result<Response, GDError> {
|
||||||
let client = ValveProtocol::new(address, port);
|
let client = ValveProtocol::new(address, port);
|
||||||
|
|
||||||
|
let info = client.get_server_info(&app)?;
|
||||||
|
|
||||||
|
App::try_from(info.id).map_err(|_| GDError::BadGame(format!("Found {} instead!", info.id)))?;
|
||||||
|
|
||||||
Ok(Response {
|
Ok(Response {
|
||||||
info: match gather.info {
|
info,
|
||||||
false => None,
|
|
||||||
true => Some(client.get_server_info(&app)?)
|
|
||||||
},
|
|
||||||
players: match gather.players {
|
players: match gather.players {
|
||||||
false => None,
|
false => None,
|
||||||
true => Some(client.get_server_players(&app)?)
|
true => Some(client.get_server_players(&app)?)
|
||||||
},
|
},
|
||||||
rules: match gather.rules {
|
rules: match gather.rules {
|
||||||
false => None,
|
false => None,
|
||||||
true => Some(client.get_server_rules(&app)?)
|
true => Some(client.get_server_rules()?)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue