assets.py 10 KB

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