doc_source_generator.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import zlib
  5. def generate_doc_source(dst, source):
  6. g = open(dst, "w", encoding="utf-8")
  7. buf = ""
  8. docbegin = ""
  9. docend = ""
  10. for src in source:
  11. src_path = str(src)
  12. if not src_path.endswith(".xml"):
  13. continue
  14. with open(src_path, "r", encoding="utf-8") as f:
  15. content = f.read()
  16. buf += content
  17. buf = (docbegin + buf + docend).encode("utf-8")
  18. decomp_size = len(buf)
  19. # Use maximum zlib compression level to further reduce file size
  20. # (at the cost of initial build times).
  21. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  22. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  23. g.write("\n")
  24. g.write("#include <godot_cpp/godot.hpp>\n")
  25. g.write("\n")
  26. g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
  27. g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
  28. g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
  29. g.write("static const unsigned char _doc_data_compressed[] = {\n")
  30. for i in range(len(buf)):
  31. g.write("\t" + str(buf[i]) + ",\n")
  32. g.write("};\n")
  33. g.write("\n")
  34. g.write(
  35. "static godot::internal::DocDataRegistration _doc_data_registration(_doc_data_hash, _doc_data_uncompressed_size, _doc_data_compressed_size, _doc_data_compressed);\n"
  36. )
  37. g.write("\n")
  38. g.close()
  39. def scons_generate_doc_source(target, source, env):
  40. generate_doc_source(str(target[0]), source)
  41. def generate_doc_source_from_directory(target, directory):
  42. generate_doc_source(target, glob.glob(os.path.join(directory, "*.xml")))