Rework inventory loading
More generic json loading allows to load a json from a specified folder rather than the "inventory" directory. Also changed achievements location to <appid> root diectorymerge-requests/24/head
parent
f15b2b0458
commit
8c45ab2003
|
@ -691,10 +691,17 @@ bool Local_Storage::update_save_filenames(std::string folder)
|
||||||
return true;
|
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);
|
if (!folder.empty() && folder.back() != *PATH_SEPARATOR) {
|
||||||
std::ifstream inventory_file(inv_path);
|
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 there is a file and we opened it
|
||||||
if (inventory_file)
|
if (inventory_file)
|
||||||
{
|
{
|
||||||
|
@ -709,34 +716,38 @@ bool Local_Storage::load_inventory_file(nlohmann::json& json, std::string const&
|
||||||
|
|
||||||
try {
|
try {
|
||||||
json = std::move(nlohmann::json::parse(buffer));
|
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;
|
return true;
|
||||||
} catch (std::exception& e) {
|
} 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
|
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;
|
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);
|
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)
|
if (inventory_file)
|
||||||
{
|
{
|
||||||
inventory_file << std::setw(2) << json;
|
inventory_file << std::setw(2) << json;
|
||||||
return true;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,8 +63,8 @@ public:
|
||||||
|
|
||||||
bool update_save_filenames(std::string folder);
|
bool update_save_filenames(std::string folder);
|
||||||
|
|
||||||
bool load_inventory_file(nlohmann::json &json, std::string const&file);
|
bool load_json_file(std::string folder, std::string const& file, nlohmann::json& json);
|
||||||
bool write_inventory_file(nlohmann::json const& json, std::string const&file);
|
bool write_json_file(std::string folder, std::string const& file, nlohmann::json const& json);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -124,7 +124,7 @@ void read_items_db()
|
||||||
void read_inventory_db()
|
void read_inventory_db()
|
||||||
{
|
{
|
||||||
// If we havn't got any inventory
|
// 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
|
// Try to load a default one
|
||||||
std::string items_db_path = Local_Storage::get_game_settings_path() + items_default_file;
|
std::string items_db_path = Local_Storage::get_game_settings_path() + items_default_file;
|
||||||
|
|
|
@ -93,7 +93,7 @@ void load_achievements_db()
|
||||||
|
|
||||||
void load_achievements()
|
void load_achievements()
|
||||||
{
|
{
|
||||||
local_storage->load_inventory_file(user_achievements, achievements_user_file);
|
local_storage->load_json_file("", achievements_user_file, user_achievements);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -319,7 +319,7 @@ bool StoreStats()
|
||||||
PRINT_DEBUG("StoreStats\n");
|
PRINT_DEBUG("StoreStats\n");
|
||||||
std::lock_guard<std::recursive_mutex> lock(global_mutex);
|
std::lock_guard<std::recursive_mutex> 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;
|
UserStatsStored_t data;
|
||||||
data.m_nGameID = settings->get_local_game_id().ToUint64();
|
data.m_nGameID = settings->get_local_game_id().ToUint64();
|
||||||
|
|
Loading…
Reference in New Issue