make_cs_compressed_header.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. def generate_header(src, dst, version_dst):
  2. from compat import byte_to_str
  3. with open(dst, 'w') as header:
  4. header.write('/* THIS FILE IS GENERATED DO NOT EDIT */\n')
  5. header.write('#ifndef CS_COMPRESSED_H\n')
  6. header.write('#define CS_COMPRESSED_H\n\n')
  7. header.write('#ifdef TOOLS_ENABLED\n\n')
  8. header.write('#include "core/map.h"\n')
  9. header.write('#include "core/ustring.h"\n')
  10. inserted_files = ''
  11. import os
  12. latest_mtime = 0
  13. cs_file_count = 0
  14. for root, _, files in os.walk(src):
  15. files = [f for f in files if f.endswith('.cs')]
  16. for file in files:
  17. cs_file_count += 1
  18. filepath = os.path.join(root, file)
  19. filepath_src_rel = os.path.relpath(filepath, src)
  20. mtime = os.path.getmtime(filepath)
  21. latest_mtime = mtime if mtime > latest_mtime else latest_mtime
  22. with open(filepath, 'rb') as f:
  23. buf = f.read()
  24. decompr_size = len(buf)
  25. import zlib
  26. buf = zlib.compress(buf)
  27. compr_size = len(buf)
  28. name = str(cs_file_count)
  29. header.write('\n')
  30. header.write('// ' + filepath_src_rel + '\n')
  31. header.write('static const int _cs_' + name + '_compressed_size = ' + str(compr_size) + ';\n')
  32. header.write('static const int _cs_' + name + '_uncompressed_size = ' + str(decompr_size) + ';\n')
  33. header.write('static const unsigned char _cs_' + name + '_compressed[] = { ')
  34. for i, buf_idx in enumerate(range(compr_size)):
  35. if i > 0:
  36. header.write(', ')
  37. header.write(byte_to_str(buf[buf_idx]))
  38. header.write(' };\n')
  39. inserted_files += '\tr_files.insert("' + filepath_src_rel.replace('\\', '\\\\') + '", ' \
  40. 'GodotCsCompressedFile(_cs_' + name + '_compressed_size, ' \
  41. '_cs_' + name + '_uncompressed_size, ' \
  42. '_cs_' + name + '_compressed));\n'
  43. header.write('\nstruct GodotCsCompressedFile\n' '{\n'
  44. '\tint compressed_size;\n' '\tint uncompressed_size;\n' '\tconst unsigned char* data;\n'
  45. '\n\tGodotCsCompressedFile(int p_comp_size, int p_uncomp_size, const unsigned char* p_data)\n'
  46. '\t{\n' '\t\tcompressed_size = p_comp_size;\n' '\t\tuncompressed_size = p_uncomp_size;\n'
  47. '\t\tdata = p_data;\n' '\t}\n' '\n\tGodotCsCompressedFile() {}\n' '};\n'
  48. '\nvoid get_compressed_files(Map<String, GodotCsCompressedFile>& r_files)\n' '{\n' + inserted_files + '}\n'
  49. )
  50. header.write('\n#endif // TOOLS_ENABLED\n')
  51. header.write('\n#endif // CS_COMPRESSED_H\n')
  52. glue_version = int(latest_mtime) # The latest modified time will do for now
  53. with open(version_dst, 'w') as version_header:
  54. version_header.write('/* THIS FILE IS GENERATED DO NOT EDIT */\n')
  55. version_header.write('#ifndef CS_GLUE_VERSION_H\n')
  56. version_header.write('#define CS_GLUE_VERSION_H\n\n')
  57. version_header.write('#define CS_GLUE_VERSION UINT32_C(' + str(glue_version) + ')\n')
  58. version_header.write('\n#endif // CS_GLUE_VERSION_H\n')