storage.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. #include <game/storage.h>
  2. 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. 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. 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 a string safely
  180. bool load_string(const char *path_name, char *buffer, size_t buffer_size)
  181. {
  182. if (!path_name || !buffer || buffer_size == 0)
  183. {
  184. FURI_LOG_E(TAG, "Invalid arguments to load_string");
  185. return false;
  186. }
  187. if (!load_char(path_name, buffer, buffer_size))
  188. {
  189. FURI_LOG_E(TAG, "Failed to load string from path: %s", path_name);
  190. return false;
  191. }
  192. return true;
  193. }
  194. // Helper function to load an integer
  195. bool load_number(const char *path_name, int *value)
  196. {
  197. if (!path_name || !value)
  198. {
  199. FURI_LOG_E(TAG, "Invalid arguments to load_number");
  200. return false;
  201. }
  202. char buffer[64];
  203. if (!load_char(path_name, buffer, sizeof(buffer)))
  204. {
  205. FURI_LOG_E(TAG, "Failed to load number from path: %s", path_name);
  206. return false;
  207. }
  208. *value = atoi(buffer);
  209. return true;
  210. }
  211. // Helper function to load a float
  212. bool load_float(const char *path_name, float *value)
  213. {
  214. if (!path_name || !value)
  215. {
  216. FURI_LOG_E(TAG, "Invalid arguments to load_float");
  217. return false;
  218. }
  219. char buffer[64];
  220. if (!load_char(path_name, buffer, sizeof(buffer)))
  221. {
  222. FURI_LOG_E(TAG, "Failed to load float from path: %s", path_name);
  223. return false;
  224. }
  225. // check if the string is a valid float
  226. char *endptr;
  227. *value = strtof(buffer, &endptr);
  228. if (endptr == buffer)
  229. {
  230. FURI_LOG_E(TAG, "Failed to parse float from path: %s", path_name);
  231. return false;
  232. }
  233. return true;
  234. }
  235. // Helper function to load an int8_t
  236. bool load_int8(const char *path_name, int8_t *value)
  237. {
  238. if (!path_name || !value)
  239. {
  240. FURI_LOG_E(TAG, "Invalid arguments to load_int8");
  241. return false;
  242. }
  243. char buffer[64];
  244. if (!load_char(path_name, buffer, sizeof(buffer)))
  245. {
  246. FURI_LOG_E(TAG, "Failed to load int8 from path: %s", path_name);
  247. return false;
  248. }
  249. long temp = strtol(buffer, NULL, 10);
  250. if (temp < INT8_MIN || temp > INT8_MAX)
  251. {
  252. FURI_LOG_E(TAG, "Value out of range for int8: %ld", temp);
  253. return false;
  254. }
  255. // check if the string is a valid int8
  256. char *endptr;
  257. *value = (int8_t)strtol(buffer, &endptr, 10);
  258. if (endptr == buffer)
  259. {
  260. FURI_LOG_E(TAG, "Failed to parse int8 from path: %s", path_name);
  261. return false;
  262. }
  263. return true;
  264. }
  265. // Helper function to load a uint32_t
  266. bool load_uint32(const char *path_name, uint32_t *value)
  267. {
  268. if (!path_name || !value)
  269. {
  270. FURI_LOG_E(TAG, "Invalid arguments to load_uint32");
  271. return false;
  272. }
  273. char buffer[64];
  274. if (!load_char(path_name, buffer, sizeof(buffer)))
  275. {
  276. FURI_LOG_E(TAG, "Failed to load uint32 from path: %s", path_name);
  277. return false;
  278. }
  279. // check if the string is a valid uint32
  280. char *endptr;
  281. *value = strtoul(buffer, &endptr, 10);
  282. if (endptr == buffer)
  283. {
  284. FURI_LOG_E(TAG, "Failed to parse uint32 from path: %s", path_name);
  285. return false;
  286. }
  287. return true;
  288. }
  289. // 1. Username (String)
  290. static bool load_player_username(char *username, size_t username_size)
  291. {
  292. if (!username || username_size == 0)
  293. {
  294. FURI_LOG_E(TAG, "Invalid username buffer");
  295. return false;
  296. }
  297. // Try to load the string
  298. if (!load_string("player/username", username, username_size))
  299. {
  300. // If loading failed, log a warning and set a default
  301. FURI_LOG_W(TAG, "No data or parse error for username. Using default: 'Unknown'");
  302. memset(username, 0, username_size);
  303. strncpy(username, "Unknown", username_size - 1);
  304. }
  305. // Always return true unless pointer was invalid
  306. return true;
  307. }
  308. // 2. Level (uint32_t)
  309. static bool load_player_level(uint32_t *level)
  310. {
  311. if (!level)
  312. {
  313. FURI_LOG_E(TAG, "Invalid level pointer");
  314. return false;
  315. }
  316. uint32_t temp = 1; // Default
  317. if (!load_uint32("player/level", &temp))
  318. {
  319. FURI_LOG_W(TAG, "No data or parse error for level. Using default: 1");
  320. temp = 1;
  321. }
  322. *level = temp;
  323. return true;
  324. }
  325. // 3. XP (uint32_t)
  326. static bool load_player_xp(uint32_t *xp)
  327. {
  328. if (!xp)
  329. {
  330. FURI_LOG_E(TAG, "Invalid xp pointer");
  331. return false;
  332. }
  333. uint32_t temp = 0; // Default
  334. if (!load_uint32("player/xp", &temp))
  335. {
  336. FURI_LOG_W(TAG, "No data or parse error for xp. Using default: 0");
  337. temp = 0;
  338. }
  339. *xp = temp;
  340. return true;
  341. }
  342. // 4. Health (uint32_t)
  343. static bool load_player_health(uint32_t *health)
  344. {
  345. if (!health)
  346. {
  347. FURI_LOG_E(TAG, "Invalid health pointer");
  348. return false;
  349. }
  350. uint32_t temp = 100; // Default
  351. if (!load_uint32("player/health", &temp))
  352. {
  353. FURI_LOG_W(TAG, "No data or parse error for health. Using default: 100");
  354. temp = 100;
  355. }
  356. *health = temp;
  357. return true;
  358. }
  359. // 5. Strength (uint32_t)
  360. static bool load_player_strength(uint32_t *strength)
  361. {
  362. if (!strength)
  363. {
  364. FURI_LOG_E(TAG, "Invalid strength pointer");
  365. return false;
  366. }
  367. uint32_t temp = 10; // Default
  368. if (!load_uint32("player/strength", &temp))
  369. {
  370. FURI_LOG_W(TAG, "No data or parse error for strength. Using default: 10");
  371. temp = 10;
  372. }
  373. *strength = temp;
  374. return true;
  375. }
  376. // 6. Max Health (uint32_t)
  377. static bool load_player_max_health(uint32_t *max_health)
  378. {
  379. if (!max_health)
  380. {
  381. FURI_LOG_E(TAG, "Invalid max_health pointer");
  382. return false;
  383. }
  384. uint32_t temp = 100; // Default
  385. if (!load_uint32("player/max_health", &temp))
  386. {
  387. FURI_LOG_W(TAG, "No data or parse error for max_health. Using default: 100");
  388. temp = 100;
  389. }
  390. *max_health = temp;
  391. return true;
  392. }
  393. // 7. Health Regen (uint32_t)
  394. static bool load_player_health_regen(uint32_t *health_regen)
  395. {
  396. if (!health_regen)
  397. {
  398. FURI_LOG_E(TAG, "Invalid health_regen pointer");
  399. return false;
  400. }
  401. uint32_t temp = 1; // Default
  402. if (!load_uint32("player/health_regen", &temp))
  403. {
  404. FURI_LOG_W(TAG, "No data or parse error for health_regen. Using default: 0");
  405. temp = 1;
  406. }
  407. *health_regen = temp;
  408. return true;
  409. }
  410. // 8. Elapsed Health Regen (float)
  411. static bool load_player_elapsed_health_regen(float *elapsed_health_regen)
  412. {
  413. if (!elapsed_health_regen)
  414. {
  415. FURI_LOG_E(TAG, "Invalid elapsed_health_regen pointer");
  416. return false;
  417. }
  418. float temp = 0.0f; // Default
  419. if (!load_float("player/elapsed_health_regen", &temp))
  420. {
  421. FURI_LOG_W(TAG, "No data or parse error for elapsed_health_regen. Using default: 0.0f");
  422. temp = 0.0f;
  423. }
  424. *elapsed_health_regen = temp;
  425. return true;
  426. }
  427. // 9. Attack Timer (float)
  428. static bool load_player_attack_timer(float *attack_timer)
  429. {
  430. if (!attack_timer)
  431. {
  432. FURI_LOG_E(TAG, "Invalid attack_timer pointer");
  433. return false;
  434. }
  435. float temp = 0.1f; // Default
  436. if (!load_float("player/attack_timer", &temp))
  437. {
  438. FURI_LOG_W(TAG, "No data or parse error for attack_timer. Using default: 0.0f");
  439. temp = 0.1f;
  440. }
  441. *attack_timer = temp;
  442. return true;
  443. }
  444. // 10. Elapsed Attack Timer (float)
  445. static bool load_player_elapsed_attack_timer(float *elapsed_attack_timer)
  446. {
  447. if (!elapsed_attack_timer)
  448. {
  449. FURI_LOG_E(TAG, "Invalid elapsed_attack_timer pointer");
  450. return false;
  451. }
  452. float temp = 0.0f; // Default
  453. if (!load_float("player/elapsed_attack_timer", &temp))
  454. {
  455. FURI_LOG_W(TAG, "No data or parse error for elapsed_attack_timer. Using default: 0.0f");
  456. temp = 0.0f;
  457. }
  458. *elapsed_attack_timer = temp;
  459. return true;
  460. }
  461. // 11. Direction (enum PlayerDirection)
  462. static bool load_player_direction(PlayerDirection *direction)
  463. {
  464. if (!direction)
  465. {
  466. FURI_LOG_E(TAG, "Invalid direction pointer");
  467. return false;
  468. }
  469. // Default to 3 -> PLAYER_RIGHT (adjust if you prefer another default)
  470. int direction_int = 3;
  471. if (!load_number("player/direction", &direction_int))
  472. {
  473. FURI_LOG_W(TAG, "No data or parse error for direction. Defaulting to PLAYER_RIGHT");
  474. direction_int = 3;
  475. }
  476. switch (direction_int)
  477. {
  478. case 0:
  479. *direction = PLAYER_UP;
  480. break;
  481. case 1:
  482. *direction = PLAYER_DOWN;
  483. break;
  484. case 2:
  485. *direction = PLAYER_LEFT;
  486. break;
  487. case 3:
  488. *direction = PLAYER_RIGHT;
  489. break;
  490. default:
  491. FURI_LOG_W(TAG, "Invalid direction value: %d. Defaulting to PLAYER_RIGHT", direction_int);
  492. *direction = PLAYER_RIGHT;
  493. break;
  494. }
  495. return true;
  496. }
  497. // 12. State (enum PlayerState)
  498. static bool load_player_state(PlayerState *state)
  499. {
  500. if (!state)
  501. {
  502. FURI_LOG_E(TAG, "Invalid state pointer");
  503. return false;
  504. }
  505. // Default to 0 -> PLAYER_IDLE
  506. int state_int = 0;
  507. if (!load_number("player/state", &state_int))
  508. {
  509. FURI_LOG_W(TAG, "No data or parse error for state. Defaulting to PLAYER_IDLE");
  510. state_int = 0;
  511. }
  512. switch (state_int)
  513. {
  514. case 0:
  515. *state = PLAYER_IDLE;
  516. break;
  517. case 1:
  518. *state = PLAYER_MOVING;
  519. break;
  520. case 2:
  521. *state = PLAYER_ATTACKING;
  522. break;
  523. case 3:
  524. *state = PLAYER_ATTACKED;
  525. break;
  526. case 4:
  527. *state = PLAYER_DEAD;
  528. break;
  529. default:
  530. FURI_LOG_W(TAG, "Invalid state value: %d. Defaulting to PLAYER_IDLE", state_int);
  531. *state = PLAYER_IDLE;
  532. break;
  533. }
  534. return true;
  535. }
  536. // 13. Start Position X (float)
  537. static bool load_player_start_position_x(float *start_position_x)
  538. {
  539. if (!start_position_x)
  540. {
  541. FURI_LOG_E(TAG, "Invalid start_position_x pointer");
  542. return false;
  543. }
  544. float temp = 192.0f; // Default
  545. if (!load_float("player/start_position_x", &temp))
  546. {
  547. FURI_LOG_W(TAG, "No data or parse error for start_position_x. Using default: 0.0f");
  548. temp = 192.0f;
  549. }
  550. *start_position_x = temp;
  551. return true;
  552. }
  553. // 14. Start Position Y (float)
  554. static bool load_player_start_position_y(float *start_position_y)
  555. {
  556. if (!start_position_y)
  557. {
  558. FURI_LOG_E(TAG, "Invalid start_position_y pointer");
  559. return false;
  560. }
  561. float temp = 96.0f; // Default
  562. if (!load_float("player/start_position_y", &temp))
  563. {
  564. FURI_LOG_W(TAG, "No data or parse error for start_position_y. Using default: 0.0f");
  565. temp = 96.0f;
  566. }
  567. *start_position_y = temp;
  568. return true;
  569. }
  570. // 15. dx (int8_t)
  571. static bool load_player_dx(int8_t *dx)
  572. {
  573. if (!dx)
  574. {
  575. FURI_LOG_E(TAG, "Invalid dx pointer");
  576. return false;
  577. }
  578. int8_t temp = 1; // Default
  579. if (!load_int8("player/dx", &temp))
  580. {
  581. FURI_LOG_W(TAG, "No data or parse error for dx. Using default: 0");
  582. temp = 1;
  583. }
  584. *dx = temp;
  585. return true;
  586. }
  587. // 16. dy (int8_t)
  588. static bool load_player_dy(int8_t *dy)
  589. {
  590. if (!dy)
  591. {
  592. FURI_LOG_E(TAG, "Invalid dy pointer");
  593. return false;
  594. }
  595. int8_t temp = 0; // Default
  596. if (!load_int8("player/dy", &temp))
  597. {
  598. FURI_LOG_W(TAG, "No data or parse error for dy. Using default: 0");
  599. temp = 0;
  600. }
  601. *dy = temp;
  602. return true;
  603. }
  604. bool load_player_context(PlayerContext *player_context)
  605. {
  606. if (!player_context)
  607. {
  608. FURI_LOG_E(TAG, "Invalid player context");
  609. return false;
  610. }
  611. // 1. Username (String)
  612. if (!load_player_username(player_context->username, sizeof(player_context->username)))
  613. {
  614. FURI_LOG_E(TAG, "Failed to load player username");
  615. return false;
  616. }
  617. // 2. Level (uint32_t)
  618. if (!load_player_level(&player_context->level))
  619. {
  620. FURI_LOG_E(TAG, "Failed to load player level");
  621. return false;
  622. }
  623. // 3. XP (uint32_t)
  624. if (!load_player_xp(&player_context->xp))
  625. {
  626. FURI_LOG_E(TAG, "Failed to load player xp");
  627. return false;
  628. }
  629. // 4. Health (uint32_t)
  630. if (!load_player_health(&player_context->health))
  631. {
  632. FURI_LOG_E(TAG, "Failed to load player health");
  633. return false;
  634. }
  635. // 5. Strength (uint32_t)
  636. if (!load_player_strength(&player_context->strength))
  637. {
  638. FURI_LOG_E(TAG, "Failed to load player strength");
  639. return false;
  640. }
  641. // 6. Max Health (uint32_t)
  642. if (!load_player_max_health(&player_context->max_health))
  643. {
  644. FURI_LOG_E(TAG, "Failed to load player max health");
  645. return false;
  646. }
  647. // 7. Health Regen (uint32_t)
  648. if (!load_player_health_regen(&player_context->health_regen))
  649. {
  650. FURI_LOG_E(TAG, "Failed to load player health regen");
  651. return false;
  652. }
  653. // 8. Elapsed Health Regen (float)
  654. if (!load_player_elapsed_health_regen(&player_context->elapsed_health_regen))
  655. {
  656. FURI_LOG_E(TAG, "Failed to load player elapsed health regen");
  657. return false;
  658. }
  659. // 9. Attack Timer (float)
  660. if (!load_player_attack_timer(&player_context->attack_timer))
  661. {
  662. FURI_LOG_E(TAG, "Failed to load player attack timer");
  663. return false;
  664. }
  665. // 10. Elapsed Attack Timer (float)
  666. if (!load_player_elapsed_attack_timer(&player_context->elapsed_attack_timer))
  667. {
  668. FURI_LOG_E(TAG, "Failed to load player elapsed attack timer");
  669. return false;
  670. }
  671. // 11. Direction (enum PlayerDirection)
  672. if (!load_player_direction(&player_context->direction))
  673. {
  674. FURI_LOG_E(TAG, "Failed to load player direction");
  675. return false;
  676. }
  677. // 12. State (enum PlayerState)
  678. if (!load_player_state(&player_context->state))
  679. {
  680. FURI_LOG_E(TAG, "Failed to load player state");
  681. return false;
  682. }
  683. // 13. Start Position X (float)
  684. if (!load_player_start_position_x(&player_context->start_position.x))
  685. {
  686. FURI_LOG_E(TAG, "Failed to load player start position x");
  687. return false;
  688. }
  689. // 14. Start Position Y (float)
  690. if (!load_player_start_position_y(&player_context->start_position.y))
  691. {
  692. FURI_LOG_E(TAG, "Failed to load player start position y");
  693. return false;
  694. }
  695. // 15. dx (int8_t)
  696. if (!load_player_dx(&player_context->dx))
  697. {
  698. FURI_LOG_E(TAG, "Failed to load player dx");
  699. return false;
  700. }
  701. // 16. dy (int8_t)
  702. if (!load_player_dy(&player_context->dy))
  703. {
  704. FURI_LOG_E(TAG, "Failed to load player dy");
  705. return false;
  706. }
  707. return true;
  708. }
  709. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  710. {
  711. furi_string_replace_str(string, needle, "", 0);
  712. }
  713. static FuriString *enemy_data(const FuriString *world_data)
  714. {
  715. size_t enemy_data_pos = furi_string_search_str(world_data, "enemy_data", 0);
  716. if (enemy_data_pos == FURI_STRING_FAILURE)
  717. {
  718. FURI_LOG_E("Game", "Failed to find enemy_data in world data");
  719. return NULL;
  720. }
  721. size_t bracket_start = furi_string_search_char(world_data, '[', enemy_data_pos);
  722. if (bracket_start == FURI_STRING_FAILURE)
  723. {
  724. FURI_LOG_E("Game", "Failed to find start of enemy_data array");
  725. return NULL;
  726. }
  727. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  728. if (bracket_end == FURI_STRING_FAILURE)
  729. {
  730. FURI_LOG_E("Game", "Failed to find end of enemy_data array");
  731. return NULL;
  732. }
  733. FuriString *enemy_data_str = furi_string_alloc();
  734. if (!enemy_data_str)
  735. {
  736. FURI_LOG_E("Game", "Failed to allocate enemy_data string");
  737. return NULL;
  738. }
  739. furi_string_cat_str(enemy_data_str, "{\"enemy_data\":");
  740. {
  741. FuriString *temp_sub = furi_string_alloc();
  742. furi_string_set_strn(
  743. temp_sub,
  744. furi_string_get_cstr(world_data) + bracket_start,
  745. (bracket_end + 1) - bracket_start);
  746. furi_string_cat(enemy_data_str, temp_sub);
  747. furi_string_free(temp_sub);
  748. }
  749. furi_string_cat_str(enemy_data_str, "}");
  750. return enemy_data_str;
  751. }
  752. static FuriString *json_data(const FuriString *world_data)
  753. {
  754. size_t json_data_pos = furi_string_search_str(world_data, "json_data", 0);
  755. if (json_data_pos == FURI_STRING_FAILURE)
  756. {
  757. FURI_LOG_E("Game", "Failed to find json_data in world data");
  758. return NULL;
  759. }
  760. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  761. if (bracket_start == FURI_STRING_FAILURE)
  762. {
  763. FURI_LOG_E("Game", "Failed to find start of json_data array");
  764. return NULL;
  765. }
  766. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  767. if (bracket_end == FURI_STRING_FAILURE)
  768. {
  769. FURI_LOG_E("Game", "Failed to find end of json_data array");
  770. return NULL;
  771. }
  772. FuriString *json_data_str = furi_string_alloc();
  773. if (!json_data_str)
  774. {
  775. FURI_LOG_E("Game", "Failed to allocate json_data string");
  776. return NULL;
  777. }
  778. furi_string_cat_str(json_data_str, "{\"json_data\":");
  779. {
  780. FuriString *temp_sub = furi_string_alloc();
  781. furi_string_set_strn(
  782. temp_sub,
  783. furi_string_get_cstr(world_data) + bracket_start,
  784. (bracket_end + 1) - bracket_start);
  785. furi_string_cat(json_data_str, temp_sub);
  786. furi_string_free(temp_sub);
  787. }
  788. furi_string_cat_str(json_data_str, "}");
  789. return json_data_str;
  790. }
  791. bool separate_world_data(char *id, FuriString *world_data)
  792. {
  793. if (!id || !world_data)
  794. {
  795. FURI_LOG_E("Game", "Invalid parameters");
  796. return false;
  797. }
  798. FuriString *file_json_data = json_data(world_data);
  799. if (!file_json_data || furi_string_size(file_json_data) == 0)
  800. {
  801. FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
  802. return false;
  803. }
  804. // Save file_json_data to disk
  805. char directory_path[256];
  806. snprintf(directory_path, sizeof(directory_path),
  807. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  808. Storage *storage = furi_record_open(RECORD_STORAGE);
  809. storage_common_mkdir(storage, directory_path);
  810. File *file = storage_file_alloc(storage);
  811. char file_path[256];
  812. snprintf(file_path, sizeof(file_path),
  813. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  814. id, id);
  815. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  816. {
  817. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  818. storage_file_free(file);
  819. furi_record_close(RECORD_STORAGE);
  820. furi_string_free(file_json_data);
  821. return false;
  822. }
  823. size_t data_size = furi_string_size(file_json_data);
  824. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  825. {
  826. FURI_LOG_E("Game", "Failed to write json_data");
  827. }
  828. storage_file_close(file);
  829. furi_string_replace_at(file_json_data, 0, 1, "");
  830. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  831. // include the comma at the end of the json_data array
  832. furi_string_cat_str(file_json_data, ",");
  833. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  834. furi_string_free(file_json_data);
  835. FuriString *file_enemy_data = enemy_data(world_data);
  836. if (!file_enemy_data)
  837. {
  838. FURI_LOG_E("Game", "Failed to get enemy data");
  839. return false;
  840. }
  841. snprintf(file_path, sizeof(file_path),
  842. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  843. id, id);
  844. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  845. {
  846. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  847. storage_file_free(file);
  848. furi_record_close(RECORD_STORAGE);
  849. furi_string_free(file_enemy_data);
  850. return false;
  851. }
  852. data_size = furi_string_size(file_enemy_data);
  853. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  854. {
  855. FURI_LOG_E("Game", "Failed to write enemy_data");
  856. }
  857. // Clean up
  858. furi_string_free(file_enemy_data);
  859. storage_file_close(file);
  860. storage_file_free(file);
  861. furi_record_close(RECORD_STORAGE);
  862. return true;
  863. }