Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thatcosmonaut/458161d448928662b422b89163f772af to your computer and use it in GitHub Desktop.
Save thatcosmonaut/458161d448928662b422b89163f772af to your computer and use it in GitHub Desktop.
GMS2 Gamepad DB Composer
#!/usr/bin/python
# @offalynne, 2021
# Modified for python 3 by @thatcosmonaut
import re
import os
import sys
import urllib.request
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")
# clean up unsupported features (axis limiters, inverters)
print("Rebuilding...")
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("~", "")
# downgrade windows GUID
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"
# omit unsupported maps
seek_after = "# Mac OS X"
seek_before = "\n\n# iOS"
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 = rebuild_content + 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