input_builders.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Functions used to generate source files during build time"""
  2. from collections import OrderedDict
  3. def make_default_controller_mappings(target, source, env):
  4. dst = str(target[0])
  5. with open(dst, "w", encoding="utf-8", newline="\n") as g:
  6. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  7. g.write('#include "core/typedefs.h"\n')
  8. g.write('#include "core/input/default_controller_mappings.h"\n')
  9. # ensure mappings have a consistent order
  10. platform_mappings: dict = OrderedDict()
  11. for src_path in source:
  12. with open(str(src_path), "r", encoding="utf-8") as f:
  13. # read mapping file and skip header
  14. mapping_file_lines = f.readlines()[2:]
  15. current_platform = None
  16. for line in mapping_file_lines:
  17. if not line:
  18. continue
  19. line = line.strip()
  20. if len(line) == 0:
  21. continue
  22. if line[0] == "#":
  23. current_platform = line[1:].strip()
  24. if current_platform not in platform_mappings:
  25. platform_mappings[current_platform] = {}
  26. elif current_platform:
  27. line_parts = line.split(",")
  28. guid = line_parts[0]
  29. if guid in platform_mappings[current_platform]:
  30. g.write(
  31. "// WARNING: DATABASE {} OVERWROTE PRIOR MAPPING: {} {}\n".format(
  32. src_path, current_platform, platform_mappings[current_platform][guid]
  33. )
  34. )
  35. platform_mappings[current_platform][guid] = line
  36. platform_variables = {
  37. "Linux": "#ifdef LINUXBSD_ENABLED",
  38. "Windows": "#ifdef WINDOWS_ENABLED",
  39. "Mac OS X": "#ifdef MACOS_ENABLED",
  40. "Android": "#ifdef ANDROID_ENABLED",
  41. "iOS": "#ifdef IOS_ENABLED",
  42. "Web": "#ifdef WEB_ENABLED",
  43. }
  44. g.write("const char* DefaultControllerMappings::mappings[] = {\n")
  45. for platform, mappings in platform_mappings.items():
  46. variable = platform_variables[platform]
  47. g.write("{}\n".format(variable))
  48. for mapping in mappings.values():
  49. g.write('\t"{}",\n'.format(mapping))
  50. g.write("#endif\n")
  51. g.write("\tnullptr\n};\n")