animation_storage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. free(*animation);
  175. *animation = NULL;
  176. }
  177. }
  178. void animation_storage_free_storage_animation(StorageAnimation** storage_animation) {
  179. furi_assert(storage_animation);
  180. furi_assert(*storage_animation);
  181. if((*storage_animation)->external) {
  182. animation_storage_free_animation((BubbleAnimation**)&(*storage_animation)->animation);
  183. if((*storage_animation)->manifest_info.name) {
  184. free((void*)(*storage_animation)->manifest_info.name);
  185. }
  186. free(*storage_animation);
  187. }
  188. *storage_animation = NULL;
  189. }
  190. static bool animation_storage_cast_align(string_t align_str, Align* align) {
  191. if(!string_cmp_str(align_str, "Bottom")) {
  192. *align = AlignBottom;
  193. } else if(!string_cmp_str(align_str, "Top")) {
  194. *align = AlignTop;
  195. } else if(!string_cmp_str(align_str, "Left")) {
  196. *align = AlignLeft;
  197. } else if(!string_cmp_str(align_str, "Right")) {
  198. *align = AlignRight;
  199. } else if(!string_cmp_str(align_str, "Center")) {
  200. *align = AlignCenter;
  201. } else {
  202. return false;
  203. }
  204. return true;
  205. }
  206. static void animation_storage_free_frames(BubbleAnimation* animation) {
  207. furi_assert(animation);
  208. const Icon* icon = &animation->icon_animation;
  209. for(int i = 0; i < icon->frame_count; ++i) {
  210. if(icon->frames[i]) {
  211. free((void*)icon->frames[i]);
  212. }
  213. }
  214. free(icon->frames);
  215. }
  216. static bool animation_storage_load_frames(
  217. Storage* storage,
  218. const char* name,
  219. BubbleAnimation* animation,
  220. uint32_t* frame_order,
  221. uint8_t width,
  222. uint8_t height) {
  223. uint16_t frame_order_size = animation->passive_frames + animation->active_frames;
  224. bool frames_ok = false;
  225. File* file = storage_file_alloc(storage);
  226. FileInfo file_info;
  227. string_t filename;
  228. string_init(filename);
  229. size_t max_filesize = ROUND_UP_TO(width, 8) * height + 1;
  230. Icon* icon = (Icon*)&animation->icon_animation;
  231. FURI_CONST_ASSIGN(icon->frame_count, frame_order_size);
  232. FURI_CONST_ASSIGN(icon->frame_rate, 0);
  233. FURI_CONST_ASSIGN(icon->height, height);
  234. FURI_CONST_ASSIGN(icon->width, width);
  235. icon->frames = furi_alloc(sizeof(const uint8_t*) * frame_order_size);
  236. for(int i = 0; i < frame_order_size; ++i) {
  237. frames_ok = false;
  238. string_printf(filename, ANIMATION_DIR "/%s/frame_%d.bm", name, frame_order[i]);
  239. if(storage_common_stat(storage, string_get_cstr(filename), &file_info) != FSE_OK) break;
  240. if(file_info.size > max_filesize) {
  241. FURI_LOG_E(
  242. TAG,
  243. "Filesize %d, max: %d (width %d, height %d)",
  244. file_info.size,
  245. max_filesize,
  246. width,
  247. height);
  248. break;
  249. }
  250. if(!storage_file_open(file, string_get_cstr(filename), FSAM_READ, FSOM_OPEN_EXISTING)) {
  251. FURI_LOG_E(TAG, "Can't open file \'%s\'", string_get_cstr(filename));
  252. break;
  253. }
  254. icon->frames[i] = furi_alloc(file_info.size);
  255. if(storage_file_read(file, (void*)icon->frames[i], file_info.size) != file_info.size) {
  256. FURI_LOG_E(TAG, "Read failed: \'%s\'", string_get_cstr(filename));
  257. break;
  258. }
  259. storage_file_close(file);
  260. frames_ok = true;
  261. }
  262. if(!frames_ok) {
  263. FURI_LOG_E(
  264. TAG,
  265. "Load \'%s\' failed, %dx%d, size: %d",
  266. string_get_cstr(filename),
  267. width,
  268. height,
  269. file_info.size);
  270. animation_storage_free_frames(animation);
  271. } else {
  272. furi_check(animation->icon_animation.frames);
  273. for(int i = 0; i < animation->icon_animation.frame_count; ++i) {
  274. furi_check(animation->icon_animation.frames[i]);
  275. }
  276. }
  277. storage_file_free(file);
  278. string_clear(filename);
  279. return frames_ok;
  280. }
  281. static bool animation_storage_load_bubbles(BubbleAnimation* animation, FlipperFile* ff) {
  282. uint32_t u32value;
  283. string_t str;
  284. string_init(str);
  285. bool success = false;
  286. furi_assert(!animation->frame_bubble_sequences);
  287. do {
  288. if(!flipper_file_read_uint32(ff, "Bubble slots", &u32value, 1)) break;
  289. if(u32value > 20) break;
  290. animation->frame_bubble_sequences_count = u32value;
  291. if(animation->frame_bubble_sequences_count == 0) {
  292. animation->frame_bubble_sequences = NULL;
  293. success = true;
  294. break;
  295. }
  296. animation->frame_bubble_sequences =
  297. furi_alloc(sizeof(FrameBubble*) * animation->frame_bubble_sequences_count);
  298. uint32_t current_slot = 0;
  299. for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
  300. FURI_CONST_ASSIGN_PTR(
  301. animation->frame_bubble_sequences[i], furi_alloc(sizeof(FrameBubble)));
  302. }
  303. const FrameBubble* bubble = animation->frame_bubble_sequences[0];
  304. int8_t index = -1;
  305. for(;;) {
  306. if(!flipper_file_read_uint32(ff, "Slot", &current_slot, 1)) break;
  307. if((current_slot != 0) && (index == -1)) break;
  308. if(current_slot == index) {
  309. FURI_CONST_ASSIGN_PTR(bubble->next_bubble, furi_alloc(sizeof(FrameBubble)));
  310. bubble = bubble->next_bubble;
  311. } else if(current_slot == index + 1) {
  312. ++index;
  313. bubble = animation->frame_bubble_sequences[index];
  314. } else {
  315. /* slots have to start from 0, be ascending sorted, and
  316. * have exact number of slots as specified in "Bubble slots" */
  317. break;
  318. }
  319. if(index >= animation->frame_bubble_sequences_count) break;
  320. if(!flipper_file_read_uint32(ff, "X", &u32value, 1)) break;
  321. FURI_CONST_ASSIGN(bubble->bubble.x, u32value);
  322. if(!flipper_file_read_uint32(ff, "Y", &u32value, 1)) break;
  323. FURI_CONST_ASSIGN(bubble->bubble.y, u32value);
  324. if(!flipper_file_read_string(ff, "Text", str)) break;
  325. if(string_size(str) > 100) break;
  326. string_replace_all_str(str, "\\n", "\n");
  327. FURI_CONST_ASSIGN_PTR(bubble->bubble.text, furi_alloc(string_size(str) + 1));
  328. strcpy((char*)bubble->bubble.text, string_get_cstr(str));
  329. if(!flipper_file_read_string(ff, "AlignH", str)) break;
  330. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_h)) break;
  331. if(!flipper_file_read_string(ff, "AlignV", str)) break;
  332. if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_v)) break;
  333. if(!flipper_file_read_uint32(ff, "StartFrame", &u32value, 1)) break;
  334. FURI_CONST_ASSIGN(bubble->start_frame, u32value);
  335. if(!flipper_file_read_uint32(ff, "EndFrame", &u32value, 1)) break;
  336. FURI_CONST_ASSIGN(bubble->end_frame, u32value);
  337. }
  338. success = (index + 1) == animation->frame_bubble_sequences_count;
  339. } while(0);
  340. if(!success) {
  341. if(animation->frame_bubble_sequences) {
  342. FURI_LOG_E(TAG, "Failed to load animation bubbles");
  343. animation_storage_free_bubbles(animation);
  344. }
  345. }
  346. string_clear(str);
  347. return success;
  348. }
  349. static BubbleAnimation* animation_storage_load_animation(const char* name) {
  350. furi_assert(name);
  351. BubbleAnimation* animation = furi_alloc(sizeof(BubbleAnimation));
  352. uint32_t height = 0;
  353. uint32_t width = 0;
  354. uint32_t* u32array = NULL;
  355. Storage* storage = furi_record_open("storage");
  356. FlipperFile* ff = flipper_file_alloc(storage);
  357. /* Forbid skipping fields */
  358. flipper_file_set_strict_mode(ff, true);
  359. string_t str;
  360. string_init(str);
  361. animation->frame_bubble_sequences = NULL;
  362. bool success = false;
  363. do {
  364. uint32_t u32value;
  365. if(FSE_OK != storage_sd_status(storage)) break;
  366. string_printf(str, ANIMATION_DIR "/%s/" ANIMATION_META_FILE, name);
  367. if(!flipper_file_open_existing(ff, string_get_cstr(str))) break;
  368. if(!flipper_file_read_header(ff, str, &u32value)) break;
  369. if(string_cmp_str(str, "Flipper Animation")) break;
  370. if(!flipper_file_read_uint32(ff, "Width", &width, 1)) break;
  371. if(!flipper_file_read_uint32(ff, "Height", &height, 1)) break;
  372. if(!flipper_file_read_uint32(ff, "Passive frames", &u32value, 1)) break;
  373. animation->passive_frames = u32value;
  374. if(!flipper_file_read_uint32(ff, "Active frames", &u32value, 1)) break;
  375. animation->active_frames = u32value;
  376. uint8_t frames = animation->passive_frames + animation->active_frames;
  377. uint32_t count = 0;
  378. if(!flipper_file_get_value_count(ff, "Frames order", &count)) break;
  379. if((count != frames) || (frames > COUNT_OF(animation->frame_order))) {
  380. FURI_LOG_E(TAG, "Error loading animation: frames order");
  381. break;
  382. }
  383. u32array = furi_alloc(sizeof(uint32_t) * frames);
  384. if(!flipper_file_read_uint32(ff, "Frames order", u32array, frames)) break;
  385. for(int i = 0; i < frames; ++i) {
  386. animation->frame_order[i] = u32array[i];
  387. }
  388. /* passive and active frames must be loaded up to this point */
  389. if(!animation_storage_load_frames(storage, name, animation, u32array, width, height))
  390. break;
  391. if(!flipper_file_read_uint32(ff, "Active cycles", &u32value, 1)) break;
  392. animation->active_cycles = u32value;
  393. if(!flipper_file_read_uint32(ff, "Frame rate", &u32value, 1)) break;
  394. FURI_CONST_ASSIGN(animation->icon_animation.frame_rate, u32value);
  395. if(!flipper_file_read_uint32(ff, "Duration", &u32value, 1)) break;
  396. animation->duration = u32value;
  397. if(!flipper_file_read_uint32(ff, "Active cooldown", &u32value, 1)) break;
  398. animation->active_cooldown = u32value;
  399. if(!animation_storage_load_bubbles(animation, ff)) break;
  400. success = true;
  401. } while(0);
  402. string_clear(str);
  403. flipper_file_close(ff);
  404. flipper_file_free(ff);
  405. if(u32array) {
  406. free(u32array);
  407. }
  408. if(!success) {
  409. free(animation);
  410. animation = NULL;
  411. }
  412. return animation;
  413. }
  414. static void animation_storage_free_bubbles(BubbleAnimation* animation) {
  415. if(!animation->frame_bubble_sequences) return;
  416. for(int i = 0; i < animation->frame_bubble_sequences_count;) {
  417. const FrameBubble* const* bubble = &animation->frame_bubble_sequences[i];
  418. if((*bubble) == NULL) break;
  419. while((*bubble)->next_bubble != NULL) {
  420. bubble = &(*bubble)->next_bubble;
  421. }
  422. if((*bubble)->bubble.text) {
  423. free((void*)(*bubble)->bubble.text);
  424. }
  425. if((*bubble) == animation->frame_bubble_sequences[i]) {
  426. ++i;
  427. }
  428. free((void*)*bubble);
  429. FURI_CONST_ASSIGN_PTR(*bubble, NULL);
  430. }
  431. free((void*)animation->frame_bubble_sequences);
  432. animation->frame_bubble_sequences = NULL;
  433. }