editor_builders.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """Functions used to generate source files during build time
  2. All such functions are invoked in a subprocess on Windows to prevent build flakiness.
  3. """
  4. import os
  5. import os.path
  6. from platform_methods import subprocess_main
  7. def make_doc_header(target, source, env):
  8. dst = target[0]
  9. g = open(dst, "w", encoding="utf-8")
  10. buf = ""
  11. docbegin = ""
  12. docend = ""
  13. for src in source:
  14. if not src.endswith(".xml"):
  15. continue
  16. with open(src, "r", encoding="utf-8") as f:
  17. content = f.read()
  18. buf += content
  19. buf = (docbegin + buf + docend).encode("utf-8")
  20. decomp_size = len(buf)
  21. import zlib
  22. # Use maximum zlib compression level to further reduce file size
  23. # (at the cost of initial build times).
  24. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  25. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  26. g.write("#ifndef _DOC_DATA_RAW_H\n")
  27. g.write("#define _DOC_DATA_RAW_H\n")
  28. g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
  29. g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
  30. g.write("static const unsigned char _doc_data_compressed[] = {\n")
  31. for i in range(len(buf)):
  32. g.write("\t" + str(buf[i]) + ",\n")
  33. g.write("};\n")
  34. g.write("#endif")
  35. g.close()
  36. def make_fonts_header(target, source, env):
  37. dst = target[0]
  38. g = open(dst, "w", encoding="utf-8")
  39. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  40. g.write("#ifndef _EDITOR_FONTS_H\n")
  41. g.write("#define _EDITOR_FONTS_H\n")
  42. # Saving uncompressed, since FreeType will reference from memory pointer.
  43. for i in range(len(source)):
  44. with open(source[i], "rb") as f:
  45. buf = f.read()
  46. name = os.path.splitext(os.path.basename(source[i]))[0]
  47. g.write("static const int _font_" + name + "_size = " + str(len(buf)) + ";\n")
  48. g.write("static const unsigned char _font_" + name + "[] = {\n")
  49. for j in range(len(buf)):
  50. g.write("\t" + str(buf[j]) + ",\n")
  51. g.write("};\n")
  52. g.write("#endif")
  53. g.close()
  54. def make_translations_header(target, source, env, category):
  55. dst = target[0]
  56. g = open(dst, "w", encoding="utf-8")
  57. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  58. g.write("#ifndef _{}_TRANSLATIONS_H\n".format(category.upper()))
  59. g.write("#define _{}_TRANSLATIONS_H\n".format(category.upper()))
  60. import zlib
  61. import os.path
  62. sorted_paths = sorted(source, key=lambda path: os.path.splitext(os.path.basename(path))[0])
  63. xl_names = []
  64. for i in range(len(sorted_paths)):
  65. with open(sorted_paths[i], "rb") as f:
  66. buf = f.read()
  67. decomp_size = len(buf)
  68. # Use maximum zlib compression level to further reduce file size
  69. # (at the cost of initial build times).
  70. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  71. name = os.path.splitext(os.path.basename(sorted_paths[i]))[0]
  72. g.write("static const unsigned char _{}_translation_{}_compressed[] = {{\n".format(category, name))
  73. for j in range(len(buf)):
  74. g.write("\t" + str(buf[j]) + ",\n")
  75. g.write("};\n")
  76. xl_names.append([name, len(buf), str(decomp_size)])
  77. g.write("struct {}TranslationList {{\n".format(category.capitalize()))
  78. g.write("\tconst char* lang;\n")
  79. g.write("\tint comp_size;\n")
  80. g.write("\tint uncomp_size;\n")
  81. g.write("\tconst unsigned char* data;\n")
  82. g.write("};\n\n")
  83. g.write("static {}TranslationList _{}_translations[] = {{\n".format(category.capitalize(), category))
  84. for x in xl_names:
  85. g.write(
  86. '\t{{ "{}", {}, {}, _{}_translation_{}_compressed }},\n'.format(x[0], str(x[1]), str(x[2]), category, x[0])
  87. )
  88. g.write("\t{nullptr, 0, 0, nullptr}\n")
  89. g.write("};\n")
  90. g.write("#endif")
  91. g.close()
  92. def make_editor_translations_header(target, source, env):
  93. make_translations_header(target, source, env, "editor")
  94. def make_doc_translations_header(target, source, env):
  95. make_translations_header(target, source, env, "doc")
  96. if __name__ == "__main__":
  97. subprocess_main(globals())