|
|
@@ -897,62 +897,9 @@ static inline void furi_string_remove_str(FuriString *string, const char *needle
|
|
|
furi_string_replace_str(string, needle, "", 0);
|
|
|
}
|
|
|
|
|
|
-static FuriString *enemy_data(const FuriString *world_data)
|
|
|
+static FuriString *json_data(const FuriString *world_data, const char *key)
|
|
|
{
|
|
|
- size_t enemy_data_pos = furi_string_search_str(world_data, "enemy_data", 0);
|
|
|
- if (enemy_data_pos == FURI_STRING_FAILURE)
|
|
|
- {
|
|
|
- FURI_LOG_E("Game", "Failed to find enemy_data in world data");
|
|
|
-
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
- size_t bracket_start = furi_string_search_char(world_data, '[', enemy_data_pos);
|
|
|
- if (bracket_start == FURI_STRING_FAILURE)
|
|
|
- {
|
|
|
- FURI_LOG_E("Game", "Failed to find start of enemy_data array");
|
|
|
-
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
- size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
|
|
|
- if (bracket_end == FURI_STRING_FAILURE)
|
|
|
- {
|
|
|
- FURI_LOG_E("Game", "Failed to find end of enemy_data array");
|
|
|
-
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
- FuriString *enemy_data_str = furi_string_alloc();
|
|
|
- if (!enemy_data_str)
|
|
|
- {
|
|
|
- FURI_LOG_E("Game", "Failed to allocate enemy_data string");
|
|
|
-
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
- furi_string_cat_str(enemy_data_str, "{\"enemy_data\":");
|
|
|
-
|
|
|
- {
|
|
|
- FuriString *temp_sub = furi_string_alloc();
|
|
|
-
|
|
|
- furi_string_set_strn(
|
|
|
- temp_sub,
|
|
|
- furi_string_get_cstr(world_data) + bracket_start,
|
|
|
- (bracket_end + 1) - bracket_start);
|
|
|
-
|
|
|
- furi_string_cat(enemy_data_str, temp_sub);
|
|
|
- furi_string_free(temp_sub);
|
|
|
- }
|
|
|
-
|
|
|
- furi_string_cat_str(enemy_data_str, "}");
|
|
|
-
|
|
|
- return enemy_data_str;
|
|
|
-}
|
|
|
-
|
|
|
-static FuriString *json_data(const FuriString *world_data)
|
|
|
-{
|
|
|
- size_t json_data_pos = furi_string_search_str(world_data, "json_data", 0);
|
|
|
+ size_t json_data_pos = furi_string_search_str(world_data, key, 0);
|
|
|
if (json_data_pos == FURI_STRING_FAILURE)
|
|
|
{
|
|
|
FURI_LOG_E("Game", "Failed to find json_data in world data");
|
|
|
@@ -984,7 +931,9 @@ static FuriString *json_data(const FuriString *world_data)
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
- furi_string_cat_str(json_data_str, "{\"json_data\":");
|
|
|
+ furi_string_cat_str(json_data_str, "{\"");
|
|
|
+ furi_string_cat_str(json_data_str, key);
|
|
|
+ furi_string_cat_str(json_data_str, "\":");
|
|
|
|
|
|
{
|
|
|
FuriString *temp_sub = furi_string_alloc();
|
|
|
@@ -1010,7 +959,7 @@ bool separate_world_data(char *id, FuriString *world_data)
|
|
|
FURI_LOG_E("Game", "Invalid parameters");
|
|
|
return false;
|
|
|
}
|
|
|
- FuriString *file_json_data = json_data(world_data);
|
|
|
+ FuriString *file_json_data = json_data(world_data, "json_data");
|
|
|
if (!file_json_data || furi_string_size(file_json_data) == 0)
|
|
|
{
|
|
|
FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
|
|
|
@@ -1051,10 +1000,48 @@ bool separate_world_data(char *id, FuriString *world_data)
|
|
|
furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
|
|
|
// include the comma at the end of the json_data array
|
|
|
furi_string_cat_str(file_json_data, ",");
|
|
|
+
|
|
|
furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
|
|
|
furi_string_free(file_json_data);
|
|
|
|
|
|
- FuriString *file_enemy_data = enemy_data(world_data);
|
|
|
+ // save npc_data to disk
|
|
|
+ FuriString *file_npc_data = json_data(world_data, "npc_data");
|
|
|
+ if (!file_npc_data)
|
|
|
+ {
|
|
|
+ FURI_LOG_E("Game", "Failed to get npc data");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ snprintf(file_path, sizeof(file_path),
|
|
|
+ STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_npc_data.json",
|
|
|
+ id, id);
|
|
|
+
|
|
|
+ if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
|
|
|
+ {
|
|
|
+ FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ furi_string_free(file_npc_data);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ data_size = furi_string_size(file_npc_data);
|
|
|
+ if (storage_file_write(file, furi_string_get_cstr(file_npc_data), data_size) != data_size)
|
|
|
+ {
|
|
|
+ FURI_LOG_E("Game", "Failed to write npc_data");
|
|
|
+ }
|
|
|
+ storage_file_close(file);
|
|
|
+
|
|
|
+ furi_string_replace_at(file_npc_data, 0, 1, "");
|
|
|
+ furi_string_replace_at(file_npc_data, furi_string_size(file_npc_data) - 1, 1, "");
|
|
|
+ // include the comma at the end of the npc_data array
|
|
|
+ furi_string_cat_str(file_npc_data, ",");
|
|
|
+
|
|
|
+ furi_string_remove_str(world_data, furi_string_get_cstr(file_npc_data));
|
|
|
+ furi_string_free(file_npc_data);
|
|
|
+
|
|
|
+ // Save enemy_data to disk
|
|
|
+ FuriString *file_enemy_data = json_data(world_data, "enemy_data");
|
|
|
if (!file_enemy_data)
|
|
|
{
|
|
|
FURI_LOG_E("Game", "Failed to get enemy data");
|
|
|
@@ -1079,12 +1066,11 @@ bool separate_world_data(char *id, FuriString *world_data)
|
|
|
{
|
|
|
FURI_LOG_E("Game", "Failed to write enemy_data");
|
|
|
}
|
|
|
+ furi_string_free(file_enemy_data);
|
|
|
|
|
|
// Clean up
|
|
|
- furi_string_free(file_enemy_data);
|
|
|
storage_file_close(file);
|
|
|
storage_file_free(file);
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
-
|
|
|
return true;
|
|
|
}
|