animation_storage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #include <stdint.h>
  2. #include <flipper_file.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. FlipperFile* file = flipper_file_alloc(storage);
  28. flipper_file_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_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
  35. if(!flipper_file_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_file_set_strict_mode(file, false);
  40. while(flipper_file_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_file_set_strict_mode(file, true);
  45. manifest_info->name = furi_alloc(string_size(read_string) + 1);
  46. strcpy((char*)manifest_info->name, string_get_cstr(read_string));
  47. if(!flipper_file_read_uint32(file, "Min butthurt", &u32value, 1)) break;
  48. manifest_info->min_butthurt = u32value;
  49. if(!flipper_file_read_uint32(file, "Max butthurt", &u32value, 1)) break;
  50. manifest_info->max_butthurt = u32value;
  51. if(!flipper_file_read_uint32(file, "Min level", &u32value, 1)) break;
  52. manifest_info->min_level = u32value;
  53. if(!flipper_file_read_uint32(file, "Max level", &u32value, 1)) break;
  54. manifest_info->max_level = u32value;
  55. if(!flipper_file_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_file_close(file);
  64. flipper_file_free(file);
  65. furi_record_close("storage");
  66. return result;
  67. }
  68. void animation_storage_fill_animation_list(StorageAnimationList_t* animation_list) {
  69. furi_assert(sizeof(StorageAnimationList_t) == sizeof(void*));
  70. furi_assert(!StorageAnimationList_size(*animation_list));
  71. Storage* storage = furi_record_open("storage");
  72. FlipperFile* file = flipper_file_alloc(storage);
  73. /* Forbid skipping fields */
  74. flipper_file_set_strict_mode(file, true);
  75. string_t read_string;
  76. string_init(read_string);
  77. do {
  78. uint32_t u32value;
  79. StorageAnimation* storage_animation = NULL;
  80. if(FSE_OK != storage_sd_status(storage)) break;
  81. if(!flipper_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
  82. if(!flipper_file_read_header(file, read_string, &u32value)) break;
  83. if(string_cmp_str(read_string, "Flipper Animation Manifest")) break;
  84. do {
  85. storage_animation = furi_alloc(sizeof(StorageAnimation));
  86. storage_animation->external = true;
  87. storage_animation->animation = NULL;
  88. storage_animation->manifest_info.name = NULL;
  89. if(!flipper_file_read_string(file, "Name", read_string)) break;
  90. storage_animation->manifest_info.name = furi_alloc(string_size(read_string) + 1);
  91. strcpy((char*)storage_animation->manifest_info.name, string_get_cstr(read_string));
  92. if(!flipper_file_read_uint32(file, "Min butthurt", &u32value, 1)) break;
  93. storage_animation->manifest_info.min_butthurt = u32value;
  94. if(!flipper_file_read_uint32(file, "Max butthurt", &u32value, 1)) break;
  95. storage_animation->manifest_info.max_butthurt = u32value;
  96. if(!flipper_file_read_uint32(file, "Min level", &u32value, 1)) break;
  97. storage_animation->manifest_info.min_level = u32value;
  98. if(!flipper_file_read_uint32(file, "Max level", &u32value, 1)) break;
  99. storage_animation->manifest_info.max_level = u32value;
  100. if(!flipper_file_read_uint32(file, "Weight", &u32value, 1)) break;
  101. storage_animation->manifest_info.weight = u32value;
  102. StorageAnimationList_push_back(*animation_list, storage_animation);
  103. } while(1);
  104. animation_storage_free_storage_animation(&storage_animation);
  105. } while(0);
  106. string_clear(read_string);
  107. flipper_file_close(file);
  108. flipper_file_free(file);
  109. // add hard-coded animations
  110. for(int i = 0; i < dolphin_internal_size; ++i) {
  111. StorageAnimationList_push_back(*animation_list, (StorageAnimation*)&dolphin_internal[i]);
  112. }
  113. furi_record_close("storage");
  114. }
  115. StorageAnimation* animation_storage_find_animation(const char* name) {
  116. furi_assert(name);
  117. furi_assert(strlen(name));
  118. StorageAnimation* storage_animation = NULL;
  119. for(int i = 0; i < dolphin_blocking_size; ++i) {
  120. if(!strcmp(dolphin_blocking[i].manifest_info.name, name)) {
  121. storage_animation = (StorageAnimation*)&dolphin_blocking[i];
  122. break;
  123. }
  124. }
  125. if(!storage_animation) {
  126. for(int i = 0; i < dolphin_internal_size; ++i) {
  127. if(!strcmp(dolphin_internal[i].manifest_info.name, name)) {
  128. storage_animation = (StorageAnimation*)&dolphin_internal[i];
  129. break;
  130. }
  131. }
  132. }
  133. /* look through external animations */
  134. if(!storage_animation) {
  135. storage_animation = furi_alloc(sizeof(StorageAnimation));
  136. storage_animation->external = true;
  137. bool result = false;
  138. result =
  139. animation_storage_load_single_manifest_info(&storage_animation->manifest_info, name);
  140. if(result) {
  141. storage_animation->animation = animation_storage_load_animation(name);
  142. result = !!storage_animation->animation;
  143. }
  144. if(!result) {
  145. animation_storage_free_storage_animation(&storage_animation);
  146. }
  147. }
  148. return storage_animation;
  149. }
  150. StorageAnimationManifestInfo* animation_storage_get_meta(StorageAnimation* storage_animation) {
  151. furi_assert(storage_animation);
  152. return &storage_animation->manifest_info;
  153. }
  154. const BubbleAnimation*
  155. animation_storage_get_bubble_animation(StorageAnimation* storage_animation) {
  156. furi_assert(storage_animation);
  157. animation_storage_cache_animation(storage_animation);
  158. return storage_animation->animation;
  159. }
  160. void animation_storage_cache_animation(StorageAnimation* storage_animation) {
  161. furi_assert(storage_animation);
  162. if(storage_animation->external) {
  163. if(!storage_animation->animation) {
  164. storage_animation->animation =
  165. animation_storage_load_animation(storage_animation->manifest_info.name);
  166. }
  167. }
  168. }
  169. static void animation_storage_free_animation(BubbleAnimation** animation) {
  170. furi_assert(animation);
  171. if(*animation) {
  172. animation_storage_free_bubbles(*animation);
  173. animation_storage_free_frames(*animation);
  174. if((*animation)->frame_order) {
  175. free((void*)(*animation)->frame_order);
  176. }
  177. free(*animation);
  178. *animation = NULL;
  179. }
  180. }
  181. void animation_storage_free_storage_animation(StorageAnimation** storage_animation) {
  182. furi_assert(storage_animation);
  183. furi_assert(*storage_animation);
  184. if((*storage_animation)->external) {
  185. animation_storage_free_animation((BubbleAnimation**)&(*storage_animation)->animation);
  186. if((*storage_animation)->manifest_info.name) {
  187. free((void*)(*storage_animation)->manifest_info.name);
  188. }
  189. free(*storage_animation);
  190. }
  191. *storage_animation = NULL;
  192. }
  193. static bool animation_storage_cast_align(string_t align_str, Align* align) {
  194. if(!string_cmp_str(align_str, "Bottom")) {
  195. *align = AlignBottom;
  196. } else if(!string_cmp_str(align_str, "Top")) {
  197. *align = AlignTop;
  198. } else if(!string_cmp_str(align_str, "Left")) {
  199. *align = AlignLeft;
  200. } else if(!string_cmp_str(align_str, "Right")) {
  201. *align = AlignRight;
  202. } else if(!string_cmp_str(align_str, "Center")) {
  203. *align = AlignCenter;
  204. } else {
  205. return false;
  206. }
  207. return true;
  208. }
  209. static void animation_storage_free_frames(BubbleAnimation* animation) {
  210. furi_assert(animation);
  211. const Icon* icon = &animation->icon_animation;
  212. for(int i = 0; i < icon->frame_count; ++i) {
  213. if(icon->frames[i]) {
  214. free((void*)icon->frames[i]);
  215. }
  216. }
  217. free(icon->frames);
  218. }
  219. static bool animation_storage_load_frames(
  220. Storage* storage,
  221. const char* name,
  222. BubbleAnimation* animation,
  223. uint32_t* frame_order,
  224. uint8_t width,
  225. uint8_t height) {
  226. uint16_t frame_order_count = animation->passive_frames + animation->active_frames;
  227. /* The frames should go in order (0...N), without omissions */
  228. size_t max_frame_count = 0;
  229. for(int i = 0; i < frame_order_count; ++i) {
  230. max_frame_count = MAX(max_frame_count, frame_order[i]);
  231. }
  232. if((max_frame_count >= frame_order_count) || (max_frame_count >= 256 /* max uint8_t */)) {
  233. return false;
  234. }
  235. Icon* icon = (Icon*)&animation->icon_animation;
  236. FURI_CONST_ASSIGN(icon->frame_count, max_frame_count + 1);
  237. FURI_CONST_ASSIGN(icon->frame_rate, 0);
  238. FURI_CONST_ASSIGN(icon->height, height);
  239. FURI_CONST_ASSIGN(icon->width, width);
  240. icon->frames = furi_alloc(sizeof(const uint8_t*) * icon->frame_count);
  241. bool frames_ok = false;
  242. File* file = storage_file_alloc(storage);
  243. FileInfo file_info;
  244. string_t filename;
  245. string_init(filename);
  246. size_t max_filesize = ROUND_UP_TO(width, 8) * height + 1;
  247. for(int i = 0; i < icon->frame_count; ++i) {
  248. frames_ok = false;
  249. string_printf(filename, ANIMATION_DIR "/%s/frame_%d.bm", name, i);
  250. if(storage_common_stat(storage, string_get_cstr(filename), &file_info) != FSE_OK) break;
  251. if(file_info.size > max_filesize) {
  252. FURI_LOG_E(
  253. TAG,
  254. "Filesize %d, max: %d (width %d, height %d)",
  255. file_info.size,
  256. max_filesize,
  257. width,
  258. height);
  259. break;
  260. }
  261. if(!storage_file_open(file, string_get_cstr(filename), FSAM_READ, FSOM_OPEN_EXISTING)) {
  262. FURI_LOG_E(TAG, "Can't open file \'%s\'", string_get_cstr(filename));
  263. break;
  264. }
  265. icon->frames[i] = furi_alloc(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\'", 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: %d",
  277. 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. string_clear(filename);
  290. return frames_ok;
  291. }
  292. static bool animation_storage_load_bubbles(BubbleAnimation* animation, FlipperFile* ff) {
  293. uint32_t u32value;
  294. string_t str;
  295. string_init(str);
  296. bool success = false;
  297. furi_assert(!animation->frame_bubble_sequences);
  298. do {
  299. if(!flipper_file_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. furi_alloc(sizeof(FrameBubble*) * animation->frame_bubble_sequences_count);
  309. uint32_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], furi_alloc(sizeof(FrameBubble)));
  313. }
  314. const FrameBubble* bubble = animation->frame_bubble_sequences[0];
  315. int8_t index = -1;
  316. for(;;) {
  317. if(!flipper_file_read_uint32(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, furi_alloc(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_file_read_uint32(ff, "X", &u32value, 1)) break;
  332. FURI_CONST_ASSIGN(bubble->bubble.x, u32value);
  333. if(!flipper_file_read_uint32(ff, "Y", &u32value, 1)) break;
  334. FURI_CONST_ASSIGN(bubble->bubble.y, u32value);
  335. if(!flipper_file_read_string(ff, "Text", str)) break;
  336. if(string_size(str) > 100) break;
  337. string_replace_all_str(str, "\\n", "\n");
  338. FURI_CONST_ASSIGN_PTR(bubble->bubble.text, furi_alloc(string_size(str) + 1));
  339. strcpy((char*)bubble->bubble.text, string_get_cstr(str));
  340. if(!flipper_file_read_string(ff, "AlignH", str)) break;
  341. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_h)) break;
  342. if(!flipper_file_read_string(ff, "AlignV", str)) break;
  343. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_v)) break;
  344. if(!flipper_file_read_uint32(ff, "StartFrame", &u32value, 1)) break;
  345. FURI_CONST_ASSIGN(bubble->start_frame, u32value);
  346. if(!flipper_file_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. string_clear(str);
  358. return success;
  359. }
  360. static BubbleAnimation* animation_storage_load_animation(const char* name) {
  361. furi_assert(name);
  362. BubbleAnimation* animation = furi_alloc(sizeof(BubbleAnimation));
  363. uint32_t height = 0;
  364. uint32_t width = 0;
  365. uint32_t* u32array = NULL;
  366. Storage* storage = furi_record_open("storage");
  367. FlipperFile* ff = flipper_file_alloc(storage);
  368. /* Forbid skipping fields */
  369. flipper_file_set_strict_mode(ff, true);
  370. string_t str;
  371. string_init(str);
  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. string_printf(str, ANIMATION_DIR "/%s/" ANIMATION_META_FILE, name);
  378. if(!flipper_file_open_existing(ff, string_get_cstr(str))) break;
  379. if(!flipper_file_read_header(ff, str, &u32value)) break;
  380. if(string_cmp_str(str, "Flipper Animation")) break;
  381. if(!flipper_file_read_uint32(ff, "Width", &width, 1)) break;
  382. if(!flipper_file_read_uint32(ff, "Height", &height, 1)) break;
  383. if(!flipper_file_read_uint32(ff, "Passive frames", &u32value, 1)) break;
  384. animation->passive_frames = u32value;
  385. if(!flipper_file_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_file_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 = furi_alloc(sizeof(uint32_t) * frames);
  395. if(!flipper_file_read_uint32(ff, "Frames order", u32array, frames)) break;
  396. animation->frame_order = furi_alloc(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_file_read_uint32(ff, "Active cycles", &u32value, 1)) break;
  404. animation->active_cycles = u32value;
  405. if(!flipper_file_read_uint32(ff, "Frame rate", &u32value, 1)) break;
  406. FURI_CONST_ASSIGN(animation->icon_animation.frame_rate, u32value);
  407. if(!flipper_file_read_uint32(ff, "Duration", &u32value, 1)) break;
  408. animation->duration = u32value;
  409. if(!flipper_file_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. string_clear(str);
  415. flipper_file_close(ff);
  416. flipper_file_free(ff);
  417. if(u32array) {
  418. free(u32array);
  419. }
  420. if(!success) {
  421. if(animation->frame_order) {
  422. free((void*)animation->frame_order);
  423. }
  424. free(animation);
  425. animation = NULL;
  426. }
  427. return animation;
  428. }
  429. static void animation_storage_free_bubbles(BubbleAnimation* animation) {
  430. if(!animation->frame_bubble_sequences) return;
  431. for(int i = 0; i < animation->frame_bubble_sequences_count;) {
  432. const FrameBubble* const* bubble = &animation->frame_bubble_sequences[i];
  433. if((*bubble) == NULL) break;
  434. while((*bubble)->next_bubble != NULL) {
  435. bubble = &(*bubble)->next_bubble;
  436. }
  437. if((*bubble)->bubble.text) {
  438. free((void*)(*bubble)->bubble.text);
  439. }
  440. if((*bubble) == animation->frame_bubble_sequences[i]) {
  441. ++i;
  442. }
  443. free((void*)*bubble);
  444. FURI_CONST_ASSIGN_PTR(*bubble, NULL);
  445. }
  446. free((void*)animation->frame_bubble_sequences);
  447. animation->frame_bubble_sequences = NULL;
  448. }