Skip to content

Latest commit

 

History

History
executable file
·
100 lines (81 loc) · 2.93 KB

sort_controllers.py

File metadata and controls

executable file
·
100 lines (81 loc) · 2.93 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python
#
# Script to sort the game controller database entries in SDL_gamecontroller.c
import re
filename = "SDL_gamecontrollerdb.h"
input = open(filename)
output = open(filename + ".new", "w")
parsing_controllers = False
controllers = []
controller_guids = {}
Mar 13, 2020
Mar 13, 2020
14
conditionals = []
15
16
split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
Mar 13, 2020
Mar 13, 2020
17
18
19
20
21
22
23
24
25
def find_element(prefix, bindings):
i=0
for element in bindings:
if element.startswith(prefix):
return i
i=(i + 1)
return -1
26
27
28
29
30
31
32
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)
Mar 13, 2020
Mar 13, 2020
33
34
35
36
37
38
39
40
41
pos=find_element("sdk", bindings)
if pos >= 0:
bindings.append(bindings.pop(pos))
pos=find_element("hint:", bindings)
if pos >= 0:
bindings.append(bindings.pop(pos))
Jan 21, 2019
Jan 21, 2019
42
entry.extend(",".join(bindings) + ",")
43
44
45
entry.append(match.group(5))
controllers.append(entry)
Mar 13, 2020
Mar 13, 2020
46
47
if ',sdk' in line or ',hint:' in line:
conditionals.append(entry[1])
Feb 17, 2020
Feb 17, 2020
48
49
50
51
def write_controllers():
global controllers
global controller_guids
Jun 12, 2018
Jun 12, 2018
52
53
# Check for duplicates
for entry in controllers:
Mar 13, 2020
Mar 13, 2020
54
if (entry[1] in controller_guids and entry[1] not in conditionals):
Jun 12, 2018
Jun 12, 2018
55
56
57
58
59
60
61
62
63
64
65
66
67
current_name = entry[2]
existing_name = controller_guids[entry[1]][2]
print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
if (not current_name.startswith("(DUPE)")):
entry[2] = "(DUPE) " + current_name
if (not existing_name.startswith("(DUPE)")):
controller_guids[entry[1]][2] = "(DUPE) " + existing_name
controller_guids[entry[1]] = entry
for entry in sorted(controllers, key=lambda entry: entry[2]+"-"+entry[1]):
68
line = "".join(entry) + "\n"
Aug 31, 2017
Aug 31, 2017
69
line = line.replace("\t", " ")
70
71
72
if not line.endswith(",\n") and not line.endswith("*/\n"):
print("Warning: '%s' is missing a comma at the end of the line" % (line))
output.write(line)
Jun 12, 2018
Jun 12, 2018
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
controllers = []
controller_guids = {}
for line in input:
if (parsing_controllers):
if (line.startswith("{")):
output.write(line)
elif (line.startswith(" NULL")):
parsing_controllers = False
write_controllers()
output.write(line)
elif (line.startswith("#if")):
print("Parsing " + line.strip())
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()
print("Finished writing %s.new" % filename)