editor_icons_builders.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. Functions used to generate source files during build time
  3. All such functions are invoked in a subprocess on Windows to prevent build flakiness.
  4. """
  5. import os
  6. from io import StringIO
  7. from platform_methods import subprocess_main
  8. # See also `scene/theme/icons/default_theme_icons_builders.py`.
  9. def make_editor_icons_action(target, source, env):
  10. dst = target[0]
  11. svg_icons = source
  12. with StringIO() as icons_string, StringIO() as s:
  13. for f in svg_icons:
  14. fname = str(f)
  15. icons_string.write('\t"')
  16. with open(fname, "rb") as svgf:
  17. b = svgf.read(1)
  18. while len(b) == 1:
  19. icons_string.write("\\" + str(hex(ord(b)))[1:])
  20. b = svgf.read(1)
  21. icons_string.write('"')
  22. if fname != svg_icons[-1]:
  23. icons_string.write(",")
  24. icons_string.write("\n")
  25. s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  26. s.write("#ifndef _EDITOR_ICONS_H\n")
  27. s.write("#define _EDITOR_ICONS_H\n")
  28. s.write("static const int editor_icons_count = {};\n".format(len(svg_icons)))
  29. s.write("static const char *editor_icons_sources[] = {\n")
  30. s.write(icons_string.getvalue())
  31. s.write("};\n\n")
  32. s.write("static const char *editor_icons_names[] = {\n")
  33. # this is used to store the indices of thumbnail icons
  34. thumb_medium_indices = []
  35. thumb_big_indices = []
  36. index = 0
  37. for f in svg_icons:
  38. fname = str(f)
  39. # Trim the `.svg` extension from the string.
  40. icon_name = os.path.basename(fname)[:-4]
  41. # some special cases
  42. if icon_name.endswith("MediumThumb"): # don't know a better way to handle this
  43. thumb_medium_indices.append(str(index))
  44. if icon_name.endswith("BigThumb"): # don't know a better way to handle this
  45. thumb_big_indices.append(str(index))
  46. if icon_name.endswith("GodotFile"): # don't know a better way to handle this
  47. thumb_big_indices.append(str(index))
  48. s.write('\t"{0}"'.format(icon_name))
  49. if fname != svg_icons[-1]:
  50. s.write(",")
  51. s.write("\n")
  52. index += 1
  53. s.write("};\n")
  54. if thumb_medium_indices:
  55. s.write("\n\n")
  56. s.write("static const int editor_md_thumbs_count = {};\n".format(len(thumb_medium_indices)))
  57. s.write("static const int editor_md_thumbs_indices[] = {")
  58. s.write(", ".join(thumb_medium_indices))
  59. s.write("};\n")
  60. if thumb_big_indices:
  61. s.write("\n\n")
  62. s.write("static const int editor_bg_thumbs_count = {};\n".format(len(thumb_big_indices)))
  63. s.write("static const int editor_bg_thumbs_indices[] = {")
  64. s.write(", ".join(thumb_big_indices))
  65. s.write("};\n")
  66. s.write("#endif\n")
  67. with open(dst, "w", encoding="utf-8", newline="\n") as f:
  68. f.write(s.getvalue())
  69. if __name__ == "__main__":
  70. subprocess_main(globals())