naive multithread

This commit is contained in:
Quaternions 2024-07-26 18:21:56 -07:00
parent 7cc018e61e
commit c7c57c1527

View File

@ -13,7 +13,7 @@ struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
ConvertToSNF(ConvertToSNFSubcommand), RobloxToSNF(RobloxToSNFSubcommand),
DownloadTextures(DownloadTexturesSubcommand), DownloadTextures(DownloadTexturesSubcommand),
ExtractTextures(ExtractTexturesSubcommand), ExtractTextures(ExtractTexturesSubcommand),
ConvertTextures(ConvertTexturesSubcommand), ConvertTextures(ConvertTexturesSubcommand),
@ -24,11 +24,11 @@ enum Commands {
} }
#[derive(Args)] #[derive(Args)]
struct ConvertToSNFSubcommand { struct RobloxToSNFSubcommand {
#[arg(long,required=true)]
input_files:Vec<PathBuf>,
#[arg(long)] #[arg(long)]
output_folder:PathBuf, output_folder:PathBuf,
#[arg(required=true)]
input_files:Vec<PathBuf>,
} }
#[derive(Args)] #[derive(Args)]
struct DownloadTexturesSubcommand { struct DownloadTexturesSubcommand {
@ -67,7 +67,7 @@ struct WriteAttributesSubcommand {
fn main() -> AResult<()> { fn main() -> AResult<()> {
let cli = Cli::parse(); let cli = Cli::parse();
match cli.command { match cli.command {
Commands::ConvertToSNF(subcommand)=>convert_to_snf(subcommand.input_files,subcommand.output_folder), Commands::RobloxToSNF(subcommand)=>convert_to_snf(subcommand.input_files,subcommand.output_folder),
Commands::DownloadTextures(subcommand)=>download_textures(subcommand.roblox_files), Commands::DownloadTextures(subcommand)=>download_textures(subcommand.roblox_files),
Commands::ExtractTextures(subcommand)=>extract_textures(vec![subcommand.bsp_file],subcommand.vpk_dir_files), Commands::ExtractTextures(subcommand)=>extract_textures(vec![subcommand.bsp_file],subcommand.vpk_dir_files),
Commands::VPKContents(subcommand)=>vpk_contents(subcommand.input_file), Commands::VPKContents(subcommand)=>vpk_contents(subcommand.input_file),
@ -641,9 +641,29 @@ fn bsp_contents(path:PathBuf)->AResult<()>{
Ok(()) Ok(())
} }
#[derive(Debug)]
enum ConvertError{
IO(std::io::Error),
SNFMap(strafesnet_snf::map::Error),
RbxLoader(strafesnet_rbx_loader::ReadError),
}
impl std::fmt::Display for ConvertError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for ConvertError{}
fn convert_to_snf(pathlist:Vec<std::path::PathBuf>,output_folder:PathBuf)->AResult<()>{ fn convert_to_snf(pathlist:Vec<std::path::PathBuf>,output_folder:PathBuf)->AResult<()>{
let start = std::time::Instant::now();
let mut threads:Vec<std::thread::JoinHandle<Result<(),ConvertError>>>=Vec::new();
for path in pathlist{ for path in pathlist{
let dom=strafesnet_rbx_loader::read(std::fs::File::open(path.as_path())?)?; let output_folder=output_folder.clone();
threads.push(std::thread::spawn(move ||{
let dom=strafesnet_rbx_loader::read(
std::fs::File::open(path.as_path())
.map_err(ConvertError::IO)?
).map_err(ConvertError::RbxLoader)?;
let mut loader=strafesnet_deferred_loader::roblox_legacy(); let mut loader=strafesnet_deferred_loader::roblox_legacy();
let (texture_loader,mesh_loader)=loader.get_inner_mut(); let (texture_loader,mesh_loader)=loader.get_inner_mut();
@ -654,7 +674,7 @@ fn convert_to_snf(pathlist:Vec<std::path::PathBuf>,output_folder:PathBuf)->AResu
|name|mesh_loader.acquire_mesh_id(name), |name|mesh_loader.acquire_mesh_id(name),
); );
let meshpart_meshes=mesh_loader.load_meshes()?; let meshpart_meshes=mesh_loader.load_meshes().map_err(ConvertError::IO)?;
let map_step2=map_step1.add_meshpart_meshes_and_calculate_attributes( let map_step2=map_step1.add_meshpart_meshes_and_calculate_attributes(
meshpart_meshes.into_iter().map(|(mesh_id,loader_model)| meshpart_meshes.into_iter().map(|(mesh_id,loader_model)|
@ -662,7 +682,7 @@ fn convert_to_snf(pathlist:Vec<std::path::PathBuf>,output_folder:PathBuf)->AResu
) )
); );
let (textures,render_configs)=loader.into_render_configs()?.consume(); let (textures,render_configs)=loader.into_render_configs().map_err(ConvertError::IO)?.consume();
let map=map_step2.add_render_configs_and_textures( let map=map_step2.add_render_configs_and_textures(
render_configs.into_iter(), render_configs.into_iter(),
@ -676,9 +696,23 @@ fn convert_to_snf(pathlist:Vec<std::path::PathBuf>,output_folder:PathBuf)->AResu
let mut dest=output_folder.clone(); let mut dest=output_folder.clone();
dest.push(path.file_stem().unwrap()); dest.push(path.file_stem().unwrap());
dest.set_extension("snfm"); dest.set_extension("snfm");
let file=std::fs::File::create(dest)?; let file=std::fs::File::create(dest).map_err(ConvertError::IO)?;
strafesnet_snf::map::write_map(file,map)?; strafesnet_snf::map::write_map(file,map).map_err(ConvertError::SNFMap)?;
Ok(())
}));
} }
let mut i=0;
let n_threads=threads.len();
for thread in threads{
i+=1;
if let Err(e)=thread.join(){
println!("thread error: {:?}",e);
}else{
println!("{}/{}",i,n_threads);
}
}
println!("{:?}", start.elapsed());
Ok(()) Ok(())
} }