rfactor_sm_reader/src/additional_rfactor.rs

71 lines
2 KiB
Rust
Raw Normal View History

use anyhow::Error as AnyError;
use std::mem;
use crate::rfactor_structs::*;
pub const MM_TELEMETRY_FILE_NAME: &str = "$rFactor2SMMP_Telemetry$";
pub const MM_SCORING_FILE_NAME: &str = "$rFactor2SMMP_Scoring$";
pub const MM_EXTENDED_FILE_NAME: &str = "$rFactor2SMMP_Extended$";
pub const MM_PITINFO_FILE_NAME: &str = "$rFactor2SMMP_PitInfo$";
pub const MM_WEATHER_FILE_NAME: &str = "$rFactor2SMMP_Weather$";
pub const MM_RULES_CONTROL_FILE_NAME: &str = "$rFactor2SMMP_RulesControl$";
pub const MAX_MAPPED_VEHICLES: usize = 128;
pub const MAX_MAPPED_IDS: usize = 512;
#[repr(C, packed(4))]
#[derive(Debug, Copy, Clone)]
pub struct rF2Telemetry {
pub version_update_begin: u32,
pub version_update_end: u32,
pub bytes_update_hint: i32,
pub num_vehicles: i32,
pub vehicle_telemetry: [TelemInfoV01; MAX_MAPPED_VEHICLES],
}
impl rF2Telemetry {
pub const SIZE: usize = mem::size_of::<Self>();
pub fn vehicles(&self) -> &[TelemInfoV01] {
&self.vehicle_telemetry[0..self.num_vehicles as usize]
}
}
impl TryFrom<&[u8]> for rF2Telemetry {
type Error = AnyError;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
debug_assert!(value.len() == Self::SIZE);
let fixed_size: [u8; Self::SIZE] = value.try_into().unwrap();
Ok(unsafe { mem::transmute(fixed_size) })
}
}
#[repr(C, packed(4))]
#[derive(Debug, Copy, Clone)]
pub struct rF2Scoring {
pub version_update_begin: u32,
pub version_update_end: u32,
pub bytes_update_hint: i32,
pub scoring_info: ScoringInfoV01,
pub vehicles: [VehicleScoringInfoV01; MAX_MAPPED_VEHICLES],
}
impl rF2Scoring {
pub const SIZE: usize = mem::size_of::<Self>();
}
impl TryFrom<&[u8]> for rF2Scoring {
type Error = AnyError;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
debug_assert!(value.len() == Self::SIZE);
let fixed_size: [u8; Self::SIZE] = value.try_into().unwrap();
Ok(unsafe { mem::transmute(fixed_size) })
}
}