mirror of
https://github.com/RetroDECK/RetroDECK.git
synced 2024-12-04 03:25:39 +00:00
Compare commits
2 commits
8edc8ad9e2
...
293eaac5bf
Author | SHA1 | Date | |
---|---|---|---|
XargonWan | 293eaac5bf | ||
XargonWan | e4c1f20684 |
|
@ -227,4 +227,6 @@ else
|
|||
multi_user_data_folder="$rdhome/multi-user-data" # The default location of multi-user environment profiles
|
||||
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
|
||||
|
|
|
@ -73,8 +73,8 @@ run_game() {
|
|||
if [[ -n "$altemulator" ]]; then
|
||||
|
||||
log d "Found <altemulator> for game: $altemulator"
|
||||
emulator=$(xmllint --recover --xpath "string(//command[@label=\"$altemulator\"])" "$es_systems" 2>/dev/null)
|
||||
|
||||
emulator=$(xmllint --recover --xpath "string(//system[name=\"$system\"]/command[@label=\"$altemulator\"])" "$es_systems" 2>/dev/null)
|
||||
|
||||
else # if no altemulator is found we search if a global one is set
|
||||
|
||||
log d "No altemulator found in the game entry, searching for alternativeEmulator to check if a global emulator is set for the system $system"
|
||||
|
@ -101,7 +101,7 @@ run_game() {
|
|||
log i " RetroDECK is now booting the game"
|
||||
log i " Game path: \"$game\""
|
||||
log i " Recognized system: $system"
|
||||
log i " Given emulator: $emulator"
|
||||
log i " Command line: $emulator"
|
||||
log i "-------------------------------------------"
|
||||
|
||||
# Now pass the final constructed command to substitute_placeholders function
|
||||
|
@ -164,7 +164,6 @@ find_system_commands() {
|
|||
echo "$selected_command"
|
||||
}
|
||||
|
||||
# Function to substitute placeholders in the command
|
||||
substitute_placeholders() {
|
||||
local cmd="$1"
|
||||
log d "Substitute placeholder: working on $cmd"
|
||||
|
@ -181,7 +180,8 @@ substitute_placeholders() {
|
|||
local rom_dir_raw="$rom_dir"
|
||||
local es_path=""
|
||||
local emulator_path=""
|
||||
|
||||
local start_dir=""
|
||||
|
||||
# Manually replace %EMULATOR_*% placeholders
|
||||
while [[ "$cmd" =~ (%EMULATOR_[A-Z0-9_]+%) ]]; do
|
||||
placeholder="${BASH_REMATCH[1]}"
|
||||
|
@ -189,6 +189,55 @@ substitute_placeholders() {
|
|||
cmd="${cmd//$placeholder/$emulator_path}"
|
||||
done
|
||||
|
||||
# Process %STARTDIR%
|
||||
local start_dir_pos=$(echo "$cmd" | grep -b -o "%STARTDIR%" | cut -d: -f1)
|
||||
if [[ -n "$start_dir_pos" ]]; then
|
||||
# Validate and extract %STARTDIR% value
|
||||
if [[ "${cmd:start_dir_pos+10:1}" != "=" ]]; then
|
||||
log e "Error: Invalid %STARTDIR% entry in command"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ "${cmd:start_dir_pos+11:1}" == "\"" ]]; then
|
||||
# Quoted path
|
||||
local closing_quotation=$(echo "${cmd:start_dir_pos+12}" | grep -bo '"' | head -n 1 | cut -d: -f1)
|
||||
if [[ -z "$closing_quotation" ]]; then
|
||||
log e "Error: Invalid %STARTDIR% entry (missing closing quotation)"
|
||||
return 1
|
||||
fi
|
||||
start_dir="${cmd:start_dir_pos+12:closing_quotation}"
|
||||
cmd="${cmd:0:start_dir_pos}${cmd:start_dir_pos+12+closing_quotation+1}"
|
||||
else
|
||||
# Non-quoted path
|
||||
local space_pos=$(echo "${cmd:start_dir_pos+11}" | grep -bo ' ' | head -n 1 | cut -d: -f1)
|
||||
if [[ -n "$space_pos" ]]; then
|
||||
start_dir="${cmd:start_dir_pos+11:space_pos}"
|
||||
cmd="${cmd:0:start_dir_pos}${cmd:start_dir_pos+11+space_pos+1}"
|
||||
else
|
||||
start_dir="${cmd:start_dir_pos+11}"
|
||||
cmd="${cmd:0:start_dir_pos}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Expand paths in %STARTDIR%
|
||||
start_dir=$(eval echo "$start_dir") # Expand ~ or environment variables
|
||||
start_dir="${start_dir//%EMUDIR%/$(dirname "$emulator_path")}"
|
||||
start_dir="${start_dir//%GAMEDIR%/$(dirname "$rom_path")}"
|
||||
start_dir="${start_dir//%GAMEENTRYDIR%/$rom_path}"
|
||||
|
||||
# Create directory if it doesn't exist
|
||||
if [[ ! -d "$start_dir" ]]; then
|
||||
mkdir -p "$start_dir" || {
|
||||
log e "Error: Directory \"$start_dir\" could not be created. Permission problems?"
|
||||
return 1
|
||||
}
|
||||
fi
|
||||
|
||||
# Normalize the path
|
||||
start_dir=$(realpath "$start_dir")
|
||||
log d "Setting start directory to: $start_dir"
|
||||
fi
|
||||
|
||||
# Substitute %BASENAME% and other placeholders
|
||||
cmd="${cmd//"%BASENAME%"/"'$base_name'"}"
|
||||
cmd="${cmd//"%FILENAME%"/"'$file_name'"}"
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
#!/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() {
|
||||
|
||||
log "i" "Starting Steam Sync"
|
||||
|
||||
steamsync_folder="$rdhome/.sync"
|
||||
steamsync_folder_tmp="$rdhome/.sync-tmp"
|
||||
create_dir $steamsync_folder
|
||||
mv $steamsync_folder $steamsync_folder_tmp
|
||||
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
|
||||
|
@ -17,98 +18,50 @@ add_to_steam() {
|
|||
prepare_component "reset" "steam-rom-manager"
|
||||
fi
|
||||
|
||||
# Build the systems array from space-separated systems
|
||||
local systems_string=$(jq -r '.system | keys[]' "$features" | paste -sd' ')
|
||||
IFS=' ' read -r -a systems <<< "$systems_string" # TODO: do we need this line?
|
||||
# Iterate through all gamelist.xml files in the folder structure
|
||||
for system_path in "$rdhome/ES-DE/gamelists/"*/; do
|
||||
system=$(basename "$system_path") # Extract the folder name as the system name
|
||||
gamelist="${system_path}gamelist.xml"
|
||||
|
||||
local games=()
|
||||
|
||||
for system in "${systems[@]}"; do
|
||||
|
||||
local gamelist="$rdhome/ES-DE/gamelists/$system/gamelist.xml"
|
||||
log d "Reading favorites for $system"
|
||||
|
||||
# Ensure gamelist.xml exists in the current folder
|
||||
if [ -f "$gamelist" ]; then
|
||||
while IFS= read -r line; do
|
||||
# Detect the start of a <game> block
|
||||
if [[ "$line" =~ \<game\> ]]; then
|
||||
to_be_added=false # Reset the flag for a new block
|
||||
path=""
|
||||
name=""
|
||||
fi
|
||||
|
||||
# Extract all <game> elements that are marked as favorite="true"
|
||||
game_blocks=$(xmllint --recover --xpath '//game[favorite="true"]' "$gamelist" 2>/dev/null)
|
||||
log d "Extracted favorite game blocks:\n\n$game_blocks\n\n"
|
||||
# Check for <favorite>true</favorite>
|
||||
if [[ "$line" =~ \<favorite\>true\<\/favorite\> ]]; then
|
||||
to_be_added=true
|
||||
fi
|
||||
|
||||
# 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' ' ')"
|
||||
# Extract the <path> and remove leading "./" if present
|
||||
if [[ "$line" =~ \<path\>(.*)\<\/path\> ]]; then
|
||||
path="${BASH_REMATCH[1]#./}"
|
||||
fi
|
||||
|
||||
# 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 and sanitize <name>
|
||||
if [[ "$line" =~ \<name\>(.*)\<\/name\> ]]; then
|
||||
name=$(sanitize "${BASH_REMATCH[1]}")
|
||||
fi
|
||||
|
||||
# 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 ./
|
||||
# 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"
|
||||
|
||||
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
|
||||
|
||||
# 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')
|
||||
local sanitized_name=$(echo "$sanitized_name" | sed -e 's/:/ -/g')
|
||||
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
|
||||
|
||||
log d "Sanitized Name: $sanitized_name"
|
||||
|
||||
local launcher="$steamsync_folder/${sanitized_name}.sh"
|
||||
local launcher_tmp="$steamsync_folder_tmp/${sanitized_name}.sh"
|
||||
|
||||
if [ ! -e "$launcher_tmp" ]; then
|
||||
|
||||
log d "Creating desktop file: $launcher"
|
||||
|
||||
# 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"
|
||||
# Create the launcher file
|
||||
if [ ! -e "$launcher_tmp" ]; then
|
||||
log d "Creating launcher file: $launcher"
|
||||
command="flatpak run net.retrodeck.retrodeck start '$roms_folder/$system/$path'"
|
||||
cat <<EOF > "$launcher"
|
||||
#!/bin/bash
|
||||
if [ test "\$(whereis flatpak)" = "flatpak:" ]; then
|
||||
flatpak-spawn --host $command
|
||||
|
@ -116,26 +69,33 @@ else
|
|||
$command
|
||||
fi
|
||||
EOF
|
||||
chmod +x "$launcher"
|
||||
chmod +x "$launcher"
|
||||
else
|
||||
log d "$launcher desktop file already exists"
|
||||
mv "$launcher_tmp" "$launcher"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Clean up variables for safety
|
||||
to_be_added=false
|
||||
path=""
|
||||
name=""
|
||||
fi
|
||||
done < "$gamelist"
|
||||
else
|
||||
log d "$launcher desktop file already exists"
|
||||
mv "$launcher_tmp" "$launcher"
|
||||
log "e" "Gamelist file not found: $gamelist"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
rm -r $steamsync_folder_tmp
|
||||
|
||||
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
|
||||
done
|
||||
|
||||
rm -r $steamsync_folder_tmp
|
||||
|
||||
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
|
||||
|
||||
log i "Steam Sync: completed"
|
||||
}
|
||||
|
||||
remove_from_steam() {
|
||||
|
|
Loading…
Reference in a new issue