mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-05-06 15:27:28 +00:00
Changed uses to have a better structure
This commit is contained in:
parent
c0d07cf6f9
commit
3ac6a8b603
18 changed files with 113 additions and 97 deletions
|
|
@ -3,7 +3,8 @@ Who knows what the future holds...
|
|||
|
||||
# 0.0.4 - ??/??/????
|
||||
Queries now support DNS resolve.
|
||||
Changed Valve Protocol parameters to (ip, port, app, gather_settings), changes include:
|
||||
Changed uses a bit, from `use gamedig::valve::ValveProtocol::query` to `use gamedig::protocols::valve::query`.
|
||||
Changed Valve Protocol Query parameters to (ip, port, app, gather_settings), changes include:
|
||||
- the app is now optional, being None means to anonymously query the server.
|
||||
- gather_settings is now also an optional, being None means all query settings.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
use std::env;
|
||||
use gamedig::{aliens, asrd, csgo, css, dods, gm, hl2dm, ins, insmic, inss, l4d, l4d2, tf2, ts};
|
||||
use gamedig::valve::ValveProtocol;
|
||||
use gamedig::protocols::valve;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
|
@ -38,7 +38,7 @@ fn main() {
|
|||
"l4d" => println!("{:?}", l4d::query(ip, port)),
|
||||
"l4d2" => println!("{:?}", l4d2::query(ip, port)),
|
||||
"ts" => println!("{:?}", ts::query(ip, port)),
|
||||
"_" => println!("{:?}", ValveProtocol::query(ip, 27015, None, None)),
|
||||
"_" => println!("{:?}", valve::query(ip, 27015, None, None)),
|
||||
_ => panic!("Undefined game: {}", args[1])
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::ALIENS), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::ASRD), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, GatheringSettings, ServerPlayer, Server};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerPlayer, GatheringSettings};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -69,7 +70,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::CSGO), Some(GatheringSettings {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::CSS), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::DODS), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::GM), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::HL2DM), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::INS), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::INSMIC), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27131,
|
||||
Some(port) => port
|
||||
}, Some(App::INSS), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::L4D), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::L4D2), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -71,7 +72,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::TF2), None)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{GDResult, valve};
|
||||
use crate::valve::{ValveProtocol, App, ServerPlayer, Server, ServerRule};
|
||||
use crate::GDResult;
|
||||
use crate::protocols::valve;
|
||||
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
|
@ -83,7 +84,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(address, match port {
|
||||
let valve_response = valve::query(address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
}, Some(App::TS), None)?;
|
||||
|
|
|
|||
|
|
@ -21,5 +21,4 @@ pub mod games;
|
|||
mod utils;
|
||||
|
||||
pub use errors::*;
|
||||
pub use protocols::*;
|
||||
pub use games::*;
|
||||
|
|
|
|||
|
|
@ -162,13 +162,6 @@ pub struct GatheringSettings {
|
|||
pub rules: bool
|
||||
}
|
||||
|
||||
pub struct ValveProtocol {
|
||||
socket: UdpSocket,
|
||||
complete_address: String
|
||||
}
|
||||
|
||||
static DEFAULT_PACKET_SIZE: usize = 2048;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Packet {
|
||||
pub header: u32,
|
||||
|
|
@ -298,6 +291,13 @@ impl SplitPacket {
|
|||
}
|
||||
}
|
||||
|
||||
struct ValveProtocol {
|
||||
socket: UdpSocket,
|
||||
complete_address: String
|
||||
}
|
||||
|
||||
static DEFAULT_PACKET_SIZE: usize = 2048;
|
||||
|
||||
impl ValveProtocol {
|
||||
fn new(address: &str, port: u16) -> GDResult<Self> {
|
||||
Ok(Self {
|
||||
|
|
@ -342,7 +342,7 @@ impl ValveProtocol {
|
|||
}
|
||||
|
||||
/// Ask for a specific request only.
|
||||
pub fn get_request_data(&self, appid: u32, kind: Request) -> GDResult<Vec<u8>> {
|
||||
fn get_request_data(&self, appid: u32, kind: Request) -> GDResult<Vec<u8>> {
|
||||
let request_initial_packet = Packet::initial(kind.clone()).to_bytes();
|
||||
|
||||
self.send(&request_initial_packet)?;
|
||||
|
|
@ -360,7 +360,7 @@ impl ValveProtocol {
|
|||
}
|
||||
|
||||
/// Get the server information's.
|
||||
pub fn get_server_info(&self, initial_appid: u32) -> GDResult<ServerInfo> {
|
||||
fn get_server_info(&self, initial_appid: u32) -> GDResult<ServerInfo> {
|
||||
let buf = self.get_request_data(initial_appid, Request::INFO)?;
|
||||
let mut pos = 0;
|
||||
|
||||
|
|
@ -452,7 +452,7 @@ impl ValveProtocol {
|
|||
}
|
||||
|
||||
/// Get the server player's.
|
||||
pub fn get_server_players(&self, appid: u32) -> GDResult<Vec<ServerPlayer>> {
|
||||
fn get_server_players(&self, appid: u32) -> GDResult<Vec<ServerPlayer>> {
|
||||
let buf = self.get_request_data(appid, Request::PLAYERS)?;
|
||||
let mut pos = 0;
|
||||
|
||||
|
|
@ -480,7 +480,7 @@ impl ValveProtocol {
|
|||
}
|
||||
|
||||
/// Get the server rules's.
|
||||
pub fn get_server_rules(&self, appid: u32) -> GDResult<Option<Vec<ServerRule>>> {
|
||||
fn get_server_rules(&self, appid: u32) -> GDResult<Option<Vec<ServerRule>>> {
|
||||
if appid == App::CSGO as u32 { //cause csgo wont respond to this since feb 21 2014 update
|
||||
return Ok(None);
|
||||
}
|
||||
|
|
@ -500,44 +500,46 @@ impl ValveProtocol {
|
|||
|
||||
Ok(Some(rules))
|
||||
}
|
||||
|
||||
/// Query any app.
|
||||
pub fn query(address: &str, port: u16, app: Option<App>, gather_settings: Option<GatheringSettings>) -> Result<Response, GDError> {
|
||||
let client = ValveProtocol::new(address, port)?;
|
||||
|
||||
let mut query_app_id = match app {
|
||||
None => 0,
|
||||
Some(app) => app as u32
|
||||
};
|
||||
|
||||
let info = client.get_server_info(query_app_id)?;
|
||||
|
||||
if query_app_id != 0 {
|
||||
if info.appid != query_app_id {
|
||||
return Err(GDError::BadGame(format!("Expected {}, found {} instead!", query_app_id, info.appid)));
|
||||
}
|
||||
} else {
|
||||
query_app_id = info.appid;
|
||||
}
|
||||
|
||||
let (gather_players, gather_rules) = match gather_settings.is_some() {
|
||||
false => (true, true),
|
||||
true => {
|
||||
let settings = gather_settings.unwrap();
|
||||
(settings.players, settings.rules)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Response {
|
||||
info,
|
||||
players: match gather_players {
|
||||
false => None,
|
||||
true => Some(client.get_server_players(query_app_id)?)
|
||||
},
|
||||
rules: match gather_rules {
|
||||
false => None,
|
||||
true => client.get_server_rules(query_app_id)?
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Query a server, you need to provide the address, the port and optionally, the app and the
|
||||
/// gather settings, the app being *None* means to anonymously query the server, and the gather
|
||||
/// settings being *None* means to get the players and the rules.
|
||||
pub fn query(address: &str, port: u16, app: Option<App>, gather_settings: Option<GatheringSettings>) -> Result<Response, GDError> {
|
||||
let client = ValveProtocol::new(address, port)?;
|
||||
|
||||
let mut query_app_id = match app {
|
||||
None => 0,
|
||||
Some(app) => app as u32
|
||||
};
|
||||
|
||||
let info = client.get_server_info(query_app_id)?;
|
||||
|
||||
if query_app_id != 0 {
|
||||
if info.appid != query_app_id {
|
||||
return Err(GDError::BadGame(format!("Expected {}, found {} instead!", query_app_id, info.appid)));
|
||||
}
|
||||
} else {
|
||||
query_app_id = info.appid;
|
||||
}
|
||||
|
||||
let (gather_players, gather_rules) = match gather_settings.is_some() {
|
||||
false => (true, true),
|
||||
true => {
|
||||
let settings = gather_settings.unwrap();
|
||||
(settings.players, settings.rules)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Response {
|
||||
info,
|
||||
players: match gather_players {
|
||||
false => None,
|
||||
true => Some(client.get_server_players(query_app_id)?)
|
||||
},
|
||||
rules: match gather_rules {
|
||||
false => None,
|
||||
true => client.get_server_rules(query_app_id)?
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue