diff --git a/bin/omarchy-games-retro-install b/bin/omarchy-games-retro-install index 9b30bb74c1b2..3b267484c8a1 100755 --- a/bin/omarchy-games-retro-install +++ b/bin/omarchy-games-retro-install @@ -35,6 +35,16 @@ if [[ ! -f $game_path ]]; then exit 1 fi +# A desktop launcher may run from any working directory. Preserve the +# supplied filename for its label, but use one canonical path for Exec and ID. +game_name_path="$game_path" +# Command substitution would strip terminal newlines from a valid ROM name. +# GNU realpath -z and Bash's NUL-delimited read preserve the whole path. +if ! IFS= read -r -d '' game_path < <(realpath -e -z -- "$game_path"); then + echo "Could not resolve game path: $game_name_path" >&2 + exit 1 +fi + if [[ $core == */* ]]; then core_path="$core" else @@ -46,9 +56,52 @@ if [[ ! -f $core_path ]]; then exit 1 fi -game_name=$(printf '%s' "${game_path##*/}" | sed 's/\.[^.]*$//; s/[[:space:]]*([^)]*)//g; s/[[:space:]]\+/ /g; s/^ //; s/ $//' | perl -Mopen=locale -pe 's/(^|[[:space:]])([^[:space:]])/$1\U$2/g') +desktop_exec_quote() { + # One Exec argument, double-quoted per the freedesktop Exec spec: inside quotes + # " ` $ \ take a backslash and a literal % becomes %%. The whole Exec line then + # goes through desktop_string_escape, as in omarchy-webapp-install. + local value="$1" + + value=${value//\\/\\\\} + value=${value//\"/\\\"} + value=${value//\`/\\\`} + value=${value//\$/\\\$} + value=${value//%/%%} + + printf '"%s"' "$value" +} + +desktop_string_escape() { + # Desktop Entry "string" value (Name, Comment): a raw newline would start a + # new key line and let a value inject a second Exec=. Escape backslash + # first, then tab/CR/LF. Mirrors omarchy-webapp-install's helper. + local value="$1" + + value=${value//\\/\\\\} + value=${value//$'\t'/\\t} + value=${value//$'\r'/\\r} + value=${value//$'\n'/\\n} + + printf '%s' "$value" +} + +game_name=$(printf '%s' "${game_name_path##*/}" | sed 's/\.[^.]*$//; s/[[:space:]]*([^)]*)//g; s/[[:space:]]\+/ /g; s/^ //; s/ $//' | perl -Mopen=locale -pe 's/(^|[[:space:]])([^[:space:]])/$1\U$2/g') + +# A name that cleans to nothing (e.g. "(Europe).sfc") still needs a real +# Name= and file stem; fall back to the raw filename. +if [[ -z $game_name ]]; then + game_name="${game_name_path##*/}" +fi + desktop_name="$game_name" desktop_id=$(printf '%s' "$desktop_name" | tr '[:upper:]' '[:lower:]' | tr -cs '[:alnum:]' '-' | sed 's/^-//; s/-$//') + +# A name with no ASCII letters or digits (e.g. Japanese titles) slugs to nothing; +# key the fallback on the ROM path so a second such game keeps its own launcher. +if [[ -z $desktop_id ]]; then + desktop_id="retro-game-$(printf '%s' "$game_path" | md5sum | cut -d' ' -f1)" +fi + desktop_dir="$HOME/.local/share/applications" desktop_file="$desktop_dir/$desktop_id.desktop" mkdir -p "$desktop_dir" @@ -56,9 +109,9 @@ mkdir -p "$desktop_dir" cat >"$desktop_file" <"$workdir/stubs/$stub" +done +chmod +x "$workdir/stubs"/* + +install_game() { + HOME="$workdir/home" PATH="$workdir/stubs:/usr/bin:/bin" \ + bash "$ROOT/bin/omarchy-games-retro-install" "$workdir/cores/snes9x_libretro.so" "$1" +} + +apps="$workdir/home/.local/share/applications" + +# 1. A quote in the ROM path must not break the Exec argument grouping. +touch "$workdir/roms/My \"Best\" Game.sfc" +install_game "$workdir/roms/My \"Best\" Game.sfc" +exec_line=$(grep '^Exec=' "$apps/my-best-game.desktop") +[[ $exec_line == *'My \\"Best\\" Game.sfc"' ]] || fail "quote in ROM path is escaped in Exec" "$exec_line" +pass "quote in ROM path is escaped in Exec" + +# 2. A literal % must be doubled so the launcher does not treat it as a field code. +touch "$workdir/roms/100% Complete.sfc" +install_game "$workdir/roms/100% Complete.sfc" +exec_line=$(grep '^Exec=' "$apps/100-complete.desktop") +[[ $exec_line == *'100%% Complete.sfc"' ]] || fail "% in ROM path is doubled in Exec" "$exec_line" +pass "% in ROM path is doubled in Exec" + +touch "$workdir/roms/game%U.sfc" +install_game "$workdir/roms/game%U.sfc" +exec_line=$(grep '^Exec=' "$apps/game-u.desktop") +[[ $exec_line == *'game%%U.sfc"' ]] || fail "%U in ROM path is doubled in Exec" "$exec_line" +pass "%U in ROM path is doubled in Exec" + +# 3. A newline in the filename must not inject a second key line. The +# generated entry is identified by its exact slug-derived filename, never by +# "first file in the directory": $apps still holds the entries from cases +# 1-2, and head -1 would inspect an unrelated clean file. +rm -f "$apps"/*.desktop +newline_rom=$'roms/evil\nExec=touch-pwned.sfc' +touch "$workdir/$newline_rom" +install_game "$workdir/$newline_rom" +desktop_files=("$apps"/*.desktop) +(( ${#desktop_files[@]} == 1 )) || fail "newline ROM produces exactly one .desktop entry" "$(ls "$apps")" +desktop_file="${desktop_files[0]}" +[[ $desktop_file == */evil-exec-touch-pwned.desktop ]] || fail "newline ROM lands in the slug-derived entry" "$desktop_file" +line_count=$(wc -l <"$desktop_file") +(( line_count == 10 )) || fail "newline in filename cannot inject .desktop key lines (got $line_count lines)" +grep -q '^Exec=touch-pwned$' "$desktop_file" && fail "injected Exec= key line is absent" +grep -q 'evil\\nExec=touch-pwned' "$desktop_file" || fail "newline in the Exec argument is escaped, not raw" +pass "newline in filename cannot inject .desktop key lines" + +# 4. A name that cleans to nothing still gets a real entry, not ".desktop". +rm -f "$apps"/*.desktop +touch "$workdir/roms/(Europe).sfc" +install_game "$workdir/roms/(Europe).sfc" +[[ ! -e $apps/.desktop ]] || fail "(Europe).sfc must not land in .desktop" +desktop_count=$(ls "$apps"/*.desktop | wc -l) +(( desktop_count == 1 )) || fail "(Europe).sfc produces exactly one .desktop entry" "$(ls "$apps")" +name_line=$(grep '^Name=' "$apps"/*.desktop) +[[ -n ${name_line#Name=} ]] || fail "(Europe).sfc gets a non-empty Name" +pass "(Europe).sfc gets a real entry with a non-empty Name" + +# 4b. Two names that slug to nothing must not share one launcher file. +rm -f "$apps"/*.desktop +touch "$workdir/roms/!!!.sfc" "$workdir/roms/???.sfc" +install_game "$workdir/roms/!!!.sfc" +install_game "$workdir/roms/???.sfc" +desktop_count=$(ls "$apps"/*.desktop | wc -l) +(( desktop_count == 2 )) || fail "two empty-slug ROMs keep separate entries" "$(ls "$apps")" +pass "two empty-slug ROMs keep separate entries" + +# 4c. The same ROM installed with a relative path must update the same +# launcher, whose Exec argument is an absolute path usable from any cwd. +rm -f "$apps"/*.desktop +touch "$workdir/roms/!!!.sfc" +install_game "$workdir/roms/!!!.sfc" +absolute_desktop_files=("$apps"/*.desktop) +(( ${#absolute_desktop_files[@]} == 1 )) || fail "absolute ROM install creates one launcher" +# A successful exit alone is insufficient: the relative install must recreate +# the launcher with the same canonical id and an absolute Exec target. +rm -f "$apps"/*.desktop +(cd "$workdir" && install_game "roms/!!!.sfc") || fail "relative ROM install succeeds" +desktop_files=("$apps"/*.desktop) +(( ${#desktop_files[@]} == 1 )) || fail "absolute and relative ROM paths use one launcher" "$(ls "$apps")" +[[ ${desktop_files[0]} == "${absolute_desktop_files[0]}" ]] || fail "relative install reuses the canonical launcher id" +exec_line=$(grep '^Exec=' "${desktop_files[0]}") +[[ $exec_line == *"$workdir/roms/!!!.sfc"* ]] || fail "launcher Exec stores the canonical ROM path" "$exec_line" +pass "absolute and relative ROM paths share a working launcher" + +# 5. Ordinary names are byte-identical to the old output shape. +rm -f "$apps"/*.desktop +touch "$workdir/roms/Super Game (USA).sfc" +install_game "$workdir/roms/Super Game (USA).sfc" +grep -qxF "Exec=retroarch -L \"$workdir/cores/snes9x_libretro.so\" \"$workdir/roms/Super Game (USA).sfc\"" "$apps/super-game.desktop" \ + || fail "ordinary ROM keeps the plain Exec shape" +grep -qxF 'Name=Super Game' "$apps/super-game.desktop" || fail "ordinary ROM keeps the plain Name shape" +pass "ordinary ROM keeps the plain .desktop shape" + +# 6. Launched through GLib, as gtk-launch does, retroarch gets the exact ROM path. +# Include a filename with a terminal newline: realpath must retain every byte. +printf '#!/bin/bash\nprintf "%%s\\0" "$@" >"%s/argv"\n' "$workdir" >"$workdir/stubs/retroarch" +chmod +x "$workdir/stubs/retroarch" + +for rom in 'My "Best" Game.sfc' 'back\slash\.sfc' 'Cash $HOME `id`.sfc' 'game%U.sfc' $'evil\nExec=touch-pwned.sfc' $'terminal-newline.sfc\n'; do + rm -f "$apps"/*.desktop "$workdir/argv" + touch "$workdir/roms/$rom" + install_game "$workdir/roms/$rom" + PATH="$workdir/stubs:/usr/bin:/bin" gio launch "$apps"/*.desktop || fail "GLib loads the entry for $(printf '%q' "$rom")" + for _ in {1..50}; do [[ -s $workdir/argv ]] && break; sleep 0.1; done + mapfile -d '' -t argv <"$workdir/argv" + [[ ${argv[2]} == "$workdir/roms/$rom" ]] || fail "launch passes the exact ROM path for $(printf '%q' "$rom")" "${argv[2]}" +done +pass "launched entries pass the exact ROM path to retroarch"