Fixed crash on load.

Fixed dll crash on load by adding a check if the Json file exists or not.
This commit is contained in:
Nemirtingas 2019-07-09 15:07:10 +02:00
parent 5f67d359b3
commit 19659da6c2
2 changed files with 30 additions and 24 deletions

View file

@ -24,33 +24,39 @@ std::map<SteamItemDef_t, std::map<std::string, std::string>> read_items_db(std::
std::map<SteamItemDef_t, std::map<std::string, std::string>> items; std::map<SteamItemDef_t, std::map<std::string, std::string>> items;
std::ifstream items_file(items_db); std::ifstream items_file(items_db);
items_file.seekg(0, std::ios::end); // If there is a file and we opened it
size_t size = items_file.tellg(); if( items_file )
std::string buffer(size, '\0');
items_file.seekg(0);
items_file.read(&buffer[0], size);
items_file.close();
try
{ {
std::map<SteamItemDef_t, std::map<std::string, std::string>> tmp; items_file.seekg(0, std::ios::end);
nlohmann::json json = nlohmann::json::parse(buffer); size_t size = items_file.tellg();
std::string buffer(size, '\0');
items_file.seekg(0);
// Read it entirely, if the .json file gets too big,
// I should look into this and split reads into smaller parts.
items_file.read(&buffer[0], size);
items_file.close();
for (auto& i : json.items()) try
{ {
SteamItemDef_t key = std::stoi((*i).key()); std::map<SteamItemDef_t, std::map<std::string, std::string>> tmp;
nlohmann::json& value = (*i).value(); nlohmann::json json = nlohmann::json::parse(buffer);
for( auto& j : value.items() )
{
tmp[key][(*j).key()] = (*j).value();
}
}
items.swap(tmp); for (auto& i : json.items())
} {
catch (std::exception& e) SteamItemDef_t key = std::stoi((*i).key());
{ nlohmann::json& value = (*i).value();
PRINT_DEBUG("Error while parsing json: %s", e.what()); for (auto& j : value.items())
{
tmp[key][(*j).key()] = (*j).value();
}
}
items.swap(tmp);
}
catch (std::exception& e)
{
PRINT_DEBUG("Error while parsing json: %s", e.what());
}
} }
return items; return items;

View file

@ -87,7 +87,7 @@ struct Steam_Inventory_Requests *get_inventory_result(SteamInventoryResult_t res
public: public:
Steam_Inventory(class Settings *settings, class SteamCallResults *callback_results, class SteamCallBacks *callbacks): Steam_Inventory(class Settings *settings, class SteamCallResults *callback_results, class SteamCallBacks *callbacks):
items(read_items_db(Local_Storage::get_program_path() + PATH_SEPARATOR + "steam_items.json")) items(std::move(read_items_db(Local_Storage::get_program_path() + PATH_SEPARATOR + "steam_items.json")))
{ {
this->settings = settings; this->settings = settings;
this->callbacks = callbacks; this->callbacks = callbacks;