Guest User

Untitled

a guest
Aug 29th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Directory for controller profiles
  4. profile_dir="/opt/retropie/configs/gc/Config/Profiles/GCPad"
  5. gameid_ini_dir="/opt/retropie/configs/gc/local/GameSettings" # Directory where GAMEID .ini files are stored
  6. DOLPHIN_TOOL="/opt/retropie/emulators/dolphin/bin/dolphin-tool"
  7. hotkeys_file="/opt/retropie/configs/gc/Config/Hotkeys.ini"
  8.  
  9. # Variables to hold the js profile name without the .ini extension
  10. js_profile_name=""
  11.  
  12. # This function checks for devices in /proc/bus/input/devices that have joystick or gamepad handlers.
  13. function find_game_controllers {
  14. echo "Scanning for game controllers..."
  15.  
  16. while IFS= read -r line; do
  17. # Check for a new device block (starts with "I:")
  18. if [[ $line == I:* ]]; then
  19. device_name=""
  20. js_device=""
  21. fi
  22.  
  23. # Look for a device name
  24. if [[ $line == N:* ]]; then
  25. device_name=$(echo $line | cut -d'"' -f2)
  26. fi
  27.  
  28. # Check for handlers that include jsX (joystick)
  29. if [[ $line == *Handlers=* ]] && [[ $line == *js[0-9]* ]]; then
  30. js_device=$(echo $line | grep -o 'js[0-9]')
  31. echo "Detected Game Controller: $device_name ($js_device)"
  32.  
  33. # Create the .ini file if it doesn't exist
  34. check_and_create_ini "$device_name"
  35.  
  36. # If this is the first detected joystick, store the profile name
  37. if [[ -z "$js_profile_name" ]]; then
  38. js_profile_name="$device_name"
  39. fi
  40. fi
  41. done < /proc/bus/input/devices
  42.  
  43. # If no joystick was found, notify the user
  44. if [[ -z "$js_profile_name" ]]; then
  45. echo "No joystick found (js0 to js4)."
  46. else
  47. echo "Using profile for: $js_profile_name"
  48. check_and_update_hotkeys "$js_profile_name"
  49. fi
  50. }
  51.  
  52. # This function checks if an .ini file exists for a controller and creates it if not.
  53. function check_and_create_ini {
  54. local controller_name="$1"
  55. local ini_file="$profile_dir/$controller_name.ini"
  56.  
  57. # Check if the .ini file exists
  58. if [[ ! -f "$ini_file" ]]; then
  59. echo "Creating .ini file for $controller_name..."
  60.  
  61. # Create the .ini file with the modified template content
  62. cat <<EOF > "$ini_file"
  63. [Profile]
  64. Device = evdev/0/$controller_name
  65. Buttons/A = SOUTH
  66. Buttons/B = EAST
  67. Buttons/X = NORTH
  68. Buttons/Y = WEST
  69. Buttons/Z = TR2
  70. Buttons/Start = \`START\`
  71. Main Stick/Up = \`Axis 1-\`
  72. Main Stick/Down = \`Axis 1+\`
  73. Main Stick/Left = \`Axis 0-\`
  74. Main Stick/Right = \`Axis 0+\`
  75. Main Stick/Modifier = \`Shift\`
  76. Main Stick/Calibration = 100.00 141.42 100.00 141.42 100.00 141.42 100.00 141.42
  77. C-Stick/Up = \`Axis 3-\`
  78. C-Stick/Down = \`Axis 3+\`
  79. C-Stick/Left = \`Axis 2-\`
  80. C-Stick/Right = \`Axis 2+\`
  81. C-Stick/Modifier = \`Ctrl\`
  82. C-Stick/Calibration = 100.00 141.42 100.00 141.42 100.00 141.42 100.00 141.42
  83. Triggers/L = TL
  84. Triggers/R = TR
  85. D-Pad/Up = \`Axis 7-\`
  86. D-Pad/Down = \`Axis 7+\`
  87. D-Pad/Left = \`Axis 6-\`
  88. D-Pad/Right = \`Axis 6+\`
  89. EOF
  90. echo "Created: $ini_file"
  91. else
  92. echo ".ini file for $controller_name already exists."
  93. fi
  94. }
  95.  
  96. # This function checks and updates the Hotkeys.ini file
  97. function check_and_update_hotkeys {
  98. local controller_name="$1"
  99. local device_line="Device = evdev/0/$controller_name"
  100.  
  101. # Path to Hotkeys.ini file
  102. local hotkeys_file="/opt/retropie/configs/gc/Config/Hotkeys.ini"
  103.  
  104. # Check if the Hotkeys file exists
  105. if [[ ! -f "$hotkeys_file" ]]; then
  106. echo "Hotkeys.ini file not found: $hotkeys_file"
  107. return
  108. fi
  109.  
  110. # Check if the correct Device line already exists
  111. if grep -q "^$device_line$" "$hotkeys_file"; then
  112. echo "The correct Device line already exists: $device_line"
  113. else
  114. # Create a backup of the file
  115. cp "$hotkeys_file" "$hotkeys_file.bak"
  116.  
  117. # Use sed to:
  118. # 1. Remove lines starting with Device
  119. # 2. Add the new Device line
  120. # 3. Replace General/Exit line with SELECT & START
  121. sed -i -e '/^Device = /d' \
  122. -e '/^General\/Exit = /d' \
  123. -e '$aDevice = evdev/0/'"$controller_name" \
  124. -e '$aGeneral/Exit = SELECT & START' \
  125. "$hotkeys_file"
  126.  
  127. echo "Updated Hotkeys.ini with the new Device line: $device_line"
  128. fi
  129. }
  130.  
  131.  
  132.  
  133. function handle_rom_and_gameid_ini {
  134. local ROM="$1"
  135. local ROM_DIR="/home/pi/RetroPie/roms/gc/"
  136.  
  137. # Check if the ROM file has an .m3u extension
  138. if [[ "${ROM##*.}" == "m3u" ]]; then
  139. if [[ -f "$ROM" ]]; then
  140. # Read the first non-comment, non-empty line of the .m3u file
  141. local M3U_ROM=$(grep -m 1 -v '^#' "$ROM" | head -n 1)
  142.  
  143. if [[ -n "$M3U_ROM" ]]; then
  144. # Append the ROM filename from the .m3u to the fixed directory path
  145. ROM="${ROM_DIR}${M3U_ROM}"
  146.  
  147. # Check if the constructed ROM path exists
  148. if [[ ! -f "$ROM" ]]; then
  149. echo "Error: The ROM file referenced in the .m3u file does not exist at $ROM."
  150. return 1
  151. fi
  152. else
  153. echo "Error: The .m3u file is empty or contains only comments."
  154. return 1
  155. fi
  156. else
  157. echo "Error: The .m3u file does not exist."
  158. return 1
  159. fi
  160. fi
  161.  
  162. # Run the Dolphin tool and capture the output using the (potentially updated) ROM file
  163. local OUTPUT=$($DOLPHIN_TOOL header -i "$ROM")
  164.  
  165. # Extract GAMEID
  166. local gameid=$(echo "$OUTPUT" | grep "Game ID:" | awk '{print $3}')
  167.  
  168. # Path to GAMEID.ini
  169. local gameid_ini="$gameid_ini_dir/$gameid.ini"
  170.  
  171. # Check if the js profile name is set
  172. if [[ -z "$js_profile_name" ]]; then
  173. echo "No controller profile found for js0 or any fallback. Skipping GAMEID.ini update."
  174. return
  175. fi
  176.  
  177. # Initialize flags
  178. local controls_section_exists=false
  179. local padprofile_exists=false
  180.  
  181. # Check if the gameid.ini file exists
  182. if [[ -f "$gameid_ini" ]]; then
  183. echo "Checking existing $gameid.ini file for [Controls] section..."
  184.  
  185. # Read the file line by line
  186. while IFS= read -r line; do
  187. # Check if the [Controls] section exists
  188. if [[ "$line" == "[Controls]" ]]; then
  189. controls_section_exists=true
  190. fi
  191.  
  192. # Check if PadProfile1 exists in the [Controls] section
  193. if [[ "$controls_section_exists" == true && "$line" == "PadProfile1 ="* ]]; then
  194. padprofile_exists=true
  195. break
  196. fi
  197. done < "$gameid_ini"
  198. fi
  199.  
  200. # If the [Controls] section exists and PadProfile1 exists, update it
  201. if [[ "$controls_section_exists" == true && "$padprofile_exists" == true ]]; then
  202. echo "Updating PadProfile1 in the [Controls] section..."
  203. sed -i "/\[Controls\]/,/^$/s/PadProfile1 =.*/PadProfile1 = $js_profile_name/" "$gameid_ini"
  204. elif [[ "$controls_section_exists" == true && "$padprofile_exists" == false ]]; then
  205. # If the [Controls] section exists but PadProfile1 doesn't, add PadProfile1
  206. echo "Adding PadProfile1 to the [Controls] section..."
  207. sed -i "/\[Controls\]/a PadProfile1 = $js_profile_name" "$gameid_ini"
  208. else
  209. # If the [Controls] section does not exist, add it at the end of the file
  210. echo "Adding [Controls] section and PadProfile1..."
  211. echo -e "\n[Controls]" >> "$gameid_ini"
  212. echo "PadProfile1 = $js_profile_name" >> "$gameid_ini"
  213. fi
  214.  
  215. echo "Updated $gameid_ini with PadProfile1 = $js_profile_name"
  216. }
  217.  
  218. # ----------------- Main script execution starts here
  219.  
  220. # Check for connected controllers and create necessary .ini files
  221. find_game_controllers
  222.  
  223. # Handle ROM file and update GAMEID.ini (pass the ROM file as an argument to the script)
  224. if [[ -n "$1" ]]; then
  225. handle_rom_and_gameid_ini "$1"
  226. else
  227. echo "No ROM file provided. Please provide a ROM file as an argument."
  228. fi
  229.  
  230. /opt/retropie/emulators/dolphin/bin/dolphin-emu -b -e "$1"
Advertisement
Add Comment
Please, Sign In to add comment