mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
refator: copy cli into mono
This commit is contained in:
parent
66ae3c296e
commit
80f6b87991
63 changed files with 244 additions and 34 deletions
119
crates/lib/examples/generic.rs
Normal file
119
crates/lib/examples/generic.rs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
use gamedig::{
|
||||
protocols::types::{CommonResponse, ExtraRequestSettings, TimeoutSettings},
|
||||
query_with_timeout_and_extra_settings,
|
||||
GDResult,
|
||||
GAMES,
|
||||
};
|
||||
|
||||
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
|
||||
|
||||
/// Make a query given the name of a game
|
||||
fn generic_query(
|
||||
game_name: &str,
|
||||
addr: &IpAddr,
|
||||
port: Option<u16>,
|
||||
timeout_settings: Option<TimeoutSettings>,
|
||||
extra_settings: Option<ExtraRequestSettings>,
|
||||
) -> GDResult<Box<dyn CommonResponse>> {
|
||||
let game = GAMES
|
||||
.get(game_name)
|
||||
.expect("Game doesn't exist, run without arguments to see a list of games");
|
||||
|
||||
println!("Querying {:#?} with game {:#?}.", addr, game);
|
||||
|
||||
let response = query_with_timeout_and_extra_settings(game, addr, port, timeout_settings, extra_settings)?;
|
||||
println!("Response: {:#?}", response.as_json());
|
||||
|
||||
let common = response.as_original();
|
||||
println!("Common response: {:#?}", common);
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut args = std::env::args().skip(1);
|
||||
|
||||
// Handle arguments
|
||||
if let Some(game_name) = args.next() {
|
||||
let hostname = args.next().expect("Must provide an address");
|
||||
// Use to_socket_addrs to resolve hostname to IP
|
||||
let addr: SocketAddr = format!("{}:0", hostname)
|
||||
.to_socket_addrs()
|
||||
.unwrap()
|
||||
.next()
|
||||
.expect("Could not lookup host");
|
||||
let port: Option<u16> = args.next().map(|s| s.parse().unwrap());
|
||||
|
||||
let timeout_settings = TimeoutSettings::new(
|
||||
TimeoutSettings::default().get_read(),
|
||||
TimeoutSettings::default().get_write(),
|
||||
2,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let extra_settings = ExtraRequestSettings::default()
|
||||
.set_hostname(hostname.to_string())
|
||||
.set_gather_rules(true)
|
||||
.set_gather_players(true)
|
||||
.set_check_app_id(false);
|
||||
|
||||
generic_query(
|
||||
&game_name,
|
||||
&addr.ip(),
|
||||
port,
|
||||
Some(timeout_settings),
|
||||
Some(extra_settings),
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
// Without arguments print a list of games
|
||||
|
||||
for (name, game) in gamedig::games::GAMES.entries() {
|
||||
println!("{}\t{}", name, game.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use gamedig::{protocols::types::TimeoutSettings, GAMES};
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use super::generic_query;
|
||||
|
||||
const ADDR: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
|
||||
|
||||
fn test_game(game_name: &str) {
|
||||
let timeout_settings = Some(
|
||||
TimeoutSettings::new(
|
||||
Some(Duration::from_nanos(1)),
|
||||
Some(Duration::from_nanos(1)),
|
||||
0,
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
assert!(generic_query(game_name, &ADDR, None, timeout_settings, None).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn battlefield1942() { test_game("battlefield1942"); }
|
||||
|
||||
#[test]
|
||||
fn minecraft() { test_game("minecraft"); }
|
||||
|
||||
#[test]
|
||||
fn teamfortress2() { test_game("teamfortress2"); }
|
||||
|
||||
#[test]
|
||||
fn quake() { test_game("quake3"); }
|
||||
|
||||
#[test]
|
||||
fn all_games() {
|
||||
for game_name in GAMES.keys() {
|
||||
test_game(game_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
crates/lib/examples/minecraft.rs
Normal file
31
crates/lib/examples/minecraft.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use gamedig::games::minecraft;
|
||||
use gamedig::protocols::minecraft::RequestSettings;
|
||||
|
||||
fn main() {
|
||||
// or Some(<port>), None is the default protocol port (which is 25565 for java
|
||||
// and 19132 for bedrock)
|
||||
let response = minecraft::query(&"127.0.0.1".parse().unwrap(), None);
|
||||
// This will fail if no server is available locally!
|
||||
|
||||
match response {
|
||||
Err(error) => println!("Couldn't query, error: {}", error),
|
||||
Ok(r) => println!("{:#?}", r),
|
||||
}
|
||||
|
||||
// This is an example to query a server with a hostname to be specified in the
|
||||
// packet. Passing -1 on the protocol_version means anything, note that
|
||||
// an invalid value here might result in server not responding.
|
||||
let response = minecraft::query_java(
|
||||
&"209.222.114.62".parse().unwrap(),
|
||||
Some(25565),
|
||||
Some(RequestSettings {
|
||||
hostname: "mc.hypixel.net".to_string(),
|
||||
protocol_version: -1,
|
||||
}),
|
||||
);
|
||||
|
||||
match response {
|
||||
Err(error) => println!("Couldn't query, error: {}", error),
|
||||
Ok(r) => println!("{:#?}", r),
|
||||
}
|
||||
}
|
||||
12
crates/lib/examples/teamfortress2.rs
Normal file
12
crates/lib/examples/teamfortress2.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use gamedig::games::teamfortress2;
|
||||
|
||||
fn main() {
|
||||
let response = teamfortress2::query(&"127.0.0.1".parse().unwrap(), None);
|
||||
// or Some(27015), None is the default protocol port (which is 27015)
|
||||
|
||||
match response {
|
||||
// Result type, must check what it is...
|
||||
Err(error) => println!("Couldn't query, error: {}", error),
|
||||
Ok(r) => println!("{:#?}", r),
|
||||
}
|
||||
}
|
||||
14
crates/lib/examples/valve_master_server_query.rs
Normal file
14
crates/lib/examples/valve_master_server_query.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use gamedig::valve_master_server::{query, Filter, Region, SearchFilters};
|
||||
|
||||
fn main() {
|
||||
let search_filters = SearchFilters::new()
|
||||
.insert(Filter::RunsAppID(440))
|
||||
.insert(Filter::CanBeEmpty(false))
|
||||
.insert(Filter::CanBeFull(false))
|
||||
.insert(Filter::CanHavePassword(false))
|
||||
.insert(Filter::IsSecured(true))
|
||||
.insert(Filter::HasTags(vec!["minecraft".to_string()]));
|
||||
|
||||
let ips = query(Region::Europe, Some(search_filters)).unwrap();
|
||||
println!("Servers: {:?} \n Amount: {}", ips, ips.len());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue