Refactor unpacker.rs- asset filtering

This commit is contained in:
Piotr Siuszko 2023-12-29 15:28:49 +01:00
parent 6a19b0fbb0
commit 760dded5eb
2 changed files with 19 additions and 27 deletions

7
Cargo.lock generated
View File

@ -350,7 +350,6 @@ dependencies = [
"clap", "clap",
"flate2", "flate2",
"gltf", "gltf",
"pathdiff",
"rayon", "rayon",
"regex", "regex",
"tar", "tar",
@ -411,12 +410,6 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "pathdiff"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
[[package]] [[package]]
name = "png" name = "png"
version = "0.17.10" version = "0.17.10"

View File

@ -35,6 +35,7 @@ impl Unpacker {
fs::remove_dir_all(output_dir).unwrap(); fs::remove_dir_all(output_dir).unwrap();
} }
} }
pub fn extract(&mut self) { pub fn extract(&mut self) {
let archive_path = Path::new(&self.args.input); let archive_path = Path::new(&self.args.input);
let tmp_path = Path::new("./tmp_dir"); let tmp_path = Path::new("./tmp_dir");
@ -63,28 +64,21 @@ impl Unpacker {
self.assets = receiver.iter().collect(); self.assets = receiver.iter().collect();
} }
pub fn assets_of_type(&self, asset_type: AssetType) -> Vec<Asset> {
self.assets
.clone()
.into_iter()
.filter(|a| a.asset_type == asset_type)
.collect()
}
pub fn update_gltf_materials(&self) { pub fn update_gltf_materials(&self) {
if self.args.fbx_to_gltf.is_none() || !self.args.get_materials_from_prefabs { if self.args.fbx_to_gltf.is_none() || !self.args.get_materials_from_prefabs {
return; return;
} }
let fbx_models: Vec<Asset> = self let fbx_models = self.assets_of_type(AssetType::FbxModel);
.assets let prefabs = self.assets_of_type(AssetType::Prefab);
.clone() let materials = self.assets_of_type(AssetType::Material);
.into_iter()
.filter(|a| a.asset_type == AssetType::FbxModel)
.collect();
let prefabs: Vec<Asset> = self
.assets
.clone()
.into_iter()
.filter(|a| a.asset_type == AssetType::Prefab)
.collect();
let materials: Vec<Asset> = self
.assets
.clone()
.into_iter()
.filter(|a| a.asset_type == AssetType::Material)
.collect();
println!( println!(
"There are {} models, {} prefabs and {} materials", "There are {} models, {} prefabs and {} materials",
fbx_models.len(), fbx_models.len(),
@ -132,13 +126,18 @@ impl Unpacker {
let mut gltf = gltf::Gltf::from_reader(reader).unwrap(); let mut gltf = gltf::Gltf::from_reader(reader).unwrap();
let mut json = gltf.document.into_json(); let mut json = gltf.document.into_json();
for image in json.images.iter_mut() { for image in json.images.iter_mut() {
let result = texture_asset.file_name().unwrap().to_str().unwrap().to_string(); let result = texture_asset
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
let required_file = gltf_path.with_file_name(&result); let required_file = gltf_path.with_file_name(&result);
if !required_file.exists() { if !required_file.exists() {
fs::copy(texture_asset, gltf_path.with_file_name(&result)).unwrap(); fs::copy(texture_asset, gltf_path.with_file_name(&result)).unwrap();
} }
if let Some(old_path) = &image.uri { if let Some(old_path) = &image.uri {
if old_path.eq(&result){ if old_path.eq(&result) {
return; return;
} }
} }