mirror of
https://github.com/RetroDECK/RetroDECK.git
synced 2025-04-10 19:15:12 +00:00
Update steam_sync to new SRM manifest utilization
This commit is contained in:
parent
ebb36f0b49
commit
94c066511e
|
|
@ -1,21 +1,33 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
steam_sync() {
|
||||||
|
|
||||||
|
# This function looks for favorited games in all ES-DE gamelists and builds a manifest of any found.
|
||||||
add_to_steam() {
|
# It then compares the new manifest to the existing one (if it exists) and runs an SRM sync if there are differences
|
||||||
|
# If all favorites were removed from ES-DE, it will remove all existing entries from Steam and then remove the favorites manifest entirely
|
||||||
|
# If there is no existing manifest, this is a first time sync and games are synced automatically
|
||||||
|
# USAGE: steam_sync
|
||||||
|
|
||||||
log "i" "Starting Steam Sync"
|
log "i" "Starting Steam Sync"
|
||||||
|
|
||||||
create_dir $steamsync_folder
|
create_dir $steamsync_folder
|
||||||
create_dir $steamsync_folder_tmp
|
|
||||||
|
|
||||||
local srm_path="/var/config/steam-rom-manager/userData/userConfigurations.json"
|
|
||||||
if [ ! -f "$srm_path" ]; then
|
if [ ! -f "$srm_path" ]; then
|
||||||
log "e" "Steam ROM Manager configuration not initialized! Initializing now."
|
log "e" "Steam ROM Manager configuration not initialized! Initializing now."
|
||||||
prepare_component "reset" "steam-rom-manager"
|
prepare_component "reset" "steam-rom-manager"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Iterate through all gamelist.xml files in the folder structure
|
# Prepare fresh log file
|
||||||
|
echo > "$srm_log"
|
||||||
|
|
||||||
|
# Prepare new favorites manifest
|
||||||
|
echo "[]" > "${retrodeck_favorites_file}.new" # Initialize favorites JSON file
|
||||||
|
favorites_found="false"
|
||||||
|
|
||||||
|
# Static definitions for all JSON objects
|
||||||
|
target="flatpak"
|
||||||
|
launch_command="run net.retrodeck.retrodeck"
|
||||||
|
startIn=""
|
||||||
|
|
||||||
for system_path in "$rdhome/ES-DE/gamelists/"*/; do
|
for system_path in "$rdhome/ES-DE/gamelists/"*/; do
|
||||||
# Skip the CLEANUP folder
|
# Skip the CLEANUP folder
|
||||||
if [[ "$system_path" == *"/CLEANUP/"* ]]; then
|
if [[ "$system_path" == *"/CLEANUP/"* ]]; then
|
||||||
|
|
@ -23,95 +35,54 @@ add_to_steam() {
|
||||||
fi
|
fi
|
||||||
system=$(basename "$system_path") # Extract the folder name as the system name
|
system=$(basename "$system_path") # Extract the folder name as the system name
|
||||||
gamelist="${system_path}gamelist.xml"
|
gamelist="${system_path}gamelist.xml"
|
||||||
|
system_favorites=$(xml sel -t -m "//game[favorite='true']" -v "path" -n "$gamelist")
|
||||||
log d "Reading favorites for $system"
|
while read -r game; do
|
||||||
|
if [[ -n "$game" ]]; then # Avoid empty lines created by xmlstarlet
|
||||||
# Ensure gamelist.xml exists in the current folder
|
favorites_found="true"
|
||||||
if [ -f "$gamelist" ]; then
|
local game="${game#./}" # Remove leading ./
|
||||||
while IFS= read -r line; do
|
# Construct launch options with the rom path in quotes, to handle spaces
|
||||||
# Detect the start of a <game> block
|
local launchOptions="$launch_command -s $system \"$roms_folder/$system/$game\""
|
||||||
if [[ "$line" =~ \<game\> ]]; then
|
jq --arg title "${game%.*}" --arg target "$target" --arg launchOptions "$launchOptions" \
|
||||||
to_be_added=false # Reset the flag for a new block
|
'. += [{"title": $title, "target": $target, "launchOptions": $launchOptions}]' "${retrodeck_favorites_file}.new" > "${retrodeck_favorites_file}.tmp" \
|
||||||
path=""
|
&& mv "${retrodeck_favorites_file}.tmp" "${retrodeck_favorites_file}.new"
|
||||||
name=""
|
fi
|
||||||
fi
|
done <<< "$system_favorites"
|
||||||
|
|
||||||
# Check for <favorite>true</favorite>
|
|
||||||
if [[ "$line" =~ \<favorite\>true\<\/favorite\> ]]; then
|
|
||||||
to_be_added=true
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Extract the <path> and remove leading "./" if present
|
|
||||||
if [[ "$line" =~ \<path\>(.*)\<\/path\> ]]; then
|
|
||||||
path="${BASH_REMATCH[1]#./}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Extract and sanitize <name>
|
|
||||||
if [[ "$line" =~ \<name\>(.*)\<\/name\> ]]; then
|
|
||||||
name=$(sanitize "${BASH_REMATCH[1]}")
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Detect the end of a </game> block
|
|
||||||
if [[ "$line" =~ \<\/game\> ]]; then
|
|
||||||
# If the block is meaningful (marked as favorite), generate the launcher
|
|
||||||
if [ "$to_be_added" = true ] && [ -n "$path" ] && [ -n "$name" ]; then
|
|
||||||
local launcher="$steamsync_folder/${name}.sh"
|
|
||||||
local launcher_tmp="$steamsync_folder_tmp/${name}.sh"
|
|
||||||
|
|
||||||
# Create the launcher file
|
|
||||||
# Check if the launcher file does not already exist
|
|
||||||
if [ ! -e "$launcher_tmp" ]; then
|
|
||||||
log d "Creating launcher file: $launcher"
|
|
||||||
command="flatpak run net.retrodeck.retrodeck -s $system '$roms_folder/$system/$path'"
|
|
||||||
echo '#!/bin/bash' > "$launcher_tmp"
|
|
||||||
echo "$command" >> "$launcher_tmp"
|
|
||||||
chmod +x "$launcher_tmp"
|
|
||||||
else
|
|
||||||
log d "$(basename "$launcher") desktop file already exists"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Clean up variables for safety
|
|
||||||
to_be_added=false
|
|
||||||
path=""
|
|
||||||
name=""
|
|
||||||
fi
|
|
||||||
done < "$gamelist"
|
|
||||||
else
|
|
||||||
log "e" "Gamelist file not found for system: $system"
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
|
|
||||||
# Remove the old Steam sync folder
|
if [[ -f "$retrodeck_favorites_file" ]]; then # If an existing favorites manifest exists
|
||||||
rm -rf "$steamsync_folder"
|
if [[ $favorites_found == "false" ]]; then # If no favorites were found in the gamelists
|
||||||
|
log i "No favorites were found in current ES-DE gamelists, removing old entries"
|
||||||
# Move the temporary Steam sync folder to the final location
|
# Remove old entries
|
||||||
log d "Moving the temporary Steam sync folder to the final location"
|
steam-rom-manager enable --names "RetroDECK Steam Sync" >> "$srm_log" 2>&1
|
||||||
mv "$steamsync_folder_tmp" "$steamsync_folder" && log d "\"$steamsync_folder_tmp\" -> \"$steamsync_folder\""
|
steam-rom-manager disable --names "RetroDECK Launcher" >> "$srm_log" 2>&1
|
||||||
|
steam-rom-manager remove >> "$srm_log" 2>&1
|
||||||
|
# Old manifest cleanup
|
||||||
|
rm "$retrodeck_favorites_file"
|
||||||
|
rm "${retrodeck_favorites_file}.new"
|
||||||
|
else
|
||||||
|
if cmp -s "$retrodeck_favorites_file" "${retrodeck_favorites_file}.new"; then # See if the favorites manifests are the same, meaning there were no changes
|
||||||
|
log i "ES-DE favorites have not changed, no need to sync again"
|
||||||
|
rm "${retrodeck_favorites_file}.new"
|
||||||
|
else
|
||||||
|
log d "New and old manifests are different, running sync"
|
||||||
|
# Remove old entries
|
||||||
|
steam-rom-manager enable --names "RetroDECK Steam Sync" >> "$srm_log" 2>&1
|
||||||
|
steam-rom-manager disable --names "RetroDECK Launcher" >> "$srm_log" 2>&1
|
||||||
|
steam-rom-manager remove >> "$srm_log" 2>&1
|
||||||
|
|
||||||
# Check if the Steam sync folder is empty
|
# Load new favorites manifest
|
||||||
if [ -z "$(ls -A $steamsync_folder)" ]; then
|
mv "${retrodeck_favorites_file}.new" "$retrodeck_favorites_file"
|
||||||
# if empty, add the remove_from_steam function
|
|
||||||
log d "No games found, cleaning shortcut"
|
# Add new favorites manifest
|
||||||
remove_from_steam
|
steam-rom-manager enable --names "RetroDECK Steam Sync" >> "$srm_log" 2>&1
|
||||||
else
|
steam-rom-manager add >> "$srm_log" 2>&1
|
||||||
log d "Updating game list"
|
fi
|
||||||
steam-rom-manager enable --names "RetroDECK Steam Sync"
|
fi
|
||||||
steam-rom-manager add
|
elif [[ $favorites_found == "true" ]]; then # Only sync if some favorites were found
|
||||||
|
log d "First time building favorites manifest, running sync"
|
||||||
|
mv "${retrodeck_favorites_file}.new" "$retrodeck_favorites_file"
|
||||||
|
# Add new favorites manifest
|
||||||
|
steam-rom-manager enable --names "RetroDECK Steam Sync" >> "$srm_log" 2>&1
|
||||||
|
steam-rom-manager add >> "$srm_log" 2>&1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to remove the games from Steam, this is a workaround to make SRM remove the games as it cannot remove the games based on a empty folder
|
|
||||||
# So a dummy file must be in place to make SRM remove the other games
|
|
||||||
remove_from_steam() {
|
|
||||||
log d "Creating dummy game"
|
|
||||||
cat "" > "$steamsync_folder/CUL0.sh"
|
|
||||||
log d "Cleaning the shortcut"
|
|
||||||
steam-rom-manager enable --names "RetroDECK Steam Sync"
|
|
||||||
steam-rom-manager disable --names "RetroDECK Launcher"
|
|
||||||
steam-rom-manager remove
|
|
||||||
log d "Removing dummy game"
|
|
||||||
rm "$steamsync_folder/CUL0.sh"
|
|
||||||
steam-rom-manager enable --names "RetroDECK Launcher"
|
|
||||||
steam-rom-manager disable --names "RetroDECK Steam Sync"
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue