Skip to content

Instantly share code, notes, and snippets.

@offalynne
Last active March 29, 2024 16:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save offalynne/e0a887556e8120390fc86c944e6861bf to your computer and use it in GitHub Desktop.
Save offalynne/e0a887556e8120390fc86c944e6861bf to your computer and use it in GitHub Desktop.
GMS2 Gamepad DB Composer
#!/usr/bin/python
# GMS2 Gamepad DB Composer, @offalynne, 2021
# processes the SDL2 Community Gamepad Mapping DB for GMS2 compatibility
# run script and place the resulting gamecontrollerdb.txt in datafiles
import re
import os
import sys
import urllib.request
print("Running revision 18")
print("Warning !! As of GMS2022.11.1.75 GUID format for Windows has changed")
print("Loading remote source")
# get source db
source_url = "https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt"
source_content = None
try:
source_content = urllib.request.urlopen(source_url).read().decode('utf-8')
except Exception as e:
print(e);
exit("Failed to fetch remote source")
print("Rebuilding DB")
# clean up unsupported features
content = source_content
# move hat-on-stick to dpad
content = content.replace("+leftx:h0.2,+lefty:h0.4,-leftx:h0.8,-lefty:h0.1", "dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1")
# move axis-on-dpad to stick
content = re.compile(r"dpdown\:\+a(.+?)\,dpleft\:\-a(.+?)\,dpright\:\+a\2\,dpup\:\-a\1").sub("leftx:a\\2,lefty:a\\1", content)
# remove negative axis triggers
content = re.compile(r"((left|right)trigger:-a[0-999],)").sub("", content)
# remove remaining maps with axis limiters
content = re.compile(r"([0-9a-f]{32},.*,[-|+].*\n)").sub("", content)
# remove inverters
content = content.replace("~", "")
# remove unsupported fields
content = re.compile(r"(\,(guide|touchpad|misc1|paddle[1-4])\:(.+?)\,)").sub(",", content)
# swap back to guide (gp_select)
content = content.replace(",back:", ",guide:")
# downgrade GUID !! Pre GMS2022.11.1.75 only !!
windows_maps = ""
re_find = re.compile(r"[03|05][0-9a-f]{6}([0-9a-f]{4})[0-9a-f]{4}([0-9a-f]{4})[0-9a-f]{12}(,.*platform:Windows,)")
for m in re.finditer(re_find, content):
windows_maps += m.group(1) + m.group(2) + "000000000000504944564944" + m.group(3) + "\n"
# remove dpad button keymaps
android_maps = ""
re_find = re.compile(r"(.*)(\,dpdown:b12\,dpleft:b13\,dpright:b14\,dpup:b11\,)(.*\,platform:Android,)")
for m in re.finditer(re_find, content):
android_maps += m.group(1) + ",dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1," + m.group(3) + "\n"
# omit unsupported platforms
seek_after = "# Mac OS X"
seek_before = "\n\n# Android"
seek_start = content.index(seek_after) + len(seek_after)
seek_end = content.index(seek_before, seek_start)
content = content[seek_start:seek_end]
# rebuild db
rebuild_content = "# Game Controller DB for SDL\n"
rebuild_content += "# Source: https://github.com/gabomdq/SDL_GameControllerDB\n\n# Windows\n"
rebuild_content += windows_maps + "\n# Mac OS X" + content + "\n\n# Android\n" + android_maps
content = rebuild_content
print("Writing DB file")
# write file
db_filename = "gamecontrollerdb.txt"
if os.path.isfile(db_filename):
os.remove(db_filename)
file_handle = open(db_filename, "a")
file_handle.write(content)
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment