#!/bin/bash # ToDo: The success and failure used in the sumamry doesnt seem to increment ################################################################################ # Script: convertFromZipToPS3.sh # Description: Converts ZIP files containing PlayStation 3 ISO images into a # format suitable for PS3 emulation. The script extracts the contents # of the ZIP file, decrypts the ISO using a provided key, and # optionally converts the decrypted files to a .squashfs format. # # Usage: ./convertFromZipToPS3.sh [-d|--delete] [-r|--recursive ] [-f|--folder] [-i|--iso] ... # Options: # -d, --delete Delete the original ZIP file after conversion. # -r, --recursive Recursively find and convert ZIP files in the specified path. # -f, --folder Stop after creating the PS3 folder, do not convert to .squashfs. # -i, --iso Stop after creating the decrypted ISO file. # # Dependencies: # - PS3Dec (for decrypting ISO files). # - mksquashfs (for creating .squashfs files). # - unzip (for extracting ZIP files). # - 7z (for extracting decrypted ISO files). # # Logging: # - Uses logger.sh for consistent and color-coded logging. # - Log levels: INFO, DEBUG, ERROR. # # Example: # ./convertFromZipToPS3.sh -d "Game.zip" # ./convertFromZipToPS3.sh -r /path/to/roms -f ################################################################################ # Determine the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Load configuration and logger from the script's directory source "${SCRIPT_DIR}/config.sh" source "${SCRIPT_DIR}/logger.sh" # Log the script name when it is called log "DEBUG" "Starting script: $(basename "$0")" # Function to display usage usage() { log "INFO" "Usage: $0 [-d|--delete] [-r|--recursive ] [-f|--folder] [-i|--iso] ..." log "INFO" "Options:" log "INFO" " -d, --delete Delete the original zip file after conversion" log "INFO" " -r, --recursive Recursively find and convert zip files in the specified path" log "INFO" " -f, --folder Stop after creating the PS3 folder, do not convert to .squashfs" log "INFO" " -i, --iso Stop after creating the decrypted ISO file" exit 1 } # Check if at least one argument is provided if [ "$#" -lt 1 ]; then usage fi # Initialize variables DELETE_ORIGINAL=false RECURSIVE=false RECURSIVE_PATH="" FOLDER_ONLY=false ISO_ONLY=false SUCCESS_COUNT=0 FAILURE_COUNT=0 FAILED_FILES=() # Parse options while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in -d | --delete ) DELETE_ORIGINAL=true log "DEBUG" "DELETE_ORIGINAL set to true" ;; -r | --recursive ) RECURSIVE=true shift RECURSIVE_PATH=$1 log "DEBUG" "RECURSIVE set to true with path: $RECURSIVE_PATH" ;; -f | --folder ) FOLDER_ONLY=true log "DEBUG" "FOLDER_ONLY set to true" ;; -i | --iso ) ISO_ONLY=true log "DEBUG" "ISO_ONLY set to true" ;; * ) usage ;; esac shift done # Check if there are any zip files provided or a recursive path if [ "$RECURSIVE" = false ] && [ "$#" -lt 1 ]; then usage fi # Function to extract a zip file extract_zip() { local zipfile=$1 local work_dir=$2 log "DEBUG" "Extracting $zipfile to $work_dir" unzip -q -o "$zipfile" -d "$work_dir" # -o: overwrite files without prompting } # Function to extract the key file extract_key_file() { local keyfile=$1 local work_dir=$2 log "DEBUG" "Extracting key file $keyfile to $work_dir" unzip -q -o "$keyfile" -d "$work_dir" # -o: overwrite files without prompting } # Function to decrypt the ISO file decrypt_iso() { local isofile=$1 local key=$2 local decrypted_iso="${isofile%.iso}.dec.iso" log "DEBUG" "Decrypting $isofile to $decrypted_iso using key: $key" /usr/local/bin/PS3Dec d key "$key" "$isofile" "$decrypted_iso" if [ $? -ne 0 ]; then log "ERROR" "Failed to decrypt $isofile" return 1 fi mv "$decrypted_iso" "$isofile" } # Function to convert to squashfs convert_to_squashfs() { local source_dir=$1 local output_file=$2 log "DEBUG" "Converting $source_dir to .squashfs: $output_file" mksquashfs "$source_dir" "$output_file" -noappend -comp xz } # Function to process zip files process_zip() { local zipfile=$1 local zipdir=$(dirname "$zipfile") # Get the directory of the zip file local name=$(basename "${zipfile%.zip}") # Remove '.zip' from file name and get the base name log "INFO" "Processing zip file: $zipfile" # Create a temporary directory WORK_DIR=$(mktemp -d) log "DEBUG" "Created temporary directory: $WORK_DIR" # Function to clean up temporary files cleanup() { log "DEBUG" "Cleaning up temporary directory: $WORK_DIR" rm -rf "$WORK_DIR" } # Trap to ensure cleanup is done on exit trap cleanup EXIT log "DEBUG" "Extracting: $name" extract_zip "$zipfile" "$WORK_DIR" if [ $? -ne 0 ]; then log "ERROR" "Failed to unzip $zipfile" FAILURE_COUNT=$((FAILURE_COUNT + 1)) FAILED_FILES+=("$zipfile") return 1 fi # Unzip the correct key file keyfile="${name}.zip" if [ -f "$PS3DKEYDIR/$keyfile" ]; then extract_key_file "$PS3DKEYDIR/$keyfile" "$WORK_DIR" if [ $? -ne 0 ]; then log "ERROR" "Failed to unzip key file $keyfile" FAILURE_COUNT=$((FAILURE_COUNT + 1)) FAILED_FILES+=("$zipfile") return 1 fi key=$(head -c 32 "$WORK_DIR/${name}.dkey" | tr -d '\n') else log "ERROR" "Key file $keyfile not found" FAILURE_COUNT=$((FAILURE_COUNT + 1)) FAILED_FILES+=("$zipfile") return 1 fi # Decrypt the ISO file for isofile in "$WORK_DIR"/*.iso; do if [ -f "$isofile" ]; then decrypt_iso "$isofile" "$key" if [ $? -ne 0 ]; then log "ERROR" "Failed to decrypt $isofile" FAILURE_COUNT=$((FAILURE_COUNT + 1)) FAILED_FILES+=("$zipfile") return 1 fi fi done # If ISO_ONLY is true, stop here if [ "$ISO_ONLY" = true ]; then log "DEBUG" "ISO_ONLY set to true, moving decrypted ISO to $zipdir" mv "$isofile" "$zipdir" SUCCESS_COUNT=$((SUCCESS_COUNT + 1)) return 0 fi # Uncompress Decrypted ISO log "DEBUG" "Extracting decrypted ISO to $WORK_DIR/${name}.ps3" 7z x "$isofile" -o"${WORK_DIR}/${name}.ps3" if [ $? -ne 0 ]; then log "ERROR" "Failed to extract decrypted ISO" FAILURE_COUNT=$((FAILURE_COUNT + 1)) FAILED_FILES+=("$zipfile") return 1 fi if [ "$FOLDER_ONLY" = true ]; then log "DEBUG" "FOLDER_ONLY set to true, moving PS3 folder to $zipdir" mv "$WORK_DIR/${name}.ps3" "$zipdir" SUCCESS_COUNT=$((SUCCESS_COUNT + 1)) return 0 fi # Convert to .squashfs with the correct naming squashfs_name="${name}.ps3.squashfs" log "DEBUG" "Converting $WORK_DIR/${name}.ps3 to .squashfs: $zipdir/$squashfs_name" convert_to_squashfs "$WORK_DIR/${name}.ps3" "$zipdir/$squashfs_name" if [ $? -ne 0 ]; then log "ERROR" "Failed to convert $name to .squashfs" FAILURE_COUNT=$((FAILURE_COUNT + 1)) FAILED_FILES+=("$zipfile") return 1 fi # Delete the original zip file if the delete option is set if [ "$DELETE_ORIGINAL" = true ]; then log "DEBUG" "Deleting original zip file: $zipfile" rm -f "$zipfile" fi SUCCESS_COUNT=$((SUCCESS_COUNT + 1)) return 0 } # Process zip files recursively if the recursive option is set if [ "$RECURSIVE" = true ]; then log "INFO" "Recursively finding and converting zip files in: $RECURSIVE_PATH" find "$RECURSIVE_PATH" -type f -name "*.zip" | while read -r zipfile; do process_zip "$zipfile" done else # Process each zip file provided as an argument for zipfile in "$@"; do if [ -f "$zipfile" ]; then process_zip "$zipfile" else log "ERROR" "File $zipfile does not exist" FAILURE_COUNT=$((FAILURE_COUNT + 1)) FAILED_FILES+=("$zipfile") fi done fi # Display summary log "INFO" "Summary:" log "INFO" "Successfully converted files: $SUCCESS_COUNT" log "INFO" "Failed conversions: $FAILURE_COUNT" if [ "$FAILURE_COUNT" -gt 0 ]; then log "INFO" "Files that couldn't be converted:" for failed_file in "${FAILED_FILES[@]}"; do log "INFO" "$failed_file" done fi log "INFO" "Conversion completed successfully" exit 0