sort_controllers.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. #
  3. # Script to sort the game controller database entries in SDL_gamecontroller.c
  4. import re
  5. filename = "SDL_gamecontrollerdb.h"
  6. input = open(filename)
  7. output = open(filename + ".new", "w")
  8. parsing_controllers = False
  9. controllers = []
  10. controller_guids = {}
  11. conditionals = []
  12. split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
  13. def find_element(prefix, bindings):
  14. i=0
  15. for element in bindings:
  16. if element.startswith(prefix):
  17. return i
  18. i=(i + 1)
  19. return -1
  20. def save_controller(line):
  21. global controllers
  22. match = split_pattern.match(line)
  23. entry = [ match.group(1), match.group(2), match.group(3) ]
  24. bindings = sorted(match.group(4).split(","))
  25. if (bindings[0] == ""):
  26. bindings.pop(0)
  27. pos=find_element("sdk", bindings)
  28. if pos >= 0:
  29. bindings.append(bindings.pop(pos))
  30. pos=find_element("hint:", bindings)
  31. if pos >= 0:
  32. bindings.append(bindings.pop(pos))
  33. entry.extend(",".join(bindings) + ",")
  34. entry.append(match.group(5))
  35. controllers.append(entry)
  36. if ',sdk' in line or ',hint:' in line:
  37. conditionals.append(entry[1])
  38. def write_controllers():
  39. global controllers
  40. global controller_guids
  41. # Check for duplicates
  42. for entry in controllers:
  43. if (entry[1] in controller_guids and entry[1] not in conditionals):
  44. current_name = entry[2]
  45. existing_name = controller_guids[entry[1]][2]
  46. print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
  47. if (not current_name.startswith("(DUPE)")):
  48. entry[2] = "(DUPE) " + current_name
  49. if (not existing_name.startswith("(DUPE)")):
  50. controller_guids[entry[1]][2] = "(DUPE) " + existing_name
  51. controller_guids[entry[1]] = entry
  52. for entry in sorted(controllers, key=lambda entry: entry[2]+"-"+entry[1]):
  53. line = "".join(entry) + "\n"
  54. line = line.replace("\t", " ")
  55. if not line.endswith(",\n") and not line.endswith("*/\n") and not line.endswith(",\r\n") and not line.endswith("*/\r\n"):
  56. print("Warning: '%s' is missing a comma at the end of the line" % (line))
  57. output.write(line)
  58. controllers = []
  59. controller_guids = {}
  60. for line in input:
  61. if (parsing_controllers):
  62. if (line.startswith("{")):
  63. output.write(line)
  64. elif (line.startswith(" NULL")):
  65. parsing_controllers = False
  66. write_controllers()
  67. output.write(line)
  68. elif (line.startswith("#if")):
  69. print("Parsing " + line.strip())
  70. output.write(line)
  71. elif (line.startswith("#endif")):
  72. write_controllers()
  73. output.write(line)
  74. else:
  75. save_controller(line)
  76. else:
  77. if (line.startswith("static const char *s_ControllerMappings")):
  78. parsing_controllers = True
  79. output.write(line)
  80. output.close()
  81. print("Finished writing %s.new" % filename)