storage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #include <game/storage.h>
  2. static bool save_uint32(const char *path_name, uint32_t value)
  3. {
  4. char buffer[32];
  5. snprintf(buffer, sizeof(buffer), "%lu", value);
  6. return save_char(path_name, buffer);
  7. }
  8. // Helper function to save an int8_t
  9. static bool save_int8(const char *path_name, int8_t value)
  10. {
  11. char buffer[32];
  12. snprintf(buffer, sizeof(buffer), "%d", value);
  13. return save_char(path_name, buffer);
  14. }
  15. // Helper function to save a float
  16. static bool save_float(const char *path_name, float value)
  17. {
  18. char buffer[32];
  19. snprintf(buffer, sizeof(buffer), "%.6f", (double)value); // Limit to 6 decimal places
  20. return save_char(path_name, buffer);
  21. }
  22. bool save_player_context(PlayerContext *player_context)
  23. {
  24. if (!player_context)
  25. {
  26. FURI_LOG_E(TAG, "Invalid player context");
  27. return false;
  28. }
  29. // Create the directory for saving settings
  30. char directory_path[256];
  31. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player");
  32. // Create the directory
  33. Storage *storage = furi_record_open(RECORD_STORAGE);
  34. storage_common_mkdir(storage, directory_path);
  35. furi_record_close(RECORD_STORAGE);
  36. // 1. Username (String)
  37. if (!save_char("player/username", player_context->username))
  38. {
  39. FURI_LOG_E(TAG, "Failed to save player username");
  40. return false;
  41. }
  42. // 2. Level (uint32_t)
  43. if (!save_uint32("player/level", player_context->level))
  44. {
  45. FURI_LOG_E(TAG, "Failed to save player level");
  46. return false;
  47. }
  48. // 3. XP (uint32_t)
  49. if (!save_uint32("player/xp", player_context->xp))
  50. {
  51. FURI_LOG_E(TAG, "Failed to save player xp");
  52. return false;
  53. }
  54. // 4. Health (uint32_t)
  55. if (!save_uint32("player/health", player_context->health))
  56. {
  57. FURI_LOG_E(TAG, "Failed to save player health");
  58. return false;
  59. }
  60. // 5. Strength (uint32_t)
  61. if (!save_uint32("player/strength", player_context->strength))
  62. {
  63. FURI_LOG_E(TAG, "Failed to save player strength");
  64. return false;
  65. }
  66. // 6. Max Health (uint32_t)
  67. if (!save_uint32("player/max_health", player_context->max_health))
  68. {
  69. FURI_LOG_E(TAG, "Failed to save player max health");
  70. return false;
  71. }
  72. // 7. Health Regen (uint32_t)
  73. if (!save_uint32("player/health_regen", player_context->health_regen))
  74. {
  75. FURI_LOG_E(TAG, "Failed to save player health regen");
  76. return false;
  77. }
  78. // 8. Elapsed Health Regen (float)
  79. if (!save_float("player/elapsed_health_regen", player_context->elapsed_health_regen))
  80. {
  81. FURI_LOG_E(TAG, "Failed to save player elapsed health regen");
  82. return false;
  83. }
  84. // 9. Attack Timer (float)
  85. if (!save_float("player/attack_timer", player_context->attack_timer))
  86. {
  87. FURI_LOG_E(TAG, "Failed to save player attack timer");
  88. return false;
  89. }
  90. // 10. Elapsed Attack Timer (float)
  91. if (!save_float("player/elapsed_attack_timer", player_context->elapsed_attack_timer))
  92. {
  93. FURI_LOG_E(TAG, "Failed to save player elapsed attack timer");
  94. return false;
  95. }
  96. // 11. Direction (enum PlayerDirection)
  97. {
  98. char direction_str[2];
  99. switch (player_context->direction)
  100. {
  101. case PLAYER_UP:
  102. strncpy(direction_str, "0", sizeof(direction_str));
  103. break;
  104. case PLAYER_DOWN:
  105. strncpy(direction_str, "1", sizeof(direction_str));
  106. break;
  107. case PLAYER_LEFT:
  108. strncpy(direction_str, "2", sizeof(direction_str));
  109. break;
  110. case PLAYER_RIGHT:
  111. default:
  112. strncpy(direction_str, "3", sizeof(direction_str));
  113. break;
  114. }
  115. direction_str[1] = '\0'; // Ensure null termination
  116. if (!save_char("player/direction", direction_str))
  117. {
  118. FURI_LOG_E(TAG, "Failed to save player direction");
  119. return false;
  120. }
  121. }
  122. // 12. State (enum PlayerState)
  123. {
  124. char state_str[2];
  125. switch (player_context->state)
  126. {
  127. case PLAYER_IDLE:
  128. strncpy(state_str, "0", sizeof(state_str));
  129. break;
  130. case PLAYER_MOVING:
  131. strncpy(state_str, "1", sizeof(state_str));
  132. break;
  133. case PLAYER_ATTACKING:
  134. strncpy(state_str, "2", sizeof(state_str));
  135. break;
  136. case PLAYER_ATTACKED:
  137. strncpy(state_str, "3", sizeof(state_str));
  138. break;
  139. case PLAYER_DEAD:
  140. strncpy(state_str, "4", sizeof(state_str));
  141. break;
  142. default:
  143. strncpy(state_str, "5", sizeof(state_str)); // Assuming '5' for unknown states
  144. break;
  145. }
  146. state_str[1] = '\0'; // Ensure null termination
  147. if (!save_char("player/state", state_str))
  148. {
  149. FURI_LOG_E(TAG, "Failed to save player state");
  150. return false;
  151. }
  152. }
  153. // 13. Start Position X (float)
  154. if (!save_float("player/start_position_x", player_context->start_position.x))
  155. {
  156. FURI_LOG_E(TAG, "Failed to save player start position x");
  157. return false;
  158. }
  159. // 14. Start Position Y (float)
  160. if (!save_float("player/start_position_y", player_context->start_position.y))
  161. {
  162. FURI_LOG_E(TAG, "Failed to save player start position y");
  163. return false;
  164. }
  165. // 15. dx (int8_t)
  166. if (!save_int8("player/dx", player_context->dx))
  167. {
  168. FURI_LOG_E(TAG, "Failed to save player dx");
  169. return false;
  170. }
  171. // 16. dy (int8_t)
  172. if (!save_int8("player/dy", player_context->dy))
  173. {
  174. FURI_LOG_E(TAG, "Failed to save player dy");
  175. return false;
  176. }
  177. return true;
  178. }
  179. // Helper function to load an integer
  180. static bool load_number(const char *path_name, int *value)
  181. {
  182. if (!path_name || !value)
  183. {
  184. FURI_LOG_E(TAG, "Invalid arguments to load_number");
  185. return false;
  186. }
  187. char buffer[64];
  188. if (!load_char(path_name, buffer, sizeof(buffer)))
  189. {
  190. FURI_LOG_E(TAG, "Failed to load number from path: %s", path_name);
  191. return false;
  192. }
  193. *value = atoi(buffer);
  194. return true;
  195. }
  196. // Helper function to load a float
  197. static bool load_float(const char *path_name, float *value)
  198. {
  199. if (!path_name || !value)
  200. {
  201. FURI_LOG_E(TAG, "Invalid arguments to load_float");
  202. return false;
  203. }
  204. char buffer[64];
  205. if (!load_char(path_name, buffer, sizeof(buffer)))
  206. {
  207. FURI_LOG_E(TAG, "Failed to load float from path: %s", path_name);
  208. return false;
  209. }
  210. // check if the string is a valid float
  211. char *endptr;
  212. *value = strtof(buffer, &endptr);
  213. if (endptr == buffer)
  214. {
  215. FURI_LOG_E(TAG, "Failed to parse float from path: %s", path_name);
  216. return false;
  217. }
  218. return true;
  219. }
  220. // Helper function to load an int8_t
  221. static bool load_int8(const char *path_name, int8_t *value)
  222. {
  223. if (!path_name || !value)
  224. {
  225. FURI_LOG_E(TAG, "Invalid arguments to load_int8");
  226. return false;
  227. }
  228. char buffer[64];
  229. if (!load_char(path_name, buffer, sizeof(buffer)))
  230. {
  231. FURI_LOG_E(TAG, "Failed to load int8 from path: %s", path_name);
  232. return false;
  233. }
  234. long temp = strtol(buffer, NULL, 10);
  235. if (temp < INT8_MIN || temp > INT8_MAX)
  236. {
  237. FURI_LOG_E(TAG, "Value out of range for int8: %ld", temp);
  238. return false;
  239. }
  240. // check if the string is a valid int8
  241. char *endptr;
  242. *value = (int8_t)strtol(buffer, &endptr, 10);
  243. if (endptr == buffer)
  244. {
  245. FURI_LOG_E(TAG, "Failed to parse int8 from path: %s", path_name);
  246. return false;
  247. }
  248. return true;
  249. }
  250. // Helper function to load a uint32_t
  251. static bool load_uint32(const char *path_name, uint32_t *value)
  252. {
  253. if (!path_name || !value)
  254. {
  255. FURI_LOG_E(TAG, "Invalid arguments to load_uint32");
  256. return false;
  257. }
  258. char buffer[64];
  259. if (!load_char(path_name, buffer, sizeof(buffer)))
  260. {
  261. FURI_LOG_E(TAG, "Failed to load uint32 from path: %s", path_name);
  262. return false;
  263. }
  264. // check if the string is a valid uint32
  265. char *endptr;
  266. *value = strtoul(buffer, &endptr, 10);
  267. if (endptr == buffer)
  268. {
  269. FURI_LOG_E(TAG, "Failed to parse uint32 from path: %s", path_name);
  270. return false;
  271. }
  272. return true;
  273. }
  274. bool load_player_context(PlayerContext *player_context)
  275. {
  276. if (!player_context)
  277. {
  278. FURI_LOG_E(TAG, "Invalid player context");
  279. return false;
  280. }
  281. // 1. Username (String)
  282. if (!load_char("player/username", player_context->username, sizeof(player_context->username)))
  283. {
  284. FURI_LOG_E(TAG, "No data or parse error for username. Using default: 'Unknown'");
  285. memset(player_context->username, 0, sizeof(player_context->username));
  286. strncpy(player_context->username, "Unknown", sizeof(player_context->username) - 1);
  287. }
  288. // 2. Level (uint32_t)
  289. {
  290. uint32_t temp = 1; // Default
  291. if (!load_char("player/level", (char *)&temp, sizeof(temp)))
  292. {
  293. FURI_LOG_E(TAG, "No data or parse error for level. Using default: 1");
  294. }
  295. else
  296. {
  297. // char buffer[64];
  298. if (load_uint32("player/level", &temp))
  299. {
  300. player_context->level = temp;
  301. }
  302. else
  303. {
  304. FURI_LOG_E(TAG, "Failed to parse level. Using default: 1");
  305. player_context->level = 1;
  306. }
  307. }
  308. player_context->level = temp;
  309. }
  310. // 3. XP (uint32_t)
  311. {
  312. uint32_t temp = 0; // Default
  313. if (!load_uint32("player/xp", &temp))
  314. {
  315. FURI_LOG_E(TAG, "No data or parse error for xp. Using default: 0");
  316. temp = 0;
  317. }
  318. player_context->xp = temp;
  319. }
  320. // 4. Health (uint32_t)
  321. {
  322. uint32_t temp = 100; // Default
  323. if (!load_uint32("player/health", &temp))
  324. {
  325. FURI_LOG_E(TAG, "No data or parse error for health. Using default: 100");
  326. temp = 100;
  327. }
  328. player_context->health = temp;
  329. }
  330. // 5. Strength (uint32_t)
  331. {
  332. uint32_t temp = 10; // Default
  333. if (!load_uint32("player/strength", &temp))
  334. {
  335. FURI_LOG_E(TAG, "No data or parse error for strength. Using default: 10");
  336. temp = 10;
  337. }
  338. player_context->strength = temp;
  339. }
  340. // 6. Max Health (uint32_t)
  341. {
  342. uint32_t temp = 100; // Default
  343. if (!load_uint32("player/max_health", &temp))
  344. {
  345. FURI_LOG_E(TAG, "No data or parse error for max_health. Using default: 100");
  346. temp = 100;
  347. }
  348. player_context->max_health = temp;
  349. }
  350. // 7. Health Regen (uint32_t)
  351. {
  352. uint32_t temp = 1; // Default
  353. if (!load_uint32("player/health_regen", &temp))
  354. {
  355. FURI_LOG_E(TAG, "No data or parse error for health_regen. Using default: 1");
  356. temp = 1;
  357. }
  358. player_context->health_regen = temp;
  359. }
  360. // 8. Elapsed Health Regen (float)
  361. {
  362. float temp = 0.0f; // Default
  363. if (!load_float("player/elapsed_health_regen", &temp))
  364. {
  365. FURI_LOG_E(TAG, "No data or parse error for elapsed_health_regen. Using default: 0.0f");
  366. temp = 0.0f;
  367. }
  368. player_context->elapsed_health_regen = temp;
  369. }
  370. // 9. Attack Timer (float)
  371. {
  372. float temp = 0.1f; // Default
  373. if (!load_float("player/attack_timer", &temp))
  374. {
  375. FURI_LOG_E(TAG, "No data or parse error for attack_timer. Using default: 0.1f");
  376. temp = 0.1f;
  377. }
  378. player_context->attack_timer = temp;
  379. }
  380. // 10. Elapsed Attack Timer (float)
  381. {
  382. float temp = 0.0f; // Default
  383. if (!load_float("player/elapsed_attack_timer", &temp))
  384. {
  385. FURI_LOG_E(TAG, "No data or parse error for elapsed_attack_timer. Using default: 0.0f");
  386. temp = 0.0f;
  387. }
  388. player_context->elapsed_attack_timer = temp;
  389. }
  390. // 11. Direction (enum PlayerDirection)
  391. {
  392. int direction_int = 3; // Default to PLAYER_RIGHT
  393. if (!load_number("player/direction", &direction_int))
  394. {
  395. FURI_LOG_E(TAG, "No data or parse error for direction. Defaulting to PLAYER_RIGHT");
  396. direction_int = 3;
  397. }
  398. switch (direction_int)
  399. {
  400. case 0:
  401. player_context->direction = PLAYER_UP;
  402. break;
  403. case 1:
  404. player_context->direction = PLAYER_DOWN;
  405. break;
  406. case 2:
  407. player_context->direction = PLAYER_LEFT;
  408. break;
  409. case 3:
  410. player_context->direction = PLAYER_RIGHT;
  411. break;
  412. default:
  413. FURI_LOG_E(TAG, "Invalid direction value: %d. Defaulting to PLAYER_RIGHT", direction_int);
  414. player_context->direction = PLAYER_RIGHT;
  415. break;
  416. }
  417. }
  418. // 12. State (enum PlayerState)
  419. {
  420. int state_int = 0; // Default to PLAYER_IDLE
  421. if (!load_number("player/state", &state_int))
  422. {
  423. FURI_LOG_E(TAG, "No data or parse error for state. Defaulting to PLAYER_IDLE");
  424. state_int = 0;
  425. }
  426. switch (state_int)
  427. {
  428. case 0:
  429. player_context->state = PLAYER_IDLE;
  430. break;
  431. case 1:
  432. player_context->state = PLAYER_MOVING;
  433. break;
  434. case 2:
  435. player_context->state = PLAYER_ATTACKING;
  436. break;
  437. case 3:
  438. player_context->state = PLAYER_ATTACKED;
  439. break;
  440. case 4:
  441. player_context->state = PLAYER_DEAD;
  442. break;
  443. default:
  444. FURI_LOG_E(TAG, "Invalid state value: %d. Defaulting to PLAYER_IDLE", state_int);
  445. player_context->state = PLAYER_IDLE;
  446. break;
  447. }
  448. }
  449. // 13. Start Position X (float)
  450. {
  451. float temp = 192.0f; // Default
  452. if (!load_float("player/start_position_x", &temp))
  453. {
  454. FURI_LOG_E(TAG, "No data or parse error for start_position_x. Using default: 192.0f");
  455. temp = 192.0f;
  456. }
  457. player_context->start_position.x = temp;
  458. }
  459. // 14. Start Position Y (float)
  460. {
  461. float temp = 96.0f; // Default
  462. if (!load_float("player/start_position_y", &temp))
  463. {
  464. FURI_LOG_E(TAG, "No data or parse error for start_position_y. Using default: 96.0f");
  465. temp = 96.0f;
  466. }
  467. player_context->start_position.y = temp;
  468. }
  469. // 15. dx (int8_t)
  470. {
  471. int8_t temp = 1; // Default
  472. if (!load_int8("player/dx", &temp))
  473. {
  474. FURI_LOG_E(TAG, "No data or parse error for dx. Using default: 1");
  475. temp = 1;
  476. }
  477. player_context->dx = temp;
  478. }
  479. // 16. dy (int8_t)
  480. {
  481. int8_t temp = 0; // Default
  482. if (!load_int8("player/dy", &temp))
  483. {
  484. FURI_LOG_E(TAG, "No data or parse error for dy. Using default: 0");
  485. temp = 0;
  486. }
  487. player_context->dy = temp;
  488. }
  489. return true;
  490. }
  491. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  492. {
  493. furi_string_replace_str(string, needle, "", 0);
  494. }
  495. static FuriString *enemy_data(const FuriString *world_data)
  496. {
  497. size_t enemy_data_pos = furi_string_search_str(world_data, "enemy_data", 0);
  498. if (enemy_data_pos == FURI_STRING_FAILURE)
  499. {
  500. FURI_LOG_E("Game", "Failed to find enemy_data in world data");
  501. return NULL;
  502. }
  503. size_t bracket_start = furi_string_search_char(world_data, '[', enemy_data_pos);
  504. if (bracket_start == FURI_STRING_FAILURE)
  505. {
  506. FURI_LOG_E("Game", "Failed to find start of enemy_data array");
  507. return NULL;
  508. }
  509. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  510. if (bracket_end == FURI_STRING_FAILURE)
  511. {
  512. FURI_LOG_E("Game", "Failed to find end of enemy_data array");
  513. return NULL;
  514. }
  515. FuriString *enemy_data_str = furi_string_alloc();
  516. if (!enemy_data_str)
  517. {
  518. FURI_LOG_E("Game", "Failed to allocate enemy_data string");
  519. return NULL;
  520. }
  521. furi_string_cat_str(enemy_data_str, "{\"enemy_data\":");
  522. {
  523. FuriString *temp_sub = furi_string_alloc();
  524. furi_string_set_strn(
  525. temp_sub,
  526. furi_string_get_cstr(world_data) + bracket_start,
  527. (bracket_end + 1) - bracket_start);
  528. furi_string_cat(enemy_data_str, temp_sub);
  529. furi_string_free(temp_sub);
  530. }
  531. furi_string_cat_str(enemy_data_str, "}");
  532. return enemy_data_str;
  533. }
  534. static FuriString *json_data(const FuriString *world_data)
  535. {
  536. size_t json_data_pos = furi_string_search_str(world_data, "json_data", 0);
  537. if (json_data_pos == FURI_STRING_FAILURE)
  538. {
  539. FURI_LOG_E("Game", "Failed to find json_data in world data");
  540. return NULL;
  541. }
  542. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  543. if (bracket_start == FURI_STRING_FAILURE)
  544. {
  545. FURI_LOG_E("Game", "Failed to find start of json_data array");
  546. return NULL;
  547. }
  548. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  549. if (bracket_end == FURI_STRING_FAILURE)
  550. {
  551. FURI_LOG_E("Game", "Failed to find end of json_data array");
  552. return NULL;
  553. }
  554. FuriString *json_data_str = furi_string_alloc();
  555. if (!json_data_str)
  556. {
  557. FURI_LOG_E("Game", "Failed to allocate json_data string");
  558. return NULL;
  559. }
  560. furi_string_cat_str(json_data_str, "{\"json_data\":");
  561. {
  562. FuriString *temp_sub = furi_string_alloc();
  563. furi_string_set_strn(
  564. temp_sub,
  565. furi_string_get_cstr(world_data) + bracket_start,
  566. (bracket_end + 1) - bracket_start);
  567. furi_string_cat(json_data_str, temp_sub);
  568. furi_string_free(temp_sub);
  569. }
  570. furi_string_cat_str(json_data_str, "}");
  571. return json_data_str;
  572. }
  573. bool separate_world_data(char *id, FuriString *world_data)
  574. {
  575. if (!id || !world_data)
  576. {
  577. FURI_LOG_E("Game", "Invalid parameters");
  578. return false;
  579. }
  580. FuriString *file_json_data = json_data(world_data);
  581. if (!file_json_data || furi_string_size(file_json_data) == 0)
  582. {
  583. FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
  584. return false;
  585. }
  586. // Save file_json_data to disk
  587. char directory_path[256];
  588. snprintf(directory_path, sizeof(directory_path),
  589. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  590. Storage *storage = furi_record_open(RECORD_STORAGE);
  591. storage_common_mkdir(storage, directory_path);
  592. File *file = storage_file_alloc(storage);
  593. char file_path[256];
  594. snprintf(file_path, sizeof(file_path),
  595. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  596. id, id);
  597. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  598. {
  599. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  600. storage_file_free(file);
  601. furi_record_close(RECORD_STORAGE);
  602. furi_string_free(file_json_data);
  603. return false;
  604. }
  605. size_t data_size = furi_string_size(file_json_data);
  606. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  607. {
  608. FURI_LOG_E("Game", "Failed to write json_data");
  609. }
  610. storage_file_close(file);
  611. furi_string_replace_at(file_json_data, 0, 1, "");
  612. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  613. // include the comma at the end of the json_data array
  614. furi_string_cat_str(file_json_data, ",");
  615. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  616. furi_string_free(file_json_data);
  617. FuriString *file_enemy_data = enemy_data(world_data);
  618. if (!file_enemy_data)
  619. {
  620. FURI_LOG_E("Game", "Failed to get enemy data");
  621. return false;
  622. }
  623. snprintf(file_path, sizeof(file_path),
  624. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  625. id, id);
  626. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  627. {
  628. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  629. storage_file_free(file);
  630. furi_record_close(RECORD_STORAGE);
  631. furi_string_free(file_enemy_data);
  632. return false;
  633. }
  634. data_size = furi_string_size(file_enemy_data);
  635. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  636. {
  637. FURI_LOG_E("Game", "Failed to write enemy_data");
  638. }
  639. // Clean up
  640. furi_string_free(file_enemy_data);
  641. storage_file_close(file);
  642. storage_file_free(file);
  643. furi_record_close(RECORD_STORAGE);
  644. return true;
  645. }