Skip to content

Commit

Permalink
Create psc works!
Browse files Browse the repository at this point in the history
Game runs fine with repacked psc
  • Loading branch information
widberg committed Oct 17, 2023
1 parent bb125f7 commit a4aa62c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions bff-cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub enum BffCliError {
Bff(BffError),
Io(std::io::Error),
SerdeJson(serde_json::Error),
StripPrefix(std::path::StripPrefixError),
#[display(
fmt = "No filler found in length range [{}, {}], consider expanding the range",
"min_filler_length",
Expand Down
29 changes: 27 additions & 2 deletions bff-cli/src/psc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
use bff::BufReader;
use bff::psc::Psc;
use crate::error::BffCliResult;

pub fn extract_psc(
Expand All @@ -23,9 +24,33 @@ pub fn extract_psc(
Ok(())
}

fn read_files_into_psc_recursively(psc: &mut Psc, directory: &Path, base: &Path) -> BffCliResult<()> {
let paths = std::fs::read_dir(directory)?;
for path in paths {
let path = path?.path();

if path.is_dir() {
read_files_into_psc_recursively(psc, &path, base)?;
} else {
let tsc = std::fs::read_to_string(&path)?;
let relative_path = path.strip_prefix(base)?.to_path_buf();
psc.tscs.insert(relative_path, tsc);
}
}
Ok(())
}

pub fn create_psc(
_directory: &Path,
_psc: &Path,
directory: &Path,
psc_path: &Path,
) -> BffCliResult<()> {

let mut psc = Psc::default();
read_files_into_psc_recursively(&mut psc, directory, directory)?;

let mut psc_writer = BufWriter::new(File::create(psc_path)?);

psc.write(&mut psc_writer)?;

Ok(())
}
11 changes: 5 additions & 6 deletions bff/src/psc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use std::path::PathBuf;
use binrw::{BinRead, BinResult, BinWrite, Endian};
use binrw::meta::{EndianKind, ReadEndian, WriteEndian};
use itertools::Itertools;
use crate::BffResult;
use crate::helpers::StringUntilNull;
use crate::lz::{lz4_compress_data_with_header_writer_internal, lz4_decompress_data_with_header_parser_internal};

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Psc {
pub tscs: HashMap<PathBuf, String>,
}
Expand All @@ -28,9 +29,7 @@ impl BinRead for Psc {
let end = reader.seek(SeekFrom::End(0))?;
reader.seek(SeekFrom::Start(begin))?;

let mut psc = Self {
tscs: HashMap::new(),
};
let mut psc = Self::default();

while reader.stream_position()? != end {
let path_string = StringUntilNull::read(reader)?.0;
Expand All @@ -51,8 +50,8 @@ impl BinWrite for Psc {
type Args<'a> = ();

fn write_options<W: Write + Seek>(&self, writer: &mut W, _endian: Endian, _args: Self::Args<'_>) -> BinResult<()> {
for (name, data) in &self.tscs {
writer.write_all(name.to_str().unwrap().as_bytes())?;
for (path, data) in self.tscs.iter() {
writer.write_all(path.components().map(|x| x.as_os_str().to_str().unwrap()).join("\\").as_bytes())?;
writer.write_all(&[0x00, 0x0D, 0x0A])?;
writer.write_all(data.as_bytes())?;
writer.write_all(&[0x00])?;
Expand Down

0 comments on commit a4aa62c

Please sign in to comment.