Files
unixtract/src/formats/funai_upg/mod.rs
T

57 lines
2.0 KiB
Rust
Raw Normal View History

2026-02-17 17:28:59 +01:00
mod include;
2026-02-05 15:18:26 +01:00
use std::any::Any;
2026-02-17 17:28:59 +01:00
use crate::AppContext;
2026-02-05 15:18:26 +01:00
2025-11-05 22:57:54 +01:00
use std::path::Path;
use std::fs::{self, OpenOptions};
2026-02-17 17:28:59 +01:00
use std::io::Write;
use binrw::BinReaderExt;
2025-11-05 22:57:54 +01:00
use crate::utils::common;
2026-02-17 17:28:59 +01:00
use include::*;
2025-11-05 22:57:54 +01:00
2026-02-05 18:53:35 +01:00
pub fn is_funai_upg_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)};
let header = common::read_file(&file, 0, 6)?;
2025-11-05 22:57:54 +01:00
if header == b"UPG\x00\x00\x00" {
2026-02-05 15:18:26 +01:00
Ok(Some(Box::new(())))
2025-11-05 22:57:54 +01:00
} else {
2026-02-05 15:18:26 +01:00
Ok(None)
2025-11-05 22:57:54 +01:00
}
}
pub fn extract_funai_upg(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
2025-11-05 22:57:54 +01:00
let header: Header = file.read_le()?;
println!("File info:\nFile size: {}\nEntry count: {}", header.file_size, header.entry_count);
for i in 0..header.entry_count {
let entry: Entry = file.read_le()?;
println!("\n({}/{}) - Type: {}, Size: {}", i + 1, header.entry_count, entry.entry_type, entry.entry_size);
2025-11-05 22:57:54 +01:00
let data = common::read_exact(&mut file, entry.entry_size as usize - 0x46)?; //size has the flags + crc32 + hash
let _crc32 = common::read_exact(&mut file, 4)?; //crc32 includes the entry header and hash
let _hash = common::read_exact(&mut file, 64)?; //hash is only used on encrypted entries
2025-11-05 22:57:54 +01:00
if entry.encryption_flag == 1 {
//not supported yet
println!("- Warning: Cannot decrypt entry, saving encrypted data!");
}
if entry.encryption_flag == 0 && entry.entry_type == 0 {
2025-11-05 22:57:54 +01:00
let entry_string = common::string_from_bytes(&data);
println!("Descriptor entry info:\n{}", entry_string);
}
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}_{}.bin", i + 1, entry.entry_type));
2025-11-05 22:57:54 +01:00
fs::create_dir_all(&app_ctx.output_dir)?;
2025-11-05 22:57:54 +01:00
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
out_file.write_all(&data)?;
println!("- Saved file!");
}
2025-11-05 22:57:54 +01:00
Ok(())
}