storage.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <game/storage.h>
  2. bool save_player_context(PlayerContext *player_context)
  3. {
  4. if (!player_context)
  5. {
  6. FURI_LOG_E(TAG, "Invalid player context");
  7. return false;
  8. }
  9. char player_context_json[512];
  10. snprintf(player_context_json, sizeof(player_context_json), "{\"username\":\"%s\",\"level\":%lu,\"xp\":%lu,\"health\":%lu,\"strength\":%lu,\"max_health\":%lu,\"health_regen\":%ld,\"elapsed_health_regen\":%f,\"attack_timer\":%f,\"elapsed_attack_timer\":%f,\"direction\":%u,\"state\":%u,\"start_position\":{\"x\":%f,\"y\":%f},\"dx\":%u,\"dy\":%u}",
  11. player_context->username, player_context->level, player_context->xp, player_context->health, player_context->strength, player_context->max_health, player_context->health_regen, (double)player_context->elapsed_health_regen, (double)player_context->attack_timer, (double)player_context->elapsed_attack_timer, player_context->direction, player_context->state, (double)player_context->start_position.x, (double)player_context->start_position.y, player_context->dx, player_context->dy);
  12. return save_char("player_context", player_context_json);
  13. }
  14. PlayerContext *load_player_context()
  15. {
  16. char player_context_json[512];
  17. if (!load_char("player_context", player_context_json, sizeof(player_context_json)))
  18. {
  19. FURI_LOG_E(TAG, "Failed to load player context");
  20. return NULL;
  21. }
  22. PlayerContext *player_context = (PlayerContext *)malloc(sizeof(PlayerContext));
  23. if (!player_context)
  24. {
  25. FURI_LOG_E(TAG, "Failed to allocate player context");
  26. return NULL;
  27. }
  28. // Parse the JSON data
  29. char *username = get_json_value("username", player_context_json);
  30. char *level = get_json_value("level", player_context_json);
  31. char *xp = get_json_value("xp", player_context_json);
  32. char *health = get_json_value("health", player_context_json);
  33. char *strength = get_json_value("strength", player_context_json);
  34. char *max_health = get_json_value("max_health", player_context_json);
  35. char *health_regen = get_json_value("health_regen", player_context_json);
  36. char *elapsed_health_regen = get_json_value("elapsed_health_regen", player_context_json);
  37. char *attack_timer = get_json_value("attack_timer", player_context_json);
  38. char *elapsed_attack_timer = get_json_value("elapsed_attack_timer", player_context_json);
  39. char *direction = get_json_value("direction", player_context_json);
  40. char *state = get_json_value("state", player_context_json);
  41. char *dx = get_json_value("dx", player_context_json);
  42. char *dy = get_json_value("dy", player_context_json);
  43. char *start_position = get_json_value("start_position", player_context_json);
  44. char *start_position_x = get_json_value("x", start_position);
  45. char *start_position_y = get_json_value("y", start_position);
  46. if (!username || !level || !xp || !health || !strength || !max_health || !health_regen || !elapsed_health_regen || !attack_timer || !elapsed_attack_timer || !direction || !state || !dx || !dy || !start_position || !start_position_x || !start_position_y)
  47. {
  48. FURI_LOG_E(TAG, "Failed to parse player context");
  49. free(player_context);
  50. return NULL;
  51. }
  52. // Copy the parsed values to the player context
  53. strncpy(player_context->username, username, sizeof(player_context->username));
  54. player_context->level = atoi(level);
  55. player_context->xp = atoi(xp);
  56. player_context->health = atoi(health);
  57. player_context->strength = atoi(strength);
  58. player_context->max_health = atoi(max_health);
  59. player_context->health_regen = atoi(health_regen);
  60. player_context->elapsed_health_regen = strtod(elapsed_health_regen, NULL);
  61. player_context->attack_timer = strtod(attack_timer, NULL);
  62. player_context->elapsed_attack_timer = strtod(elapsed_attack_timer, NULL);
  63. player_context->direction = atoi(direction);
  64. player_context->state = atoi(state);
  65. player_context->dx = atoi(dx);
  66. player_context->dy = atoi(dy);
  67. player_context->start_position.x = atoi(start_position_x);
  68. player_context->start_position.y = atoi(start_position_y);
  69. return player_context;
  70. }
  71. #include <stdio.h>
  72. #include <furi.h>
  73. #include <furi_hal.h>
  74. // etc. Make sure you include the relevant headers for your project
  75. // 1) Helper: remove first occurrence of "needle" from "string"
  76. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  77. {
  78. // Remove the FIRST occurrence
  79. furi_string_replace_str(string, needle, "", 0);
  80. }
  81. // 2) Adjusted function: returns exactly "json_data":[ ... ]
  82. static FuriString *enemy_data(const FuriString *world_data)
  83. {
  84. size_t enemy_data_pos = furi_string_search_str(world_data, "enemy_data", 0);
  85. if (enemy_data_pos == FURI_STRING_FAILURE)
  86. {
  87. FURI_LOG_E("Game", "Failed to find enemy_data in world data");
  88. return NULL;
  89. }
  90. size_t bracket_start = furi_string_search_char(world_data, '[', enemy_data_pos);
  91. if (bracket_start == FURI_STRING_FAILURE)
  92. {
  93. FURI_LOG_E("Game", "Failed to find start of enemy_data array");
  94. return NULL;
  95. }
  96. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  97. if (bracket_end == FURI_STRING_FAILURE)
  98. {
  99. FURI_LOG_E("Game", "Failed to find end of enemy_data array");
  100. return NULL;
  101. }
  102. FuriString *enemy_data_str = furi_string_alloc();
  103. if (!enemy_data_str)
  104. {
  105. FURI_LOG_E("Game", "Failed to allocate enemy_data string");
  106. return NULL;
  107. }
  108. furi_string_cat_str(enemy_data_str, "{\"enemy_data\":");
  109. {
  110. FuriString *temp_sub = furi_string_alloc();
  111. furi_string_set_strn(
  112. temp_sub,
  113. furi_string_get_cstr(world_data) + bracket_start,
  114. (bracket_end + 1) - bracket_start);
  115. furi_string_cat(enemy_data_str, temp_sub);
  116. furi_string_free(temp_sub);
  117. }
  118. furi_string_cat_str(enemy_data_str, "}");
  119. return enemy_data_str;
  120. }
  121. static FuriString *json_data(const FuriString *world_data)
  122. {
  123. size_t json_data_pos = furi_string_search_str(world_data, "json_data", 0);
  124. if (json_data_pos == FURI_STRING_FAILURE)
  125. {
  126. FURI_LOG_E("Game", "Failed to find json_data in world data");
  127. return NULL;
  128. }
  129. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  130. if (bracket_start == FURI_STRING_FAILURE)
  131. {
  132. FURI_LOG_E("Game", "Failed to find start of json_data array");
  133. return NULL;
  134. }
  135. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  136. if (bracket_end == FURI_STRING_FAILURE)
  137. {
  138. FURI_LOG_E("Game", "Failed to find end of json_data array");
  139. return NULL;
  140. }
  141. FuriString *json_data_str = furi_string_alloc();
  142. if (!json_data_str)
  143. {
  144. FURI_LOG_E("Game", "Failed to allocate json_data string");
  145. return NULL;
  146. }
  147. furi_string_cat_str(json_data_str, "{\"json_data\":");
  148. {
  149. FuriString *temp_sub = furi_string_alloc();
  150. furi_string_set_strn(
  151. temp_sub,
  152. furi_string_get_cstr(world_data) + bracket_start,
  153. (bracket_end + 1) - bracket_start);
  154. furi_string_cat(json_data_str, temp_sub);
  155. furi_string_free(temp_sub);
  156. }
  157. furi_string_cat_str(json_data_str, "}");
  158. return json_data_str;
  159. }
  160. bool separate_world_data(char *id, FuriString *world_data)
  161. {
  162. if (!id || !world_data)
  163. {
  164. FURI_LOG_E("Game", "Invalid parameters");
  165. return false;
  166. }
  167. FuriString *file_json_data = json_data(world_data);
  168. if (!file_json_data)
  169. {
  170. FURI_LOG_E("Game", "Failed to get json data");
  171. return false;
  172. }
  173. // Save file_json_data to disk
  174. char directory_path[256];
  175. snprintf(directory_path, sizeof(directory_path),
  176. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  177. Storage *storage = furi_record_open(RECORD_STORAGE);
  178. storage_common_mkdir(storage, directory_path);
  179. File *file = storage_file_alloc(storage);
  180. char file_path[256];
  181. snprintf(file_path, sizeof(file_path),
  182. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  183. id, id);
  184. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  185. {
  186. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  187. storage_file_free(file);
  188. furi_record_close(RECORD_STORAGE);
  189. furi_string_free(file_json_data);
  190. return false;
  191. }
  192. size_t data_size = furi_string_size(file_json_data);
  193. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  194. {
  195. FURI_LOG_E("Game", "Failed to write json_data");
  196. }
  197. furi_string_replace_at(file_json_data, 0, 1, "");
  198. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  199. // include the comma at the end of the json_data array
  200. furi_string_cat_str(file_json_data, ",");
  201. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  202. furi_string_free(file_json_data);
  203. FuriString *file_enemy_data = enemy_data(world_data);
  204. if (!file_enemy_data)
  205. {
  206. FURI_LOG_E("Game", "Failed to get enemy data");
  207. return false;
  208. }
  209. snprintf(file_path, sizeof(file_path),
  210. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  211. id, id);
  212. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  213. {
  214. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  215. storage_file_free(file);
  216. furi_record_close(RECORD_STORAGE);
  217. furi_string_free(file_enemy_data);
  218. return false;
  219. }
  220. data_size = furi_string_size(file_enemy_data);
  221. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  222. {
  223. FURI_LOG_E("Game", "Failed to write enemy_data");
  224. }
  225. // Clean up
  226. furi_string_free(file_enemy_data);
  227. storage_file_close(file);
  228. storage_file_free(file);
  229. furi_record_close(RECORD_STORAGE);
  230. return true;
  231. }