2023-08-06 08:25:04 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
use std::fs::read;
|
|
|
|
use std::io::Cursor;
|
2024-04-15 19:03:02 -04:00
|
|
|
use std::path::Path;
|
2022-08-06 20:38:15 -04:00
|
|
|
|
2023-09-22 17:44:31 -04:00
|
|
|
use binrw::{BinRead, BinWrite};
|
2023-08-06 08:25:04 -04:00
|
|
|
use binrw::binrw;
|
2023-10-13 16:16:04 -04:00
|
|
|
use crate::{ByteBuffer, ByteSpan};
|
2023-08-06 08:25:04 -04:00
|
|
|
|
|
|
|
use crate::sha1::Sha1;
|
|
|
|
|
2022-08-06 21:16:09 -04:00
|
|
|
#[binrw]
|
|
|
|
#[brw(magic = b"FileInfo")]
|
2022-08-06 20:38:15 -04:00
|
|
|
#[derive(Debug)]
|
2022-10-13 17:11:03 -04:00
|
|
|
#[brw(little)]
|
2023-12-02 20:28:28 -05:00
|
|
|
/// File info, which contains SHA1 of one or more files
|
2022-08-06 20:38:15 -04:00
|
|
|
pub struct FileInfo {
|
2022-08-06 21:16:09 -04:00
|
|
|
#[brw(pad_before = 16)]
|
|
|
|
#[bw(calc = 1024)]
|
2023-09-22 19:17:24 -04:00
|
|
|
_unknown: i32,
|
2022-08-06 21:16:09 -04:00
|
|
|
|
2022-08-06 20:38:15 -04:00
|
|
|
#[br(temp)]
|
2022-08-06 21:16:09 -04:00
|
|
|
#[bw(calc = (entries.len() * 96) as i32)]
|
2022-08-16 11:52:07 -04:00
|
|
|
entries_size: i32,
|
2022-08-06 20:38:15 -04:00
|
|
|
|
2022-08-06 21:16:09 -04:00
|
|
|
#[brw(pad_before = 992)]
|
2022-08-06 20:38:15 -04:00
|
|
|
#[br(count = entries_size / 96)]
|
2023-12-02 20:28:28 -05:00
|
|
|
/// File info entries
|
2022-10-20 11:44:54 -04:00
|
|
|
pub entries: Vec<FIINEntry>,
|
2022-08-06 20:38:15 -04:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:16:09 -04:00
|
|
|
#[binrw]
|
2022-08-06 20:38:15 -04:00
|
|
|
#[derive(Debug)]
|
2023-12-02 20:28:28 -05:00
|
|
|
/// A file info entry
|
2022-08-06 20:38:15 -04:00
|
|
|
pub struct FIINEntry {
|
2023-12-02 20:28:28 -05:00
|
|
|
/// File size (in bytes)
|
2022-10-20 11:44:54 -04:00
|
|
|
pub file_size: i32,
|
2022-08-06 20:38:15 -04:00
|
|
|
|
2023-12-02 20:28:28 -05:00
|
|
|
/// The file name
|
2022-08-06 21:16:09 -04:00
|
|
|
#[brw(pad_before = 4)]
|
2022-08-06 20:38:15 -04:00
|
|
|
#[br(count = 64)]
|
2022-08-06 21:16:09 -04:00
|
|
|
#[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,
|
2022-08-06 20:38:15 -04:00
|
|
|
|
2023-12-02 20:28:28 -05:00
|
|
|
/// SHA1 of the file
|
2022-08-06 20:38:15 -04:00
|
|
|
#[br(count = 24)]
|
2022-08-06 21:16:09 -04:00
|
|
|
#[bw(pad_size_to = 24)]
|
2022-10-20 11:44:54 -04:00
|
|
|
pub sha1: Vec<u8>,
|
2022-08-06 20:38:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FileInfo {
|
2022-08-06 21:21:55 -04:00
|
|
|
/// Parses an existing FIIN file.
|
2023-10-13 16:16:04 -04:00
|
|
|
pub fn from_existing(buffer: ByteSpan) -> Option<FileInfo> {
|
2022-08-06 20:38:15 -04:00
|
|
|
let mut cursor = Cursor::new(buffer);
|
2022-08-16 11:50:18 -04:00
|
|
|
FileInfo::read(&mut cursor).ok()
|
2022-08-06 20:38:15 -04:00
|
|
|
}
|
2022-08-06 21:16:09 -04:00
|
|
|
|
2023-12-02 20:28:28 -05:00
|
|
|
/// Writes file info into a new file
|
2023-10-13 16:16:04 -04:00
|
|
|
pub fn write_to_buffer(&self) -> Option<ByteBuffer> {
|
|
|
|
let mut buffer = ByteBuffer::new();
|
2023-09-22 17:44:31 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut cursor = Cursor::new(&mut buffer);
|
|
|
|
self.write(&mut cursor).ok()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(buffer)
|
|
|
|
}
|
|
|
|
|
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.
|
2024-04-15 19:03:02 -04:00
|
|
|
///
|
|
|
|
/// These paths are converted to just their filenames.
|
2022-08-06 21:21:55 -04:00
|
|
|
///
|
|
|
|
/// The new FileInfo structure can then be serialized back into retail-compatible form.
|
2024-04-15 19:03:02 -04:00
|
|
|
pub fn new(files: &[&str]) -> Option<FileInfo> {
|
2022-08-06 21:16:09 -04:00
|
|
|
let mut entries = vec![];
|
|
|
|
|
2024-04-15 19:03:02 -04:00
|
|
|
for path in files {
|
|
|
|
let file = &read(path).expect("Cannot read file.");
|
2022-08-16 11:52:07 -04:00
|
|
|
|
2022-08-06 21:16:09 -04:00
|
|
|
entries.push(FIINEntry {
|
|
|
|
file_size: file.len() as i32,
|
2024-04-15 19:03:02 -04:00
|
|
|
file_name: Path::new(path).file_name()?.to_str()?.to_string(),
|
2022-10-25 13:00:05 -04:00
|
|
|
sha1: Sha1::from(file).digest().bytes().to_vec(),
|
2022-08-06 21:16:09 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
Some(FileInfo { entries })
|
2022-08-06 21:16:09 -04:00
|
|
|
}
|
2022-08-16 11:52:07 -04:00
|
|
|
}
|
2022-10-20 11:44:54 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-04-16 21:19:08 -04:00
|
|
|
use std::fs::read;
|
2022-10-20 11:44:54 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-08-06 08:25:04 -04:00
|
|
|
use crate::fiin::FileInfo;
|
|
|
|
|
2022-10-20 11:44:54 -04:00
|
|
|
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
|
|
|
}
|
2024-04-15 19:03:02 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_writing() {
|
|
|
|
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
d.push("resources/tests");
|
|
|
|
d.push("test.fiin");
|
|
|
|
|
|
|
|
let valid_fiin = &read(d).unwrap();
|
|
|
|
|
|
|
|
let mut d2 = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
d2.push("resources/tests");
|
|
|
|
d2.push("test.txt");
|
|
|
|
|
|
|
|
let mut d3 = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
d3.push("resources/tests");
|
|
|
|
d3.push("test.exl");
|
|
|
|
|
|
|
|
let testing_fiin = FileInfo::new(&[
|
|
|
|
d2.to_str().unwrap(),
|
|
|
|
d3.to_str().unwrap()
|
|
|
|
]).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(*valid_fiin, testing_fiin.write_to_buffer().unwrap());
|
|
|
|
}
|
2022-10-20 11:44:54 -04:00
|
|
|
}
|