directory path stuff, support uncompressed entries

This commit is contained in:
lifning 2021-01-14 00:51:08 -08:00
parent 23f8959b01
commit 218489cb37
4 changed files with 30 additions and 6 deletions

View File

@ -8,3 +8,4 @@ binread = "1"
binwrite = "0.2"
byteorder = "1"
prs-rust = { path = "../prs-rust" }
log = "0.4"

View File

@ -6,6 +6,7 @@ use std::fs::File;
use std::io::prelude::*;
use binread::prelude::*;
use one_rust::JodOne;
use std::path::PathBuf;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();
@ -13,8 +14,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut infile = File::open(arg)?;
let mut archive: JodOne = infile.read_le()?;
let entries = archive.extract();
let dir = PathBuf::from(format!("{}.d", arg));
for (name, data) in entries {
File::create(name)?.write_all(&data)?;
let full_path = dir.join(name);
if let Some(path) = full_path.parent() {
std::fs::create_dir_all(path)?;
}
File::create(full_path)?.write_all(&data)?;
}
}
Ok(())

View File

@ -2,6 +2,7 @@ extern crate byteorder;
extern crate prs_rust;
#[macro_use] extern crate binread;
#[macro_use] extern crate binwrite;
#[macro_use] extern crate log;
mod one;
pub use one::*;

View File

@ -2,8 +2,9 @@ use binread::{FilePtr, NullString};
use prs_rust::prs;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
use std::fmt::Formatter;
#[derive(BinRead)]
#[br(little, magic = b"ThisIsOneFile\0\0\0")]
@ -32,13 +33,28 @@ pub struct JodOneEntry {
pub name: NullString,
}
impl std::fmt::Debug for JodOneEntry {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "JodOneEntry{{id: {}, name: {:?}, size_cmp: {}, size_dec: {}, ..}}", self.id, self.name.to_string(), self.size_cmp, self.size_dec)
}
}
impl JodOne {
pub fn extract(&mut self) -> BTreeMap<PathBuf, Vec<u8>> {
let mut map = BTreeMap::new();
for entry in &self.entries {
// TODO: improve prs crate to use size_dec hint?
let dec_buf = prs::decompress::Decompressor::new(&*entry.data).decompress();
map.insert(PathBuf::from(OsStr::from_bytes(&entry.name.0)), dec_buf);
debug!("{:?}", entry);
let os_string = OsString::from_vec(entry.name.0.iter()
.map(|c| if char::from(*c) == '\\' { std::path::MAIN_SEPARATOR as u8 } else { *c })
.collect());
let key = PathBuf::from(os_string);
if entry.size_dec == 0 {
map.insert(key, entry.data.clone());
} else {
// TODO: improve prs crate to use size_dec hint?
let dec_buf = prs::decompress::Decompressor::new(&*entry.data).decompress();
map.insert(key, dec_buf);
}
}
map
}