Script to extract rom from archive

That’s alright. I already added the cleanup method cleanup_files(). Same counts for the extract method which extracts depending on the archive type.

1 Like

Hello again :blush: hope you’ve been well. I’ve finally gotten some free time again. That code looks amazing! :grinning_face_with_smiling_eyes:

I just gave it a spin and there appears to be something wrong around the cleanup cache area of the code, but not sure what the issue is. Will do a little more googling and see if I can figure it out…

Start monitoring process.
Cleaning up old files
find: ‘/media/storage/Games/Games/Cache/*’: No such file or directory
Extracting Game from Archive
No existing ROM image found, extracting...
/media/storage/Games/Utilities/Lutris/extraction_test_03.sh: line 57: [: too many arguments
'/media/storage/test/Minecraft (World) (En,Ja,Fr,De,Es,It,Nl,Pt,Zh,Ko,Ru) (Rev 1).7z' is not a valid file
Running Yuzu with /media/storage/Games/Games/Cache/Minecraft.nsp does not exist. Testing for xci...
Unhandled SIGSEGV at rip 0x0055ba11fd468e
/media/storage/Games/Utilities/Lutris/extraction_test_03.sh: line 122: 114450 Segmentation fault      ~/.local/share/lutris/runners/yuzu/yuzu "$final_rom_file_name"
Monitored process exited.
Initial process has exited (return code: 35584)
All processes have quit
Exit with return code 35584
DEBUG    2021-11-19 21:20:03,016 [command.on_stop:193]:Process 114444 has terminated with code 35584

Welcome back! :slight_smile: I hope so too.
Good for you.
Thank you! :blush:

The error is because of the spaces in the file name. These must be escaped with \, for example This\ is\ a\ test or quotes need to be placed around the path. This last one is better for scripting.

The function now looks like this:

#
# Extract. Detect format and use corresponding dearchiver.
# $1: archive
# $2: target path
#
extract_archive () {
  if [ -f "$1" ] ; then
    case $1 in
      *.rar)       unrar x -y "$1" "$2" ;;
      *.zip)       unzip -o "$1" -d "$2" ;;
      *.7z)        7z x -o"$2" "$1" -y ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

Is the path /media/storage/Games/Games/Cache/* correct? The find method reports that the path doesn’t exist.

This is the script updated with the new extract_archive function. Have fun! :+1:

#!/bin/bash

#
# A function for testing for existence of ROMS.
# $1: extraction directory
# $2: filename without extension
#
check_for_existing_roms () {
  local extraction_dir=$1
  local filename_no_ext=$2

  # Set a variable to the extension we want to test. We begin with NSP.
  local ext_to_test="nsp"
  # We also set the final_rom variable to an empty string.
  local final_rom_file=""
  # We now create a FILE variable which contains the target path and the NSP filename.
  # THE LINE BELOW WAS CHANGED
  local FILE=$extraction_dir$filename_no_ext.$ext_to_test
  # We now test if this file exists.
  if [ -f "$FILE" ]; then
      # It exists so we set the final_rom_file to this NSP ROM file.
      local final_rom_file=$FILE
  else
      echo "$FILE does not exist. Testing for xci..."
      # The NSP wasn't found. So we set the ext_to_test variable to XCI.
      local ext_to_test="xci"
      # The FILE variable is now set to the target path and the XCI ROM image filename.
      # THE LINE BELOW WAS CHANGED
      local FILE=$extraction_dir$filename_no_ext.$ext_to_test
      # We test again for existence of the XCI ROM file.
      if [ -f "$FILE" ]; then
          # It exists so we set final_rom_file to this file.
          local final_rom_file=$FILE
      fi
  fi

  local func_result=$final_rom_file
  echo "$func_result"
}

#
# Clean up files in directory
# $1: directory
#
cleanup_files () {
    if [ -d $1 ]; then
        find $1* -atime +7 -exec rm {} \;
    fi
}

#
# Extract. Detect format and use corresponding dearchiver.
# $1: archive
# $2: target path
#
extract_archive () {
  if [ -f "$1" ] ; then
    case $1 in
      *.rar)       unrar x -y "$1" "$2" ;;
      *.zip)       unzip -o "$1" -d "$2" ;;
      *.7z)        7z x -o"$2" "$1" -y ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

# Set a variable with the target directory so you don't have to go through all of the script when this location changes...
#extraction_dir="/media/storage/extract_test/cache/"
extraction_dir="/tmp/extracttest/cache/"

# Filename parsing. Create variables which hold parts of the filename.
# Full path and filename
# The filename
# The filename without extension
# Removed the one which gets just the extension
full_filename=$1
# path + filename.7z

filename="${full_filename##*/}"
# filename.7z

filename_no_ext=${filename%.7z}
# filename

echo "Cleaning up old files"
cleanup_files $extraction_dir

echo "Extracting Game from Archive"
# Extract the provided 7zip file...
# But first check for existing ROM...
# Check for nsp or xci. After extraction there is either a NSP or a XCI file right?
# So we need to test if one of those files exist.
# Otherwise we can't call Yuzu.

final_rom_file_name=$(check_for_existing_roms $extraction_dir $filename_no_ext)
if [ "$final_rom_file" == "" ]; then
    echo "No existing ROM image found, extracting..."
    extract_archive "$1" "$extraction_dir"

    # Call again to check whether the extraction was succesful...
    final_rom_file_name=$(check_for_existing_roms $extraction_dir $filename_no_ext)
else
    echo "ROM image already in target location."
fi

# In both cases we called the function so now we can go straight to the Run part of the script...

# Run...
# Now we just test whether the final_rom_file is not empty.
# THE LINE BELOW WAS CHANGED (AGAIN)
if [ "final_rom_file_name" != ""  ]; then
  echo "Running Yuzu with $final_rom_file_name"
  # Its not empty so we can run Yuzu.
  ~/.local/share/lutris/runners/yuzu/yuzu "$final_rom_file_name"
  #echo "Final rom file: $final_rom_file_name"
else
  # Or maybe not...
  # It is empty so both files don't exist.
  echo "ROM file not found!"
fi