assets.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env python3
  2. from flipper.app import App
  3. from flipper.assets.icon import file2image
  4. import os
  5. ICONS_SUPPORTED_FORMATS = ["png"]
  6. ICONS_TEMPLATE_H_HEADER = """#pragma once
  7. #include <gui/icon.h>
  8. """
  9. ICONS_TEMPLATE_H_ICON_NAME = "extern const Icon {name};\n"
  10. ICONS_TEMPLATE_C_HEADER = """#include \"assets_icons.h\"
  11. #include <gui/icon_i.h>
  12. """
  13. ICONS_TEMPLATE_C_FRAME = "const uint8_t {name}[] = {data};\n"
  14. ICONS_TEMPLATE_C_DATA = "const uint8_t* const {name}[] = {data};\n"
  15. ICONS_TEMPLATE_C_ICONS = "const Icon {name} = {{.width={width},.height={height},.frame_count={frame_count},.frame_rate={frame_rate},.frames=_{name}}};\n"
  16. class Main(App):
  17. def init(self):
  18. # command args
  19. self.subparsers = self.parser.add_subparsers(help="sub-command help")
  20. self.parser_icons = self.subparsers.add_parser(
  21. "icons", help="Process icons and build icon registry"
  22. )
  23. self.parser_icons.add_argument("input_directory", help="Source directory")
  24. self.parser_icons.add_argument("output_directory", help="Output directory")
  25. self.parser_icons.set_defaults(func=self.icons)
  26. self.parser_manifest = self.subparsers.add_parser(
  27. "manifest", help="Create directory Manifest"
  28. )
  29. self.parser_manifest.add_argument("local_path", help="local_path")
  30. self.parser_manifest.set_defaults(func=self.manifest)
  31. self.parser_copro = self.subparsers.add_parser(
  32. "copro", help="Gather copro binaries for packaging"
  33. )
  34. self.parser_copro.add_argument("cube_dir", help="Path to Cube folder")
  35. self.parser_copro.add_argument("output_dir", help="Path to output folder")
  36. self.parser_copro.add_argument("mcu", help="MCU series as in copro folder")
  37. self.parser_copro.add_argument(
  38. "--cube_ver", dest="cube_ver", help="Cube version", required=True
  39. )
  40. self.parser_copro.add_argument(
  41. "--stack_type", dest="stack_type", help="Stack type", required=True
  42. )
  43. self.parser_copro.add_argument(
  44. "--stack_file",
  45. dest="stack_file",
  46. help="Stack file name in copro folder",
  47. required=True,
  48. )
  49. self.parser_copro.add_argument(
  50. "--stack_addr",
  51. dest="stack_addr",
  52. help="Stack flash address, as per release_notes",
  53. type=lambda x: int(x, 16),
  54. default=0,
  55. required=False,
  56. )
  57. self.parser_copro.set_defaults(func=self.copro)
  58. self.parser_dolphin = self.subparsers.add_parser(
  59. "dolphin", help="Assemble dolphin resources"
  60. )
  61. self.parser_dolphin.add_argument(
  62. "-s",
  63. "--symbol-name",
  64. help="Symbol and file name in dolphin output directory",
  65. default=None,
  66. )
  67. self.parser_dolphin.add_argument(
  68. "input_directory", help="Dolphin source directory"
  69. )
  70. self.parser_dolphin.add_argument(
  71. "output_directory", help="Dolphin output directory"
  72. )
  73. self.parser_dolphin.set_defaults(func=self.dolphin)
  74. def _icon2header(self, file):
  75. image = file2image(file)
  76. return image.width, image.height, image.data_as_carray()
  77. def _iconIsSupported(self, filename):
  78. extension = filename.lower().split(".")[-1]
  79. return extension in ICONS_SUPPORTED_FORMATS
  80. def icons(self):
  81. self.logger.debug(f"Converting icons")
  82. icons_c = open(
  83. os.path.join(self.args.output_directory, "assets_icons.c"),
  84. "w",
  85. newline="\n",
  86. )
  87. icons_c.write(ICONS_TEMPLATE_C_HEADER)
  88. icons = []
  89. # Traverse icons tree, append image data to source file
  90. for dirpath, dirnames, filenames in os.walk(self.args.input_directory):
  91. self.logger.debug(f"Processing directory {dirpath}")
  92. dirnames.sort()
  93. filenames.sort()
  94. if not filenames:
  95. continue
  96. if "frame_rate" in filenames:
  97. self.logger.debug(f"Folder contatins animation")
  98. icon_name = "A_" + os.path.split(dirpath)[1].replace("-", "_")
  99. width = height = None
  100. frame_count = 0
  101. frame_rate = 0
  102. frame_names = []
  103. for filename in sorted(filenames):
  104. fullfilename = os.path.join(dirpath, filename)
  105. if filename == "frame_rate":
  106. frame_rate = int(open(fullfilename, "r").read().strip())
  107. continue
  108. elif not self._iconIsSupported(filename):
  109. continue
  110. self.logger.debug(f"Processing animation frame {filename}")
  111. temp_width, temp_height, data = self._icon2header(fullfilename)
  112. if width is None:
  113. width = temp_width
  114. if height is None:
  115. height = temp_height
  116. assert width == temp_width
  117. assert height == temp_height
  118. frame_name = f"_{icon_name}_{frame_count}"
  119. frame_names.append(frame_name)
  120. icons_c.write(
  121. ICONS_TEMPLATE_C_FRAME.format(name=frame_name, data=data)
  122. )
  123. frame_count += 1
  124. assert frame_rate > 0
  125. assert frame_count > 0
  126. icons_c.write(
  127. ICONS_TEMPLATE_C_DATA.format(
  128. name=f"_{icon_name}", data=f'{{{",".join(frame_names)}}}'
  129. )
  130. )
  131. icons_c.write("\n")
  132. icons.append((icon_name, width, height, frame_rate, frame_count))
  133. else:
  134. # process icons
  135. for filename in filenames:
  136. if not self._iconIsSupported(filename):
  137. continue
  138. self.logger.debug(f"Processing icon {filename}")
  139. icon_name = "I_" + "_".join(filename.split(".")[:-1]).replace(
  140. "-", "_"
  141. )
  142. fullfilename = os.path.join(dirpath, filename)
  143. width, height, data = self._icon2header(fullfilename)
  144. frame_name = f"_{icon_name}_0"
  145. icons_c.write(
  146. ICONS_TEMPLATE_C_FRAME.format(name=frame_name, data=data)
  147. )
  148. icons_c.write(
  149. ICONS_TEMPLATE_C_DATA.format(
  150. name=f"_{icon_name}", data=f"{{{frame_name}}}"
  151. )
  152. )
  153. icons_c.write("\n")
  154. icons.append((icon_name, width, height, 0, 1))
  155. # Create array of images:
  156. self.logger.debug(f"Finalizing source file")
  157. for name, width, height, frame_rate, frame_count in icons:
  158. icons_c.write(
  159. ICONS_TEMPLATE_C_ICONS.format(
  160. name=name,
  161. width=width,
  162. height=height,
  163. frame_rate=frame_rate,
  164. frame_count=frame_count,
  165. )
  166. )
  167. icons_c.write("\n")
  168. icons_c.close()
  169. # Create Public Header
  170. self.logger.debug(f"Creating header")
  171. icons_h = open(
  172. os.path.join(self.args.output_directory, "assets_icons.h"),
  173. "w",
  174. newline="\n",
  175. )
  176. icons_h.write(ICONS_TEMPLATE_H_HEADER)
  177. for name, width, height, frame_rate, frame_count in icons:
  178. icons_h.write(ICONS_TEMPLATE_H_ICON_NAME.format(name=name))
  179. icons_h.close()
  180. self.logger.debug(f"Done")
  181. return 0
  182. def manifest(self):
  183. from flipper.assets.manifest import Manifest
  184. directory_path = os.path.normpath(self.args.local_path)
  185. if not os.path.isdir(directory_path):
  186. self.logger.error(f'"{directory_path}" is not a directory')
  187. exit(255)
  188. manifest_file = os.path.join(directory_path, "Manifest")
  189. old_manifest = Manifest()
  190. if os.path.exists(manifest_file):
  191. self.logger.info("Manifest is present, loading to compare")
  192. old_manifest.load(manifest_file)
  193. self.logger.info(
  194. f'Creating temporary Manifest for directory "{directory_path}"'
  195. )
  196. new_manifest = Manifest()
  197. new_manifest.create(directory_path)
  198. self.logger.info(f"Comparing new manifest with existing")
  199. only_in_old, changed, only_in_new = Manifest.compare(old_manifest, new_manifest)
  200. for record in only_in_old:
  201. self.logger.info(f"Only in old: {record}")
  202. for record in changed:
  203. self.logger.info(f"Changed: {record}")
  204. for record in only_in_new:
  205. self.logger.info(f"Only in new: {record}")
  206. if any((only_in_old, changed, only_in_new)):
  207. self.logger.warning("Manifests are different, updating")
  208. new_manifest.save(manifest_file)
  209. else:
  210. self.logger.info("Manifest is up-to-date!")
  211. self.logger.info(f"Complete")
  212. return 0
  213. def copro(self):
  214. from flipper.assets.copro import Copro
  215. self.logger.info(f"Bundling coprocessor binaries")
  216. copro = Copro(self.args.mcu)
  217. self.logger.info(f"Loading CUBE info")
  218. copro.loadCubeInfo(self.args.cube_dir, self.args.cube_ver)
  219. self.logger.info(f"Bundling")
  220. copro.bundle(
  221. self.args.output_dir,
  222. self.args.stack_file,
  223. self.args.stack_type,
  224. self.args.stack_addr,
  225. )
  226. self.logger.info(f"Complete")
  227. return 0
  228. def dolphin(self):
  229. from flipper.assets.dolphin import Dolphin
  230. self.logger.info(f"Processing Dolphin sources")
  231. dolphin = Dolphin()
  232. self.logger.info(f"Loading data")
  233. dolphin.load(self.args.input_directory)
  234. self.logger.info(f"Packing")
  235. dolphin.pack(self.args.output_directory, self.args.symbol_name)
  236. self.logger.info(f"Complete")
  237. return 0
  238. if __name__ == "__main__":
  239. Main()()