Files
unixtract/src/formats/slp.rs
T

120 lines
3.3 KiB
Rust
Raw Normal View History

2026-02-05 15:18:26 +01:00
use std::any::Any;
2026-02-05 18:53:35 +01:00
use crate::{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: "slp", detector_func: is_slp_file, extractor_func: extract_slp }
2026-02-05 15:18:26 +01:00
}
2025-10-29 18:36:42 +01:00
use std::path::{Path};
2026-02-05 15:18:26 +01:00
use std::fs::{self, OpenOptions};
2025-10-29 18:36:42 +01:00
use std::io::{Write, Seek, SeekFrom};
use binrw::{BinRead, BinReaderExt};
use crate::utils::common;
2025-10-29 18:36:42 +01:00
#[derive(BinRead)]
struct Header {
#[br(count = 4)] _magic_bytes: Vec<u8>,
#[br(count = 8)] version_bytes: Vec<u8>,
#[br(count = 16)] model_bytes: Vec<u8>,
#[br(count = 16)] firmware_bytes: Vec<u8>,
_unk: u32,
#[br(count = 8)] check: Vec<u8>,
#[br(count = 8)] _unk2: Vec<u8>,
}
impl Header {
fn version(&self) -> String {
common::string_from_bytes(&self.version_bytes)
}
fn model(&self) -> String {
common::string_from_bytes(&self.model_bytes)
}
fn firmware(&self) -> String {
common::string_from_bytes(&self.firmware_bytes)
}
fn is_new_type(&self) -> bool {
self.check == b"\x01VER_PR1"
}
}
#[derive(BinRead)]
struct EntryOld {
size: u32,
_unk: u32,
offset: u32,
_unk2: u32,
}
#[derive(BinRead)]
struct EntryNew {
size: u32,
_unk: u32,
offset: u32,
#[br(count = 12)] _unk2: Vec<u8>,
}
2026-02-05 18:53:35 +01:00
pub fn is_slp_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
2026-02-05 15:18:26 +01:00
let header = common::read_file(app_ctx.file, 0, 4)?;
2025-10-29 18:36:42 +01:00
if header == b"SLP\x00" {
2026-02-05 15:18:26 +01:00
Ok(Some(Box::new(())))
2025-10-29 18:36:42 +01:00
} else {
2026-02-05 15:18:26 +01:00
Ok(None)
2025-10-29 18:36:42 +01:00
}
}
2026-02-05 18:53:35 +01:00
pub fn extract_slp(app_ctx: &AppContext, _ctx: Option<Box<dyn Any>>) -> Result<(), Box<dyn std::error::Error>> {
2026-02-05 15:18:26 +01:00
let mut file = app_ctx.file;
2025-10-29 18:36:42 +01:00
let header: Header = file.read_le()?;
println!("File info:\nModel: {}\nVersion: {}\nFirmware: {}\nNew type: {}\n",
2025-10-29 18:36:42 +01:00
header.model(), header.version(), header.firmware(), header.is_new_type());
let mut first_entry_offset = 0;
let mut entries: Vec<EntryOld> = Vec::new();
for i in 0..100 {
if (i != 0) && (file.stream_position()? >= first_entry_offset) {
break
}
let offset;
let size;
if header.is_new_type() {
let entry: EntryNew = file.read_le()?;
offset = entry.offset;
size = entry.size;
} else {
let entry: EntryOld = file.read_le()?;
offset = entry.offset;
size = entry.size;
}
if i == 0 {
first_entry_offset = offset as u64;
}
println!("{}. Offset: {}, Size: {}", i + 1, offset, size);
entries.push(EntryOld {size: size, _unk: 0, offset: offset, _unk2: 0});
}
let mut i = 1;
for entry in &entries {
println!("\n({}/{}) - Offset: {}, Size: {}", i, &entries.len(), entry.offset, entry.size);
2025-10-29 18:36:42 +01:00
file.seek(SeekFrom::Start(entry.offset.into()))?;
let data = common::read_exact(&mut file, entry.size as usize)?;
2026-02-05 15:18:26 +01:00
let output_path = Path::new(app_ctx.output_dir).join(format!("{}.bin", i));
2025-10-29 18:36:42 +01:00
2026-02-05 15:18:26 +01:00
fs::create_dir_all(app_ctx.output_dir)?;
2025-10-29 18:36:42 +01:00
let mut out_file = OpenOptions::new()
.write(true)
.create(true)
.open(output_path)?;
out_file.write_all(&data)?;
println!("- Saved file!");
i += 1;
}
println!("\nExtraction finished!");
Ok(())
}