diff --git a/Cargo.toml b/Cargo.toml index 4c4f940..fc2e43a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,13 +2,16 @@ name = "gamedig" version = "0.0.0" edition = "2021" -authors = ["CosminPerRam [cosmin.p@live.com]", "mmorrisontx [https://github.com/mmorrisontx]"] +authors = ["CosminPerRam [cosmin.p@live.com]", "node-GameDig [https://github.com/gamedig/node-gamedig]"] license-file = "LICENSE.md" description = "Check out servers with this." homepage = "https://github.com/CosminPerRam/rust-gamedig" -documentation = "https://github.com/CosminPerRam/rust-gamedig" +documentation = "https://docs.rs/gamedig/latest/gamedig/" repository = "https://github.com/CosminPerRam/rust-gamedig" readme = "README.md" keywords = ["server", "valve", "games", "checker", "status"] +[package.metadata] +msrv = "1.58.1" + [dependencies] diff --git a/GAMES.md b/GAMES.md new file mode 100644 index 0000000..13fd16b --- /dev/null +++ b/GAMES.md @@ -0,0 +1,7 @@ +# Supported games: +| Type ID | Name | Notes | +|---------|-----------------|-------| +| TF2 | Team Fortress 2 | | + +# Planned to add support: +All Valve titles. diff --git a/README.md b/README.md index c4c8038..4be53e8 100644 --- a/README.md +++ b/README.md @@ -1 +1,31 @@ -# rust-gamedig \ No newline at end of file +# rust-gamedig +rust-GameDig is a game server/services query library, capable of querying the status of many games/services, this library brings what [node-GameDig](https://github.com/gamedig/node-gamedig) does, to pure Rust! + +MSRV is `1.58.1` and the code is cross-platform. + +# Example +Basic usage of the library is: +```rust +use gamedig::TF2; + +fn main() { + let response = TF2::query("91.216.250.10", None); + //query your favorite game/protocol/service, some might come with different parameters + //here its just the IP and the port (if None, its gonna be the default from the protocol) + + match response { + Err(error) => println!("Couldn't query, error: {error}"), + Ok(r) => println!("{:?}", r) + } +} +``` +To see more examples, see the [examples](examples) folder. + +# Documentation +The documentation is available at [docs.rs](https://docs.rs/gamedig/latest/gamedig/). + +# Games List +To see the supported games, see [GAMES](GAMES.md). + +# Contributing +If you want see your favorite game/service being supported here, open an issue (or do a pull request if you want to implement it yourself)! diff --git a/examples/tf2.rs b/examples/tf2.rs index 1f8b659..e77a699 100644 --- a/examples/tf2.rs +++ b/examples/tf2.rs @@ -4,7 +4,7 @@ use gamedig::TF2; fn main() { let response = TF2::query("91.216.250.10", None); match response { - Err(error) => println!("Couldn't query, error: {}", error), + Err(error) => println!("Couldn't query, error: {error}"), Ok(r) => println!("{:?}", r) } } diff --git a/src/protocols/valve.rs b/src/protocols/valve.rs index 64f9fcc..42e8151 100644 --- a/src/protocols/valve.rs +++ b/src/protocols/valve.rs @@ -62,7 +62,7 @@ pub struct ValveProtocol { complete_address: String } -static default_packet_size: usize = 256; +static DEFAULT_PACKET_SIZE: usize = 256; impl ValveProtocol { fn new(address: &str, port: u16) -> Self { @@ -95,11 +95,7 @@ impl ValveProtocol { } } - pub fn receive(&self) -> Vec { - self.receive_with_size(64) - } - - pub fn receive_with_size(&self, buffer_size: usize) -> Vec { + pub fn receive(&self, buffer_size: usize) -> Vec { let mut buffer: Vec = vec![0; buffer_size]; let (amt, _) = self.socket.recv_from(&mut buffer.as_mut_slice()).unwrap(); buffer[..amt].to_vec() @@ -111,11 +107,11 @@ impl ValveProtocol { let client = ValveProtocol::new(address, port); client.do_request(Request::A2sInfo(None), None); - let mut buf = client.receive_with_size(default_packet_size); + let mut buf = client.receive(DEFAULT_PACKET_SIZE); if buf[4] == 0x41 { client.do_request(Request::A2sInfo(Some([buf[5], buf[6], buf[7], buf[8]])), None); - buf = client.receive_with_size(default_packet_size); + buf = client.receive(DEFAULT_PACKET_SIZE); } println!("{:x?}", &buf); diff --git a/src/utils.rs b/src/utils.rs index 2428578..496a23d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -26,3 +26,32 @@ pub fn combine_eight_u8(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8, g: u8, h: u8) pub fn get_u64_from_buf(buf: &[u8]) -> u64 { combine_eight_u8(buf[7], buf[6], buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]) } + +#[cfg(test)] +mod utils { + use super::*; + + #[test] + fn concat_u8_test() { + let a: [u8; 2] = [1, 2]; + let b: [u8; 2] = [3, 4]; + let combined = concat_u8(&a, &b); + assert_eq!(a[0], combined[0]); + assert_eq!(a[1], combined[1]); + assert_eq!(b[0], combined[2]); + assert_eq!(b[1], combined[3]); + } + + #[test] + fn find_null_in_array_test() { + let arr: [u8; 4] = [0x64, 0x32, 0x00, 0x20]; + assert_eq!(2, find_null_in_array(&arr)); + } + + #[test] + fn complete_address_test() { + let address = "192.168.0.1"; + let port = 27015; + assert_eq!(complete_address(address, port), "192.168.0.1:27015"); + } +} diff --git a/tests/test.rs b/tests/test.rs deleted file mode 100644 index 776744b..0000000 --- a/tests/test.rs +++ /dev/null @@ -1,15 +0,0 @@ - -#[cfg(test)] -mod tests { - use gamedig::protocols::valve::ValveProtocol; - - #[test] - fn tf2() { - let response = ValveProtocol::query("5.15.202.107", 27015); - match response { - Err(_) => println!("fuck"), - Ok(r) => println!("{}", r.name) - } - assert_eq!(4, 4); - } -}