if[[ ! -L $rdhome&& ! -L $rdhome/roms ]];then# Add a roms folder symlink back to ~/retrodeck if missing, to fix things like PS2 autosaves until user migrates whole ~retrodeck directory
ln -s $roms_folder$rdhome/roms
fi
else
# ROMs on Internal
roms_folder="$HOME/retrodeck/roms"
fi
echo"ROMs folder found at $roms_folder"
# Unhiding downloaded media from the previous versions
if[ -d "$rdhome/.downloaded_media"]
then
mv -fv "$rdhome/.downloaded_media""$media_folder"
fi
# Unhiding themes folder from the previous versions
if[ -d "$rdhome/.themes"]
then
mv -fv "$rdhome/.themes""$themes_folder"
fi
# Doing the dir prep as we don't know from which version we came
versionwheresaveschanged="0.4.5b"# Hardcoded break point between unsorted and sorted saves
if[[$(sed -e "s/\.//g"<<<$hard_version) > $(sed -e "s/\.//g"<<<$versionwheresaveschanged)]]&&[[ ! $(sed -e "s/\.//g"<<<$version) > $(sed -e "s/\.//g"<<<$versionwheresaveschanged)]];then# Check if user is upgrading from the version where save organization was changed. Try not to reuse this, it things 0.4.5b is newer than 0.4.5
--text="You are updating to a version of RetroDECK where save file locations have changed!\n\nYour existing files will be backed up for safety and then sorted automatically.\n\nIf a file cannot be sorted automatically it will remain where it is for manual sorting.\n\nPLEASE BE PATIENT! This process can take several minutes if you have a large ROM library."
allgames=($(find "$roms_folder" -maxdepth 2 -mindepth 2 ! -name "systeminfo.txt" ! -name "systems.txt" ! -name "gc" ! -name "n3ds" ! -name "nds" ! -name "wii" ! -name "xbox" ! -name "*^*"| sed -e "s/ /\^/g"))# Build an array of all games and multi-disc-game-containing folders, adding whitespace placeholder
allsaves=($(find "$saves_folder" -mindepth 1 -maxdepth 1 -name "*.*" ! -name "gc" ! -name "n3ds" ! -name "nds" ! -name "wii" ! -name "xbox"| sed -e "s/ /\^/g"))# Build an array of all save files, ignoring standalone emulator sub-folders, adding whitespace placeholder
allstates=($(find "$states_folder" -mindepth 1 -maxdepth 1 -name "*.*" ! -name "gc" ! -name "n3ds" ! -name "nds" ! -name "wii" ! -name "xbox"| sed -e "s/ /\^/g"))# Build an array of all state files, ignoring standalone emulator sub-folders, adding whitespace placeholder
totalsaves=${#allsaves[@]}
totalstates=${#allstates[@]}
filesleft=
current_dest_folder=
gamestoskip=
tar -C $rdhome -czf $save_backup_file saves # Backup save directory for safety
echo"Saves backed up to"$save_backup_file >> $migration_logfile
tar -C $rdhome -czf $state_backup_file states # Backup state directory for safety
echo"States backed up to"$state_backup_file >> $migration_logfile
(
movefile(){# Take matching save and rom files and sort save into appropriate system folder
echo"# $filesleft$currentlybeingmoved remaining..."# These lines update the Zenity progress bar
if[[ ! "${gamestoskip[*]}"=~ "${1}"]];then# If the current game name exists multiple times in array ie. /roms/snes/Mortal Kombat 3.zip and /roms/genesis/Mortal Kombat 3.zip, skip and alert user to sort manually
gamebasename=$(basename "$game"| sed -e 's/\..*//')# Extract pure file name ie. /roms/snes/game1.zip becomes game1
systemdir="$(basename "$(dirname "$1")")"# Extract parent directory identifying system ROM belongs to
matches=($(find "$roms_folder" -maxdepth 2 -mindepth 2 -name "$gamebasename"".*"| sed -e 's/ /^/g'| sed -e 's/\..*//'))# Search for multiple instances of pure game name, adding to skip list if found
if[[${#matches[@]} -gt 1]];then
echo"ERROR: Multiple ROMS found with name:"$gamebasename"Please sort saves and states for these ROMS manually" >> $migration_logfile
gamestoskip+=("$1")
return
fi
echo"INFO: Examining ROM file:""$game" >> $migration_logfile
echo"INFO: System detected as"$systemdir >> $migration_logfile
sospurebasename="$(basename "$sosfile")"# Extract pure file name ie. /saves/game1.sav becomes game1
echo"INFO: Current save or state being examined for match:"$sosfile >> $migration_logfile
echo"INFO: Matching save or state"$sosfile"and game"$game"found." >> $migration_logfile
echo"INFO: Moving save or state to"$current_dest_folder"/"$systemdir"/"$sosbasename >> $migration_logfile
if[[ ! -d $current_dest_folder"/"$systemdir]];then# If system directory doesn't exist for save yet, create it
echo"WARNING: Creating missing system directory"$current_dest_folder"/"$systemdir
mkdir $current_dest_folder/$systemdir
fi
mv "$sosfile" -t $current_dest_folder/$systemdir# Move save to appropriate system directory
return
else
echo"WARNING: Game with name""$(basename "$1"| sed -e "s/\^/ /g")""already found. Skipping to next game..." >> $migration_logfile# Inform user of game being skipped due to duplicate ROM names
fi
}
find "$roms_folder" -mindepth 2 -maxdepth 2 -name "*\^*" -exec echo"ERROR: Game named"{}"found, please move save manually"\; >> $migration_logfile# Warn user if any of their files have the whitespace replacement character used by the script
totalfiles=$totalsaves#set variables for save file migration
filesleft=$totalsaves
currentlybeingmoved="saves"
current_dest_folder=$saves_folder
for i in "${allsaves[@]}";do# For each save file, compare to every ROM file name looking for a match
found=
currentsave=($(basename "$i"| sed -e 's/\..*//'))# Extract pure file name ie. /saves/game1.sav becomes game1
for j in "${allgames[@]}";do
currentgame=($(basename "$j"| sed -e 's/\..*//'))# Extract pure file name ie. /roms/snes/game1.zip becomes game1
[[$currentgame==$currentsave]]&&{found=1; break;}# If names match move to next stage, otherwise skip
done
[[ -n $found]]&& movefile $j$i||echo"ERROR: No ROM match found for save file"$i| sed -e 's/\^/ /g' >> $migration_logfile# If a match is found, run movefile() otherwise warn user of stranded save file
done
totalfiles=$totalstates#set variables for state file migration
filesleft=$totalstates
currentlybeingmoved="states"
current_dest_folder=$states_folder
for i in "${allstates[@]}";do# For each state file, compare to every ROM file name looking for a match
found=
currentstate=($(basename "$i"| sed -e 's/\..*//'))# Extract pure file name ie. /states/game1.sav becomes game1
for j in "${allgames[@]}";do
currentgame=($(basename "$j"| sed -e 's/\..*//'))# Extract pure file name ie. /roms/snes/game1.zip becomes game1
[[$currentgame==$currentstate]]&&{found=1; break;}# If names match move to next stage, otherwise skip
done
[[ -n $found]]&& movefile $j$i||echo"ERROR: No ROM match found for state file"$i| sed -e 's/\^/ /g' >> $migration_logfile# If a match is found, run movefile() otherwise warn user of stranded state file
--text="The migration process has sorted all of your files automatically.\n\nEverything should be working normally, if you experience any issues please check the RetroDECK wiki or contact us directly on the Discord."
--text="The migration process was unable to sort $(cat $migration_logfile| grep "ERROR"| wc -l) files automatically.\n\nThese files will need to be moved manually to their new locations, find more detail on the RetroDECK wiki.\n\nA log of the files that need manual sorting can be found at $rdhome/manual_sort_needed.log"
fi
else
echo"Version"$version"is after the save and state organization was changed, no need to sort again"
# Everything within the following ( <code> ) will happen behind the Zenity dialog. The save migration was a long process so it has its own individual dialogs.
# - Primehack preconfiguration completely redone. "Stop emulation" hotkey set to Start+Select, Xbox and Nintendo keymap profiles were created, Xbox set as default.
# - Duckstation save and state locations were dir_prep'd to the rdhome/save and /state folders, which was not previously done. Much safer now!