2024-09-05 02:44:54 +00:00
#!/bin/bash
# Add games to Steam function
2024-09-08 01:31:52 +00:00
add_to_steam( ) {
2024-09-05 02:44:54 +00:00
log "i" "Starting Steam Sync"
2024-09-08 01:31:52 +00:00
steamsync_folder = " $rdhome /.sync "
2024-09-24 20:33:20 +00:00
steamsync_folder_tmp = " $rdhome /.sync-tmp "
create_dir $steamsync_folder
mv $steamsync_folder $steamsync_folder_tmp
2024-09-08 01:31:52 +00:00
create_dir $steamsync_folder
2024-09-05 05:54:21 +00:00
local srm_path = "/var/config/steam-rom-manager/userData/userConfigurations.json"
if [ ! -f " $srm_path " ] ; then
2024-09-08 11:53:43 +00:00
log "e" "Steam ROM Manager configuration not initialized! Initializing now."
prepare_component "reset" "steam-rom-manager"
2024-09-05 05:54:21 +00:00
fi
2024-09-05 02:44:54 +00:00
# Build the systems array from space-separated systems
2024-09-05 05:54:21 +00:00
local systems_string = $( jq -r '.system | keys[]' " $features " | paste -sd' ' )
2024-09-08 11:53:43 +00:00
IFS = ' ' read -r -a systems <<< " $systems_string " # TODO: do we need this line?
2024-09-05 02:44:54 +00:00
local games = ( )
for system in " ${ systems [@] } " ; do
local gamelist = " $rdhome /ES-DE/gamelists/ $system /gamelist.xml "
2024-09-05 05:54:21 +00:00
if [ -f " $gamelist " ] ; then
2024-09-05 02:44:54 +00:00
# Extract all <game> elements that are marked as favorite="true"
game_blocks = $( xmllint --recover --xpath '//game[favorite="true"]' " $gamelist " 2>/dev/null)
2024-09-05 05:54:21 +00:00
log d " Extracted favorite game blocks:\n\n $game_blocks \n\n "
2024-09-05 02:44:54 +00:00
# Split the game_blocks into an array, where each element is a full <game> block
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
2024-09-05 05:54:21 +00:00
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
2024-09-08 03:33:08 +00:00
# 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
2024-09-05 05:54:21 +00:00
log "d" " Steam Sync: found favorite game: $name "
else
log "w" "Steam Sync: failed to find valid name or path for favorite game"
fi
# Sanitize the game name for the filename: replace special characters with underscores
local sanitized_name = $( echo " $name " | sed -e 's/^A-Za-z0-9._-/ /g' )
2024-09-08 01:31:52 +00:00
local sanitized_name = $( echo " $sanitized_name " | sed -e 's/:/ -/g' )
local sanitized_name = $( echo " $sanitized_name " | sed -e 's/&/and/g' )
2024-09-08 11:53:43 +00:00
local sanitized_name = $( echo " $sanitized_name " | sed -e 's%/%and%g' )
2024-09-05 05:54:21 +00:00
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
2024-09-07 23:57:36 +00:00
log d " Sanitized Name: $sanitized_name "
2024-09-05 05:54:21 +00:00
2024-09-11 10:12:52 +00:00
local launcher = " $steamsync_folder / ${ sanitized_name } .sh "
2024-09-24 20:33:20 +00:00
local launcher_tmp = " $steamsync_folder_tmp / ${ sanitized_name } .sh "
2024-09-24 19:53:52 +00:00
2024-09-24 20:33:20 +00:00
if [ ! -e " $launcher_tmp " ] ; then
2024-09-24 19:53:52 +00:00
log d " Creating desktop file: $launcher "
2024-09-05 05:54:21 +00:00
2024-09-08 03:33:08 +00:00
# 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
2024-09-05 05:54:21 +00:00
# Populate the .sync script with the correct command
2024-09-08 13:47:24 +00:00
# TODO: if there is any emulator defined in the xml we use that, else... how we can know which is the default one?
2024-09-11 10:12:52 +00:00
# TODO: if steam is flatpak the command wrapping will change in .desktop
2024-09-24 19:53:52 +00:00
local command = " flatpak run net.retrodeck.retrodeck start ' $roms_folder / $system / $path ' "
2024-09-11 10:12:52 +00:00
# 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
2024-09-24 19:53:52 +00:00
cat <<EOF > " $launcher "
2024-09-11 10:12:52 +00:00
#!/bin/bash
2024-09-24 19:53:52 +00:00
if [ test "\$(whereis flatpak)" = "flatpak:" ] ; then
2024-09-11 10:12:52 +00:00
flatpak-spawn --host $command
else
$command
fi
2024-09-11 07:01:52 +00:00
EOF
2024-09-24 19:53:52 +00:00
else
log d " $launcher desktop file already exists "
2024-09-24 20:33:20 +00:00
mv " $launcher_tmp " " $launcher "
2024-09-24 19:53:52 +00:00
fi
2024-09-05 02:44:54 +00:00
done
fi
2024-09-05 05:54:21 +00:00
done
2024-09-24 20:06:53 +00:00
2024-09-24 20:33:20 +00:00
rm -r $steamsync_folder_tmp
2024-09-24 20:06:53 +00:00
if [ -z " $( ls -A $steamsync_folder ) " ] ; then
log d "No games found, cleaning shortcut"
remove_from_steam
else
log d "Updating game list"
#steam-rom-manager add
fi
2024-09-05 05:54:21 +00:00
log i "Steam Sync: completed"
2024-09-05 12:07:47 +00:00
}
2024-09-07 23:57:36 +00:00
remove_from_steam( ) {
2024-09-24 20:06:53 +00:00
log d "Creating fake game"
cat "" > " $steamsync_folder /CUL0.sh "
log d "Cleaning the shortcut"
steam-rom-manager remove
log d "Removing fake game"
rm " $steamsync_folder /CUL0.sh "
2024-09-24 19:53:52 +00:00
}