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<()> {
|
2025-03-24 23:31:25 +01:00
|
|
|
self.name.serialize(writer)?;
|
2026-04-28 13:37:00 +02:00
|
|
|
let u32_len = self.data.len() as u32 - 1;
|
2026-04-25 14:15:42 +02:00
|
|
|
(u32_len + 4).serialize(writer)?;
|
2026-04-28 13:37:00 +02:00
|
|
|
u32_len.serialize(writer)?;
|
|
|
|
|
writer.write_all(&self.data)?;
|
2025-03-24 23:31:25 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-26 23:21:35 +01:00
|
|
|
}
|
2025-11-13 10:06:58 +01:00
|
|
|
fn deserialize(reader: &mut impl Read) -> Result<Self> {
|
2025-01-26 23:21:35 +01:00
|
|
|
let name = String::deserialize(reader)?;
|
2025-01-26 12:09:56 +01:00
|
|
|
|
2026-04-28 13:37:00 +02:00
|
|
|
let size = u32::deserialize(reader)? as usize + 1;
|
|
|
|
|
let mut buf = vec![0; size];
|
|
|
|
|
reader.read_exact(&mut buf[..])?;
|
|
|
|
|
let mut cursor = Cursor::new(&buf);
|
2026-04-28 13:23:26 +02:00
|
|
|
let len2: u32 = cursor.read_struct(IS_BIG_ENDIAN)?;
|
|
|
|
|
|
2026-04-28 13:37:00 +02:00
|
|
|
if len2 as usize + 1 != size {
|
|
|
|
|
warn!("mismatched sizes on any: {} vs {}", size, len2 + 1);
|
2026-04-28 13:23:26 +02:00
|
|
|
}
|
2025-01-26 12:09:56 +01:00
|
|
|
|
2026-04-28 13:23:26 +02:00
|
|
|
Ok(Any {
|
|
|
|
|
name,
|
2026-04-28 13:37:00 +02:00
|
|
|
data: (&buf[4..]).to_owned(),
|
2026-04-28 13:23:26 +02:00
|
|
|
})
|
2025-01-26 23:21:35 +01:00
|
|
|
}
|
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 {
|
2026-04-28 13:37:00 +02:00
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
2026-04-28 13:23:26 +02:00
|
|
|
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() {
|
|
|
|
|
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(),
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-28 13:37:00 +02:00
|
|
|
let any = Any::new(&sess).unwrap().to_data().unwrap();
|
2026-04-28 13:23:26 +02:00
|
|
|
|
2026-04-28 13:37:00 +02:00
|
|
|
let sess2: MatchmakeSession = Any::deserialize(&mut Cursor::new(any))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.try_get()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.unwrap();
|
2026-04-28 13:23:26 +02:00
|
|
|
|
|
|
|
|
assert_eq!(sess, sess2);
|
|
|
|
|
}
|
|
|
|
|
}
|