diff --git a/dll/local_storage.cpp b/dll/local_storage.cpp index 98bb284..93be15f 100644 --- a/dll/local_storage.cpp +++ b/dll/local_storage.cpp @@ -691,10 +691,17 @@ bool Local_Storage::update_save_filenames(std::string folder) return true; } -bool Local_Storage::load_inventory_file(nlohmann::json& json, std::string const&file) +bool Local_Storage::load_json_file(std::string folder, std::string const&file, nlohmann::json& json) { - std::string inv_path = std::move(save_directory + appid + inventory_storage_folder + PATH_SEPARATOR + file); - std::ifstream inventory_file(inv_path); + if (!folder.empty() && folder.back() != *PATH_SEPARATOR) { + folder.append(PATH_SEPARATOR); + } + std::string inv_path = std::move(save_directory + appid + folder); + std::string full_path = inv_path + file; + + create_directory(inv_path); + + std::ifstream inventory_file(full_path); // If there is a file and we opened it if (inventory_file) { @@ -709,34 +716,38 @@ bool Local_Storage::load_inventory_file(nlohmann::json& json, std::string const& try { json = std::move(nlohmann::json::parse(buffer)); - PRINT_DEBUG("Loaded inventory \"%s\". Loaded %u items.\n", inv_path.c_str(), json.size()); + PRINT_DEBUG("Loaded json \"%s\". Loaded %u items.\n", full_path.c_str(), json.size()); return true; } catch (std::exception& e) { - PRINT_DEBUG("Error while parsing \"%s\" inventory json: %s\n", inv_path.c_str(), e.what()); + PRINT_DEBUG("Error while parsing \"%s\" json: %s\n", full_path.c_str(), e.what()); } } else { - PRINT_DEBUG("Couldn't open file \"%s\" to read inventory\n", inv_path.c_str()); + PRINT_DEBUG("Couldn't open file \"%s\" to read json\n", full_path.c_str()); } return false; } -bool Local_Storage::write_inventory_file(nlohmann::json const& json, std::string const&file) +bool Local_Storage::write_json_file(std::string folder, std::string const&file, nlohmann::json const& json) { - std::string inv_path = std::move(save_directory + appid + inventory_storage_folder); + if (!folder.empty() && folder.back() != *PATH_SEPARATOR) { + folder.append(PATH_SEPARATOR); + } + std::string inv_path = std::move(save_directory + appid + folder); + std::string full_path = inv_path + file; create_directory(inv_path); - std::ofstream inventory_file(inv_path + PATH_SEPARATOR + file, std::ios::trunc | std::ios::out); + std::ofstream inventory_file(full_path, std::ios::trunc | std::ios::out); if (inventory_file) { inventory_file << std::setw(2) << json; return true; } - PRINT_DEBUG("Couldn't open file \"%s\" to write inventory\n", inv_path.c_str()); + PRINT_DEBUG("Couldn't open file \"%s\" to write json\n", full_path.c_str()); return false; } diff --git a/dll/local_storage.h b/dll/local_storage.h index 38d676b..3c550bd 100644 --- a/dll/local_storage.h +++ b/dll/local_storage.h @@ -63,8 +63,8 @@ public: bool update_save_filenames(std::string folder); - bool load_inventory_file(nlohmann::json &json, std::string const&file); - bool write_inventory_file(nlohmann::json const& json, std::string const&file); + bool load_json_file(std::string folder, std::string const& file, nlohmann::json& json); + bool write_json_file(std::string folder, std::string const& file, nlohmann::json const& json); }; #endif diff --git a/dll/steam_inventory.h b/dll/steam_inventory.h index e116e65..ad90a4c 100644 --- a/dll/steam_inventory.h +++ b/dll/steam_inventory.h @@ -124,7 +124,7 @@ void read_items_db() void read_inventory_db() { // If we havn't got any inventory - if (!local_storage->load_inventory_file(user_items, items_user_file)) + if (!local_storage->load_json_file("inventory", items_user_file, user_items)) { // Try to load a default one std::string items_db_path = Local_Storage::get_game_settings_path() + items_default_file; diff --git a/dll/steam_user_stats.h b/dll/steam_user_stats.h index a659fbb..98337c3 100644 --- a/dll/steam_user_stats.h +++ b/dll/steam_user_stats.h @@ -93,7 +93,7 @@ void load_achievements_db() void load_achievements() { - local_storage->load_inventory_file(user_achievements, achievements_user_file); + local_storage->load_json_file("", achievements_user_file, user_achievements); } public: @@ -319,7 +319,7 @@ bool StoreStats() PRINT_DEBUG("StoreStats\n"); std::lock_guard lock(global_mutex); - local_storage->write_inventory_file(user_achievements, achievements_user_file); + local_storage->write_json_file("", achievements_user_file, user_achievements); UserStatsStored_t data; data.m_nGameID = settings->get_local_game_id().ToUint64();