make_interface_dumper.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import zlib
  2. def run(target, source, env):
  3. src = source[0]
  4. dst = target[0]
  5. f = open(src, "rb")
  6. g = open(dst, "w", encoding="utf-8")
  7. buf = f.read()
  8. decomp_size = len(buf)
  9. # Use maximum zlib compression level to further reduce file size
  10. # (at the cost of initial build times).
  11. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  12. g.write(
  13. """/* THIS FILE IS GENERATED DO NOT EDIT */
  14. #ifndef GDEXTENSION_INTERFACE_DUMP_H
  15. #define GDEXTENSION_INTERFACE_DUMP_H
  16. #ifdef TOOLS_ENABLED
  17. #include "core/io/compression.h"
  18. #include "core/io/file_access.h"
  19. #include "core/string/ustring.h"
  20. """
  21. )
  22. g.write("static const int _gdextension_interface_data_compressed_size = " + str(len(buf)) + ";\n")
  23. g.write("static const int _gdextension_interface_data_uncompressed_size = " + str(decomp_size) + ";\n")
  24. g.write("static const unsigned char _gdextension_interface_data_compressed[] = {\n")
  25. for i in range(len(buf)):
  26. g.write("\t" + str(buf[i]) + ",\n")
  27. g.write("};\n")
  28. g.write(
  29. """
  30. class GDExtensionInterfaceDump {
  31. public:
  32. static void generate_gdextension_interface_file(const String &p_path) {
  33. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  34. ERR_FAIL_COND_MSG(fa.is_null(), vformat("Cannot open file '%s' for writing.", p_path));
  35. Vector<uint8_t> data;
  36. data.resize(_gdextension_interface_data_uncompressed_size);
  37. int ret = Compression::decompress(data.ptrw(), _gdextension_interface_data_uncompressed_size, _gdextension_interface_data_compressed, _gdextension_interface_data_compressed_size, Compression::MODE_DEFLATE);
  38. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  39. fa->store_buffer(data.ptr(), data.size());
  40. };
  41. };
  42. #endif // TOOLS_ENABLED
  43. #endif // GDEXTENSION_INTERFACE_DUMP_H
  44. """
  45. )
  46. g.close()
  47. f.close()
  48. if __name__ == "__main__":
  49. from platform_methods import subprocess_main
  50. subprocess_main(globals())