From 2f26662ddaa7544c2e592f3c68b2153fd478ee95 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 22 Jan 2025 09:36:31 -0800 Subject: [PATCH] it: drop strafe-client::file dependency --- integration-testing/src/main.rs | 124 ++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 53 deletions(-) diff --git a/integration-testing/src/main.rs b/integration-testing/src/main.rs index 9d25d3b98..fa48bc8d1 100644 --- a/integration-testing/src/main.rs +++ b/integration-testing/src/main.rs @@ -1,52 +1,77 @@ -use crate::file; +use std::{io::{Cursor,Read},path::Path}; use strafesnet_physics::physics::{PhysicsData,PhysicsState,PhysicsContext}; -#[test] -fn run_replay(){ - println!("loading map file.."); - let map=file::load("../tools/bhop_maps/5692113331.snfm"); - println!("loading bot file.."); - let bot=file::load("../tools/replays/534s+997497968ns.snfb"); - if let (Ok(file::LoadFormat::Map(map)),Ok(file::LoadFormat::Bot(bot)))=(map,bot){ - // create recording - let mut physics_data=PhysicsData::default(); - println!("generating models.."); - physics_data.generate_models(&map); - println!("simulating..."); - let mut physics=PhysicsState::default(); - for ins in bot.instructions{ - PhysicsContext::run_input_instruction(&mut physics,&physics_data,ins); - } - match physics.get_finish_time(){ - Some(time)=>println!("finish time:{}",time), - None=>println!("simulation did not end in finished state"), - } - }else{ - panic!("missing files"); - } -} -enum DeterminismResult{ - Deterministic, - NonDeterministic, +fn main(){ + test_determinism().unwrap(); } + #[allow(unused)] #[derive(Debug)] enum ReplayError{ - Load(file::LoadError), IO(std::io::Error), -} -impl From for ReplayError{ - fn from(value:file::LoadError)->Self{ - Self::Load(value) - } + SNF(strafesnet_snf::Error), + SNFM(strafesnet_snf::map::Error), + SNFB(strafesnet_snf::bot::Error), } impl From for ReplayError{ fn from(value:std::io::Error)->Self{ Self::IO(value) } } +impl From for ReplayError{ + fn from(value:strafesnet_snf::Error)->Self{ + Self::SNF(value) + } +} +impl From for ReplayError{ + fn from(value:strafesnet_snf::map::Error)->Self{ + Self::SNFM(value) + } +} +impl From for ReplayError{ + fn from(value:strafesnet_snf::bot::Error)->Self{ + Self::SNFB(value) + } +} + +fn read_entire_file(path:impl AsRef)->Result>,std::io::Error>{ + let mut file=std::fs::File::open(path)?; + let mut data=Vec::new(); + file.read_to_end(&mut data)?; + Ok(Cursor::new(data)) +} + +fn run_replay()->Result<(),ReplayError>{ + println!("loading map file.."); + let data=read_entire_file("../tools/bhop_maps/5692113331.snfm")?; + let map=strafesnet_snf::read_map(data)?.into_complete_map()?; + + println!("loading bot file.."); + let data=read_entire_file("../tools/replays/535s+159764769ns.snfb")?; + let bot=strafesnet_snf::read_bot(data)?.read_all()?; + + // create recording + let mut physics_data=PhysicsData::default(); + println!("generating models.."); + physics_data.generate_models(&map); + println!("simulating..."); + let mut physics=PhysicsState::default(); + for ins in bot.instructions{ + PhysicsContext::run_input_instruction(&mut physics,&physics_data,ins); + } + match physics.get_finish_time(){ + Some(time)=>println!("finish time:{}",time), + None=>println!("simulation did not end in finished state"), + } + + Ok(()) +} +enum DeterminismResult{ + Deterministic, + NonDeterministic, +} fn segment_determinism(bot:strafesnet_snf::bot::Segment,physics_data:&PhysicsData)->DeterminismResult{ // create default physics state let mut physics_deterministic=PhysicsState::default(); @@ -94,23 +119,16 @@ fn segment_determinism(bot:strafesnet_snf::bot::Segment,physics_data:&PhysicsDat } DeterminismResult::Deterministic } -type ThreadResult=Result,file::LoadError>; +type ThreadResult=Result,ReplayError>; +fn read_and_run(file_path:std::path::PathBuf,physics_data:&PhysicsData)->ThreadResult{ + let data=read_entire_file(file_path.as_path())?; + let bot=strafesnet_snf::read_bot(data)?.read_all()?; + println!("Running {:?}",file_path.file_stem()); + Ok(Some(segment_determinism(bot,physics_data))) +} fn do_thread<'a>(s:&'a std::thread::Scope<'a,'_>,file_path:std::path::PathBuf,send:std::sync::mpsc::Sender,physics_data:&'a PhysicsData){ s.spawn(move ||{ - let result=match file::load(file_path.as_path()){ - Ok(file::LoadFormat::Bot(bot))=>{ - println!("Running {:?}",file_path.file_stem()); - Ok(Some(segment_determinism(bot,physics_data))) - }, - Ok(_)=>{ - println!("Provided bot file is not a bot file!"); - Ok(None) - } - Err(e)=>{ - println!("Load error"); - Err(e) - }, - }; + let result=read_and_run(file_path,physics_data); // send when thread is complete send.send(result).unwrap(); }); @@ -120,16 +138,16 @@ fn get_file_path(dir_entry:std::fs::DirEntry)->Result dir_entry.path() )) } -#[test] fn test_determinism()->Result<(),ReplayError>{ let thread_limit=std::thread::available_parallelism()?.get(); println!("loading map file.."); - let file::LoadFormat::Map(map)=file::load("../tools/bhop_maps/5692113331.snfm")? else{ - panic!("Provided map file is not a map file!"); - }; + let data=read_entire_file("../tools/bhop_maps/5692113331.snfm")?; + let map=strafesnet_snf::read_map(data)?.into_complete_map()?; + let mut physics_data=PhysicsData::default(); println!("generating models.."); physics_data.generate_models(&map); + let (send,recv)=std::sync::mpsc::channel(); let mut read_dir=std::fs::read_dir("../tools/replays")?;