mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Protocol] Implement generic response with dyn (#56)
* Implement generic response as enum * First draft of implementing into_common() * Make common response type generic * Use macros and generics to reduce repetition * [Games] Add dynamically dispatched CommonResponse trait This adds two traits: "CommonResponse", and "CommonPlayer", when the generic game query function returns a response it returns a pointer to its original response type that implements "CommonResponse". Both common traits require that "as_original()" be implemented, this returns an enum containing a pointer to the original type. Both traits have a concrete method "as_json()" that returns a struct containing data fetched from all of its methods as. This struct implements serde and can hence be serialized as required. The traits require a few other methods be implemented, those being the fields that are common across all types. All other methods have a default None implementation so that each response type only needs to implement methods for fields that it has. * [Game] Implement common traits for JCMP2 response * [Fmt] Run cargo fmt * Fix doctest failing * Run cargo fmt
This commit is contained in:
parent
bf14ecb4a4
commit
b368877031
17 changed files with 405 additions and 430 deletions
|
|
@ -114,6 +114,7 @@ pub mod vr;
|
|||
|
||||
use crate::protocols::gamespy::GameSpyVersion;
|
||||
use crate::protocols::quake::QuakeVersion;
|
||||
use crate::protocols::types::CommonResponse;
|
||||
use crate::protocols::{self, Protocol};
|
||||
use crate::GDResult;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
|
|
@ -137,42 +138,42 @@ mod definitions;
|
|||
pub use definitions::GAMES;
|
||||
|
||||
/// Make a query given a game definition
|
||||
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<protocols::GenericResponse> {
|
||||
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<Box<dyn CommonResponse>> {
|
||||
let socket_addr = SocketAddr::new(*address, port.unwrap_or(game.default_port));
|
||||
Ok(match &game.protocol {
|
||||
Protocol::Valve(steam_app) => {
|
||||
protocols::valve::query(&socket_addr, steam_app.as_engine(), None, None).map(|r| r.into())?
|
||||
protocols::valve::query(&socket_addr, steam_app.as_engine(), None, None).map(Box::new)?
|
||||
}
|
||||
Protocol::Minecraft(version) => {
|
||||
match version {
|
||||
Some(protocols::minecraft::Server::Java) => {
|
||||
protocols::minecraft::query_java(&socket_addr, None).map(|r| r.into())?
|
||||
protocols::minecraft::query_java(&socket_addr, None).map(Box::new)?
|
||||
}
|
||||
Some(protocols::minecraft::Server::Bedrock) => {
|
||||
protocols::minecraft::query_bedrock(&socket_addr, None).map(|r| r.into())?
|
||||
protocols::minecraft::query_bedrock(&socket_addr, None).map(Box::new)?
|
||||
}
|
||||
Some(protocols::minecraft::Server::Legacy(group)) => {
|
||||
protocols::minecraft::query_legacy_specific(*group, &socket_addr, None).map(|r| r.into())?
|
||||
protocols::minecraft::query_legacy_specific(*group, &socket_addr, None).map(Box::new)?
|
||||
}
|
||||
None => protocols::minecraft::query(&socket_addr, None).map(|r| r.into())?,
|
||||
None => protocols::minecraft::query(&socket_addr, None).map(Box::new)?,
|
||||
}
|
||||
}
|
||||
Protocol::Gamespy(version) => {
|
||||
match version {
|
||||
GameSpyVersion::One => protocols::gamespy::one::query(&socket_addr, None).map(|r| r.into())?,
|
||||
GameSpyVersion::Two => protocols::gamespy::two::query(&socket_addr, None).map(|r| r.into())?,
|
||||
GameSpyVersion::Three => protocols::gamespy::three::query(&socket_addr, None).map(|r| r.into())?,
|
||||
GameSpyVersion::One => protocols::gamespy::one::query(&socket_addr, None).map(Box::new)?,
|
||||
GameSpyVersion::Two => protocols::gamespy::two::query(&socket_addr, None).map(Box::new)?,
|
||||
GameSpyVersion::Three => protocols::gamespy::three::query(&socket_addr, None).map(Box::new)?,
|
||||
}
|
||||
}
|
||||
Protocol::Quake(version) => {
|
||||
match version {
|
||||
QuakeVersion::One => protocols::quake::one::query(&socket_addr, None).map(|r| r.into())?,
|
||||
QuakeVersion::Two => protocols::quake::two::query(&socket_addr, None).map(|r| r.into())?,
|
||||
QuakeVersion::Three => protocols::quake::three::query(&socket_addr, None).map(|r| r.into())?,
|
||||
QuakeVersion::One => protocols::quake::one::query(&socket_addr, None).map(Box::new)?,
|
||||
QuakeVersion::Two => protocols::quake::two::query(&socket_addr, None).map(Box::new)?,
|
||||
QuakeVersion::Three => protocols::quake::three::query(&socket_addr, None).map(Box::new)?,
|
||||
}
|
||||
}
|
||||
Protocol::TheShip => ts::query(address, port).map(|r| r.into())?,
|
||||
Protocol::FFOW => ffow::query(address, port).map(|r| r.into())?,
|
||||
Protocol::JC2MP => jc2mp::query(address, port).map(|r| r.into())?,
|
||||
Protocol::TheShip => ts::query(address, port).map(Box::new)?,
|
||||
Protocol::FFOW => ffow::query(address, port).map(Box::new)?,
|
||||
Protocol::JC2MP => jc2mp::query(address, port).map(Box::new)?,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue