make_interface_dumper.py 2.0 KB

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