convert snf

This commit is contained in:
Quaternions 2024-01-30 18:48:51 -08:00
parent 52ba44c6be
commit 4d97a490c1
3 changed files with 535 additions and 387 deletions

832
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -16,6 +16,9 @@ rbx_binary = "0.7.1"
rbx_dom_weak = "2.5.0"
rbx_reflection_database = "0.2.7"
rbx_xml = "0.13.1"
strafesnet_deferred_loader = { version = "0.3.1", features = ["legacy"], registry = "strafesnet" }
strafesnet_rbx_loader = { version = "0.3.1", registry = "strafesnet" }
strafesnet_snf = { version = "0.1.0", registry = "strafesnet" }
vbsp = "0.5.0"
vmdl = "0.1.1"
vmt-parser = "0.1.1"

View File

@ -13,6 +13,7 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
RobloxToSNF(RobloxToSNFSubcommand),
DownloadTextures(DownloadTexturesSubcommand),
ExtractTextures(ExtractTexturesSubcommand),
ConvertTextures(ConvertTexturesSubcommand),
@ -22,6 +23,13 @@ enum Commands {
WriteAttributes(WriteAttributesSubcommand),
}
#[derive(Args)]
struct RobloxToSNFSubcommand {
#[arg(long)]
output_folder:PathBuf,
#[arg(required=true)]
input_files:Vec<PathBuf>,
}
#[derive(Args)]
struct DownloadTexturesSubcommand {
#[arg(long,required=true)]
@ -59,6 +67,7 @@ struct WriteAttributesSubcommand {
fn main() -> AResult<()> {
let cli = Cli::parse();
match cli.command {
Commands::RobloxToSNF(subcommand)=>convert_to_snf(subcommand.input_files,subcommand.output_folder),
Commands::DownloadTextures(subcommand)=>download_textures(subcommand.roblox_files),
Commands::ExtractTextures(subcommand)=>extract_textures(vec![subcommand.bsp_file],subcommand.vpk_dir_files),
Commands::VPKContents(subcommand)=>vpk_contents(subcommand.input_file),
@ -631,3 +640,79 @@ fn bsp_contents(path:PathBuf)->AResult<()>{
}
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<()>{
let start = std::time::Instant::now();
let mut threads:Vec<std::thread::JoinHandle<Result<(),ConvertError>>>=Vec::new();
for path in pathlist{
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 (texture_loader,mesh_loader)=loader.get_inner_mut();
let map_step1=strafesnet_rbx_loader::convert(
&dom,
|name|texture_loader.acquire_render_config_id(name),
|name|mesh_loader.acquire_mesh_id(name),
);
let meshpart_meshes=mesh_loader.load_meshes().map_err(ConvertError::IO)?;
let map_step2=map_step1.add_meshpart_meshes_and_calculate_attributes(
meshpart_meshes.into_iter().map(|(mesh_id,loader_model)|
(mesh_id,strafesnet_rbx_loader::data::RobloxMeshBytes::new(loader_model.get()))
)
);
let (textures,render_configs)=loader.into_render_configs().map_err(ConvertError::IO)?.consume();
let map=map_step2.add_render_configs_and_textures(
render_configs.into_iter(),
textures.into_iter().map(|(texture_id,texture)|
(texture_id,match texture{
strafesnet_deferred_loader::texture::Texture::ImageDDS(data)=>data,
})
)
);
let mut dest=output_folder.clone();
dest.push(path.file_stem().unwrap());
dest.set_extension("snfm");
let file=std::fs::File::create(dest).map_err(ConvertError::IO)?;
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(())
}