rust-nex/rnex-core/src/rmc/structures/any.rs

98 lines
2.6 KiB
Rust
Raw Normal View History

2026-04-28 13:23:26 +02:00
use log::warn;
2026-02-01 20:59:23 +01:00
use rnex_core::rmc::structures::{Result, RmcSerialize};
2026-04-25 14:15:42 +02:00
use std::io::{Cursor, Read, Write};
2025-09-21 15:59:27 +02:00
use v_byte_helpers::{IS_BIG_ENDIAN, ReadExtensions};
2025-01-26 12:09:56 +01:00
2026-04-25 14:15:42 +02:00
#[derive(Debug, Default, Clone)]
2026-02-01 20:59:23 +01:00
pub struct Any {
2025-01-26 12:09:56 +01:00
pub name: String,
2026-02-01 20:59:23 +01:00
pub data: Vec<u8>,
2025-01-26 12:09:56 +01:00
}
2026-02-01 20:59:23 +01:00
impl RmcSerialize for Any {
2025-11-12 22:41:34 +01:00
fn serialize(&self, writer: &mut impl Write) -> Result<()> {
self.name.serialize(writer)?;
let u32_len = self.data.len() as u32;
2026-04-25 14:15:42 +02:00
(u32_len + 4).serialize(writer)?;
self.data.serialize(writer)?;
Ok(())
}
2025-11-13 10:06:58 +01:00
fn deserialize(reader: &mut impl Read) -> Result<Self> {
let name = String::deserialize(reader)?;
2025-01-26 12:09:56 +01:00
2026-04-25 20:49:31 +02:00
let data = Vec::deserialize(reader)?;
2026-04-28 13:23:26 +02:00
let mut cursor = Cursor::new(&data);
// also length ?
let len2: u32 = cursor.read_struct(IS_BIG_ENDIAN)?;
if len2 as usize != data.len().overflowing_sub(4).0 {
warn!(
"mismatched sizes on any: {} vs {}",
data.len().overflowing_sub(4).0,
len2
);
}
2025-01-26 12:09:56 +01:00
2026-04-28 13:23:26 +02:00
Ok(Any {
name,
data: (&data[4..]).to_owned(),
})
}
2026-02-01 20:59:23 +01:00
}
2026-04-25 14:15:42 +02:00
impl Any {
pub fn try_get<T: RmcSerialize>(&self) -> Option<Result<T>> {
if self.name != T::name() {
return None;
}
return Some(T::deserialize(&mut Cursor::new(&self.data[..])));
}
pub fn new<T: RmcSerialize>(val: &T) -> Result<Self> {
return Ok(Self {
name: T::name().to_owned(),
data: val.to_data()?,
});
}
}
2026-04-28 13:23:26 +02:00
#[cfg(test)]
mod test {
use crate::rmc::structures::{
any::Any,
matchmake::{Gathering, MatchmakeSession},
};
#[test]
fn test() {
let sess = MatchmakeSession {
gathering: Gathering {
self_gid: 0,
owner_pid: 0,
host_pid: 0,
minimum_participants: 2,
maximum_participants: 2,
participant_policy: 98,
policy_argument: 0,
flags: 32,
state: 0,
description: "Doors Friend Invite".into(),
},
gamemode: 0,
attributes: [2, 3, 0, 0, 0, 0].into(),
open_participation: false,
matchmake_system_type: 2,
application_buffer: [1, 2, 3].into(),
participation_count: 0,
progress_score: 0,
session_key: [].into(),
};
let any = Any::new(&sess).unwrap();
let sess2: MatchmakeSession = any.try_get().unwrap().unwrap();
assert_eq!(sess, sess2);
}
}