make_interface_dumper.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import methods
  2. def run(target, source, env):
  3. buffer = methods.get_buffer(str(source[0]))
  4. decomp_size = len(buffer)
  5. buffer = methods.compress_buffer(buffer)
  6. with methods.generated_wrapper(str(target[0])) as file:
  7. file.write(f"""\
  8. #ifdef TOOLS_ENABLED
  9. #include "core/io/compression.h"
  10. #include "core/io/file_access.h"
  11. #include "core/string/ustring.h"
  12. inline constexpr int _gdextension_interface_data_compressed_size = {len(buffer)};
  13. inline constexpr int _gdextension_interface_data_uncompressed_size = {decomp_size};
  14. inline constexpr unsigned char _gdextension_interface_data_compressed[] = {{
  15. {methods.format_buffer(buffer, 1)}
  16. }};
  17. class GDExtensionInterfaceDump {{
  18. public:
  19. static void generate_gdextension_interface_file(const String &p_path) {{
  20. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  21. ERR_FAIL_COND_MSG(fa.is_null(), vformat("Cannot open file '%s' for writing.", p_path));
  22. Vector<uint8_t> data;
  23. data.resize(_gdextension_interface_data_uncompressed_size);
  24. int ret = Compression::decompress(data.ptrw(), _gdextension_interface_data_uncompressed_size, _gdextension_interface_data_compressed, _gdextension_interface_data_compressed_size, Compression::MODE_DEFLATE);
  25. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  26. fa->store_buffer(data.ptr(), data.size());
  27. }};
  28. }};
  29. #endif // TOOLS_ENABLED
  30. """)