Fix length reading and remove tmp logging

This commit is contained in:
hodasemi 2023-01-15 06:56:58 +01:00
parent ede1fba66a
commit 61a1ad99f7
2 changed files with 31 additions and 73 deletions

View file

@ -55,15 +55,6 @@ impl rF2Telemetry {
} }
} }
impl From<&[u8]> for rF2Telemetry {
fn from(value: &[u8]) -> Self {
debug_assert!(value.len() == Self::SIZE);
let fixed_size: [u8; Self::SIZE] = value.try_into().unwrap();
unsafe { mem::transmute(fixed_size) }
}
}
impl From<&[u8]> for ScoringInfoV01 { impl From<&[u8]> for ScoringInfoV01 {
fn from(value: &[u8]) -> Self { fn from(value: &[u8]) -> Self {
debug_assert!(value.len() == mem::size_of::<Self>()); debug_assert!(value.len() == mem::size_of::<Self>());
@ -73,6 +64,25 @@ impl From<&[u8]> for ScoringInfoV01 {
} }
} }
macro_rules! vec_from_raw {
($ty:ty, $raw:ident) => {{
const SIZE: usize = std::mem::size_of::<$ty>();
let count = $raw.len() / SIZE;
debug_assert_eq!($raw.len(), count * SIZE);
let mut v = Vec::with_capacity(count);
for i in 0..count {
let tmp: [u8; SIZE] = $raw[i * SIZE..(i + 1) * SIZE].try_into().unwrap();
v.push(unsafe { std::mem::transmute(tmp) });
}
v
}};
}
impl From<&[u8]> for VehicleScoringInfoV01 { impl From<&[u8]> for VehicleScoringInfoV01 {
fn from(value: &[u8]) -> Self { fn from(value: &[u8]) -> Self {
debug_assert!(value.len() == mem::size_of::<Self>()); debug_assert!(value.len() == mem::size_of::<Self>());

View file

@ -1,3 +1,4 @@
#[macro_use]
pub mod additional_rfactor; pub mod additional_rfactor;
#[allow(warnings)] #[allow(warnings)]
mod rfactor_structs; mod rfactor_structs;
@ -37,13 +38,11 @@ impl ShMReader {
} }
fn check_version_update(&self) -> bool { fn check_version_update(&self) -> bool {
log("In ShMReader: start check version update");
let new_version = VersionHeader::from(&self.shm[0..VersionHeader::SIZE]); let new_version = VersionHeader::from(&self.shm[0..VersionHeader::SIZE]);
let are_different = self.version.get().check_for_update(&new_version); let are_different = self.version.get().check_for_update(&new_version);
if are_different { if are_different {
log("In ShMReader: new version found");
self.version.set(new_version); self.version.set(new_version);
} }
@ -51,10 +50,7 @@ impl ShMReader {
} }
fn check_time(&self, now: f32) -> bool { fn check_time(&self, now: f32) -> bool {
log("In ShMReader: start time check");
if now < self.last_query.get() + self.time_between_writes { if now < self.last_query.get() + self.time_between_writes {
log("In ShMReader: time check failed");
return false; return false;
} }
@ -64,14 +60,9 @@ impl ShMReader {
} }
fn read(&self, offset: usize, length: usize) -> &[u8] { fn read(&self, offset: usize, length: usize) -> &[u8] {
log(format!(
"In ShMReader: read (offset: {}, length: {})",
offset, length
));
let b = &self.shm[offset..offset + length]; let b = &self.shm[offset..offset + length];
debug_assert_eq!(b.len(), length); debug_assert_eq!(b.len(), length);
log(format!("In ShMReader: successfully read {} bytes", b.len()));
b b
} }
@ -92,44 +83,27 @@ impl TelemetryReader {
} }
pub fn query_telemetry(&self, now: f32) -> Option<Vec<rF2VehicleTelemetry>> { pub fn query_telemetry(&self, now: f32) -> Option<Vec<rF2VehicleTelemetry>> {
log("In TelemetryReader: start query telemetry");
if !self.mm_reader.check_time(now) || !self.mm_reader.check_version_update() { if !self.mm_reader.check_time(now) || !self.mm_reader.check_version_update() {
return None; return None;
} }
log("In TelemetryReader: read num vehicles");
// first: query only the amount of vehicles // first: query only the amount of vehicles
let num_vehicles = i32::from_be_bytes( let num_vehicles = i32::from_ne_bytes(
self.mm_reader self.mm_reader
.read(VersionHeader::SIZE, mem::size_of::<i32>()) .read(VersionHeader::SIZE, mem::size_of::<i32>())
.try_into() .try_into()
.unwrap(), .unwrap(),
); );
log(format!(
"In TelemetryReader: vehicle count {}",
num_vehicles
));
debug_assert!(num_vehicles >= 0); debug_assert!(num_vehicles >= 0);
debug_assert!(num_vehicles < MAX_MAPPED_VEHICLES as i32); debug_assert!(num_vehicles < MAX_MAPPED_VEHICLES as i32);
log("In TelemetryReader: read vehicles");
// query only the required amount of memory based on vehicle count // query only the required amount of memory based on vehicle count
let vehicles: &[rF2VehicleTelemetry] = unsafe { let bytes = self.mm_reader.read(
mem::transmute(self.mm_reader.read( VersionHeader::SIZE + mem::size_of::<i32>(),
VersionHeader::SIZE + mem::size_of::<i32>(), mem::size_of::<rF2VehicleTelemetry>() * num_vehicles as usize,
mem::size_of::<rF2VehicleTelemetry>() * num_vehicles as usize, );
)) let vehicles = vec_from_raw!(rF2VehicleTelemetry, bytes);
};
log(format!(
"In TelemetryReader: successfully read vehicles ({})",
vehicles.len()
));
debug_assert_eq!(num_vehicles as usize, vehicles.len()); debug_assert_eq!(num_vehicles as usize, vehicles.len());
@ -155,30 +129,20 @@ impl ScoringReader {
&self, &self,
now: f32, now: f32,
) -> Option<(ScoringInfoV01, Vec<VehicleScoringInfoV01>)> { ) -> Option<(ScoringInfoV01, Vec<VehicleScoringInfoV01>)> {
log("In ScoringReader: start vehicle scoring");
if !self.mm_reader.check_time(now) || !self.mm_reader.check_version_update() { if !self.mm_reader.check_time(now) || !self.mm_reader.check_version_update() {
return None; return None;
} }
log("In ScoringReader: read scoring info");
let scoring_info = ScoringInfoV01::from( let scoring_info = ScoringInfoV01::from(
self.mm_reader self.mm_reader
.read(VersionHeader::SIZE, mem::size_of::<ScoringInfoV01>()), .read(VersionHeader::SIZE, mem::size_of::<ScoringInfoV01>()),
); );
log(format!( let bytes = self.mm_reader.read(
"In ScoringReader: successfully read scoring info (num vehicles {})", VersionHeader::SIZE + mem::size_of::<ScoringInfoV01>(),
scoring_info.mNumVehicles mem::size_of::<VehicleScoringInfoV01>() * scoring_info.mNumVehicles as usize,
)); );
let vehicle_info = vec_from_raw!(VehicleScoringInfoV01, bytes);
let vehicle_info: &[VehicleScoringInfoV01] = unsafe {
mem::transmute(self.mm_reader.read(
VersionHeader::SIZE + mem::size_of::<ScoringInfoV01>(),
mem::size_of::<VehicleScoringInfoV01>() * scoring_info.mNumVehicles as usize,
))
};
debug_assert_eq!(scoring_info.mNumVehicles as usize, vehicle_info.len()); debug_assert_eq!(scoring_info.mNumVehicles as usize, vehicle_info.len());
@ -186,22 +150,6 @@ impl ScoringReader {
} }
} }
pub fn log(msg: impl ToString) {
let home = std::env::var("HOME").unwrap();
let log_file = format!("{}/rf2_vk_hud.log", home);
if let Ok(mut file) = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(&log_file)
{
if let Err(_) =
std::io::Write::write_all(&mut file, format!("{}\n", msg.to_string()).as_bytes())
{
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ use std::{