animation_storage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include <stdint.h>
  2. #include <flipper_format/flipper_format.h>
  3. #include <furi.h>
  4. #include <core/dangerous_defines.h>
  5. #include <storage/storage.h>
  6. #include <gui/icon_i.h>
  7. #include "animation_manager.h"
  8. #include "animation_storage.h"
  9. #include "animation_storage_i.h"
  10. #include <assets_dolphin_internal.h>
  11. #include <assets_dolphin_blocking.h>
  12. #define ANIMATION_META_FILE "meta.txt"
  13. #define ANIMATION_DIR EXT_PATH("dolphin")
  14. #define ANIMATION_MANIFEST_FILE ANIMATION_DIR "/manifest.txt"
  15. #define TAG "AnimationStorage"
  16. static void animation_storage_free_bubbles(BubbleAnimation* animation);
  17. static void animation_storage_free_frames(BubbleAnimation* animation);
  18. static void animation_storage_free_animation(BubbleAnimation** storage_animation);
  19. static BubbleAnimation* animation_storage_load_animation(const char* name);
  20. static bool animation_storage_load_single_manifest_info(
  21. StorageAnimationManifestInfo* manifest_info,
  22. const char* name) {
  23. furi_assert(manifest_info);
  24. bool result = false;
  25. Storage* storage = furi_record_open(RECORD_STORAGE);
  26. FlipperFormat* file = flipper_format_file_alloc(storage);
  27. flipper_format_set_strict_mode(file, true);
  28. FuriString* read_string;
  29. read_string = furi_string_alloc();
  30. do {
  31. uint32_t u32value;
  32. if(FSE_OK != storage_sd_status(storage)) break;
  33. if(!flipper_format_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
  34. if(!flipper_format_read_header(file, read_string, &u32value)) break;
  35. if(furi_string_cmp_str(read_string, "Flipper Animation Manifest")) break;
  36. manifest_info->name = NULL;
  37. /* skip other animation names */
  38. flipper_format_set_strict_mode(file, false);
  39. while(flipper_format_read_string(file, "Name", read_string) &&
  40. furi_string_cmp_str(read_string, name))
  41. ;
  42. if(furi_string_cmp_str(read_string, name)) break;
  43. flipper_format_set_strict_mode(file, true);
  44. manifest_info->name = malloc(furi_string_size(read_string) + 1);
  45. strcpy((char*)manifest_info->name, furi_string_get_cstr(read_string));
  46. if(!flipper_format_read_uint32(file, "Min butthurt", &u32value, 1)) break;
  47. manifest_info->min_butthurt = u32value;
  48. if(!flipper_format_read_uint32(file, "Max butthurt", &u32value, 1)) break;
  49. manifest_info->max_butthurt = u32value;
  50. if(!flipper_format_read_uint32(file, "Min level", &u32value, 1)) break;
  51. manifest_info->min_level = u32value;
  52. if(!flipper_format_read_uint32(file, "Max level", &u32value, 1)) break;
  53. manifest_info->max_level = u32value;
  54. if(!flipper_format_read_uint32(file, "Weight", &u32value, 1)) break;
  55. manifest_info->weight = u32value;
  56. result = true;
  57. } while(0);
  58. if(!result && manifest_info->name) {
  59. free((void*)manifest_info->name);
  60. }
  61. furi_string_free(read_string);
  62. flipper_format_free(file);
  63. furi_record_close(RECORD_STORAGE);
  64. return result;
  65. }
  66. void animation_storage_fill_animation_list(StorageAnimationList_t* animation_list) {
  67. furi_assert(sizeof(StorageAnimationList_t) == sizeof(void*));
  68. furi_assert(!StorageAnimationList_size(*animation_list));
  69. Storage* storage = furi_record_open(RECORD_STORAGE);
  70. FlipperFormat* file = flipper_format_file_alloc(storage);
  71. /* Forbid skipping fields */
  72. flipper_format_set_strict_mode(file, true);
  73. FuriString* read_string;
  74. read_string = furi_string_alloc();
  75. do {
  76. uint32_t u32value;
  77. StorageAnimation* storage_animation = NULL;
  78. if(FSE_OK != storage_sd_status(storage)) break;
  79. if(!flipper_format_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
  80. if(!flipper_format_read_header(file, read_string, &u32value)) break;
  81. if(furi_string_cmp_str(read_string, "Flipper Animation Manifest")) break;
  82. do {
  83. storage_animation = malloc(sizeof(StorageAnimation));
  84. storage_animation->external = true;
  85. storage_animation->animation = NULL;
  86. storage_animation->manifest_info.name = NULL;
  87. if(!flipper_format_read_string(file, "Name", read_string)) break;
  88. storage_animation->manifest_info.name = malloc(furi_string_size(read_string) + 1);
  89. strcpy(
  90. (char*)storage_animation->manifest_info.name, furi_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. furi_string_free(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(RECORD_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(FuriString* align_str, Align* align) {
  192. if(!furi_string_cmp(align_str, "Bottom")) {
  193. *align = AlignBottom;
  194. } else if(!furi_string_cmp(align_str, "Top")) {
  195. *align = AlignTop;
  196. } else if(!furi_string_cmp(align_str, "Left")) {
  197. *align = AlignLeft;
  198. } else if(!furi_string_cmp(align_str, "Right")) {
  199. *align = AlignRight;
  200. } else if(!furi_string_cmp(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. FuriString* filename;
  243. filename = furi_string_alloc();
  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. furi_string_printf(filename, ANIMATION_DIR "/%s/frame_%d.bm", name, i);
  248. if(storage_common_stat(storage, furi_string_get_cstr(filename), &file_info) != FSE_OK)
  249. break;
  250. if(file_info.size > max_filesize) {
  251. FURI_LOG_E(
  252. TAG,
  253. "Filesize %lld, max: %d (width %d, height %d)",
  254. file_info.size,
  255. max_filesize,
  256. width,
  257. height);
  258. break;
  259. }
  260. if(!storage_file_open(
  261. file, furi_string_get_cstr(filename), FSAM_READ, FSOM_OPEN_EXISTING)) {
  262. FURI_LOG_E(TAG, "Can't open file \'%s\'", furi_string_get_cstr(filename));
  263. break;
  264. }
  265. FURI_CONST_ASSIGN_PTR(icon->frames[i], malloc(file_info.size));
  266. if(storage_file_read(file, (void*)icon->frames[i], file_info.size) != file_info.size) {
  267. FURI_LOG_E(TAG, "Read failed: \'%s\'", furi_string_get_cstr(filename));
  268. break;
  269. }
  270. storage_file_close(file);
  271. frames_ok = true;
  272. }
  273. if(!frames_ok) {
  274. FURI_LOG_E(
  275. TAG,
  276. "Load \'%s\' failed, %dx%d, size: %lld",
  277. furi_string_get_cstr(filename),
  278. width,
  279. height,
  280. file_info.size);
  281. animation_storage_free_frames(animation);
  282. } else {
  283. furi_check(animation->icon_animation.frames);
  284. for(int i = 0; i < animation->icon_animation.frame_count; ++i) {
  285. furi_check(animation->icon_animation.frames[i]);
  286. }
  287. }
  288. storage_file_free(file);
  289. furi_string_free(filename);
  290. return frames_ok;
  291. }
  292. static bool animation_storage_load_bubbles(BubbleAnimation* animation, FlipperFormat* ff) {
  293. uint32_t u32value;
  294. FuriString* str;
  295. str = furi_string_alloc();
  296. bool success = false;
  297. furi_assert(!animation->frame_bubble_sequences);
  298. do {
  299. if(!flipper_format_read_uint32(ff, "Bubble slots", &u32value, 1)) break;
  300. if(u32value > 20) break;
  301. animation->frame_bubble_sequences_count = u32value;
  302. if(animation->frame_bubble_sequences_count == 0) {
  303. animation->frame_bubble_sequences = NULL;
  304. success = true;
  305. break;
  306. }
  307. animation->frame_bubble_sequences =
  308. malloc(sizeof(FrameBubble*) * animation->frame_bubble_sequences_count);
  309. int32_t current_slot = 0;
  310. for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
  311. FURI_CONST_ASSIGN_PTR(
  312. animation->frame_bubble_sequences[i], malloc(sizeof(FrameBubble)));
  313. }
  314. const FrameBubble* bubble = animation->frame_bubble_sequences[0];
  315. int8_t index = -1;
  316. for(;;) {
  317. if(!flipper_format_read_int32(ff, "Slot", &current_slot, 1)) break;
  318. if((current_slot != 0) && (index == -1)) break;
  319. if(current_slot == index) {
  320. FURI_CONST_ASSIGN_PTR(bubble->next_bubble, malloc(sizeof(FrameBubble)));
  321. bubble = bubble->next_bubble;
  322. } else if(current_slot == index + 1) {
  323. ++index;
  324. bubble = animation->frame_bubble_sequences[index];
  325. } else {
  326. /* slots have to start from 0, be ascending sorted, and
  327. * have exact number of slots as specified in "Bubble slots" */
  328. break;
  329. }
  330. if(index >= animation->frame_bubble_sequences_count) break;
  331. if(!flipper_format_read_uint32(ff, "X", &u32value, 1)) break;
  332. FURI_CONST_ASSIGN(bubble->bubble.x, u32value);
  333. if(!flipper_format_read_uint32(ff, "Y", &u32value, 1)) break;
  334. FURI_CONST_ASSIGN(bubble->bubble.y, u32value);
  335. if(!flipper_format_read_string(ff, "Text", str)) break;
  336. if(furi_string_size(str) > 100) break;
  337. furi_string_replace_all(str, "\\n", "\n");
  338. FURI_CONST_ASSIGN_PTR(bubble->bubble.text, malloc(furi_string_size(str) + 1));
  339. strcpy((char*)bubble->bubble.text, furi_string_get_cstr(str));
  340. if(!flipper_format_read_string(ff, "AlignH", str)) break;
  341. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_h)) break;
  342. if(!flipper_format_read_string(ff, "AlignV", str)) break;
  343. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_v)) break;
  344. if(!flipper_format_read_uint32(ff, "StartFrame", &u32value, 1)) break;
  345. FURI_CONST_ASSIGN(bubble->start_frame, u32value);
  346. if(!flipper_format_read_uint32(ff, "EndFrame", &u32value, 1)) break;
  347. FURI_CONST_ASSIGN(bubble->end_frame, u32value);
  348. }
  349. success = (index + 1) == animation->frame_bubble_sequences_count;
  350. } while(0);
  351. if(!success) {
  352. if(animation->frame_bubble_sequences) {
  353. FURI_LOG_E(TAG, "Failed to load animation bubbles");
  354. animation_storage_free_bubbles(animation);
  355. }
  356. }
  357. furi_string_free(str);
  358. return success;
  359. }
  360. static BubbleAnimation* animation_storage_load_animation(const char* name) {
  361. furi_assert(name);
  362. BubbleAnimation* animation = malloc(sizeof(BubbleAnimation));
  363. uint32_t height = 0;
  364. uint32_t width = 0;
  365. uint32_t* u32array = NULL;
  366. Storage* storage = furi_record_open(RECORD_STORAGE);
  367. FlipperFormat* ff = flipper_format_file_alloc(storage);
  368. /* Forbid skipping fields */
  369. flipper_format_set_strict_mode(ff, true);
  370. FuriString* str;
  371. str = furi_string_alloc();
  372. animation->frame_bubble_sequences = NULL;
  373. bool success = false;
  374. do {
  375. uint32_t u32value;
  376. if(FSE_OK != storage_sd_status(storage)) break;
  377. furi_string_printf(str, ANIMATION_DIR "/%s/" ANIMATION_META_FILE, name);
  378. if(!flipper_format_file_open_existing(ff, furi_string_get_cstr(str))) break;
  379. if(!flipper_format_read_header(ff, str, &u32value)) break;
  380. if(furi_string_cmp_str(str, "Flipper Animation")) break;
  381. if(!flipper_format_read_uint32(ff, "Width", &width, 1)) break;
  382. if(!flipper_format_read_uint32(ff, "Height", &height, 1)) break;
  383. if(!flipper_format_read_uint32(ff, "Passive frames", &u32value, 1)) break;
  384. animation->passive_frames = u32value;
  385. if(!flipper_format_read_uint32(ff, "Active frames", &u32value, 1)) break;
  386. animation->active_frames = u32value;
  387. uint8_t frames = animation->passive_frames + animation->active_frames;
  388. uint32_t count = 0;
  389. if(!flipper_format_get_value_count(ff, "Frames order", &count)) break;
  390. if(count != frames) {
  391. FURI_LOG_E(TAG, "Error loading animation: frames order");
  392. break;
  393. }
  394. u32array = malloc(sizeof(uint32_t) * frames);
  395. if(!flipper_format_read_uint32(ff, "Frames order", u32array, frames)) break;
  396. animation->frame_order = malloc(sizeof(uint8_t) * frames);
  397. for(int i = 0; i < frames; ++i) {
  398. FURI_CONST_ASSIGN(animation->frame_order[i], u32array[i]);
  399. }
  400. /* passive and active frames must be loaded up to this point */
  401. if(!animation_storage_load_frames(storage, name, animation, u32array, width, height))
  402. break;
  403. if(!flipper_format_read_uint32(ff, "Active cycles", &u32value, 1)) break;
  404. animation->active_cycles = u32value;
  405. if(!flipper_format_read_uint32(ff, "Frame rate", &u32value, 1)) break;
  406. FURI_CONST_ASSIGN(animation->icon_animation.frame_rate, u32value);
  407. if(!flipper_format_read_uint32(ff, "Duration", &u32value, 1)) break;
  408. animation->duration = u32value;
  409. if(!flipper_format_read_uint32(ff, "Active cooldown", &u32value, 1)) break;
  410. animation->active_cooldown = u32value;
  411. if(!animation_storage_load_bubbles(animation, ff)) break;
  412. success = true;
  413. } while(0);
  414. furi_string_free(str);
  415. flipper_format_free(ff);
  416. if(u32array) {
  417. free(u32array);
  418. }
  419. if(!success) {
  420. if(animation->frame_order) {
  421. free((void*)animation->frame_order);
  422. }
  423. free(animation);
  424. animation = NULL;
  425. }
  426. return animation;
  427. }
  428. static void animation_storage_free_bubbles(BubbleAnimation* animation) {
  429. if(!animation->frame_bubble_sequences) return;
  430. for(int i = 0; i < animation->frame_bubble_sequences_count;) {
  431. const FrameBubble* const* bubble = &animation->frame_bubble_sequences[i];
  432. if((*bubble) == NULL) break;
  433. while((*bubble)->next_bubble != NULL) {
  434. bubble = &(*bubble)->next_bubble;
  435. }
  436. if((*bubble)->bubble.text) {
  437. free((void*)(*bubble)->bubble.text);
  438. }
  439. if((*bubble) == animation->frame_bubble_sequences[i]) {
  440. ++i;
  441. }
  442. free((void*)*bubble);
  443. FURI_CONST_ASSIGN_PTR(*bubble, NULL);
  444. }
  445. free((void*)animation->frame_bubble_sequences);
  446. animation->frame_bubble_sequences = NULL;
  447. }