From 846ed27397fda3c3ff1c9d619e51be6358122d06 Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Mon, 23 Sep 2019 18:42:13 +0200 Subject: [PATCH] Add a default inventory support Put a file in /steam_settings/default_items.json with all default items you want (same syntax as the inventory file) --- dll/steam_inventory.h | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/dll/steam_inventory.h b/dll/steam_inventory.h index acc6319..e116e65 100644 --- a/dll/steam_inventory.h +++ b/dll/steam_inventory.h @@ -44,6 +44,7 @@ class Steam_Inventory : { public: static constexpr auto items_user_file = "items.json"; + static constexpr auto items_default_file = "default_items.json"; private: class Settings *settings; @@ -122,7 +123,36 @@ void read_items_db() void read_inventory_db() { - local_storage->load_inventory_file(user_items, items_user_file); + // If we havn't got any inventory + if (!local_storage->load_inventory_file(user_items, items_user_file)) + { + // Try to load a default one + std::string items_db_path = Local_Storage::get_game_settings_path() + items_default_file; + PRINT_DEBUG("Default items file path: %s\n", items_db_path.c_str()); + std::ifstream inventory_file(items_db_path); + // If there is a file and we opened it + if (inventory_file) + { + inventory_file.seekg(0, std::ios::end); + size_t size = inventory_file.tellg(); + std::string buffer(size, '\0'); + inventory_file.seekg(0); + // Read it entirely, if the .json file gets too big, + // I should look into this and split reads into smaller parts. + inventory_file.read(&buffer[0], size); + inventory_file.close(); + + try + { + user_items = std::move(nlohmann::json::parse(buffer)); + PRINT_DEBUG("Loaded default inventory. Loaded %u items.\n", user_items.size()); + } + catch (std::exception& e) + { + PRINT_DEBUG("Error while parsing inventory json: %s\n", e.what()); + } + } + } } public: