Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions bin/omarchy-games-retro-install
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,65 @@ if [[ ! -f $core_path ]]; then
exit 1
fi

desktop_exec_quote() {
# One Exec argument, double-quoted per the freedesktop Desktop Entry spec:
# inside quotes a literal " and \ take a backslash, and a literal % becomes
# %%. Without this a quote in a path breaks the argument grouping and a %
# is treated as a field code (%U, %F, ...) by the launcher.
# A raw newline/CR/tab would likewise break the value onto a new key line,
# so escape them after the backslash doubling, mirroring
# desktop_string_escape.
local value="$1"

value=${value//\\/\\\\}
value=${value//\"/\\\"}
value=${value//%/%%}
value=${value//$'\t'/\\t}
value=${value//$'\r'/\\r}
value=${value//$'\n'/\\n}

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_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_path##*/}"
fi

desktop_name="$game_name"
desktop_id=$(printf '%s' "$desktop_name" | tr '[:upper:]' '[:lower:]' | tr -cs '[:alnum:]' '-' | sed 's/^-//; s/-$//')

if [[ -z $desktop_id ]]; then
desktop_id="retro-game"
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
fi

desktop_dir="$HOME/.local/share/applications"
desktop_file="$desktop_dir/$desktop_id.desktop"
mkdir -p "$desktop_dir"

cat >"$desktop_file" <<EOF
[Desktop Entry]
Version=1.0
Name=$desktop_name
Comment=Play $game_name with RetroArch
Exec=retroarch -L "$core_path" "$game_path"
Name=$(desktop_string_escape "$desktop_name")
Comment=Play $(desktop_string_escape "$game_name") with RetroArch
Exec=retroarch -L $(desktop_exec_quote "$core_path") $(desktop_exec_quote "$game_path")
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Terminal=false
Type=Application
Icon=retro-gaming
Expand Down
83 changes: 83 additions & 0 deletions test/shell.d/games-retro-install-desktop-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

# games-retro-install must emit .desktop files whose Exec quoting and Name
# values survive adversarial ROM filenames (quotes, %, newlines, names that
# clean to nothing).

source "$(dirname "$0")/base-test.sh"

workdir=$(mktemp -d)
trap 'rm -rf "$workdir"' EXIT

mkdir -p "$workdir/stubs" "$workdir/cores" "$workdir/roms" "$workdir/home"
touch "$workdir/cores/snes9x_libretro.so"

for stub in update-desktop-database omarchy-notification-send; do
printf '#!/bin/bash\nexit 0\n' >"$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"
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

# 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"

# 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"