1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-23 21:17:45 +00:00
physis/src/fiin.rs

95 lines
2.4 KiB
Rust
Raw Normal View History

use crate::gamedata::MemoryBuffer;
2022-10-25 13:02:06 -04:00
use crate::sha1::Sha1;
2022-10-25 14:00:04 -04:00
use binrw::binrw;
2022-12-17 08:23:19 -05:00
use binrw::BinRead;
2022-08-16 11:52:07 -04:00
use std::fs::read;
use std::io::Cursor;
#[binrw]
#[brw(magic = b"FileInfo")]
#[derive(Debug)]
#[brw(little)]
pub struct FileInfo {
#[brw(pad_before = 16)]
#[bw(calc = 1024)]
2022-08-16 11:52:07 -04:00
unknown: i32,
#[br(temp)]
#[bw(calc = (entries.len() * 96) as i32)]
2022-08-16 11:52:07 -04:00
entries_size: i32,
#[brw(pad_before = 992)]
#[br(count = entries_size / 96)]
2022-10-20 11:44:54 -04:00
pub entries: Vec<FIINEntry>,
}
#[binrw]
#[derive(Debug)]
pub struct FIINEntry {
2022-10-20 11:44:54 -04:00
pub file_size: i32,
#[brw(pad_before = 4)]
#[br(count = 64)]
#[bw(pad_size_to = 64)]
2022-10-20 11:44:54 -04:00
#[bw(map = |x : &String | x.as_bytes())]
#[br(map = | x: Vec<u8> | String::from_utf8(x).unwrap().trim_matches(char::from(0)).to_string())]
pub file_name: String,
#[br(count = 24)]
#[bw(pad_size_to = 24)]
2022-10-20 11:44:54 -04:00
pub sha1: Vec<u8>,
}
impl FileInfo {
2022-08-06 21:21:55 -04:00
/// Parses an existing FIIN file.
2022-08-16 11:52:07 -04:00
pub fn from_existing(buffer: &MemoryBuffer) -> Option<FileInfo> {
let mut cursor = Cursor::new(buffer);
2022-08-16 11:50:18 -04:00
FileInfo::read(&mut cursor).ok()
}
2022-08-06 21:21:55 -04:00
/// Creates a new FileInfo structure from a list of filenames. These filenames must be present in
/// the current working directory in order to be read properly, since it also generates SHA1
/// hashes.
///
/// The new FileInfo structure can then be serialized back into retail-compatible form.
2022-10-20 11:44:54 -04:00
pub fn new(file_names: &[&str]) -> Option<FileInfo> {
let mut entries = vec![];
for name in file_names {
let file = &read(name).expect("Cannot read file.");
2022-08-16 11:52:07 -04:00
entries.push(FIINEntry {
file_size: file.len() as i32,
file_name: name.to_string(),
sha1: Sha1::from(file).digest().bytes().to_vec(),
});
}
2022-08-16 11:52:07 -04:00
Some(FileInfo { entries })
}
2022-08-16 11:52:07 -04:00
}
2022-10-20 11:44:54 -04:00
#[cfg(test)]
mod tests {
2022-10-20 11:45:55 -04:00
use crate::fiin::FileInfo;
2022-10-20 11:44:54 -04:00
use std::fs::read;
use std::path::PathBuf;
fn common_setup() -> FileInfo {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("resources/tests");
d.push("test.fiin");
FileInfo::from_existing(&read(d).unwrap()).unwrap()
}
#[test]
fn basic_parsing() {
let fiin = common_setup();
2022-10-20 11:45:55 -04:00
assert_eq!(fiin.entries[0].file_name, "test.txt");
2022-10-20 11:44:54 -04:00
2022-10-20 11:45:55 -04:00
assert_eq!(fiin.entries[1].file_name, "test.exl");
2022-10-20 11:44:54 -04:00
}
}