Files
unixtract/src/formats/funai_upg.rs
T

68 lines
2.3 KiB
Rust
Raw Normal View History

2026-02-05 15:18:26 +01:00
use std::any::Any;
use crate::{InputTarget, AppContext, formats::Format};
2026-02-05 15:18:26 +01:00
pub fn format() -> Format {
2026-02-05 18:53:35 +01:00
Format { name: "funai_upg", detector_func: is_funai_upg_file, extractor_func: extract_funai_upg }
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};
use std::io::{Write};
use binrw::{BinRead, BinReaderExt};
use crate::utils::common;
2025-11-05 22:57:54 +01:00
#[derive(BinRead)]
struct Header {
#[br(count = 6)] _magic_bytes: Vec<u8>,
entry_count: u16,
file_size: u32,
}
#[derive(BinRead)]
struct Entry {
entry_type: u16,
entry_size: u32,
_unk: u16,
}
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.input {InputTarget::File(f) => f, InputTarget::Directory(_) => 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
}
}
2026-02-05 18:53:35 +01:00
pub fn extract_funai_upg(app_ctx: &AppContext, _ctx: Option<Box<dyn Any>>) -> Result<(), Box<dyn std::error::Error>> {
let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())};
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 - 2 - 4)?; //size has the unk field + crc32 at the end
let _crc32 = common::read_exact(&mut file, 4)?; //btw the CRC32 includes the entry header
if entry.entry_type == 0 {
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!");
}
println!("\nExtraction finished!");
Ok(())
}