Script to extract rom from archive

OK. Get it now. :slight_smile:

# /tmp/filename.7z is fictional...
sh ./test.sh /tmp/filename.7z
#!/bin/bash
s="$1"
echo "${s##*/}"
# filename.7z
s="${s##*/}"
echo "${s%.7z}"
# filename
echo "${s##*.}"
# 7z

You need to determine the extension of the ROM file. Thatā€™s why the last one is added to this test.

A basis for the script:

#!/bin/bash
extraction_dir="/media/storage/extract_test/cache/"
echo "Extracting Game from Archive"
7z x -o"$extraction_dir" "$1"

# Filename parsing
full_filename=$1
# path + filename.7z

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

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

ext=${filename##*.}
# 7z - we are not using this one. Just for illustration...

# Check for nxp or xci...
ext_to_test="nxp"
final_rom_file=""
FILE=$extraction_dir$filename_no_ext$ext_to_test
if [ -f "$FILE" ]; then
    echo "$FILE exists."
    final_rom_file=$FILE
else 
    echo "$FILE does not exist. Testing for xci..."
    ext_to_test="xci"
    FILE=$extraction_dir$filename_no_ext$ext_to_test
    if [ -f "$FILE" ]; then
        echo "$FILE exists."
        final_rom_file=$FILE
    fi
fi

# Run...
if [ ! $final_rom_file -eq ""  ]; then
  echo "Running Yuzu with $final_rom_file"
  ~/.local/share/lutris/runners/yuzu/yuzu '$final_rom_file'
else
  # Or maybe not...
  echo "ROM file not found!"
fi

No, this can still be done with a bash script.

UUID is the unique id for a game.

1 Like

Just when I thought I was starting to get the hang of this I read your code! :rofl: I kind of get what your doing, but a lot of stuff in there that is over my head. So why are there three blockquotes of code? Not sure what Iā€™m supposed to be doing in addition to the last piece of code for the script?

EDIT: Oh, and Iā€™m guessing ā€œnxpā€ = ā€œnspā€?

Letā€™s go through it step by step then. :slight_smile:

Basically we set some variables, extract the file and then test for existence of one of the ROM formats.
If one of those exist then we execute Yuzu.

Iā€™ve added comments for each statement in below code.

#!/bin/bash
# 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/"

echo "Extracting Game from Archive"
# Extract the provided 7zip file...
7z x -o"$extraction_dir" "$1"

# 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

# 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.

# Set a variable to the extension we want to test. We begin with NSP.
ext_to_test="nsp"
# We also set the final_rom variable to an empty string.
final_rom_file=""
# We now create a FILE variable which contains the target path and the NSP filename.
FILE=$extraction_dir$filename_no_ext$ext_to_test
# We now test if this file exists.
if [ -f "$FILE" ]; then
    echo "$FILE exists."
    # It exists so we set the final_rom_file to this NSP ROM file.
    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.
    ext_to_test="xci"
    # The FILE variable is now set to the target path and the XCI ROM image filename.
    FILE=$extraction_dir$filename_no_ext$ext_to_test
    # We test again for existence of the XCI ROM file.
    if [ -f "$FILE" ]; then
        echo "$FILE exists."
        # It exists so we set final_rom_file to this file.
        final_rom_file=$FILE
    fi
fi

# Run...
# Now we just test whether the final_rom_file is not empty.
if [ ! $final_rom_file -eq ""  ]; then
  echo "Running Yuzu with $final_rom_file"
  # Its not empty so we can run Yuzu.
  ~/.local/share/lutris/runners/yuzu/yuzu '$final_rom_file'
else
  # Or maybe not...
  # It is empty so both files don't exist.
  echo "ROM file not found!"
fi

Is this useful?

Edit: nspā€¦ow. Itā€™s getting late over here. I must be getting tired. :slight_smile:
Luckily nsp was only mentioned 100 times in the codeā€¦

1 Like

Nā€™aww thankyou! Very sweet of you to comment it all! :grinning_face_with_smiling_eyes: I see what your doing now. Still donā€™t get some of the actual syntax, but get what itā€™s for. Just ran it and thereā€™s something wrong at line 54ā€¦ not sure what thoughā€¦

DEBUG    2021-11-01 12:17:43,407 [command.start:139]:LUTRIS_GAME_UUID="9265ac75-b799-40b8-b40d-cc8c2dfb8652"
Started initial process 93780 from /media/storage/extract_test/extract_7zip_script_custom_exec.sh /media/storage/extract_test/ROM.7z
Start monitoring process.
Extracting Game from Archive

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_AU.UTF-8,Utf16=on,HugeFiles=on,64 bits,16 CPUs Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz (A0655),ASM,AES-NI)

Scanning the drive for archives:
1 file, 580038845 bytes (554 MiB)

Extracting archive: /media/storage/extract_test/ROM.7z
--
Path = /media/storage/extract_test/ROM.7z
Type = 7z
Physical Size = 580038845
Headers Size = 122
Method = LZMA2:14
Solid = -
Blocks = 1

Everything is Ok

Size:       580084672
Compressed: 580038845
/media/storage/extract_test/cache/ROMnsp does not exist. Testing for xci...
/media/storage/extract_test/extract_7zip_script_custom_exec.sh: line 54: [: !: integer expression expected
ROM file not found!
Monitored process exited.
Initial process has exited (return code: 0)
All processes have quit
Exit with return code 0
DEBUG    2021-11-01 12:17:45,074 [command.on_stop:193]:Process 93778 has terminated with code 0
ERROR    2021-11-01 12:17:45,424 [process.environ:119]:Failed to parse environment variables: il local:/run/user/1000/klauncherIZCdaW.1.slave-socket local:/run/user/1000/dolphinGXTAua.210.slave-socket1
DEBUG    2021-11-01 12:17:45,424 [game.beat:588]:Game thread stopped
WARNING  2021-11-01 12:17:45,424 [game.on_game_quit:614]:Game still running (state: running)
INFO     2021-11-01 12:17:45,425 [game.stop:599]:Stopping Test 7zip (yuzu)
DEBUG    2021-11-01 12:17:45,426 [game.stop_game:554]:Test 7zip (yuzu) has run for 2 seconds
WARNING  2021-11-01 12:17:45,426 [game.stop_game:556]:The game has run for a very short time, did it crash?
DEBUG    2021-11-01 12:17:45,443 [game.on_game_quit:632]:Test 7zip stopped at Mon, 01 Nov 2021 12:17:45
DEBUG    2021-11-01 12:17:45,443 [game.save:257]:Saving Test 7zip (yuzu) with config ID test-1635216155

Hahaha and guess weā€™re in different time zones, its noon over here!

EDIT: the test file is an .nsp file, so think maybe itā€™s missing something detecting the nsp?

Maybe its the -eq statement.

Letā€™s try this.

#!/bin/bash
# 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/"

echo "Extracting Game from Archive"
# Extract the provided 7zip file...
7z x -o"$extraction_dir" "$1"

# 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

# 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.

# Set a variable to the extension we want to test. We begin with NSP.
ext_to_test="nsp"
# We also set the final_rom variable to an empty string.
final_rom_file=""
# We now create a FILE variable which contains the target path and the NSP filename.
# THE LINE BELOW WAS CHANGED
FILE=$extraction_dir$filename_no_ext.$ext_to_test
# We now test if this file exists.
if [ -f "$FILE" ]; then
    echo "$FILE exists."
    # It exists so we set the final_rom_file to this NSP ROM file.
    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.
    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
    FILE=$extraction_dir$filename_no_ext.$ext_to_test
    # We test again for existence of the XCI ROM file.
    if [ -f "$FILE" ]; then
        echo "$FILE exists."
        # It exists so we set final_rom_file to this file.
        final_rom_file=$FILE
    fi
fi

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

This doesnā€™t work either. Is the path /media/storage/extract_test/cache/ROM.xpi (or nsp)?

/media/storage/extract_test/cache/ROMnsp

Added a dot to create:

/media/storage/extract_test/cache/ROM.nsp
1 Like

Donā€™t know if you saw my edit in my post above so will repeat here just in case: the test file is an .nsp file, just in case thatā€™s relevant. Still something wierd, now says:

/media/storage/extract_test/cache/ROMnsp does not exist. Testing for xci...
/media/storage/extract_test/extract_7zip_script_custom_exec.sh: line 55: [: !=: unary operator expected
ROM file not found!
Monitored process exited.
Initial process has exited (return code: 0)
All processes have quit
Exit with return code 0

Yeah its nsp. Okay well youā€™ve fixed something but now it terminated after opening :face_with_raised_eyebrow:

/media/storage/extract_test/cache/ROM.nsp exists.
Running Yuzu with /media/storage/extract_test/cache/ROM.nsp
DEBUG    2021-11-01 12:28:15,144 [command.on_stop:193]:Process 94294 has terminated with code 35584
DEBUG    2021-11-01 12:28:16,993 [game.beat:588]:Game thread stopped
WARNING  2021-11-01 12:28:16,993 [game.on_game_quit:614]:Game still running (state: running)
INFO     2021-11-01 12:28:16,993 [game.stop:599]:Stopping Test 7zip (yuzu)
DEBUG    2021-11-01 12:28:16,993 [game.stop_game:554]:Test 7zip (yuzu) has run for 6 seconds
DEBUG    2021-11-01 12:28:17,002 [game.on_game_quit:632]:Test 7zip stopped at Mon, 01 Nov 2021 12:28:17
DEBUG    2021-11-01 12:28:17,002 [game.save:257]:Saving Test 7zip (yuzu) with config ID test-1635216155

Ah. Yes. Iā€™m getting tired.

When testing a string variable against a string, surround the variable with quotes. Otherwise an empty variable will be seen as follows.

if [ != "test"]; then...

Soā€¦

#!/bin/bash
# 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/"

echo "Extracting Game from Archive"
# Extract the provided 7zip file...
7z x -o"$extraction_dir" "$1"

# 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

# 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.

# Set a variable to the extension we want to test. We begin with NSP.
ext_to_test="nsp"
# We also set the final_rom variable to an empty string.
final_rom_file=""
# We now create a FILE variable which contains the target path and the NSP filename.
# THE LINE BELOW WAS CHANGED
FILE=$extraction_dir$filename_no_ext.$ext_to_test
# We now test if this file exists.
if [ -f "$FILE" ]; then
    echo "$FILE exists."
    # It exists so we set the final_rom_file to this NSP ROM file.
    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.
    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
    FILE=$extraction_dir$filename_no_ext.$ext_to_test
    # We test again for existence of the XCI ROM file.
    if [ -f "$FILE" ]; then
        echo "$FILE exists."
        # It exists so we set final_rom_file to this file.
        final_rom_file=$FILE
    fi
fi

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

Oh no! I donā€™t want to keep you up! :joy: Happy to figure it out tomorrow :sweat_smile:

Hm. Ok. The script runs. Thatā€™s somethingā€¦

Homework for tomorrow.

Check what happens when you run yuzu directly from the command line with an existing ROM:

~/.local/share/lutris/runners/yuzu/yuzu /media/storage/extract_test/cache/ROM.nsp

If it works directly from bash we can continue on this path. Lutris is just a pass through.

Alright, Iā€™ll have a tinker and post my findings for tomorrow :joy: Thanks again!

Hmm okay, something else is happening now:

/media/storage/extract_test/cache/ROM.nsp exists.
Running Yuzu with /media/storage/extract_test/cache/ROM.nsp
Unhandled SIGSEGV at rip 0x0055f97f5d468e
/media/storage/extract_test/extract_7zip_script_custom_exec.sh: line 65: 94915 Segmentation fault      ~/.local/share/lutris/runners/yuzu/yuzu '$final_rom_file'
Monitored process exited.
Initial process has exited (return code: 35584)
All processes have quit
Exit with return code 35584
DEBUG    2021-11-01 12:35:34,474 [command.on_stop:193]:Process 94911 has terminated with code 35584
DEBUG    2021-11-01 12:35:36,413 [game.beat:588]:Game thread stopped
WARNING  2021-11-01 12:35:36,413 [game.on_game_quit:614]:Game still running (state: running)
INFO     2021-11-01 12:35:36,413 [game.stop:599]:Stopping Test 7zip (yuzu)
DEBUG    2021-11-01 12:35:36,414 [game.stop_game:554]:Test 7zip (yuzu) has run for 6 seconds
DEBUG    2021-11-01 12:35:36,454 [game.on_game_quit:632]:Test 7zip stopped at Mon, 01 Nov 2021 12:35:36
DEBUG    2021-11-01 12:35:36,455 [game.save:257]:Saving Test 7zip (yuzu) with config ID test-1635216155

And just tried launching the rom directly from cli and worked fineā€¦ :face_with_raised_eyebrow: but I can taste victory. Must be close!

Single quotes means literal string. You could try to replace those in the final call with double quotes.

1 Like

Thatā€™s it! It works :partying_face: Fanks! Hahaha, before you made that suggestion I had just tried removing the single quotes :woman_facepalming:

Do you have any suggestions for how to auto respond with ā€œnā€ to the archive extraction query when it detects that the file has already been extracted? Thereā€™s only a yes auto respond option in the 7z commands. But actually maybe ignore that question, cause now come to think of it Iā€™m wondering if I should just replace that part of the script with something thatā€™s archive format agnostic so the same code can handle extracting zip, 7z and rar (realised last night there a few split rar archived games in my collections). Will do some googling and see what I can find later today.

EDIT: Can I use something like this?

ex ()
{
  if [ -f $1 ] ; then
    case $1 in
      *.rar)       unrar x $1   ;;
      *.zip)       unzip $1     ;;
      *.7z)        7z x $1      ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

Cool! Very well done. A very well done start into bash scripting.

Yes, in many programming languages the single quotes mean a literal string. So in this case it wanted to execute the variable name. Double quotes just put double quotes around the variable name and this translates into the value of the variable to be inserted here.

There are two ways to handle already existing files. If you do a rm -rf in the target directory then you have some sort of a auto cleanup system.

With the other option howeverā€¦you can test for the existence of the ROM file the same way the existence check is done now. This can be done via a function. In this case a function which returns a value.

#!/bin/bash

# A function for testing for existence of ROMS.
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
      echo "$FILE exists."
      # 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
          echo "$FILE exists."
          # 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"
}

# 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/"

# 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 "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..."
    7z x -o"$extraction_dir" "$1"

    # 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" != ""  ]; then
  echo "Running Yuzu with $final_rom_file"
  # Its not empty so we can run Yuzu.
  ~/.local/share/lutris/runners/yuzu/yuzu "$final_rom_file"
else
  # Or maybe not...
  # It is empty so both files don't exist.
  echo "ROM file not found!"
fi

Something like this. This probably needs some workā€¦

Edit: added the $extraction_dir $filename_no_ext as function parameters.

Edit2: Yes, this is a valid way to call different extractors.

1 Like

:blush: Hehe

Ah okay I get what you mean about the literal vs value.

Ah yeah nice. That looks great. Smarter way to handle whether there is the file present. Although something wrong on line 64:

DEBUG    2021-11-02 10:56:44,768 [command.start:139]:game_name="ROM.7z"
DEBUG    2021-11-02 10:56:44,768 [command.start:139]:PYTHONPATH="/usr/lib/lutris:/usr/games:/usr/lib/python39.zip:/usr/lib/python3.9:/usr/lib/python3.9/lib-dynload:/usr/local/lib/python3.9/dist-packages:/usr/lib/python3/dist-packages"
DEBUG    2021-11-02 10:56:44,768 [command.start:139]:LUTRIS_GAME_UUID="001b86c6-343e-4ba7-8ce6-d933d92c7083"
Started initial process 148287 from /media/storage/Games/Utilities/Lutris/extraction_exec_yuzu.sh /media/storage/extract_test/ROM.7z
Start monitoring process.
Extracting Game from Archive
/media/storage/Games/Utilities/Lutris/extraction_exec_yuzu.sh: line 64: [: ==: unary operator expected
ROM image already in target location.
ROM file not found!
Monitored process exited.
Initial process has exited (return code: 0)
All processes have quit
Exit with return code 0
DEBUG    2021-11-02 10:56:44,939 [command.on_stop:193]:Process 148285 has terminated with code 0
DEBUG    2021-11-02 10:56:46,811 [game.beat:588]:Game thread stopped
WARNING  2021-11-02 10:56:46,811 [game.on_game_quit:614]:Game still running (state: running)
INFO     2021-11-02 10:56:46,811 [game.stop:599]:Stopping ROM.7z (yuzu)
DEBUG    2021-11-02 10:56:46,812 [game.stop_game:554]:ROM.7z (yuzu) has run for 2 seconds
WARNING  2021-11-02 10:56:46,812 [game.stop_game:556]:The game has run for a very short time, did it crash?
DEBUG    2021-11-02 10:56:46,817 [game.on_game_quit:632]:ROM.7z stopped at Tue, 02 Nov 2021 10:56:46
DEBUG    2021-11-02 10:56:46,817 [game.save:257]:Saving ROM.7z (yuzu) with config ID rom7z-1635735076

Regarding rm -rf auto cleanup, I was actually thinking of implementing something like this:

echo "Cleaning up cache..."

cache_size_limit=100000

size=$(du -mb $1 | awk '{print $1}')

if [ $size -gt $cache_size_limit ];
then

find $1 -atime -exec rm -f {} \;
else
fi

But donā€™t know how to make it delete only the highest -atime(s)ā€¦ :thinking:

Oopsā€¦

#!/bin/bash

# A function for testing for existence of ROMS.
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
      echo "$FILE exists."
      # 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
          echo "$FILE exists."
          # 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"
}

# 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/"

# 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 "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..."
    7z x -o"$extraction_dir" "$1"

    # 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" != ""  ]; then
  echo "Running Yuzu with $final_rom_file"
  # Its not empty so we can run Yuzu.
  ~/.local/share/lutris/runners/yuzu/yuzu "$final_rom_file"
else
  # Or maybe not...
  # It is empty so both files don't exist.
  echo "ROM file not found!"
fi

For deleting files older than a certain period of time you can use the common Linux shell commands.

find /path/to/files/* -mtime +7 -exec rm {} \;

Source:

1 Like

Hmmā€¦ now the error has shifted to something else :joy:

DEBUG    2021-11-02 11:05:05,379 [command.start:139]:game_name="ROM.7z"
DEBUG    2021-11-02 11:05:05,379 [command.start:139]:PYTHONPATH="/usr/lib/lutris:/usr/games:/usr/lib/python39.zip:/usr/lib/python3.9:/usr/lib/python3.9/lib-dynload:/usr/local/lib/python3.9/dist-packages:/usr/lib/python3/dist-packages"
DEBUG    2021-11-02 11:05:05,379 [command.start:139]:LUTRIS_GAME_UUID="6166f63a-0fd5-4305-b35c-7372976c3c3a"
Started initial process 148609 from /media/storage/Games/Utilities/Lutris/extraction_exec_yuzu.sh /media/storage/extract_test/ROM.7z
Start monitoring process.
Extracting Game from Archive
No existing ROM image found, extracting...

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_AU.UTF-8,Utf16=on,HugeFiles=on,64 bits,16 CPUs Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz (A0655),ASM,AES-NI)

Scanning the drive for archives:
1 file, 580038845 bytes (554 MiB)

Extracting archive: /media/storage/extract_test/ROM.7z
--
Path = /media/storage/extract_test/ROM.7z
Type = 7z
Physical Size = 580038845
Headers Size = 122
Method = LZMA2:14
Solid = -
Blocks = 1


Would you like to replace the existing file:
  Path:     /media/storage/extract_test/cache/ROM.nsp
  Size:     580084672 bytes (554 MiB)
  Modified: 2021-10-16 19:36:40
with the file from archive:
  Path:     ROM.nsp
  Size:     580084672 bytes (554 MiB)
  Modified: 2021-10-16 19:36:40
? (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? n

Everything is Ok

Size:       580084672
Compressed: 580038845
ROM file not found!
Monitored process exited.
Initial process has exited (return code: 0)
All processes have quit
Exit with return code 0
DEBUG    2021-11-02 11:05:09,757 [command.on_stop:193]:Process 148607 has terminated with code 0
DEBUG    2021-11-02 11:05:11,392 [game.beat:588]:Game thread stopped
WARNING  2021-11-02 11:05:11,393 [game.on_game_quit:614]:Game still running (state: running)
INFO     2021-11-02 11:05:11,393 [game.stop:599]:Stopping ROM.7z (yuzu)
DEBUG    2021-11-02 11:05:11,394 [game.stop_game:554]:ROM.7z (yuzu) has run for 6 seconds
DEBUG    2021-11-02 11:05:11,404 [game.on_game_quit:632]:ROM.7z stopped at Tue, 02 Nov 2021 11:05:11
DEBUG    2021-11-02 11:05:11,404 [game.save:257]:Saving ROM.7z (yuzu) with config ID rom7z-1635735076

Yeah the only downside with mtime is that I could have a ROM file that was created ages ago that I still access regularly that would get deleted right? But with atime itā€™ll delete the ROMs that I used least recently, even if they were created more recently? Or am I misunderstanding mtime?

atime - yes indeed. atime is the one you need for access time.

We have to look at the function. I think the values are not correctly passed to it so it reports that the file isnā€™t present.

The extract calls should have an auto overwrite part so manual input shouldnā€™t be necessary.

This will have to be done tomorrow thoughā€¦


Edit: updated version with multiple archive tools and cleanup.

#!/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
1 Like

Hey! Sorry been swamped with work since our coding marathon last week when I had a long weekend! Hopefully will get some time in the next few days to test the code and figure out the auto cleanup stuff! :crossed_fingers: