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

102 lines
3 KiB
Rust
Raw Normal View History

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)?;
2026-04-28 15:07:33 +02:00
let u32_len = self.data.len() as u32;
2026-04-25 14:15:42 +02:00
(u32_len + 4).serialize(writer)?;
2026-04-28 15:07:33 +02:00
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-28 15:07:33 +02:00
// also length ?
let _len2: u32 = reader.read_struct(IS_BIG_ENDIAN)?;
let data = Vec::deserialize(reader)?;
2026-04-28 14:52:19 +02:00
2026-04-28 15:07:33 +02:00
Ok(Any { name, data })
}
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::{
2026-04-28 13:37:00 +02:00
RmcSerialize,
2026-04-28 13:23:26 +02:00
any::Any,
matchmake::{Gathering, MatchmakeSession},
};
#[test]
fn test() {
2026-04-28 15:07:33 +02:00
let any = Any {
name: "MatchmakeSession".into(),
data: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 98, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0,
0, 0, 0, 0, 0, 20, 0, 68, 111, 111, 114, 115, 32, 70, 114, 105, 101, 110, 100, 32,
73, 110, 118, 105, 116, 101, 0, 0, 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 2, 3, 0,
0, 0, 0, 0, 0, 0, 0, 0,
]
.into(),
};
println!("{}", hex::encode(&any.data));
let _: MatchmakeSession = any.try_get().unwrap().unwrap();
2026-04-28 13:23:26 +02:00
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,
2026-04-28 15:07:33 +02:00
#[cfg(feature = "v3-5-0")]
2026-04-28 13:23:26 +02:00
progress_score: 0,
session_key: [].into(),
};
2026-04-28 15:07:33 +02:00
let any = Any::new(&sess).unwrap();
2026-04-28 13:23:26 +02:00
2026-04-28 15:07:33 +02:00
let sess2: MatchmakeSession = any.try_get().unwrap().unwrap();
2026-04-28 13:23:26 +02:00
2026-04-28 15:07:33 +02:00
assert_eq!(sess, sess2)
2026-04-28 13:23:26 +02:00
}
}