2022-06-15 21:10:22 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-06-21 15:33:46 +00:00
|
|
|
#
|
|
|
|
# Script to sort the game controller database entries in SDL_gamecontroller.c
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
filename = "SDL_gamecontrollerdb.h"
|
|
|
|
input = open(filename)
|
2022-06-27 05:00:43 +00:00
|
|
|
output = open(f"{filename}.new", "w")
|
2015-06-21 15:33:46 +00:00
|
|
|
parsing_controllers = False
|
|
|
|
controllers = []
|
|
|
|
controller_guids = {}
|
2020-03-13 02:47:28 +00:00
|
|
|
conditionals = []
|
2015-06-21 15:33:46 +00:00
|
|
|
split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
|
2022-08-28 01:55:55 +00:00
|
|
|
guid_crc_pattern = re.compile(r'^([0-9a-zA-Z]{4})([0-9a-zA-Z]{4})([0-9a-zA-Z]{24},)$')
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2020-03-13 02:47:28 +00:00
|
|
|
def find_element(prefix, bindings):
|
|
|
|
i=0
|
|
|
|
for element in bindings:
|
|
|
|
if element.startswith(prefix):
|
|
|
|
return i
|
|
|
|
i=(i + 1)
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
def save_controller(line):
|
|
|
|
global controllers
|
|
|
|
match = split_pattern.match(line)
|
|
|
|
entry = [ match.group(1), match.group(2), match.group(3) ]
|
|
|
|
bindings = sorted(match.group(4).split(","))
|
|
|
|
if (bindings[0] == ""):
|
|
|
|
bindings.pop(0)
|
2020-03-13 02:47:28 +00:00
|
|
|
|
2022-08-28 01:55:55 +00:00
|
|
|
crc = ""
|
|
|
|
pos = find_element("crc:", bindings)
|
|
|
|
if pos >= 0:
|
|
|
|
crc = bindings[pos] + ","
|
|
|
|
bindings.pop(pos)
|
|
|
|
|
|
|
|
# Look for CRC embedded in the GUID and convert to crc element
|
|
|
|
crc_match = guid_crc_pattern.match(entry[1])
|
|
|
|
if crc_match and crc_match.group(2) != '0000':
|
|
|
|
print("Extracting CRC from GUID of " + entry[2])
|
|
|
|
entry[1] = crc_match.group(1) + '0000' + crc_match.group(3)
|
|
|
|
crc = "crc:" + crc_match.group(2) + ","
|
|
|
|
|
|
|
|
pos = find_element("sdk", bindings)
|
2020-03-13 02:47:28 +00:00
|
|
|
if pos >= 0:
|
|
|
|
bindings.append(bindings.pop(pos))
|
|
|
|
|
2022-08-28 01:55:55 +00:00
|
|
|
pos = find_element("hint:", bindings)
|
2020-03-13 02:47:28 +00:00
|
|
|
if pos >= 0:
|
|
|
|
bindings.append(bindings.pop(pos))
|
|
|
|
|
2022-08-28 01:55:55 +00:00
|
|
|
entry.extend(crc)
|
2019-01-21 19:49:08 +00:00
|
|
|
entry.extend(",".join(bindings) + ",")
|
2015-06-21 15:33:46 +00:00
|
|
|
entry.append(match.group(5))
|
|
|
|
controllers.append(entry)
|
|
|
|
|
2022-08-28 01:55:55 +00:00
|
|
|
entry_id = entry[1] + entry[3]
|
2020-03-13 02:47:28 +00:00
|
|
|
if ',sdk' in line or ',hint:' in line:
|
2022-08-28 01:55:55 +00:00
|
|
|
conditionals.append(entry_id)
|
2020-02-17 22:15:47 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
def write_controllers():
|
|
|
|
global controllers
|
|
|
|
global controller_guids
|
2018-06-12 07:18:10 +00:00
|
|
|
# Check for duplicates
|
|
|
|
for entry in controllers:
|
2022-08-28 01:55:55 +00:00
|
|
|
entry_id = entry[1] + entry[3]
|
|
|
|
if (entry_id in controller_guids and entry_id not in conditionals):
|
2018-06-12 07:18:10 +00:00
|
|
|
current_name = entry[2]
|
2022-08-28 01:55:55 +00:00
|
|
|
existing_name = controller_guids[entry_id][2]
|
2018-06-12 07:18:10 +00:00
|
|
|
print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
|
|
|
|
|
|
|
|
if (not current_name.startswith("(DUPE)")):
|
2022-06-27 05:00:43 +00:00
|
|
|
entry[2] = f"(DUPE) {current_name}"
|
2018-06-12 07:18:10 +00:00
|
|
|
|
|
|
|
if (not existing_name.startswith("(DUPE)")):
|
2022-08-28 01:55:55 +00:00
|
|
|
controller_guids[entry_id][2] = f"(DUPE) {existing_name}"
|
2018-06-12 07:18:10 +00:00
|
|
|
|
2022-08-28 01:55:55 +00:00
|
|
|
controller_guids[entry_id] = entry
|
2018-06-12 07:18:10 +00:00
|
|
|
|
2022-06-27 05:00:43 +00:00
|
|
|
for entry in sorted(controllers, key=lambda entry: f"{entry[2]}-{entry[1]}"):
|
2015-06-21 15:33:46 +00:00
|
|
|
line = "".join(entry) + "\n"
|
2017-08-31 06:02:39 +00:00
|
|
|
line = line.replace("\t", " ")
|
2021-06-25 01:09:04 +00:00
|
|
|
if not line.endswith(",\n") and not line.endswith("*/\n") and not line.endswith(",\r\n") and not line.endswith("*/\r\n"):
|
2015-06-21 15:33:46 +00:00
|
|
|
print("Warning: '%s' is missing a comma at the end of the line" % (line))
|
|
|
|
output.write(line)
|
2018-06-12 07:18:10 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
controllers = []
|
|
|
|
controller_guids = {}
|
|
|
|
|
|
|
|
for line in input:
|
2022-06-27 05:00:43 +00:00
|
|
|
if parsing_controllers:
|
2015-06-21 15:33:46 +00:00
|
|
|
if (line.startswith("{")):
|
|
|
|
output.write(line)
|
|
|
|
elif (line.startswith(" NULL")):
|
|
|
|
parsing_controllers = False
|
|
|
|
write_controllers()
|
|
|
|
output.write(line)
|
|
|
|
elif (line.startswith("#if")):
|
2022-06-27 05:00:43 +00:00
|
|
|
print(f"Parsing {line.strip()}")
|
2015-06-21 15:33:46 +00:00
|
|
|
output.write(line)
|
|
|
|
elif (line.startswith("#endif")):
|
|
|
|
write_controllers()
|
|
|
|
output.write(line)
|
|
|
|
else:
|
|
|
|
save_controller(line)
|
|
|
|
else:
|
|
|
|
if (line.startswith("static const char *s_ControllerMappings")):
|
|
|
|
parsing_controllers = True
|
|
|
|
|
|
|
|
output.write(line)
|
|
|
|
|
|
|
|
output.close()
|
2022-06-27 05:00:43 +00:00
|
|
|
print(f"Finished writing {filename}.new")
|