Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Log file for debugging
- LOG_FILE="$HOME/.local/share/Steam/userdata/controller_settings.log"
- log() {
- echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
- }
- # Check if protontricks is installed
- if ! command -v flatpak run com.github.Matoking.protontricks &>/dev/null; then
- kdialog --error "Protontricks is required but not installed. Please install it and try again."
- exit 1
- fi
- # Get the list of Non-Steam games
- games=$(flatpak run com.github.Matoking.protontricks -l | grep "Non-Steam shortcut: " | sed 's/Non-Steam shortcut: //')
- if [[ -z "$games" ]]; then
- kdialog --error "No Non-Steam games found."
- exit 1
- fi
- # Build an array of games (each with game name and AppID)
- declare -a game_list
- while IFS= read -r line; do
- game_list+=("$line")
- done <<< "$games"
- # Build the kdialog menu parameters.
- # kdialog expects key-value pairs: key1 value1 key2 value2 ...
- menu_params=()
- for i in "${!game_list[@]}"; do
- # Extract the game name (remove the AppID portion)
- display_name=$(echo "${game_list[i]}" | sed -E 's/\s\([0-9]+\)//')
- key=$((i+1))
- menu_params+=("$key" "$display_name")
- done
- # Define the userdata directory (for backups)
- STEAM_CONFIG_DIR="$HOME/.local/share/Steam/userdata"
- # Check if any backup files exist (looking in any userdata/config folder)
- backup_exist=$(find "$STEAM_CONFIG_DIR" -type f -name "localconfig_backup_*.vdf" | head -n 1)
- if [[ -n "$backup_exist" ]]; then
- # Add restore option at the bottom.
- restore_key=$(( ${#game_list[@]} + 1 ))
- menu_params+=("$restore_key" "[[ Restore Backup ]]")
- fi
- # Present the menu using kdialog
- selection=$(kdialog --menu "Select a game to modify controller settings:" "${menu_params[@]}")
- if [[ -z "$selection" ]]; then
- kdialog --error "No selection made. Exiting."
- exit 1
- fi
- # If the selection equals the restore option key, perform backup restoration.
- if [[ -n "$restore_key" && "$selection" -eq "$restore_key" ]]; then
- # Find all backups matching our naming pattern
- backups=$(find "$STEAM_CONFIG_DIR" -type f -name "localconfig_backup_*.vdf")
- if [[ -z "$backups" ]]; then
- kdialog --error "No backups found."
- exit 1
- fi
- # Build the restore menu parameters.
- backup_menu=()
- count=1
- declare -A backup_map
- while IFS= read -r backup; do
- # Extract a cleaned-up name: remove the prefix and the .vdf suffix.
- filename=$(basename "$backup")
- display_backup=${filename#localconfig_backup_}
- display_backup=${display_backup%.vdf}
- backup_menu+=("$count" "$display_backup")
- backup_map["$count"]="$backup"
- ((count++))
- done <<< "$backups"
- backup_choice=$(kdialog --menu "Select a backup to restore:" "${backup_menu[@]}")
- if [[ -n "$backup_choice" ]]; then
- selected_backup=${backup_map["$backup_choice"]}
- # Restore the selected backup to all userdata folders that contain a localconfig.vdf.
- for USER_FOLDER in $(find "$STEAM_CONFIG_DIR" -maxdepth 1 -type d -regex '.*/[0-9]+'); do
- CONFIG_FILE="$USER_FOLDER/config/localconfig.vdf"
- if [[ -f "$CONFIG_FILE" ]]; then
- cp "$selected_backup" "$CONFIG_FILE"
- log "Backup restored from $selected_backup for user $USER_FOLDER."
- kdialog --msgbox "Backup restored from $selected_backup for user $USER_FOLDER."
- fi
- done
- fi
- exit 0
- fi
- # Otherwise, process the selected game.
- # Array indices are 0-based.
- selected_entry="${game_list[selection-1]}"
- # Extract AppID and game name.
- app_id=$(echo "$selected_entry" | grep -oE '[0-9]+')
- game_name=$(echo "$selected_entry" | sed -E 's/\s\([0-9]+\)//')
- # Calculate Short ID as a signed 32-bit integer.
- short_id=$((app_id & 0xFFFFFFFF))
- if (( short_id > 2147483647 )); then
- short_id=$((short_id - 4294967296))
- fi
- log "Selected Game: $game_name | AppID: $app_id | Short ID: $short_id"
- kdialog --msgbox "Selected Game: $game_name\nAppID: $app_id\nShort ID: $short_id"
- # Process each userdata folder.
- USERDATA_FOLDERS=$(find "$STEAM_CONFIG_DIR" -maxdepth 1 -type d -regex '.*/[0-9]+')
- if [[ -z "$USERDATA_FOLDERS" ]]; then
- kdialog --error "No Steam user data folders found!"
- exit 1
- fi
- for USER_FOLDER in $USERDATA_FOLDERS; do
- CONFIG_FILE="$USER_FOLDER/config/localconfig.vdf"
- if [[ ! -f "$CONFIG_FILE" ]]; then
- log "Skipping user folder: $USER_FOLDER (no localconfig.vdf found)"
- continue
- fi
- TIMESTAMP=$(date +'%Y-%m-%dT%H-%M-%S')
- BACKUP_FILE="$USER_FOLDER/config/localconfig_backup_${game_name}_${TIMESTAMP}.vdf"
- cp "$CONFIG_FILE" "$BACKUP_FILE"
- log "Backup created: $BACKUP_FILE for user $USER_FOLDER"
- # Create a temporary file and insert our new settings inside the "apps" section.
- temp_file=$(mktemp)
- inside_apps=0
- while IFS= read -r line; do
- echo "$line" >> "$temp_file"
- # Detect the start of the "apps" section.
- if [[ "$line" =~ \"apps\" ]]; then
- inside_apps=1
- fi
- # After the opening brace of "apps", insert our new settings (only once).
- if [[ $inside_apps -eq 1 && "$line" =~ \{ ]]; then
- inside_apps=0
- # Use printf to generate text with literal tab characters.
- insert_text=$(printf "\t\t\t\"%s\"\n\t\t\t{\n\t\t\t\t\"UseSteamControllerConfig\"\t\t\t\"0\"\n\t\t\t\t\"SteamControllerRumble\"\t\t\t\"-1\"\n\t\t\t\t\"SteamControllerRumbleIntensity\"\t\t\t\"320\"\n\t\t\t}\n" "$short_id")
- echo "$insert_text" >> "$temp_file"
- fi
- done < "$CONFIG_FILE"
- mv "$temp_file" "$CONFIG_FILE"
- if grep -q "\"$short_id\"" "$CONFIG_FILE"; then
- log "Successfully added controller settings for $game_name (User: $USER_FOLDER)."
- kdialog --msgbox "Settings updated successfully for user: $USER_FOLDER"
- else
- log "Verification failed for user $USER_FOLDER! Restoring backup."
- cp "$BACKUP_FILE" "$CONFIG_FILE"
- kdialog --error "Error: Changes could not be verified for user $USER_FOLDER. Backup restored."
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement