Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
Run fmt and move csc stuff into lib
  • Loading branch information
widberg committed Oct 17, 2023
1 parent a4aa62c commit 3d34502
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 54 deletions.
8 changes: 3 additions & 5 deletions bff-cli/src/csc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io;
use std::io::{BufWriter, Read, Write};
use std::io::BufWriter;

use bff::csc::csc_copy;
use bff::BufReader;

use crate::error::BffCliResult;
Expand All @@ -9,10 +10,7 @@ pub fn csc() -> BffCliResult<()> {
let stdin = BufReader::new(io::stdin().lock());
let mut stdout = BufWriter::new(io::stdout().lock());

for byte in stdin.bytes() {
let byte = byte?;
stdout.write_all(&[!byte])?;
}
csc_copy(stdin, &mut stdout)?;

Ok(())
}
11 changes: 9 additions & 2 deletions bff-cli/src/lz.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use std::io::{self, Cursor, Read, Write};

use bff::lz::{compress_data_with_header_writer, decompress_data_with_header_parser, lz4_compress_data_with_header_writer, lz4_decompress_data_with_header_parser, lzo_compress, lzo_decompress};
use bff::{Endian, BufReader};
use bff::lz::{
compress_data_with_header_writer,
decompress_data_with_header_parser,
lz4_compress_data_with_header_writer,
lz4_decompress_data_with_header_parser,
lzo_compress,
lzo_decompress,
};
use bff::{BufReader, Endian};
use clap::ValueEnum;

use crate::error::BffCliResult;
Expand Down
2 changes: 1 addition & 1 deletion bff-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ mod error;
mod extract;
mod info;
mod lz;
mod psc;
mod reverse_crc32;
mod round_trip;
mod psc;

#[derive(Subcommand)]
enum Commands {
Expand Down
21 changes: 10 additions & 11 deletions bff-cli/src/psc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
use bff::BufReader;

use bff::psc::Psc;
use bff::BufReader;

use crate::error::BffCliResult;

pub fn extract_psc(
psc: &Path,
directory: &Path,
) -> BffCliResult<()> {
pub fn extract_psc(psc: &Path, directory: &Path) -> BffCliResult<()> {
let mut psc_reader = BufReader::new(File::open(psc)?);
let psc = bff::psc::Psc::read(&mut psc_reader)?;

Expand All @@ -24,7 +23,11 @@ pub fn extract_psc(
Ok(())
}

fn read_files_into_psc_recursively(psc: &mut Psc, directory: &Path, base: &Path) -> BffCliResult<()> {
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();
Expand All @@ -40,11 +43,7 @@ fn read_files_into_psc_recursively(psc: &mut Psc, directory: &Path, base: &Path)
Ok(())
}

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

pub fn create_psc(directory: &Path, psc_path: &Path) -> BffCliResult<()> {
let mut psc = Psc::default();
read_files_into_psc_recursively(&mut psc, directory, directory)?;

Expand Down
18 changes: 18 additions & 0 deletions bff/src/csc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::io::{Read, Write};

use crate::BffResult;

pub fn csc_copy<R: Read, W: Write>(reader: R, writer: &mut W) -> BffResult<()> {
for byte in reader.bytes() {
let byte = byte?;
writer.write_all(&[!byte])?;
}

Ok(())
}

pub fn csc_buffer(data: &mut [u8]) {
for byte in data {
*byte = !*byte;
}
}
43 changes: 26 additions & 17 deletions bff/src/helpers/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::str::from_utf8;

use bff_derive::ReferencedNames;
use binrw::io::{Read, Seek};
use binrw::{args, BinRead, BinResult, BinWrite, BinWriterExt, Endian, Error, NullString};
use binrw::meta::{EndianKind, ReadEndian, WriteEndian};
use binrw::{args, BinRead, BinResult, BinWrite, BinWriterExt, Endian, Error, NullString};
use derive_more::{Constructor, Deref, DerefMut, Display, Error, From, Into};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -232,27 +232,31 @@ impl BinWrite for PascalStringNull {
}

#[derive(
Clone,
PartialEq,
Eq,
Default,
Debug,
Deref,
DerefMut,
Display,
From,
Serialize,
Hash,
Deserialize,
ReferencedNames,
Clone,
PartialEq,
Eq,
Default,
Debug,
Deref,
DerefMut,
Display,
From,
Serialize,
Hash,
Deserialize,
ReferencedNames,
)]
#[serde(transparent)]
pub struct StringUntilNull(pub String);

impl BinRead for StringUntilNull {
type Args<'a> = ();

fn read_options<R: Read + Seek>(reader: &mut R, _endian: Endian, _args: Self::Args<'_>) -> BinResult<Self> {
fn read_options<R: Read + Seek>(
reader: &mut R,
_endian: Endian,
_args: Self::Args<'_>,
) -> BinResult<Self> {
let ascii_string_position = reader.stream_position()?;
let value = NullString::read(reader)?;
match from_utf8(&value) {
Expand All @@ -268,9 +272,14 @@ impl BinRead for StringUntilNull {
impl BinWrite for StringUntilNull {
type Args<'a> = ();

fn write_options<W: Write + Seek>(&self, writer: &mut W, _endian: Endian, _args: Self::Args<'_>) -> BinResult<()> {
fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
_endian: Endian,
_args: Self::Args<'_>,
) -> BinResult<()> {
let value = self.0.as_bytes();
writer.write_all(&value)?;
writer.write_all(value)?;
writer.write_be(&0u8)?;

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion bff/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
pub mod bigfile;
pub mod class;
pub mod crc;
pub mod csc;
pub mod error;
pub mod helpers;
pub mod lz;
pub mod macros;
pub mod names;
pub mod platforms;
pub mod psc;
pub mod traits;
pub mod versions;
pub mod psc;

pub type BffError = crate::error::Error;
pub type BffResult<T> = std::result::Result<T, BffError>;
Expand Down
32 changes: 23 additions & 9 deletions bff/src/lz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::cmp::{max, min};
use std::io::{Read, Seek, SeekFrom, Write};
use std::ptr::null_mut;

use binrw::{args, BinRead, BinReaderExt, BinResult, BinWrite, BinWriterExt};
use binrw::Endian;
use binrw::{args, BinRead, BinReaderExt, BinResult, BinWrite, BinWriterExt, Endian};
use minilzo_rs::LZO;

use crate::{BffResult};
use crate::BffResult;

#[binrw::parser(reader, endian)]
pub fn decompress_body_parser(decompressed_size: u32, compressed_size: u32) -> BinResult<Vec<u8>> {
Expand Down Expand Up @@ -421,10 +420,15 @@ pub fn lzo_decompress<R: Read>(reader: &mut R, _endian: Endian) -> BffResult<Vec
pub fn lz4_compress_data_writer(data: &[u8]) -> BinResult<()> {
// TODO: Don't use asserts. Add proper error handling.
// TODO: Not the same parameters used by the game.
let compress_bound = unsafe { lz4::liblz4::LZ4_compressBound(data.len() as i32) as usize};
let compress_bound = unsafe { lz4::liblz4::LZ4_compressBound(data.len() as i32) as usize };
let mut compressed_buffer: Vec<u8> = Vec::with_capacity(compress_bound);
unsafe {
let result = lz4::liblz4::LZ4_compress_default(data.as_ptr() as *const i8, compressed_buffer.as_mut_ptr() as *mut i8, data.len() as i32, compressed_buffer.capacity() as i32);
let result = lz4::liblz4::LZ4_compress_default(
data.as_ptr() as *const i8,
compressed_buffer.as_mut_ptr() as *mut i8,
data.len() as i32,
compressed_buffer.capacity() as i32,
);
assert!(result >= 0);
compressed_buffer.set_len(result as usize);
}
Expand Down Expand Up @@ -460,21 +464,31 @@ pub fn lz4_compress_data_with_header_writer<W: Write + Seek>(
}

#[binrw::parser(reader)]
pub fn lz4_decompress_data_parser(decompressed_size: u32, compressed_size: u32) -> BinResult<Vec<u8>> {
pub fn lz4_decompress_data_parser(
decompressed_size: u32,
compressed_size: u32,
) -> BinResult<Vec<u8>> {
// TODO: Don't use asserts. Add proper error handling.
if compressed_size != 0 {
let compressed_buffer = Vec::<u8>::read_args(reader, args! { count: compressed_size as usize })?;
let compressed_buffer =
Vec::<u8>::read_args(reader, args! { count: compressed_size as usize })?;
let mut decompressed_buffer: Vec<u8> = Vec::with_capacity(decompressed_size as usize);
unsafe {
let result = lz4::liblz4::LZ4_decompress_safe(compressed_buffer.as_ptr() as *const i8, decompressed_buffer.as_mut_ptr() as *mut i8, compressed_size as i32, decompressed_size as i32);
let result = lz4::liblz4::LZ4_decompress_safe(
compressed_buffer.as_ptr() as *const i8,
decompressed_buffer.as_mut_ptr() as *mut i8,
compressed_size as i32,
decompressed_size as i32,
);
assert!(result >= 0);
decompressed_buffer.set_len(result as usize);
}
assert_eq!(decompressed_buffer.len(), decompressed_size as usize);

Ok(decompressed_buffer)
} else {
let decompressed_buffer = Vec::<u8>::read_args(reader, args! { count: decompressed_size as usize })?;
let decompressed_buffer =
Vec::<u8>::read_args(reader, args! { count: decompressed_size as usize })?;
Ok(decompressed_buffer)
}
}
Expand Down
44 changes: 36 additions & 8 deletions bff/src/psc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::collections::HashMap;
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 binrw::{BinRead, BinResult, BinWrite, Endian};
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};
use crate::lz::{
lz4_compress_data_with_header_writer_internal,
lz4_decompress_data_with_header_parser_internal,
};
use crate::BffResult;

#[derive(Debug, Default)]
pub struct Psc {
Expand All @@ -24,7 +29,11 @@ impl WriteEndian for Psc {
impl BinRead for Psc {
type Args<'a> = ();

fn read_options<R: Read + Seek>(reader: &mut R, _endian: Endian, _args: Self::Args<'_>) -> BinResult<Self> {
fn read_options<R: Read + Seek>(
reader: &mut R,
_endian: Endian,
_args: Self::Args<'_>,
) -> BinResult<Self> {
let begin = reader.stream_position()?;
let end = reader.seek(SeekFrom::End(0))?;
reader.seek(SeekFrom::Start(begin))?;
Expand All @@ -49,9 +58,19 @@ impl BinRead for Psc {
impl BinWrite for Psc {
type Args<'a> = ();

fn write_options<W: Write + Seek>(&self, writer: &mut W, _endian: Endian, _args: Self::Args<'_>) -> BinResult<()> {
fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
_endian: Endian,
_args: Self::Args<'_>,
) -> BinResult<()> {
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(
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 All @@ -63,15 +82,24 @@ impl BinWrite for Psc {

impl Psc {
pub fn read<R: Read + Seek>(reader: &mut R) -> BffResult<Self> {
let mut psc_data = Cursor::new(lz4_decompress_data_with_header_parser_internal(reader, Endian::Little, ())?);
let mut psc_data = Cursor::new(lz4_decompress_data_with_header_parser_internal(
reader,
Endian::Little,
(),
)?);

Ok(<Self as BinRead>::read(&mut psc_data)?)
}

pub fn write<W: Write + Seek>(&self, writer: &mut W) -> BffResult<()> {
let mut psc_data = Cursor::new(Vec::new());
<Self as BinWrite>::write(self, &mut psc_data)?;
lz4_compress_data_with_header_writer_internal(&psc_data.into_inner(), writer, Endian::Little, ())?;
lz4_compress_data_with_header_writer_internal(
&psc_data.into_inner(),
writer,
Endian::Little,
(),
)?;
Ok(())
}
}

0 comments on commit 3d34502

Please sign in to comment.