chore(*): cargo fmt

This commit is contained in:
Lily Tsuru 2023-06-28 19:27:15 -04:00
parent 15d438f294
commit 94823a21e3
12 changed files with 50 additions and 44 deletions

View File

@ -3,8 +3,8 @@
pub mod package; pub mod package;
pub mod package_toc; pub mod package_toc;
pub mod ps2_texture;
pub mod ps2_palette; pub mod ps2_palette;
pub mod ps2_texture;
/// A trait validatable format objects should implement. /// A trait validatable format objects should implement.
/// TODO: integrate this with some FourCC crate, or re-invent the wheel. /// TODO: integrate this with some FourCC crate, or re-invent the wheel.
@ -13,25 +13,25 @@ pub trait Validatable {
fn valid(&self) -> bool; fn valid(&self) -> bool;
} }
/// Make a Rust [String] from a byte slice that came from a C string/structure. /// Make a Rust [String] from a byte slice that came from a C string/structure.
/// ///
/// # Usage /// # Usage
/// ///
/// The byte slice has to be a valid UTF-8 string. /// The byte slice has to be a valid UTF-8 string.
/// (Note that in most cases, ASCII strings are valid UTF-8, so this isn't something you'll particularly /// (Note that in most cases, ASCII strings are valid UTF-8, so this isn't something you'll particularly
/// have to worry about). /// have to worry about).
/// ///
/// # Safety /// # Safety
/// ///
/// This function does not directly make use of any unsafe Rust code. /// This function does not directly make use of any unsafe Rust code.
pub fn make_c_string(bytes: &[u8]) -> Option<String> { pub fn make_c_string(bytes: &[u8]) -> Option<String> {
let bytes_without_null = match bytes.iter().position(|&b| b == 0) { let bytes_without_null = match bytes.iter().position(|&b| b == 0) {
Some(ix) => &bytes[..ix], Some(ix) => &bytes[..ix],
None => bytes, None => bytes,
}; };
match std::str::from_utf8(bytes_without_null).ok() { match std::str::from_utf8(bytes_without_null).ok() {
Some(string) => Some(String::from(string)), Some(string) => Some(String::from(string)),
None => None None => None,
} }
} }

View File

@ -1,7 +1,7 @@
//! Package file format structures //! Package file format structures
use crate::lzss::header::LzssHeader;
use super::Validatable; use super::Validatable;
use crate::lzss::header::LzssHeader;
/// "EOF" header. The QuickBMS script uses this to seek to the PGRP entry. /// "EOF" header. The QuickBMS script uses this to seek to the PGRP entry.
#[repr(C)] #[repr(C)]
@ -13,7 +13,7 @@ pub struct PackageEofHeader {
pub stringtable_size: u32, pub stringtable_size: u32,
/// Start offset of the [PackageGroup] in the package file. /// Start offset of the [PackageGroup] in the package file.
pub header_start_offset: u32 pub header_start_offset: u32,
} }
/// A Package Group. I have no idea what this is yet /// A Package Group. I have no idea what this is yet
@ -29,12 +29,12 @@ pub struct PackageGroup {
pub group_file_count: u32, pub group_file_count: u32,
/// Padding. Set to a fill of 0xCD. /// Padding. Set to a fill of 0xCD.
pub pad: u32 pub pad: u32,
} }
impl PackageGroup { impl PackageGroup {
/// 'PGRP' /// 'PGRP'
pub const VALID_FOURCC : u32 = 0x50524750; pub const VALID_FOURCC: u32 = 0x50524750;
} }
impl Validatable for PackageGroup { impl Validatable for PackageGroup {
@ -87,13 +87,12 @@ pub struct PackageFileChunk {
pub file_uncompressed_size: u32, pub file_uncompressed_size: u32,
/// LZSS header. Only used if the file chunk is compressed. /// LZSS header. Only used if the file chunk is compressed.
pub lzss_header: LzssHeader pub lzss_header: LzssHeader,
} }
impl PackageFileChunk { impl PackageFileChunk {
/// 'PFIL' /// 'PFIL'
pub const VALID_FOURCC : u32 = 0x4C494650; pub const VALID_FOURCC: u32 = 0x4C494650;
pub fn is_compressed(&self) -> bool { pub fn is_compressed(&self) -> bool {
// If the compressed size matches the uncompressed size // If the compressed size matches the uncompressed size

View File

@ -17,12 +17,12 @@ pub struct Ps2PaletteHeader {
pub data_start: u32, pub data_start: u32,
pub header_size: u32, pub header_size: u32,
pub pad: [u32; 6] // reserved for game code, like .ps2_texture? pub pad: [u32; 6], // reserved for game code, like .ps2_texture?
} }
impl Ps2PaletteHeader { impl Ps2PaletteHeader {
/// 'PAL1' /// 'PAL1'
pub const VALID_FOURCC : u32 = 0x314c4150; pub const VALID_FOURCC: u32 = 0x314c4150;
} }
impl Validatable for Ps2PaletteHeader { impl Validatable for Ps2PaletteHeader {

View File

@ -65,9 +65,8 @@ impl Crc32Hash {
pub fn update_case_insensitive(&self, data: &[u8]) { pub fn update_case_insensitive(&self, data: &[u8]) {
for b in data.iter() { for b in data.iter() {
let old = self.state.take(); let old = self.state.take();
self.state.set( self.state
CRC32_TABLE[((old ^ (*b & !0x20) as u32) & 0xff) as usize] ^ (old >> 8), .set(CRC32_TABLE[((old ^ (*b & !0x20) as u32) & 0xff) as usize] ^ (old >> 8));
);
} }
} }

View File

@ -1,5 +1,5 @@
//! Utilities for making .DAT and .MET file names //! Utilities for making .DAT and .MET file names
//! //!
//! .DAT and .MET filenames are formatted like "{:X}.DAT" in fmt parlance. //! .DAT and .MET filenames are formatted like "{:X}.DAT" in fmt parlance.
//! The name component is the CRC32 of the original filename. //! The name component is the CRC32 of the original filename.
//! //!

View File

@ -1,5 +1,5 @@
use std::mem::size_of;
use crate::format::Validatable; use crate::format::Validatable;
use std::mem::size_of;
#[repr(C)] #[repr(C)]
#[derive(Debug, Default)] #[derive(Debug, Default)]

View File

@ -1,9 +1,8 @@
//! LZSS //! LZSS
// TODO: // TODO:
// - Port decompression code to Rust // - Port decompression code to Rust
// - investigate using `lzss` crate? // - investigate using `lzss` crate?
// - if we can't do that, investigate reimplementing compression based on `lzss` crate? // - if we can't do that, investigate reimplementing compression based on `lzss` crate?
pub mod header; pub mod header;

View File

@ -1,10 +1,8 @@
use std::fs::File; use std::fs::File;
use std::io::{Seek, SeekFrom}; use std::io::{Seek, SeekFrom};
use std::path::{PathBuf}; use std::path::PathBuf;
use crate::format::{ use crate::format::package_toc::*;
package_toc::*
};
use binext::BinaryRead; use binext::BinaryRead;
use thiserror::Error; use thiserror::Error;
@ -30,13 +28,13 @@ pub fn read_package_toc(path: PathBuf) -> Result<Vec<PackageTocEntry>> {
let file_size = toc_file.seek(SeekFrom::End(0))?; let file_size = toc_file.seek(SeekFrom::End(0))?;
toc_file.seek(SeekFrom::Start(0))?; toc_file.seek(SeekFrom::Start(0))?;
let vec_size : usize = file_size as usize / std::mem::size_of::<PackageTocEntry>(); let vec_size: usize = file_size as usize / std::mem::size_of::<PackageTocEntry>();
if vec_size == 0 { if vec_size == 0 {
return Err(Error::FileTooSmall); return Err(Error::FileTooSmall);
} }
let mut vec : Vec<PackageTocEntry> = Vec::with_capacity(vec_size); let mut vec: Vec<PackageTocEntry> = Vec::with_capacity(vec_size);
for _ in 0..vec_size { for _ in 0..vec_size {
vec.push(toc_file.read_binary::<PackageTocEntry>()?); vec.push(toc_file.read_binary::<PackageTocEntry>()?);

View File

@ -91,7 +91,7 @@ impl Ps2TextureReader {
); );
} else { } else {
// this is a bad error to return here, but for now it works I guess // this is a bad error to return here, but for now it works I guess
return Err(Error::InvalidHeader); return Err(Error::InvalidHeader);
} }
} }
None => { None => {

View File

@ -27,7 +27,7 @@ impl Ps2Rgba {
r: ((value & 0x7C00) >> 7) as u8, r: ((value & 0x7C00) >> 7) as u8,
g: ((value & 0x03E0) >> 2) as u8, g: ((value & 0x03E0) >> 2) as u8,
b: ((value & 0x001F) << 3) as u8, b: ((value & 0x001F) << 3) as u8,
a: 255 a: 255,
}; };
} }
} }

View File

@ -2,9 +2,9 @@
//! This program should be run in the root directory //! This program should be run in the root directory
//! of an extracted (from image) copy of the game. //! of an extracted (from image) copy of the game.
use std::{fs, path::Path};
use jmmt::hash::filename::*; use jmmt::hash::filename::*;
use jmmt::read::package_toc::{read_package_toc}; use jmmt::read::package_toc::read_package_toc;
use std::{fs, path::Path};
// TODO: A mode that will re-name everything back? This wouldn't be too hard to implement // TODO: A mode that will re-name everything back? This wouldn't be too hard to implement
@ -21,14 +21,22 @@ fn main() {
match read_package_toc(Path::new(package_toc_filename.as_str()).to_path_buf()) { match read_package_toc(Path::new(package_toc_filename.as_str()).to_path_buf()) {
Ok(toc) => { Ok(toc) => {
for toc_entry in toc { for toc_entry in toc {
let dat_src = format!("DATA/{}", dat_filename_from_hash(toc_entry.file_name_hash())); let dat_src = format!(
"DATA/{}",
dat_filename_from_hash(toc_entry.file_name_hash())
);
let src_path = Path::new(dat_src.as_str()); let src_path = Path::new(dat_src.as_str());
let dat_clearname = format!("DATA/{}", toc_entry.file_name().expect("How did invalid ASCII get here?")); let dat_clearname = format!(
"DATA/{}",
toc_entry
.file_name()
.expect("How did invalid ASCII get here?")
);
let dest_path = Path::new(dat_clearname.as_str()); let dest_path = Path::new(dat_clearname.as_str());
if src_path.exists() { if src_path.exists() {
match fs::rename(src_path, dest_path) { match fs::rename(src_path, dest_path) {
Ok(_) => {}, Ok(_) => {}
Err(error) => { Err(error) => {
println!("Error renaming {}: {}", src_path.display(), error); println!("Error renaming {}: {}", src_path.display(), error);
return (); return ();
@ -38,8 +46,11 @@ fn main() {
} }
} }
match fs::rename(Path::new(package_toc_filename.as_str()), Path::new("DATA/package.toc")) { match fs::rename(
Ok(_) => {}, Path::new(package_toc_filename.as_str()),
Path::new("DATA/package.toc"),
) {
Ok(_) => {}
Err(error) => { Err(error) => {
println!("Error renaming TOC file: {}", error); println!("Error renaming TOC file: {}", error);
return (); return ();

View File

@ -1,7 +1,7 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use std::path::Path;
use jmmt::read::texture::Ps2TextureReader; use jmmt::read::texture::Ps2TextureReader;
use std::path::Path;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]