storage.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. // loads from STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json
  492. // then gets each key-value pair and saves it as it's own file so it can be loaded separately using
  493. // load_player_context
  494. bool set_player_context()
  495. {
  496. char file_path[256];
  497. snprintf(file_path, sizeof(file_path),
  498. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  499. FuriString *player_stats = flipper_http_load_from_file(file_path);
  500. if (!player_stats)
  501. {
  502. FURI_LOG_E(TAG, "Failed to load player stats from file: %s", file_path);
  503. return false;
  504. }
  505. // Get the key one-by-one and save it to a separate file
  506. // 1. Username (String)
  507. FuriString *username = get_json_value_furi("username", player_stats);
  508. if (username)
  509. {
  510. save_char("player/username", furi_string_get_cstr(username));
  511. furi_string_free(username);
  512. }
  513. // 2. Level (uint32_t)
  514. FuriString *level = get_json_value_furi("level", player_stats);
  515. if (level)
  516. {
  517. save_uint32("player/level", atoi(furi_string_get_cstr(level)));
  518. furi_string_free(level);
  519. }
  520. // 3. XP (uint32_t)
  521. FuriString *xp = get_json_value_furi("xp", player_stats);
  522. if (xp)
  523. {
  524. save_uint32("player/xp", atoi(furi_string_get_cstr(xp)));
  525. furi_string_free(xp);
  526. }
  527. // 4. Health (uint32_t)
  528. FuriString *health = get_json_value_furi("health", player_stats);
  529. if (health)
  530. {
  531. save_uint32("player/health", atoi(furi_string_get_cstr(health)));
  532. furi_string_free(health);
  533. }
  534. // 5. Strength (uint32_t)
  535. FuriString *strength = get_json_value_furi("strength", player_stats);
  536. if (strength)
  537. {
  538. save_uint32("player/strength", atoi(furi_string_get_cstr(strength)));
  539. furi_string_free(strength);
  540. }
  541. // 6. Max Health (uint32_t)
  542. FuriString *max_health = get_json_value_furi("max_health", player_stats);
  543. if (max_health)
  544. {
  545. save_uint32("player/max_health", atoi(furi_string_get_cstr(max_health)));
  546. furi_string_free(max_health);
  547. }
  548. // 7. Health Regen (uint32_t)
  549. FuriString *health_regen = get_json_value_furi("health_regen", player_stats);
  550. if (health_regen)
  551. {
  552. save_uint32("player/health_regen", atoi(furi_string_get_cstr(health_regen)));
  553. furi_string_free(health_regen);
  554. }
  555. // 8. Elapsed Health Regen (float)
  556. FuriString *elapsed_health_regen = get_json_value_furi("elapsed_health_regen", player_stats);
  557. if (elapsed_health_regen)
  558. {
  559. save_float("player/elapsed_health_regen", strtof(furi_string_get_cstr(elapsed_health_regen), NULL));
  560. furi_string_free(elapsed_health_regen);
  561. }
  562. // 9. Attack Timer (float)
  563. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  564. if (attack_timer)
  565. {
  566. save_float("player/attack_timer", strtof(furi_string_get_cstr(attack_timer), NULL));
  567. furi_string_free(attack_timer);
  568. }
  569. // 10. Elapsed Attack Timer (float)
  570. FuriString *elapsed_attack_timer = get_json_value_furi("elapsed_attack_timer", player_stats);
  571. if (elapsed_attack_timer)
  572. {
  573. save_float("player/elapsed_attack_timer", strtof(furi_string_get_cstr(elapsed_attack_timer), NULL));
  574. furi_string_free(elapsed_attack_timer);
  575. }
  576. // 11. Direction (enum PlayerDirection)
  577. FuriString *direction = get_json_value_furi("direction", player_stats);
  578. if (direction)
  579. {
  580. save_char("player/direction", furi_string_get_cstr(direction));
  581. furi_string_free(direction);
  582. }
  583. // 12. State (enum PlayerState)
  584. FuriString *state = get_json_value_furi("state", player_stats);
  585. if (state)
  586. {
  587. save_char("player/state", furi_string_get_cstr(state));
  588. furi_string_free(state);
  589. }
  590. // 13. Start Position X (float)
  591. FuriString *start_position_x = get_json_value_furi("start_position_x", player_stats);
  592. if (start_position_x)
  593. {
  594. save_float("player/start_position_x", strtof(furi_string_get_cstr(start_position_x), NULL));
  595. furi_string_free(start_position_x);
  596. }
  597. // 14. Start Position Y (float)
  598. FuriString *start_position_y = get_json_value_furi("start_position_y", player_stats);
  599. if (start_position_y)
  600. {
  601. save_float("player/start_position_y", strtof(furi_string_get_cstr(start_position_y), NULL));
  602. furi_string_free(start_position_y);
  603. }
  604. // 15. dx (int8_t)
  605. FuriString *dx = get_json_value_furi("dx", player_stats);
  606. if (dx)
  607. {
  608. save_int8("player/dx", atoi(furi_string_get_cstr(dx)));
  609. furi_string_free(dx);
  610. }
  611. // 16. dy (int8_t)
  612. FuriString *dy = get_json_value_furi("dy", player_stats);
  613. if (dy)
  614. {
  615. save_int8("player/dy", atoi(furi_string_get_cstr(dy)));
  616. furi_string_free(dy);
  617. }
  618. furi_string_free(player_stats);
  619. return true;
  620. }
  621. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  622. {
  623. furi_string_replace_str(string, needle, "", 0);
  624. }
  625. static FuriString *enemy_data(const FuriString *world_data)
  626. {
  627. size_t enemy_data_pos = furi_string_search_str(world_data, "enemy_data", 0);
  628. if (enemy_data_pos == FURI_STRING_FAILURE)
  629. {
  630. FURI_LOG_E("Game", "Failed to find enemy_data in world data");
  631. return NULL;
  632. }
  633. size_t bracket_start = furi_string_search_char(world_data, '[', enemy_data_pos);
  634. if (bracket_start == FURI_STRING_FAILURE)
  635. {
  636. FURI_LOG_E("Game", "Failed to find start of enemy_data array");
  637. return NULL;
  638. }
  639. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  640. if (bracket_end == FURI_STRING_FAILURE)
  641. {
  642. FURI_LOG_E("Game", "Failed to find end of enemy_data array");
  643. return NULL;
  644. }
  645. FuriString *enemy_data_str = furi_string_alloc();
  646. if (!enemy_data_str)
  647. {
  648. FURI_LOG_E("Game", "Failed to allocate enemy_data string");
  649. return NULL;
  650. }
  651. furi_string_cat_str(enemy_data_str, "{\"enemy_data\":");
  652. {
  653. FuriString *temp_sub = furi_string_alloc();
  654. furi_string_set_strn(
  655. temp_sub,
  656. furi_string_get_cstr(world_data) + bracket_start,
  657. (bracket_end + 1) - bracket_start);
  658. furi_string_cat(enemy_data_str, temp_sub);
  659. furi_string_free(temp_sub);
  660. }
  661. furi_string_cat_str(enemy_data_str, "}");
  662. return enemy_data_str;
  663. }
  664. static FuriString *json_data(const FuriString *world_data)
  665. {
  666. size_t json_data_pos = furi_string_search_str(world_data, "json_data", 0);
  667. if (json_data_pos == FURI_STRING_FAILURE)
  668. {
  669. FURI_LOG_E("Game", "Failed to find json_data in world data");
  670. return NULL;
  671. }
  672. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  673. if (bracket_start == FURI_STRING_FAILURE)
  674. {
  675. FURI_LOG_E("Game", "Failed to find start of json_data array");
  676. return NULL;
  677. }
  678. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  679. if (bracket_end == FURI_STRING_FAILURE)
  680. {
  681. FURI_LOG_E("Game", "Failed to find end of json_data array");
  682. return NULL;
  683. }
  684. FuriString *json_data_str = furi_string_alloc();
  685. if (!json_data_str)
  686. {
  687. FURI_LOG_E("Game", "Failed to allocate json_data string");
  688. return NULL;
  689. }
  690. furi_string_cat_str(json_data_str, "{\"json_data\":");
  691. {
  692. FuriString *temp_sub = furi_string_alloc();
  693. furi_string_set_strn(
  694. temp_sub,
  695. furi_string_get_cstr(world_data) + bracket_start,
  696. (bracket_end + 1) - bracket_start);
  697. furi_string_cat(json_data_str, temp_sub);
  698. furi_string_free(temp_sub);
  699. }
  700. furi_string_cat_str(json_data_str, "}");
  701. return json_data_str;
  702. }
  703. bool separate_world_data(char *id, FuriString *world_data)
  704. {
  705. if (!id || !world_data)
  706. {
  707. FURI_LOG_E("Game", "Invalid parameters");
  708. return false;
  709. }
  710. FuriString *file_json_data = json_data(world_data);
  711. if (!file_json_data || furi_string_size(file_json_data) == 0)
  712. {
  713. FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
  714. return false;
  715. }
  716. // Save file_json_data to disk
  717. char directory_path[256];
  718. snprintf(directory_path, sizeof(directory_path),
  719. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  720. Storage *storage = furi_record_open(RECORD_STORAGE);
  721. storage_common_mkdir(storage, directory_path);
  722. File *file = storage_file_alloc(storage);
  723. char file_path[256];
  724. snprintf(file_path, sizeof(file_path),
  725. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  726. id, id);
  727. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  728. {
  729. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  730. storage_file_free(file);
  731. furi_record_close(RECORD_STORAGE);
  732. furi_string_free(file_json_data);
  733. return false;
  734. }
  735. size_t data_size = furi_string_size(file_json_data);
  736. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  737. {
  738. FURI_LOG_E("Game", "Failed to write json_data");
  739. }
  740. storage_file_close(file);
  741. furi_string_replace_at(file_json_data, 0, 1, "");
  742. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  743. // include the comma at the end of the json_data array
  744. furi_string_cat_str(file_json_data, ",");
  745. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  746. furi_string_free(file_json_data);
  747. FuriString *file_enemy_data = enemy_data(world_data);
  748. if (!file_enemy_data)
  749. {
  750. FURI_LOG_E("Game", "Failed to get enemy data");
  751. return false;
  752. }
  753. snprintf(file_path, sizeof(file_path),
  754. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  755. id, id);
  756. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  757. {
  758. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  759. storage_file_free(file);
  760. furi_record_close(RECORD_STORAGE);
  761. furi_string_free(file_enemy_data);
  762. return false;
  763. }
  764. data_size = furi_string_size(file_enemy_data);
  765. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  766. {
  767. FURI_LOG_E("Game", "Failed to write enemy_data");
  768. }
  769. // Clean up
  770. furi_string_free(file_enemy_data);
  771. storage_file_close(file);
  772. storage_file_free(file);
  773. furi_record_close(RECORD_STORAGE);
  774. return true;
  775. }