|
| 1 | +#!/bin/bash |
| 2 | +# Steam GameMode Configuration Script |
| 3 | + |
| 4 | +TEXTDOMAIN=biglinux-settings |
| 5 | +export TEXTDOMAIN |
| 6 | + |
| 7 | +error_json() { echo "{\"error\": \"$1\"}"; exit 0; } |
| 8 | + |
| 9 | +# Detect Steam paths |
| 10 | +get_steam_path() { |
| 11 | + for p in "$HOME/.steam/steam" "$HOME/.local/share/Steam" "$HOME/.steam/debian-installation"; do |
| 12 | + [ -f "$p/config/config.vdf" ] && echo "$p" && return |
| 13 | + done |
| 14 | +} |
| 15 | + |
| 16 | +STEAM_PATH=$(get_steam_path) |
| 17 | +[ -z "$STEAM_PATH" ] && error_json "Steam path not found." |
| 18 | + |
| 19 | +LOCAL_CONFIG=$(find "$STEAM_PATH/userdata" -maxdepth 3 -name "localconfig.vdf" 2>/dev/null | head -1) |
| 20 | + |
| 21 | +# Exclusions |
| 22 | +EXCLUDE_IDS=("228980" "1070560" "1391110" "1628350" "1493710" "2180100" "961940" "1054830" "1113280" "1245040" "1420170" "1580130" "1887720" "2348590" "2805730" "250820" "1826330") |
| 23 | +EXCLUDE_KEYWORDS=("proton" "steam linux runtime" "steamworks" "redistributable" "redist" "directx" "vcredist" "visual c++" "physx" "openal" ".net" "easyanticheat" "battleye" "steam client" "steamvr" "steam controller" "dedicated server" "sdk" "soldier" "sniper" "scout" "pressure-vessel") |
| 24 | + |
| 25 | +is_excluded() { |
| 26 | + local id=$1 name="${2,,}" |
| 27 | + for ex in "${EXCLUDE_IDS[@]}"; do [ "$id" == "$ex" ] && return 0; done |
| 28 | + for kw in "${EXCLUDE_KEYWORDS[@]}"; do [[ "$name" == *"$kw"* ]] && return 0; done |
| 29 | + return 1 |
| 30 | +} |
| 31 | + |
| 32 | +json_escape() { echo "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'; } |
| 33 | + |
| 34 | +ACTION="$1" |
| 35 | +ARGS="${@:2}" |
| 36 | + |
| 37 | +list_games() { |
| 38 | + [ ! -f "$LOCAL_CONFIG" ] && error_json "Local config not found." |
| 39 | + |
| 40 | + # Find IDs with gamemoderun in LaunchOptions |
| 41 | + ENABLED=$(awk ' |
| 42 | + /^[[:space:]]*"[0-9]+"/ { match($0, /[0-9]+/); id = substr($0, RSTART, RLENGTH) } |
| 43 | + tolower($0) ~ /launchoptions/ && tolower($0) ~ /gamemoderun/ { if (id) print id } |
| 44 | + ' "$LOCAL_CONFIG") |
| 45 | + ENABLED_STR=" $(echo "$ENABLED" | tr '\n' ' ') " |
| 46 | + |
| 47 | + echo "[" |
| 48 | + first=1 |
| 49 | + for manifest in "$STEAM_PATH"/steamapps/appmanifest_*.acf; do |
| 50 | + [ -f "$manifest" ] || continue |
| 51 | + appid=$(awk -F'"' '/"appid"/ {print $4; exit}' "$manifest") |
| 52 | + name=$(awk -F'"' '/"name"/ {print $4; exit}' "$manifest") |
| 53 | + [ -z "$appid" ] || [ -z "$name" ] && continue |
| 54 | + is_excluded "$appid" "$name" && continue |
| 55 | + |
| 56 | + has_gm="false" |
| 57 | + [[ "$ENABLED_STR" == *" $appid "* ]] && has_gm="true" |
| 58 | + |
| 59 | + [ $first -eq 0 ] && echo "," |
| 60 | + echo "{\"app_id\": \"$appid\", \"name\": \"$(json_escape "$name")\", \"has_gamemode\": $has_gm}" |
| 61 | + first=0 |
| 62 | + done |
| 63 | + echo "]" |
| 64 | +} |
| 65 | + |
| 66 | +apply_changes() { |
| 67 | + [ ! -f "$LOCAL_CONFIG" ] && error_json "Local config not found." |
| 68 | + cp "$LOCAL_CONFIG" "$LOCAL_CONFIG.backup" |
| 69 | + |
| 70 | + TMP=$(mktemp) |
| 71 | + awk -v enabled=" $ARGS " ' |
| 72 | + BEGIN { depth=0; id=""; app_d=0; found=0; pending="" } |
| 73 | + { |
| 74 | + line=$0; out=1 |
| 75 | + temp=line; gsub(/"[^"]*"/,"",temp) |
| 76 | + nopen=gsub(/{/,"{",temp); nclose=gsub(/}/,"}",temp) |
| 77 | + |
| 78 | + # Detect app ID |
| 79 | + if (match(line, /^[[:space:]]*"[0-9]+"[[:space:]]*$/)) { |
| 80 | + gsub(/[^0-9]/,"",line); pending=line; line=$0 |
| 81 | + } else if (match(line, /^[[:space:]]*"[0-9]+"[[:space:]]*\{/)) { |
| 82 | + str=line; gsub(/[^0-9]/,"",str); id=str; app_d=depth+1; found=0; pending="" |
| 83 | + } else if (match(line, /^[[:space:]]*\{[[:space:]]*$/) && pending) { |
| 84 | + id=pending; app_d=depth+1; found=0; pending="" |
| 85 | + } |
| 86 | + |
| 87 | + # Process LaunchOptions |
| 88 | + if (id && depth>=app_d && match(line, /"[Ll]aunch[Oo]ptions"/)) { |
| 89 | + found=1 |
| 90 | + is_on=(index(enabled," "id" ")>0) |
| 91 | + if (match(line, /"[Ll]aunch[Oo]ptions"[[:space:]]*"[^"]*"/)) { |
| 92 | + match(line, /^.*"[Ll]aunch[Oo]ptions"[[:space:]]*/) |
| 93 | + pre=substr(line,1,RLENGTH) |
| 94 | + rest=substr(line,RLENGTH+1) |
| 95 | + if (match(rest, /^"[^"]*"/)) { |
| 96 | + val=substr(rest,2,RLENGTH-2) |
| 97 | + gsub(/gamemoderun[[:space:]]*%command%[[:space:]]*/,"",val) |
| 98 | + gsub(/^[[:space:]]+|[[:space:]]+$/,"",val) |
| 99 | + if (is_on) val = (val?"gamemoderun %command% "val:"gamemoderun %command%") |
| 100 | + print pre"\""val"\""; out=0 |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + depth+=nopen |
| 106 | + |
| 107 | + # Add LaunchOptions before closing if needed |
| 108 | + if (nclose && id) { |
| 109 | + nd=depth-nclose |
| 110 | + if (nd<app_d) { |
| 111 | + if (index(enabled," "id" ")>0 && !found) { |
| 112 | + match($0,/^[[:space:]]*/); ind=substr($0,1,RLENGTH) |
| 113 | + print ind"\t\"LaunchOptions\"\t\t\"gamemoderun %command%\"" |
| 114 | + } |
| 115 | + id=""; app_d=0; found=0 |
| 116 | + } |
| 117 | + } |
| 118 | + depth-=nclose |
| 119 | + if (out) print $0 |
| 120 | + }' "$LOCAL_CONFIG" > "$TMP" |
| 121 | + |
| 122 | + mv "$TMP" "$LOCAL_CONFIG" |
| 123 | + echo "Success" |
| 124 | +} |
| 125 | + |
| 126 | +close_steam() { pkill -9 steam; pkill -9 steamwebhelper; echo "done"; } |
| 127 | +check_steam() { pgrep -x steam >/dev/null && echo "running" || echo "stopped"; } |
| 128 | + |
| 129 | +case "$ACTION" in |
| 130 | + list) list_games ;; |
| 131 | + apply) apply_changes ;; |
| 132 | + close_steam) close_steam ;; |
| 133 | + check_steam) check_steam ;; |
| 134 | + *) echo "Usage: $0 {list|apply|close_steam|check_steam}"; exit 1 ;; |
| 135 | +esac |
0 commit comments