Advertisement
Olliebrown

copySteamControllerToCave.sh

Jan 29th, 2013
1,823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.52 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. USER_HOME=$(eval echo ~)
  4. STEAM_USER_HOME="${USER_HOME}/Library/Application Support/Steam/"
  5. STEAM_CONFIG_FILE="${STEAM_USER_HOME}config/config.vdf"
  6. CAVE_CONFIG_FILE="${STEAM_USER_HOME}SteamApps/common/TheCave/Cave.app/Contents/Resources/Data/Config/SDLGamepad.config"
  7.  
  8. if [ ! -e "${STEAM_CONFIG_FILE}" ]; then
  9.     echo "Could not find Steam user config file."
  10.     exit 1
  11. fi
  12.  
  13. if [ ! -e "${CAVE_CONFIG_FILE}" ]; then
  14.     echo "Could not find 'The Cave' SDL Gamepad config file."
  15.     exit 1
  16. fi
  17.  
  18. clear
  19. printf "Steam user data found: '%s'\n" "${STEAM_USER_HOME}"
  20. printf "Steam config file found: 'config/config.vdf'\n"
  21. printf "The Cave gamepad file found: 'SteamApps/common/TheCave/Cave.app/Contents/Resources/Data/Config/SDLGamepad.config'\n\n"
  22.  
  23. SDL_GAMECONTROLLER_STRING=`cat "${STEAM_CONFIG_FILE}" | grep '"SDL_GamepadBind"' | perl -nle 'm/"SDL_GamepadBind"\s*"(.*)"/; print $1'`
  24. if [ -z "${SDL_GAMECONTROLLER_STRING}" ]; then
  25.     echo "Failed to extract controller key binding.  Did you configure it in Steam's 'Big Picture' mode?"
  26.     exit 1
  27. fi
  28.  
  29. OLD_IFS=$IFS
  30. printf "Controller data:\n"
  31. IFS=','
  32. CONTROLLER_DATA=( $SDL_GAMECONTROLLER_STRING )
  33. CONTROLLER_NAME=${CONTROLLER_DATA[1]}
  34. CONTROLLER_GUID=${CONTROLLER_DATA[0]}
  35. printf "\tName = %s\n\tGUID = %s\n" "${CONTROLLER_NAME}" "${CONTROLLER_GUID}"
  36.  
  37. IFS=':'
  38. INDEX=0;
  39. for i in {2..22}; do
  40.     BUTTON_DATA=( ${CONTROLLER_DATA[i]} )
  41.     if [ "${BUTTON_DATA[1]}" = "" ]; then
  42.         printf "\t%s --> %s\n" "${BUTTON_DATA[0]}" "UNBOUND"   
  43.     elif [ "${BUTTON_DATA[1]:0:1}" = "b" ]; then
  44.         printf "\t%s --> button %s\n" "${BUTTON_DATA[0]}" "${BUTTON_DATA[1]:1}"
  45.     else
  46.         printf "\t%s --> axis %s\n" "${BUTTON_DATA[0]}" "${BUTTON_DATA[1]:1}"  
  47.     fi
  48. done
  49. IFS=$OLD_IFS
  50.  
  51. printf "\n"
  52.  
  53. TEST_FOR_EXACT_EXISTENCE=`grep "${SDL_GAMECONTROLLER_STRING}" "${CAVE_CONFIG_FILE}"`
  54. if [ -n "${TEST_FOR_EXACT_EXISTENCE}" ]; then
  55.     echo "This exact controller config string is already in the gamepad config file for The Cave."
  56.     exit 0
  57. fi
  58.  
  59. TEST_FOR_GUID_EXISTENCE=`grep "${CONTROLLER_GUID}" "${CAVE_CONFIG_FILE}"`
  60. if [ -n "${TEST_FOR_GUID_EXISTENCE}" ]; then
  61.     echo "This controller ID is already in the gamepad config file for The Cave but the name or button bindings have changed."
  62.  
  63.     # Look for backup of origianl config file
  64.     if [ ! -f "${CAVE_CONFIG_FILE}.orig" ]; then
  65.         echo "There is no backup file to use.  You must delete the existing string in the config file manually in order to use your new settings."
  66.         exit 1
  67.     fi
  68.  
  69.     # Is this controller ID in the backup file too
  70.     TEST_FOR_GUID_EXISTENCE2=`grep "${CONTROLLER_GUID}" "${CAVE_CONFIG_FILE}.orig"`
  71.     if [ -n "${TEST_FOR_GUID_EXISTENCE2}" ]; then   # (shit)
  72.         echo "The backup config file already has this controller ID too."
  73.         echo "You must manually remove the existing string for this controller in the config file in order to use your new settings."
  74.         exit 1
  75.     fi
  76.  
  77.     # If user wants to use the new settings then just restore the backup
  78.     while true; do
  79.         read -p "Do you wish to replace the old name and bindings with the new ones? [y/n]: " yn
  80.         case $yn in
  81.             [Yy]* ) cp "${CAVE_CONFIG_FILE}.orig" "${CAVE_CONFIG_FILE}"; break;;
  82.             [Nn]* ) exit 1;;
  83.             * ) echo "Please answer yes or no.";;
  84.         esac
  85.     done
  86. fi
  87.  
  88. printf "Backing up original The Cave config file to %s\n" "${CAVE_CONFIG_FILE}.orig"
  89. cp "${CAVE_CONFIG_FILE}" "${CAVE_CONFIG_FILE}.orig"
  90.  
  91. printf "Adding controller SDL binding to config file ... "
  92. printf "\n# Controller configuration extracted from Steam 'config.vdf' file\n%s\n\n" "${SDL_GAMECONTROLLER_STRING}" >> "${CAVE_CONFIG_FILE}"
  93. printf "Done!\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement