From 19659da6c21c59918497a223bf063aabd3b7d0b7 Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Tue, 9 Jul 2019 15:07:10 +0200 Subject: [PATCH] Fixed crash on load. Fixed dll crash on load by adding a check if the Json file exists or not. --- dll/item_db_loader.cpp | 52 +++++++++++++++++++++++------------------- dll/steam_inventory.h | 2 +- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/dll/item_db_loader.cpp b/dll/item_db_loader.cpp index 9817be5..4307840 100644 --- a/dll/item_db_loader.cpp +++ b/dll/item_db_loader.cpp @@ -24,33 +24,39 @@ std::map> read_items_db(std:: std::map> items; std::ifstream items_file(items_db); - items_file.seekg(0, std::ios::end); - size_t size = items_file.tellg(); - std::string buffer(size, '\0'); - items_file.seekg(0); - items_file.read(&buffer[0], size); - items_file.close(); - - try + // If there is a file and we opened it + if( items_file ) { - std::map> tmp; - nlohmann::json json = nlohmann::json::parse(buffer); + items_file.seekg(0, std::ios::end); + 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()); - nlohmann::json& value = (*i).value(); - for( auto& j : value.items() ) - { - tmp[key][(*j).key()] = (*j).value(); - } - } + std::map> tmp; + nlohmann::json json = nlohmann::json::parse(buffer); - items.swap(tmp); - } - catch (std::exception& e) - { - PRINT_DEBUG("Error while parsing json: %s", e.what()); + for (auto& i : json.items()) + { + SteamItemDef_t key = std::stoi((*i).key()); + nlohmann::json& value = (*i).value(); + 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; diff --git a/dll/steam_inventory.h b/dll/steam_inventory.h index d30d203..c3e8af4 100644 --- a/dll/steam_inventory.h +++ b/dll/steam_inventory.h @@ -87,7 +87,7 @@ struct Steam_Inventory_Requests *get_inventory_result(SteamInventoryResult_t res public: 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->callbacks = callbacks;