it: drop strafe-client::file dependency

This commit is contained in:
Quaternions 2025-01-22 09:36:31 -08:00
parent 6f739dba8d
commit 2f26662dda

View File

@ -1,52 +1,77 @@
use crate::file; use std::{io::{Cursor,Read},path::Path};
use strafesnet_physics::physics::{PhysicsData,PhysicsState,PhysicsContext}; use strafesnet_physics::physics::{PhysicsData,PhysicsState,PhysicsContext};
#[test] fn main(){
fn run_replay(){ test_determinism().unwrap();
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,
} }
#[allow(unused)] #[allow(unused)]
#[derive(Debug)] #[derive(Debug)]
enum ReplayError{ enum ReplayError{
Load(file::LoadError),
IO(std::io::Error), IO(std::io::Error),
} SNF(strafesnet_snf::Error),
impl From<file::LoadError> for ReplayError{ SNFM(strafesnet_snf::map::Error),
fn from(value:file::LoadError)->Self{ SNFB(strafesnet_snf::bot::Error),
Self::Load(value)
}
} }
impl From<std::io::Error> for ReplayError{ impl From<std::io::Error> for ReplayError{
fn from(value:std::io::Error)->Self{ fn from(value:std::io::Error)->Self{
Self::IO(value) Self::IO(value)
} }
} }
impl From<strafesnet_snf::Error> for ReplayError{
fn from(value:strafesnet_snf::Error)->Self{
Self::SNF(value)
}
}
impl From<strafesnet_snf::map::Error> for ReplayError{
fn from(value:strafesnet_snf::map::Error)->Self{
Self::SNFM(value)
}
}
impl From<strafesnet_snf::bot::Error> for ReplayError{
fn from(value:strafesnet_snf::bot::Error)->Self{
Self::SNFB(value)
}
}
fn read_entire_file(path:impl AsRef<Path>)->Result<Cursor<Vec<u8>>,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{ fn segment_determinism(bot:strafesnet_snf::bot::Segment,physics_data:&PhysicsData)->DeterminismResult{
// create default physics state // create default physics state
let mut physics_deterministic=PhysicsState::default(); let mut physics_deterministic=PhysicsState::default();
@ -94,23 +119,16 @@ fn segment_determinism(bot:strafesnet_snf::bot::Segment,physics_data:&PhysicsDat
} }
DeterminismResult::Deterministic DeterminismResult::Deterministic
} }
type ThreadResult=Result<Option<DeterminismResult>,file::LoadError>; type ThreadResult=Result<Option<DeterminismResult>,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<ThreadResult>,physics_data:&'a PhysicsData){ fn do_thread<'a>(s:&'a std::thread::Scope<'a,'_>,file_path:std::path::PathBuf,send:std::sync::mpsc::Sender<ThreadResult>,physics_data:&'a PhysicsData){
s.spawn(move ||{ s.spawn(move ||{
let result=match file::load(file_path.as_path()){ let result=read_and_run(file_path,physics_data);
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)
},
};
// send when thread is complete // send when thread is complete
send.send(result).unwrap(); send.send(result).unwrap();
}); });
@ -120,16 +138,16 @@ fn get_file_path(dir_entry:std::fs::DirEntry)->Result<Option<std::path::PathBuf>
dir_entry.path() dir_entry.path()
)) ))
} }
#[test]
fn test_determinism()->Result<(),ReplayError>{ fn test_determinism()->Result<(),ReplayError>{
let thread_limit=std::thread::available_parallelism()?.get(); let thread_limit=std::thread::available_parallelism()?.get();
println!("loading map file.."); println!("loading map file..");
let file::LoadFormat::Map(map)=file::load("../tools/bhop_maps/5692113331.snfm")? else{ let data=read_entire_file("../tools/bhop_maps/5692113331.snfm")?;
panic!("Provided map file is not a map file!"); let map=strafesnet_snf::read_map(data)?.into_complete_map()?;
};
let mut physics_data=PhysicsData::default(); let mut physics_data=PhysicsData::default();
println!("generating models.."); println!("generating models..");
physics_data.generate_models(&map); physics_data.generate_models(&map);
let (send,recv)=std::sync::mpsc::channel(); let (send,recv)=std::sync::mpsc::channel();
let mut read_dir=std::fs::read_dir("../tools/replays")?; let mut read_dir=std::fs::read_dir("../tools/replays")?;