diff --git a/Cargo.toml b/Cargo.toml index 7447f84..557478a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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/" diff --git a/src/errors.rs b/src/errors.rs index 782886e..657b163 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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}"), } } } diff --git a/src/games/tf2.rs b/src/games/tf2.rs index 3b6ea8c..4f39798 100644 --- a/src/games/tf2.rs +++ b/src/games/tf2.rs @@ -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 }) } diff --git a/src/protocols/valve.rs b/src/protocols/valve.rs index fb08afe..84c8a8f 100644 --- a/src/protocols/valve.rs +++ b/src/protocols/valve.rs @@ -19,7 +19,7 @@ pub enum Environment { #[derive(Debug)] pub struct Response { - pub info: Option, + pub info: ServerInfo, pub players: Option, pub rules: Option } @@ -62,7 +62,7 @@ pub struct Player { #[derive(Debug)] pub struct ServerRules { pub count: u16, - pub rules: HashMap + pub map: HashMap } #[derive(Debug)] @@ -96,8 +96,20 @@ pub enum App { TheShip = 2400 } +impl TryFrom for App { + type Error = GDError; + + fn try_from(value: u16) -> Result { + 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::>()) } } @@ -276,7 +288,7 @@ impl ValveProtocol { }) } - fn get_server_rules(&self, app: &App) -> Result { + fn get_server_rules(&self) -> Result { 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 { 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()?) } }) }