Specified MIT license and made the valve protocol check if the queried game is the right game

This commit is contained in:
CosminPerRam 2022-10-18 18:20:37 +03:00
parent 1cb00f826a
commit 192d50a11d
4 changed files with 34 additions and 18 deletions

View file

@ -3,7 +3,7 @@ name = "gamedig"
version = "0.0.1"
edition = "2021"
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."
homepage = "https://github.com/CosminPerRam/rust-gamedig"
documentation = "https://docs.rs/gamedig/latest/gamedig/"

View file

@ -7,7 +7,9 @@ pub enum GDError {
PacketUnderflow(String),
PacketBad(String),
PacketSend(String),
PacketReceive(String)
PacketReceive(String),
UnknownEnumCast,
BadGame(String)
}
impl fmt::Display for GDError {
@ -17,7 +19,9 @@ impl fmt::Display for GDError {
GDError::PacketUnderflow(details) => write!(f, "Packet underflow: {details}"),
GDError::PacketBad(details) => write!(f, "Packet bad: {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}"),
}
}
}

View file

@ -1,5 +1,5 @@
use crate::errors::GDError;
use crate::valve::{Response, ValveProtocol, App, GatheringSettings};
use crate::valve::{ValveProtocol, App, GatheringSettings, Response};
pub struct TF2;
@ -9,8 +9,7 @@ impl TF2 {
None => 27015,
Some(port) => port
}, GatheringSettings {
info: false,
players: false,
players: true,
rules: true
})
}

View file

@ -19,7 +19,7 @@ pub enum Environment {
#[derive(Debug)]
pub struct Response {
pub info: Option<ServerInfo>,
pub info: ServerInfo,
pub players: Option<ServerPlayers>,
pub rules: Option<ServerRules>
}
@ -62,7 +62,7 @@ pub struct Player {
#[derive(Debug)]
pub struct ServerRules {
pub count: u16,
pub rules: HashMap<String, String>
pub map: HashMap<String, String>
}
#[derive(Debug)]
@ -96,8 +96,20 @@ pub enum App {
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 info: bool,
pub players: bool,
pub rules: bool
}
@ -171,11 +183,11 @@ impl ValveProtocol {
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'
self.receive_truncated(&packet)
} 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 mut pos = 0;
@ -290,25 +302,26 @@ impl ValveProtocol {
Ok(ServerRules {
count,
rules
map: rules
})
}
pub(crate) fn query(app: App, address: &str, port: u16, gather: GatheringSettings) -> Result<Response, GDError> {
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 {
info: match gather.info {
false => None,
true => Some(client.get_server_info(&app)?)
},
info,
players: match gather.players {
false => None,
true => Some(client.get_server_players(&app)?)
},
rules: match gather.rules {
false => None,
true => Some(client.get_server_rules(&app)?)
true => Some(client.get_server_rules()?)
}
})
}