From fb31aef9b222cfd2cd887b75cf4523d754643fd9 Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Wed, 17 Jul 2019 17:28:39 +0200 Subject: [PATCH] Set items to static Set items to static and call the json load only once. --- dll/steam_inventory.cpp | 5 +++++ dll/steam_inventory.h | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 dll/steam_inventory.cpp diff --git a/dll/steam_inventory.cpp b/dll/steam_inventory.cpp new file mode 100644 index 00000000..03968943 --- /dev/null +++ b/dll/steam_inventory.cpp @@ -0,0 +1,5 @@ +#include "steam_inventory.h" + +std::once_flag Steam_Inventory::items_loading; +std::atomic_bool Steam_Inventory::items_loaded(false); +std::map> Steam_Inventory::items; \ No newline at end of file diff --git a/dll/steam_inventory.h b/dll/steam_inventory.h index 5ebce4c8..1aedef94 100644 --- a/dll/steam_inventory.h +++ b/dll/steam_inventory.h @@ -46,7 +46,9 @@ class Steam_Inventory : std::vector inventory_requests; - std::map> items; + static std::once_flag items_loading; + static std::atomic_bool items_loaded; + static std::map> items; // Like typedefs using item_iterator = std::map>::iterator; using attr_iterator = std::map::iterator; @@ -58,8 +60,6 @@ class Steam_Inventory : // Or find a server somewhere to hold the data for us then cache on local settings. bool need_load_definitions = true; - std::atomic_bool items_loaded; - struct Steam_Inventory_Requests* new_inventory_result(const SteamItemInstanceID_t* pInstanceIDs = NULL, uint32 unCountInstanceIDs = 0) { static SteamInventoryResult_t result; @@ -91,11 +91,13 @@ public: Steam_Inventory(class Settings *settings, class SteamCallResults *callback_results, class SteamCallBacks *callbacks) { - std::string items_db_file(Local_Storage::get_game_settings_path() + "items.json"); - PRINT_DEBUG("Items file path: %s\n", items_db_file.c_str()); - items_loaded = false; - std::thread items_load_thread(read_items_db, items_db_file, &items, &items_loaded); - items_load_thread.detach(); + std::call_once(items_loading, [&]() + { + std::string items_db_file(Local_Storage::get_game_settings_path() + "items.json"); + PRINT_DEBUG("Items file path: %s\n", items_db_file.c_str()); + std::thread items_load_thread(read_items_db, items_db_file, &items, &items_loaded); + items_load_thread.detach(); + }); this->settings = settings; this->callbacks = callbacks; @@ -815,4 +817,4 @@ bool SubmitUpdateProperties( SteamInventoryUpdateHandle_t handle, SteamInventory PRINT_DEBUG("SubmitUpdateProperties\n"); } -}; +}; \ No newline at end of file