animation_storage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include <stdint.h>
  2. #include <flipper_format/flipper_format.h>
  3. #include <furi.h>
  4. #include <furi/dangerous_defines.h>
  5. #include <storage/storage.h>
  6. #include <gui/icon_i.h>
  7. #include <m-string.h>
  8. #include "animation_manager.h"
  9. #include "animation_storage.h"
  10. #include "animation_storage_i.h"
  11. #include <assets_dolphin_internal.h>
  12. #include <assets_dolphin_blocking.h>
  13. #define ANIMATION_META_FILE "meta.txt"
  14. #define ANIMATION_DIR "/ext/dolphin"
  15. #define ANIMATION_MANIFEST_FILE ANIMATION_DIR "/manifest.txt"
  16. #define TAG "AnimationStorage"
  17. static void animation_storage_free_bubbles(BubbleAnimation* animation);
  18. static void animation_storage_free_frames(BubbleAnimation* animation);
  19. static void animation_storage_free_animation(BubbleAnimation** storage_animation);
  20. static BubbleAnimation* animation_storage_load_animation(const char* name);
  21. static bool animation_storage_load_single_manifest_info(
  22. StorageAnimationManifestInfo* manifest_info,
  23. const char* name) {
  24. furi_assert(manifest_info);
  25. bool result = false;
  26. Storage* storage = furi_record_open("storage");
  27. FlipperFormat* file = flipper_format_file_alloc(storage);
  28. flipper_format_set_strict_mode(file, true);
  29. string_t read_string;
  30. string_init(read_string);
  31. do {
  32. uint32_t u32value;
  33. if(FSE_OK != storage_sd_status(storage)) break;
  34. if(!flipper_format_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
  35. if(!flipper_format_read_header(file, read_string, &u32value)) break;
  36. if(string_cmp_str(read_string, "Flipper Animation Manifest")) break;
  37. manifest_info->name = NULL;
  38. /* skip other animation names */
  39. flipper_format_set_strict_mode(file, false);
  40. while(flipper_format_read_string(file, "Name", read_string) &&
  41. string_cmp_str(read_string, name))
  42. ;
  43. if(string_cmp_str(read_string, name)) break;
  44. flipper_format_set_strict_mode(file, true);
  45. manifest_info->name = malloc(string_size(read_string) + 1);
  46. strcpy((char*)manifest_info->name, string_get_cstr(read_string));
  47. if(!flipper_format_read_uint32(file, "Min butthurt", &u32value, 1)) break;
  48. manifest_info->min_butthurt = u32value;
  49. if(!flipper_format_read_uint32(file, "Max butthurt", &u32value, 1)) break;
  50. manifest_info->max_butthurt = u32value;
  51. if(!flipper_format_read_uint32(file, "Min level", &u32value, 1)) break;
  52. manifest_info->min_level = u32value;
  53. if(!flipper_format_read_uint32(file, "Max level", &u32value, 1)) break;
  54. manifest_info->max_level = u32value;
  55. if(!flipper_format_read_uint32(file, "Weight", &u32value, 1)) break;
  56. manifest_info->weight = u32value;
  57. result = true;
  58. } while(0);
  59. if(!result && manifest_info->name) {
  60. free((void*)manifest_info->name);
  61. }
  62. string_clear(read_string);
  63. flipper_format_free(file);
  64. furi_record_close("storage");
  65. return result;
  66. }
  67. void animation_storage_fill_animation_list(StorageAnimationList_t* animation_list) {
  68. furi_assert(sizeof(StorageAnimationList_t) == sizeof(void*));
  69. furi_assert(!StorageAnimationList_size(*animation_list));
  70. Storage* storage = furi_record_open("storage");
  71. FlipperFormat* file = flipper_format_file_alloc(storage);
  72. /* Forbid skipping fields */
  73. flipper_format_set_strict_mode(file, true);
  74. string_t read_string;
  75. string_init(read_string);
  76. do {
  77. uint32_t u32value;
  78. StorageAnimation* storage_animation = NULL;
  79. if(FSE_OK != storage_sd_status(storage)) break;
  80. if(!flipper_format_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
  81. if(!flipper_format_read_header(file, read_string, &u32value)) break;
  82. if(string_cmp_str(read_string, "Flipper Animation Manifest")) break;
  83. do {
  84. storage_animation = malloc(sizeof(StorageAnimation));
  85. storage_animation->external = true;
  86. storage_animation->animation = NULL;
  87. storage_animation->manifest_info.name = NULL;
  88. if(!flipper_format_read_string(file, "Name", read_string)) break;
  89. storage_animation->manifest_info.name = malloc(string_size(read_string) + 1);
  90. strcpy((char*)storage_animation->manifest_info.name, string_get_cstr(read_string));
  91. if(!flipper_format_read_uint32(file, "Min butthurt", &u32value, 1)) break;
  92. storage_animation->manifest_info.min_butthurt = u32value;
  93. if(!flipper_format_read_uint32(file, "Max butthurt", &u32value, 1)) break;
  94. storage_animation->manifest_info.max_butthurt = u32value;
  95. if(!flipper_format_read_uint32(file, "Min level", &u32value, 1)) break;
  96. storage_animation->manifest_info.min_level = u32value;
  97. if(!flipper_format_read_uint32(file, "Max level", &u32value, 1)) break;
  98. storage_animation->manifest_info.max_level = u32value;
  99. if(!flipper_format_read_uint32(file, "Weight", &u32value, 1)) break;
  100. storage_animation->manifest_info.weight = u32value;
  101. StorageAnimationList_push_back(*animation_list, storage_animation);
  102. } while(1);
  103. animation_storage_free_storage_animation(&storage_animation);
  104. } while(0);
  105. string_clear(read_string);
  106. flipper_format_free(file);
  107. // add hard-coded animations
  108. for(size_t i = 0; i < dolphin_internal_size; ++i) {
  109. StorageAnimationList_push_back(*animation_list, (StorageAnimation*)&dolphin_internal[i]);
  110. }
  111. furi_record_close("storage");
  112. }
  113. StorageAnimation* animation_storage_find_animation(const char* name) {
  114. furi_assert(name);
  115. furi_assert(strlen(name));
  116. StorageAnimation* storage_animation = NULL;
  117. for(size_t i = 0; i < dolphin_blocking_size; ++i) {
  118. if(!strcmp(dolphin_blocking[i].manifest_info.name, name)) {
  119. storage_animation = (StorageAnimation*)&dolphin_blocking[i];
  120. break;
  121. }
  122. }
  123. if(!storage_animation) {
  124. for(size_t i = 0; i < dolphin_internal_size; ++i) {
  125. if(!strcmp(dolphin_internal[i].manifest_info.name, name)) {
  126. storage_animation = (StorageAnimation*)&dolphin_internal[i];
  127. break;
  128. }
  129. }
  130. }
  131. /* look through external animations */
  132. if(!storage_animation) {
  133. storage_animation = malloc(sizeof(StorageAnimation));
  134. storage_animation->external = true;
  135. bool result = false;
  136. result =
  137. animation_storage_load_single_manifest_info(&storage_animation->manifest_info, name);
  138. if(result) {
  139. storage_animation->animation = animation_storage_load_animation(name);
  140. result = !!storage_animation->animation;
  141. }
  142. if(!result) {
  143. animation_storage_free_storage_animation(&storage_animation);
  144. }
  145. }
  146. return storage_animation;
  147. }
  148. StorageAnimationManifestInfo* animation_storage_get_meta(StorageAnimation* storage_animation) {
  149. furi_assert(storage_animation);
  150. return &storage_animation->manifest_info;
  151. }
  152. const BubbleAnimation*
  153. animation_storage_get_bubble_animation(StorageAnimation* storage_animation) {
  154. furi_assert(storage_animation);
  155. animation_storage_cache_animation(storage_animation);
  156. return storage_animation->animation;
  157. }
  158. void animation_storage_cache_animation(StorageAnimation* storage_animation) {
  159. furi_assert(storage_animation);
  160. if(storage_animation->external) {
  161. if(!storage_animation->animation) {
  162. storage_animation->animation =
  163. animation_storage_load_animation(storage_animation->manifest_info.name);
  164. }
  165. }
  166. }
  167. static void animation_storage_free_animation(BubbleAnimation** animation) {
  168. furi_assert(animation);
  169. if(*animation) {
  170. animation_storage_free_bubbles(*animation);
  171. animation_storage_free_frames(*animation);
  172. if((*animation)->frame_order) {
  173. free((void*)(*animation)->frame_order);
  174. }
  175. free(*animation);
  176. *animation = NULL;
  177. }
  178. }
  179. void animation_storage_free_storage_animation(StorageAnimation** storage_animation) {
  180. furi_assert(storage_animation);
  181. furi_assert(*storage_animation);
  182. if((*storage_animation)->external) {
  183. animation_storage_free_animation((BubbleAnimation**)&(*storage_animation)->animation);
  184. if((*storage_animation)->manifest_info.name) {
  185. free((void*)(*storage_animation)->manifest_info.name);
  186. }
  187. free(*storage_animation);
  188. }
  189. *storage_animation = NULL;
  190. }
  191. static bool animation_storage_cast_align(string_t align_str, Align* align) {
  192. if(!string_cmp_str(align_str, "Bottom")) {
  193. *align = AlignBottom;
  194. } else if(!string_cmp_str(align_str, "Top")) {
  195. *align = AlignTop;
  196. } else if(!string_cmp_str(align_str, "Left")) {
  197. *align = AlignLeft;
  198. } else if(!string_cmp_str(align_str, "Right")) {
  199. *align = AlignRight;
  200. } else if(!string_cmp_str(align_str, "Center")) {
  201. *align = AlignCenter;
  202. } else {
  203. return false;
  204. }
  205. return true;
  206. }
  207. static void animation_storage_free_frames(BubbleAnimation* animation) {
  208. furi_assert(animation);
  209. const Icon* icon = &animation->icon_animation;
  210. for(int i = 0; i < icon->frame_count; ++i) {
  211. if(icon->frames[i]) {
  212. free((void*)icon->frames[i]);
  213. }
  214. }
  215. free((void*)icon->frames);
  216. }
  217. static bool animation_storage_load_frames(
  218. Storage* storage,
  219. const char* name,
  220. BubbleAnimation* animation,
  221. uint32_t* frame_order,
  222. uint8_t width,
  223. uint8_t height) {
  224. uint16_t frame_order_count = animation->passive_frames + animation->active_frames;
  225. /* The frames should go in order (0...N), without omissions */
  226. size_t max_frame_count = 0;
  227. for(int i = 0; i < frame_order_count; ++i) {
  228. max_frame_count = MAX(max_frame_count, frame_order[i]);
  229. }
  230. if((max_frame_count >= frame_order_count) || (max_frame_count >= 256 /* max uint8_t */)) {
  231. return false;
  232. }
  233. Icon* icon = (Icon*)&animation->icon_animation;
  234. FURI_CONST_ASSIGN(icon->frame_count, max_frame_count + 1);
  235. FURI_CONST_ASSIGN(icon->frame_rate, 0);
  236. FURI_CONST_ASSIGN(icon->height, height);
  237. FURI_CONST_ASSIGN(icon->width, width);
  238. icon->frames = malloc(sizeof(const uint8_t*) * icon->frame_count);
  239. bool frames_ok = false;
  240. File* file = storage_file_alloc(storage);
  241. FileInfo file_info;
  242. string_t filename;
  243. string_init(filename);
  244. size_t max_filesize = ROUND_UP_TO(width, 8) * height + 1;
  245. for(int i = 0; i < icon->frame_count; ++i) {
  246. frames_ok = false;
  247. string_printf(filename, ANIMATION_DIR "/%s/frame_%d.bm", name, i);
  248. if(storage_common_stat(storage, string_get_cstr(filename), &file_info) != FSE_OK) break;
  249. if(file_info.size > max_filesize) {
  250. FURI_LOG_E(
  251. TAG,
  252. "Filesize %d, max: %d (width %d, height %d)",
  253. file_info.size,
  254. max_filesize,
  255. width,
  256. height);
  257. break;
  258. }
  259. if(!storage_file_open(file, string_get_cstr(filename), FSAM_READ, FSOM_OPEN_EXISTING)) {
  260. FURI_LOG_E(TAG, "Can't open file \'%s\'", string_get_cstr(filename));
  261. break;
  262. }
  263. FURI_CONST_ASSIGN_PTR(icon->frames[i], malloc(file_info.size));
  264. if(storage_file_read(file, (void*)icon->frames[i], file_info.size) != file_info.size) {
  265. FURI_LOG_E(TAG, "Read failed: \'%s\'", string_get_cstr(filename));
  266. break;
  267. }
  268. storage_file_close(file);
  269. frames_ok = true;
  270. }
  271. if(!frames_ok) {
  272. FURI_LOG_E(
  273. TAG,
  274. "Load \'%s\' failed, %dx%d, size: %d",
  275. string_get_cstr(filename),
  276. width,
  277. height,
  278. file_info.size);
  279. animation_storage_free_frames(animation);
  280. } else {
  281. furi_check(animation->icon_animation.frames);
  282. for(int i = 0; i < animation->icon_animation.frame_count; ++i) {
  283. furi_check(animation->icon_animation.frames[i]);
  284. }
  285. }
  286. storage_file_free(file);
  287. string_clear(filename);
  288. return frames_ok;
  289. }
  290. static bool animation_storage_load_bubbles(BubbleAnimation* animation, FlipperFormat* ff) {
  291. uint32_t u32value;
  292. string_t str;
  293. string_init(str);
  294. bool success = false;
  295. furi_assert(!animation->frame_bubble_sequences);
  296. do {
  297. if(!flipper_format_read_uint32(ff, "Bubble slots", &u32value, 1)) break;
  298. if(u32value > 20) break;
  299. animation->frame_bubble_sequences_count = u32value;
  300. if(animation->frame_bubble_sequences_count == 0) {
  301. animation->frame_bubble_sequences = NULL;
  302. success = true;
  303. break;
  304. }
  305. animation->frame_bubble_sequences =
  306. malloc(sizeof(FrameBubble*) * animation->frame_bubble_sequences_count);
  307. int32_t current_slot = 0;
  308. for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
  309. FURI_CONST_ASSIGN_PTR(
  310. animation->frame_bubble_sequences[i], malloc(sizeof(FrameBubble)));
  311. }
  312. const FrameBubble* bubble = animation->frame_bubble_sequences[0];
  313. int8_t index = -1;
  314. for(;;) {
  315. if(!flipper_format_read_int32(ff, "Slot", &current_slot, 1)) break;
  316. if((current_slot != 0) && (index == -1)) break;
  317. if(current_slot == index) {
  318. FURI_CONST_ASSIGN_PTR(bubble->next_bubble, malloc(sizeof(FrameBubble)));
  319. bubble = bubble->next_bubble;
  320. } else if(current_slot == index + 1) {
  321. ++index;
  322. bubble = animation->frame_bubble_sequences[index];
  323. } else {
  324. /* slots have to start from 0, be ascending sorted, and
  325. * have exact number of slots as specified in "Bubble slots" */
  326. break;
  327. }
  328. if(index >= animation->frame_bubble_sequences_count) break;
  329. if(!flipper_format_read_uint32(ff, "X", &u32value, 1)) break;
  330. FURI_CONST_ASSIGN(bubble->bubble.x, u32value);
  331. if(!flipper_format_read_uint32(ff, "Y", &u32value, 1)) break;
  332. FURI_CONST_ASSIGN(bubble->bubble.y, u32value);
  333. if(!flipper_format_read_string(ff, "Text", str)) break;
  334. if(string_size(str) > 100) break;
  335. string_replace_all_str(str, "\\n", "\n");
  336. FURI_CONST_ASSIGN_PTR(bubble->bubble.text, malloc(string_size(str) + 1));
  337. strcpy((char*)bubble->bubble.text, string_get_cstr(str));
  338. if(!flipper_format_read_string(ff, "AlignH", str)) break;
  339. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_h)) break;
  340. if(!flipper_format_read_string(ff, "AlignV", str)) break;
  341. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_v)) break;
  342. if(!flipper_format_read_uint32(ff, "StartFrame", &u32value, 1)) break;
  343. FURI_CONST_ASSIGN(bubble->start_frame, u32value);
  344. if(!flipper_format_read_uint32(ff, "EndFrame", &u32value, 1)) break;
  345. FURI_CONST_ASSIGN(bubble->end_frame, u32value);
  346. }
  347. success = (index + 1) == animation->frame_bubble_sequences_count;
  348. } while(0);
  349. if(!success) {
  350. if(animation->frame_bubble_sequences) {
  351. FURI_LOG_E(TAG, "Failed to load animation bubbles");
  352. animation_storage_free_bubbles(animation);
  353. }
  354. }
  355. string_clear(str);
  356. return success;
  357. }
  358. static BubbleAnimation* animation_storage_load_animation(const char* name) {
  359. furi_assert(name);
  360. BubbleAnimation* animation = malloc(sizeof(BubbleAnimation));
  361. uint32_t height = 0;
  362. uint32_t width = 0;
  363. uint32_t* u32array = NULL;
  364. Storage* storage = furi_record_open("storage");
  365. FlipperFormat* ff = flipper_format_file_alloc(storage);
  366. /* Forbid skipping fields */
  367. flipper_format_set_strict_mode(ff, true);
  368. string_t str;
  369. string_init(str);
  370. animation->frame_bubble_sequences = NULL;
  371. bool success = false;
  372. do {
  373. uint32_t u32value;
  374. if(FSE_OK != storage_sd_status(storage)) break;
  375. string_printf(str, ANIMATION_DIR "/%s/" ANIMATION_META_FILE, name);
  376. if(!flipper_format_file_open_existing(ff, string_get_cstr(str))) break;
  377. if(!flipper_format_read_header(ff, str, &u32value)) break;
  378. if(string_cmp_str(str, "Flipper Animation")) break;
  379. if(!flipper_format_read_uint32(ff, "Width", &width, 1)) break;
  380. if(!flipper_format_read_uint32(ff, "Height", &height, 1)) break;
  381. if(!flipper_format_read_uint32(ff, "Passive frames", &u32value, 1)) break;
  382. animation->passive_frames = u32value;
  383. if(!flipper_format_read_uint32(ff, "Active frames", &u32value, 1)) break;
  384. animation->active_frames = u32value;
  385. uint8_t frames = animation->passive_frames + animation->active_frames;
  386. uint32_t count = 0;
  387. if(!flipper_format_get_value_count(ff, "Frames order", &count)) break;
  388. if(count != frames) {
  389. FURI_LOG_E(TAG, "Error loading animation: frames order");
  390. break;
  391. }
  392. u32array = malloc(sizeof(uint32_t) * frames);
  393. if(!flipper_format_read_uint32(ff, "Frames order", u32array, frames)) break;
  394. animation->frame_order = malloc(sizeof(uint8_t) * frames);
  395. for(int i = 0; i < frames; ++i) {
  396. FURI_CONST_ASSIGN(animation->frame_order[i], u32array[i]);
  397. }
  398. /* passive and active frames must be loaded up to this point */
  399. if(!animation_storage_load_frames(storage, name, animation, u32array, width, height))
  400. break;
  401. if(!flipper_format_read_uint32(ff, "Active cycles", &u32value, 1)) break;
  402. animation->active_cycles = u32value;
  403. if(!flipper_format_read_uint32(ff, "Frame rate", &u32value, 1)) break;
  404. FURI_CONST_ASSIGN(animation->icon_animation.frame_rate, u32value);
  405. if(!flipper_format_read_uint32(ff, "Duration", &u32value, 1)) break;
  406. animation->duration = u32value;
  407. if(!flipper_format_read_uint32(ff, "Active cooldown", &u32value, 1)) break;
  408. animation->active_cooldown = u32value;
  409. if(!animation_storage_load_bubbles(animation, ff)) break;
  410. success = true;
  411. } while(0);
  412. string_clear(str);
  413. flipper_format_free(ff);
  414. if(u32array) {
  415. free(u32array);
  416. }
  417. if(!success) {
  418. if(animation->frame_order) {
  419. free((void*)animation->frame_order);
  420. }
  421. free(animation);
  422. animation = NULL;
  423. }
  424. return animation;
  425. }
  426. static void animation_storage_free_bubbles(BubbleAnimation* animation) {
  427. if(!animation->frame_bubble_sequences) return;
  428. for(int i = 0; i < animation->frame_bubble_sequences_count;) {
  429. const FrameBubble* const* bubble = &animation->frame_bubble_sequences[i];
  430. if((*bubble) == NULL) break;
  431. while((*bubble)->next_bubble != NULL) {
  432. bubble = &(*bubble)->next_bubble;
  433. }
  434. if((*bubble)->bubble.text) {
  435. free((void*)(*bubble)->bubble.text);
  436. }
  437. if((*bubble) == animation->frame_bubble_sequences[i]) {
  438. ++i;
  439. }
  440. free((void*)*bubble);
  441. FURI_CONST_ASSIGN_PTR(*bubble, NULL);
  442. }
  443. free((void*)animation->frame_bubble_sequences);
  444. animation->frame_bubble_sequences = NULL;
  445. }