editor_builders.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. from compat import encode_utf8, byte_to_str, open_utf8, escape_string
  8. def make_doc_header(target, source, env):
  9. dst = target[0]
  10. g = open_utf8(dst, "w")
  11. buf = ""
  12. docbegin = ""
  13. docend = ""
  14. for src in source:
  15. if not src.endswith(".xml"):
  16. continue
  17. with open_utf8(src, "r") as f:
  18. content = f.read()
  19. buf += content
  20. buf = encode_utf8(docbegin + buf + docend)
  21. decomp_size = len(buf)
  22. import zlib
  23. buf = zlib.compress(buf)
  24. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  25. g.write("#ifndef _DOC_DATA_RAW_H\n")
  26. g.write("#define _DOC_DATA_RAW_H\n")
  27. g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
  28. g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
  29. g.write("static const unsigned char _doc_data_compressed[] = {\n")
  30. for i in range(len(buf)):
  31. g.write("\t" + byte_to_str(buf[i]) + ",\n")
  32. g.write("};\n")
  33. g.write("#endif")
  34. g.close()
  35. def make_fonts_header(target, source, env):
  36. dst = target[0]
  37. g = open_utf8(dst, "w")
  38. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  39. g.write("#ifndef _EDITOR_FONTS_H\n")
  40. g.write("#define _EDITOR_FONTS_H\n")
  41. # saving uncompressed, since freetype will reference from memory pointer
  42. xl_names = []
  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 i in range(len(buf)):
  50. g.write("\t" + byte_to_str(buf[i]) + ",\n")
  51. g.write("};\n")
  52. g.write("#endif")
  53. g.close()
  54. def make_translations_header(target, source, env):
  55. dst = target[0]
  56. g = open_utf8(dst, "w")
  57. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  58. g.write("#ifndef _EDITOR_TRANSLATIONS_H\n")
  59. g.write("#define _EDITOR_TRANSLATIONS_H\n")
  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. buf = zlib.compress(buf)
  69. name = os.path.splitext(os.path.basename(sorted_paths[i]))[0]
  70. g.write("static const unsigned char _translation_" + name + "_compressed[] = {\n")
  71. for i in range(len(buf)):
  72. g.write("\t" + byte_to_str(buf[i]) + ",\n")
  73. g.write("};\n")
  74. xl_names.append([name, len(buf), str(decomp_size)])
  75. g.write("struct EditorTranslationList {\n")
  76. g.write("\tconst char* lang;\n")
  77. g.write("\tint comp_size;\n")
  78. g.write("\tint uncomp_size;\n")
  79. g.write("\tconst unsigned char* data;\n")
  80. g.write("};\n\n")
  81. g.write("static EditorTranslationList _editor_translations[] = {\n")
  82. for x in xl_names:
  83. g.write("\t{ \"" + x[0] + "\", " + str(x[1]) + ", " + str(x[2]) + ", _translation_" + x[0] + "_compressed},\n")
  84. g.write("\t{NULL, 0, 0, NULL}\n")
  85. g.write("};\n")
  86. g.write("#endif")
  87. g.close()
  88. if __name__ == '__main__':
  89. subprocess_main(globals())