psarc2 0.1.0

PlayStation archive reader
Documentation
//! Simple CLI example for reading `.psarc` files.

use std::{fs::File, io::Write as _, path::PathBuf};

use anyhow::Result;
use clap::{Parser, Subcommand};
use psarc2::{PlaystationArchive, read::Config, toc::DecryptionKey};

/// Command line arguments.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, propagate_version = true)]
struct Cli {
    /// Path to a Rocksmith '*.psarc' file.
    path: PathBuf,
    /// Decryption key as hex values, if required.
    #[arg(global(true), value_parser = parse_hex_arg::<32>, short = 'k', long, requires("decryption_iv"))]
    decryption_key: Option<[u8; 32]>,
    /// Decryption key as hex values, if required.
    #[arg(global(true), value_parser = parse_hex_arg::<16>, short = 'i', long, requires("decryption_key"))]
    decryption_iv: Option<[u8; 16]>,
    /// Subcommands.
    #[command(subcommand)]
    command: Commands,
}

/// CLI subcommands.
#[derive(Debug, Subcommand)]
enum Commands {
    /// List all paths in the psarc file.
    List,
    /// Export all file to the target destination.
    ExtractAll {
        /// Target directory of the archive.
        target: PathBuf,
    },
    /// Export a specific file to the target destination.
    Extract {
        /// Which file to export.
        path: String,
        /// Target destination of the file.
        target: PathBuf,
    },
}

fn main() -> Result<()> {
    // Parse command line arguments
    let cli = Cli::parse();

    // Open the archive
    let file = File::open(cli.path)?;

    // Set the config from the CLI args
    let config = Config {
        decryption_key: cli
            .decryption_key
            .zip(cli.decryption_iv)
            .map(|(key, iv)| DecryptionKey { key, iv }),
    };

    // Read the archive
    let mut archive = PlaystationArchive::with_config(config, file)?;

    match cli.command {
        Commands::List => archive
            .paths()
            .for_each(|file| println!("{}", file.display())),
        Commands::Extract { path, target } => {
            let extracted = archive.by_path(&path)?;

            let mut target_file = File::create(&target)?;
            target_file.write_all(&extracted.into_inner())?;

            println!("written to {}", target.display());
        }
        Commands::ExtractAll { target } => archive.extract(target)?,
    }

    Ok(())
}

/// Parse a hex string CLI argument.
fn parse_hex_arg<const N: usize>(argument: &str) -> Result<[u8; N], String> {
    // Remove the "0x" prefix if it exists
    let hex_data = argument.strip_prefix("0x").unwrap_or(argument);

    // Hex-decode as bytes
    let bytes = hex::decode(hex_data)
        .map_err(|error| format!("Invalid hex string '{argument}': {error}"))?;
    let len = bytes.len();

    // Convert into array
    bytes
        .try_into()
        .map_err(|_| format!("Expected {N} bytes ({} hex chars), got {len} bytes", N * 2))
}