From cd86f19b0edc69e78e93892c482baa055b5128ca Mon Sep 17 00:00:00 2001 From: XargonWan Date: Tue, 19 Dec 2023 12:43:08 +0100 Subject: [PATCH] GZDOOM> enhanced script with recurse --- emu-configs/gzdoom/gzdoom.sh | 130 +++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 37 deletions(-) diff --git a/emu-configs/gzdoom/gzdoom.sh b/emu-configs/gzdoom/gzdoom.sh index 00a0549c..5df22c6c 100644 --- a/emu-configs/gzdoom/gzdoom.sh +++ b/emu-configs/gzdoom/gzdoom.sh @@ -1,49 +1,105 @@ #!/bin/bash -LOG_FILE="$rdhome/.logs/gzdoom.log" - -if [ -e "$LOG_FILE" ]; then - rm "$LOG_FILE" -fi - -echo "RetroDECK GZDOOM wrapper init." | tee -a "$LOG_FILE" - -# List of IWAD files +# Define the IWAD files list IWAD_FILES=("DOOM1.WAD" "DOOM.WAD" "DOOM2.WAD" "DOOM2F.WAD" "DOOM64.WAD" "TNT.WAD" "PLUTONIA.WAD" "HERETIC1.WAD" "HERETIC.WAD" "HEXEN.WAD" "HEXDD.WAD" "STRIFE0.WAD" "STRIFE1.WAD" "VOICES.WAD" "CHEX.WAD" - "CHEX3.WAD" "HACX.WAD" "freedoom1.wad" "freedoom2.wad" "freedm.wad" # unlicensed iwads - "doom_complete.pk3" # this includes them all - ) + "CHEX3.WAD" "HACX.WAD" "freedoom1.wad" "freedoom2.wad" "freedm.wad" + "doom_complete.pk3" +) -echo "Trying to load \"$1\"." | tee -a "$LOG_FILE" +# Function to log messages to terminal and a log file +log() { + local message="$1" + echo "$(date +"[%Y-%m-%d %H:%M:%S]"): $message" + echo "$(date +"[%Y-%m-%d %H:%M:%S]"): $message" >> "$rdhome/.logs/gzdoom.log" +} -if [ ! -e "$1" ]; then - echo "$1 not found. Quitting." | tee -a "$LOG_FILE" - exit 0 -fi +# Function to check if a file is an IWAD +is_iwad() { + local file="$1" + for iwad in "${IWAD_FILES[@]}"; do + if [[ "${iwad,,}" == "$(basename "${file,,}")" ]]; then + echo "true" + return + fi + done + echo "false" +} -filename=$(basename "$1") # Extracts only the filename from the full path -extension="${filename##*.}" # Extracts the file extension - -if [ "$extension" == "doom" ]; then - map_file="$1" - iwad="-iwad $(head -n 1 "$map_file")" - file="$(tail -n +2 "$map_file" | sed 's/.*/-file &/')" - - echo "Found a .doom file: $1, parsing it." | tee -a "$LOG_FILE" - echo "Calling GZDoom with: \"$iwad $file\"." | tee -a "$LOG_FILE" -else - shopt -s nocasematch # Enable case-insensitive matching - if [[ "${IWAD_FILES[@]}" =~ "$filename" ]]; then - type="iwad" - iwad="-iwad $1" +# Function to search for files recursively +search_file_recursive() { + local file="$1" + local directory="$2" + local found_file="" + + # Check if the file exists in the current directory + if [[ -e "$directory/$file" ]]; then + found_file="$directory/$file" else - type="file" - file="-file $1" + # Search recursively + found_file=$(find "$directory" -type f -name "$file" | head -n 1) fi - shopt -u nocasematch # Disable case-insensitive matching after use - echo "Found $type: $1, loading it." | tee -a "$LOG_FILE" + + echo "$found_file" +} + +# Main script + +# Check if $1 is not a .doom file +if [[ "${1##*.}" != "doom" ]]; then + # Check if the file is in the IWAD list + if [[ $(is_iwad "$1") == "true" ]]; then + command="gzdoom -config /var/config/gzdoom/gzdoom.ini -iwad $1" + else + command="gzdoom -config /var/config/gzdoom/gzdoom.ini -file $1" + fi + + # Log the command + log "Command: $command" + + # Execute the command + eval "$command" + +# Check if $1 is a .doom file +else + doom_file="$1" + + # Check if the .doom file exists + if [[ ! -e "$doom_file" ]]; then + log "Error: .doom file not found - $doom_file" + zenity --error --text="Error: .doom file not found - $doom_file" + exit 1 + fi + + # Read the .doom file and compose the command + command="gzdoom -config /var/config/gzdoom/gzdoom.ini" + + while IFS= read -r line; do + # Search for the file recursively + found_file=$(search_file_recursive "$line" "$(dirname "$doom_file")") + + # If the file is not found, exit with an error + if [[ -z "$found_file" ]]; then + log "Error: File not found - $line" + zenity --error --text="Error: File not found - $line" + exit 1 + fi + + # Check if the file is an IWAD + if [[ $(is_iwad "$found_file") == "true" ]]; then + command+=" -iwad $found_file" + else + command+=" -file $found_file" + fi + done < "$doom_file" + + # Log the command + log "Command: $command" + + # Execute the command + eval "$command" fi -gzdoom -config /var/config/gzdoom/gzdoom.ini $iwad $file | tee -a "$LOG_FILE" +# Ask if you wish to get a more in-depth explanation +echo "Do you wish me to explain more?"