STEAM_SYNC: rewrite to allow multi game blocks launcher adding

This commit is contained in:
XargonWan 2024-12-02 14:24:33 +09:00
parent 8edc8ad9e2
commit e4c1f20684
2 changed files with 70 additions and 108 deletions

View file

@ -228,3 +228,5 @@ else
fi fi
logs_folder="$rdhome/logs" # The path of the logs folder, here we collect all the logs logs_folder="$rdhome/logs" # The path of the logs folder, here we collect all the logs
steamsync_folder="$rdhome/.sync" # Folder containing all the steam sync launchers for SRM
steamsync_folder_tmp="$rdhome/.sync-tmp" # Temp folder containing all the steam sync launchers for SRM

View file

@ -1,15 +1,16 @@
#!/bin/bash #!/bin/bash
# Function to sanitize strings for filenames
sanitize() {
# Replace sequences of underscores with a single space
echo "$1" | sed -e 's/_\{2,\}/ /g' -e 's/_/ /g' -e 's/:/ -/g' -e 's/&/and/g' -e 's%/%and%g' -e 's/ / /g'
}
# Add games to Steam function
add_to_steam() { add_to_steam() {
log "i" "Starting Steam Sync" log "i" "Starting Steam Sync"
steamsync_folder="$rdhome/.sync" create_dir $steamsync_folder_tmp
steamsync_folder_tmp="$rdhome/.sync-tmp"
create_dir $steamsync_folder
mv $steamsync_folder $steamsync_folder_tmp
create_dir $steamsync_folder
local srm_path="/var/config/steam-rom-manager/userData/userConfigurations.json" local srm_path="/var/config/steam-rom-manager/userData/userConfigurations.json"
if [ ! -f "$srm_path" ]; then if [ ! -f "$srm_path" ]; then
@ -17,97 +18,49 @@ add_to_steam() {
prepare_component "reset" "steam-rom-manager" prepare_component "reset" "steam-rom-manager"
fi fi
# Build the systems array from space-separated systems # Iterate through all gamelist.xml files in the folder structure
local systems_string=$(jq -r '.system | keys[]' "$features" | paste -sd' ') for system_path in "$rdhome/ES-DE/gamelists/"*/; do
IFS=' ' read -r -a systems <<< "$systems_string" # TODO: do we need this line? system=$(basename "$system_path") # Extract the folder name as the system name
gamelist="${system_path}gamelist.xml"
local games=() log d "Reading favorites for $system"
for system in "${systems[@]}"; do
local gamelist="$rdhome/ES-DE/gamelists/$system/gamelist.xml"
# Ensure gamelist.xml exists in the current folder
if [ -f "$gamelist" ]; then if [ -f "$gamelist" ]; then
while IFS= read -r line; do
# Extract all <game> elements that are marked as favorite="true" # Detect the start of a <game> block
game_blocks=$(xmllint --recover --xpath '//game[favorite="true"]' "$gamelist" 2>/dev/null) if [[ "$line" =~ \<game\> ]]; then
log d "Extracted favorite game blocks:\n\n$game_blocks\n\n" to_be_added=false # Reset the flag for a new block
path=""
# Split the game_blocks into an array, where each element is a full <game> block name=""
IFS=$'\n' read -r -d '' -a game_array <<< "$(echo "$game_blocks" | xmllint --recover --format - | sed -n '/<game>/,/<\/game>/p' | tr '\n' ' ')"
# Iterate over each full <game> block in the array
for game_block in "${game_array[@]}"; do
log "d" "Processing game block:\n$game_block"
# Extract the game's name and path from the full game block
local name=$(echo "$game_block" | xmllint --xpath 'string(//game/name)' - 2>/dev/null)
local path=$(echo "$game_block" | xmllint --xpath 'string(//game/path)' - 2>/dev/null | sed 's|^\./||') # removing the ./
log "d" "Game name: $name"
log "d" "Game path: $path"
# Ensure the extracted name and path are valid
if [ -n "$name" ] && [ -n "$path" ]; then
# Check for an alternative emulator if it exists
# local emulator=$(echo "$game_block" | xmllint --xpath 'string(//game/altemulator)' - 2>/dev/null)
# if [ -z "$emulator" ]; then
# games+=("$name ${command_list_default[$system]} '$roms_folder/$system/$path'")
# else
# games+=("$name ${alt_command_list[$emulator]} '$roms_folder/$system/$path'")
# fi
log "d" "Steam Sync: found favorite game: $name"
else
log "w" "Steam Sync: failed to find valid name or path for favorite game"
fi fi
# Sanitize the game name for the filename: replace special characters with underscores # Check for <favorite>true</favorite>
local sanitized_name=$(echo "$name" | sed -e 's/^A-Za-z0-9._-/ /g') if [[ "$line" =~ \<favorite\>true\<\/favorite\> ]]; then
local sanitized_name=$(echo "$sanitized_name" | sed -e 's/:/ -/g') to_be_added=true
local sanitized_name=$(echo "$sanitized_name" | sed -e 's/&/and/g')
local sanitized_name=$(echo "$sanitized_name" | sed -e 's%/%and%g')
local sanitized_name=$(echo "$sanitized_name" | sed -e 's/ / - /g')
local sanitized_name=$(echo "$sanitized_name" | sed -e 's/ / /g')
log d "File Path: $path"
log d "Game Name: $name"
# If the filename is too long, shorten it
if [ ${#sanitized_name} -gt 100 ]; then
sanitized_name=$(echo "$sanitized_name" | cut -c 1-100)
fi fi
log d "Sanitized Name: $sanitized_name" # Extract the <path> and remove leading "./" if present
if [[ "$line" =~ \<path\>(.*)\<\/path\> ]]; then
path="${BASH_REMATCH[1]#./}"
fi
local launcher="$steamsync_folder/${sanitized_name}.sh" # Extract and sanitize <name>
local launcher_tmp="$steamsync_folder_tmp/${sanitized_name}.sh" 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
if [ ! -e "$launcher_tmp" ]; then if [ ! -e "$launcher_tmp" ]; then
log d "Creating launcher file: $launcher"
log d "Creating desktop file: $launcher" command="flatpak run net.retrodeck.retrodeck start '$roms_folder/$system/$path'"
# if [[ -v command_list_default[$system] ]]; then
# command="${command_list_default[$system]}"
# else
# log e "$system is not included in the commands array."
# continue
# fi
# Populate the .sync script with the correct command
# TODO: if there is any emulator defined in the xml we use that, else... how we can know which is the default one?
# TODO: if steam is flatpak the command wrapping will change in .desktop
local command="flatpak run net.retrodeck.retrodeck start '$roms_folder/$system/$path'"
# Create the launcher file using a heredoc - if you enable .desktp this remember to edit .desktop in SRM userConfigurations.json and the above launcher variable (and vice versa)
# cat <<EOF > "$launcher"
# [Desktop Entry]
# Version=1.0
# Name=$name
# Comment=$name via RetroDECK
# Exec=$command
# Icon=net.retrodeck.retrodeck
# Terminal=false
# Type=Application
# Categories=Game;Emulator;
# EOF
cat <<EOF > "$launcher" cat <<EOF > "$launcher"
#!/bin/bash #!/bin/bash
if [ test "\$(whereis flatpak)" = "flatpak:" ]; then if [ test "\$(whereis flatpak)" = "flatpak:" ]; then
@ -121,7 +74,16 @@ EOF
log d "$launcher desktop file already exists" log d "$launcher desktop file already exists"
mv "$launcher_tmp" "$launcher" mv "$launcher_tmp" "$launcher"
fi fi
done fi
# Clean up variables for safety
to_be_added=false
path=""
name=""
fi
done < "$gamelist"
else
log "e" "Gamelist file not found: $gamelist"
fi fi
done done
@ -134,8 +96,6 @@ EOF
log d "Updating game list" log d "Updating game list"
steam-rom-manager add steam-rom-manager add
fi fi
log i "Steam Sync: completed"
} }
remove_from_steam() { remove_from_steam() {