From 5259d249f37d7779644388b88ce1ad2b76ae8e1c Mon Sep 17 00:00:00 2001 From: Quentin Date: Mon, 18 Sep 2023 23:13:46 +0200 Subject: [PATCH] Small refactor: main start/cleanup threads logic (#2142) --- src/core/settings.cpp | 11 +++---- src/core/settings.hpp | 3 -- src/main.cpp | 12 ++----- .../player_database_service.cpp | 31 ++++++++++++------- src/util/ped.hpp | 4 ++- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 10c67a53..e1424fd1 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -4,19 +4,16 @@ namespace big { - void menu_settings::destroy() - { - m_running = false; - } - void menu_settings::init(const file& save_file) { - m_running = true; m_save_file = save_file; load(); g_thread_pool->push([this] { - while (m_running) + while (!g_running) + std::this_thread::yield(); + + while (g_running) { std::this_thread::sleep_for(100ms); attempt_save(); diff --git a/src/core/settings.hpp b/src/core/settings.hpp index b099657d..57884b42 100644 --- a/src/core/settings.hpp +++ b/src/core/settings.hpp @@ -47,7 +47,6 @@ namespace big class menu_settings { public: - void destroy(); void init(const file& save_file); void attempt_save(); @@ -59,8 +58,6 @@ namespace big bool save(); private: - bool m_running; - file m_save_file; nlohmann::json m_default_options; diff --git a/src/main.cpp b/src/main.cpp index a00ee8b2..1a44d0dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -142,13 +142,10 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID) g_running = true; - // start update loop after setting g_running to true to prevent it from exiting instantly - g_player_database_service->start_update_loop(); - while (g_running) std::this_thread::sleep_for(500ms); - g_script_mgr.remove_all_scripts(); + g_script_mgr.remove_all_scripts(); LOG(INFO) << "Scripts unregistered."; lua_manager_instance.reset(); @@ -160,9 +157,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID) native_hooks_instance.reset(); LOG(INFO) << "Dynamic native hooker uninitialized."; - // cleans up the thread responsible for saving settings - g.destroy(); - // Make sure that all threads created don't have any blocking loops // otherwise make sure that they have stopped executing thread_pool_instance->destroy(); @@ -200,8 +194,8 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID) LOG(INFO) << "Custom Text Service reset."; context_menu_service_instance.reset(); LOG(INFO) << "Context Service reset."; - xml_vehicles_service_instance.reset(); - LOG(INFO) << "Xml Vehicles Service reset."; + xml_vehicles_service_instance.reset(); + LOG(INFO) << "Xml Vehicles Service reset."; LOG(INFO) << "Services uninitialized."; hooking_instance.reset(); diff --git a/src/services/player_database/player_database_service.cpp b/src/services/player_database/player_database_service.cpp index 610e6ab9..b8d83b57 100644 --- a/src/services/player_database/player_database_service.cpp +++ b/src/services/player_database/player_database_service.cpp @@ -138,6 +138,9 @@ namespace big m_file_path(g_file_manager.get_project_file("./players.json").get_path()) { load(); + + start_update_loop(); + g_player_database_service = this; } @@ -270,22 +273,26 @@ namespace big void player_database_service::start_update_loop() { - if (!g.player_db.update_player_online_states) - return; - g_thread_pool->push([this] { + while (!g_running) + std::this_thread::yield(); + static auto last_update = std::chrono::high_resolution_clock::now() - 45s; - while (g_running && g.player_db.update_player_online_states) + + while (g_running) { - const auto cur = std::chrono::high_resolution_clock::now(); - if (cur - last_update > 45s && !updating) + if (g.player_db.update_player_online_states) { - updating = true; - g_fiber_pool->queue_job([this] { - update_player_states(true); - updating = false; - last_update = std::chrono::high_resolution_clock::now(); - }); + const auto cur = std::chrono::high_resolution_clock::now(); + if (cur - last_update > 45s && !updating) + { + updating = true; + g_fiber_pool->queue_job([this] { + update_player_states(true); + updating = false; + last_update = std::chrono::high_resolution_clock::now(); + }); + } } std::this_thread::sleep_for(1s); diff --git a/src/util/ped.hpp b/src/util/ped.hpp index 58235d42..fbd515a3 100644 --- a/src/util/ped.hpp +++ b/src/util/ped.hpp @@ -540,10 +540,12 @@ namespace big::ped inline void set_ped_random_component_variation(Ped ped) { - auto range = [](int lower_bound, int upper_bound) -> int { + constexpr auto range = [](int lower_bound, int upper_bound) -> int { return std::rand() % (upper_bound - lower_bound + 1) + lower_bound; }; + outfit::components_t components; + for (auto& item : components.items) { int drawable_id_max = PED::GET_NUMBER_OF_PED_DRAWABLE_VARIATIONS(ped, item.id) - 1;