Change Valve Protocol Rules to HashMap<String, String>

This commit is contained in:
CosminPerRam 2023-01-12 23:04:57 +02:00
parent d8aef7d9e5
commit 50012dd49f
4 changed files with 18 additions and 22 deletions

View file

@ -6,7 +6,7 @@ Who knows what the future holds...
[Risk of Rain 2](https://store.steampowered.com/app/632360/Risk_of_Rain_2/) support.
### Breaking:
Nothing (yet).
Valve Protocol - Rules is now a `HashMap<String, String>` instead of a `Vec<ServerRule>` (where the `ServerRule` structure had a name and value fields).
# 0.0.7 - 03/01/2023
### Changes:

View file

@ -1,6 +1,7 @@
use std::collections::HashMap;
use crate::GDResult;
use crate::protocols::valve;
use crate::protocols::valve::{Server, ServerRule, ServerPlayer, get_optional_extracted_data, SteamID};
use crate::protocols::valve::{Server, ServerPlayer, get_optional_extracted_data, SteamID};
#[derive(Debug)]
pub struct TheShipPlayer {
@ -42,7 +43,7 @@ pub struct Response {
pub tv_port: Option<u16>,
pub tv_name: Option<String>,
pub keywords: Option<String>,
pub rules: Vec<ServerRule>,
pub rules: HashMap<String, String>,
pub mode: u8,
pub witnesses: u8,
pub duration: u8

View file

@ -1,9 +1,10 @@
use std::collections::HashMap;
use bzip2_rs::decoder::Decoder;
use crate::{GDError, GDResult};
use crate::bufferer::{Bufferer, Endianess};
use crate::protocols::types::TimeoutSettings;
use crate::protocols::valve::{App, ModData, SteamID};
use crate::protocols::valve::types::{Environment, ExtraData, GatheringSettings, Request, Response, Server, ServerInfo, ServerPlayer, ServerRule, TheShip};
use crate::protocols::valve::types::{Environment, ExtraData, GatheringSettings, Request, Response, Server, ServerInfo, ServerPlayer, TheShip};
use crate::socket::{Socket, UdpSocket};
use crate::utils::u8_lower_upper;
@ -384,17 +385,17 @@ impl ValveProtocol {
}
/// Get the server's rules.
fn get_server_rules(&mut self, app: &App, protocol: u8) -> GDResult<Vec<ServerRule>> {
fn get_server_rules(&mut self, app: &App, protocol: u8) -> GDResult<HashMap<String, String>> {
let mut buffer = self.get_request_data(&app, protocol, Request::RULES)?;
let count = buffer.get_u16()? as usize;
let mut rules: Vec<ServerRule> = Vec::with_capacity(count);
let mut rules: HashMap<String, String> = HashMap::with_capacity(count);
for _ in 0..count {
rules.push(ServerRule {
name: buffer.get_string_utf8()?,
value: buffer.get_string_utf8()?
})
let name = buffer.get_string_utf8()?;
let value = buffer.get_string_utf8()?;
rules.insert(name, value);
}
Ok(rules)

View file

@ -1,3 +1,4 @@
use std::collections::HashMap;
/// The type of the server.
#[derive(Debug)]
@ -20,7 +21,7 @@ pub enum Environment {
pub struct Response {
pub info: ServerInfo,
pub players: Option<Vec<ServerPlayer>>,
pub rules: Option<Vec<ServerRule>>
pub rules: Option<HashMap<String, String>>
}
/// General server information's.
@ -79,13 +80,6 @@ pub struct ServerPlayer {
pub money: Option<u32>, //the_ship
}
/// A server rule.
#[derive(Debug)]
pub struct ServerRule {
pub name: String,
pub value: String
}
/// Only present for [the ship](https://developer.valvesoftware.com/wiki/The_Ship).
#[derive(Debug)]
pub struct TheShip {
@ -244,8 +238,9 @@ impl Default for GatheringSettings {
/// Generic response types that are used by many games, they are the protocol ones, but without the
/// unnecessary bits (example: the **The Ship**-only fields).
pub mod game {
use std::collections::HashMap;
use crate::protocols::valve::types::get_optional_extracted_data;
use super::{Server, ServerRule, ServerPlayer};
use super::{Server, ServerPlayer};
#[derive(Debug)]
pub struct Player {
@ -283,7 +278,7 @@ pub mod game {
pub tv_port: Option<u16>,
pub tv_name: Option<String>,
pub keywords: Option<String>,
pub rules: Vec<ServerRule>
pub rules: HashMap<String, String>
}
impl Response {
@ -308,9 +303,8 @@ pub mod game {
tv_port,
tv_name,
keywords,
rules: response.rules.unwrap_or(vec![])
rules: response.rules.unwrap_or(HashMap::new())
}
}
}
}