Small refactor: main start/cleanup threads logic (#2142)
This commit is contained in:
parent
347f33fb2d
commit
5259d249f3
@ -4,19 +4,16 @@
|
|||||||
|
|
||||||
namespace big
|
namespace big
|
||||||
{
|
{
|
||||||
void menu_settings::destroy()
|
|
||||||
{
|
|
||||||
m_running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void menu_settings::init(const file& save_file)
|
void menu_settings::init(const file& save_file)
|
||||||
{
|
{
|
||||||
m_running = true;
|
|
||||||
m_save_file = save_file;
|
m_save_file = save_file;
|
||||||
load();
|
load();
|
||||||
|
|
||||||
g_thread_pool->push([this] {
|
g_thread_pool->push([this] {
|
||||||
while (m_running)
|
while (!g_running)
|
||||||
|
std::this_thread::yield();
|
||||||
|
|
||||||
|
while (g_running)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(100ms);
|
std::this_thread::sleep_for(100ms);
|
||||||
attempt_save();
|
attempt_save();
|
||||||
|
@ -47,7 +47,6 @@ namespace big
|
|||||||
class menu_settings
|
class menu_settings
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void destroy();
|
|
||||||
void init(const file& save_file);
|
void init(const file& save_file);
|
||||||
|
|
||||||
void attempt_save();
|
void attempt_save();
|
||||||
@ -59,8 +58,6 @@ namespace big
|
|||||||
bool save();
|
bool save();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_running;
|
|
||||||
|
|
||||||
file m_save_file;
|
file m_save_file;
|
||||||
|
|
||||||
nlohmann::json m_default_options;
|
nlohmann::json m_default_options;
|
||||||
|
@ -142,9 +142,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
|
|||||||
|
|
||||||
g_running = true;
|
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)
|
while (g_running)
|
||||||
std::this_thread::sleep_for(500ms);
|
std::this_thread::sleep_for(500ms);
|
||||||
|
|
||||||
@ -160,9 +157,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
|
|||||||
native_hooks_instance.reset();
|
native_hooks_instance.reset();
|
||||||
LOG(INFO) << "Dynamic native hooker uninitialized.";
|
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
|
// Make sure that all threads created don't have any blocking loops
|
||||||
// otherwise make sure that they have stopped executing
|
// otherwise make sure that they have stopped executing
|
||||||
thread_pool_instance->destroy();
|
thread_pool_instance->destroy();
|
||||||
|
@ -138,6 +138,9 @@ namespace big
|
|||||||
m_file_path(g_file_manager.get_project_file("./players.json").get_path())
|
m_file_path(g_file_manager.get_project_file("./players.json").get_path())
|
||||||
{
|
{
|
||||||
load();
|
load();
|
||||||
|
|
||||||
|
start_update_loop();
|
||||||
|
|
||||||
g_player_database_service = this;
|
g_player_database_service = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,12 +273,15 @@ namespace big
|
|||||||
|
|
||||||
void player_database_service::start_update_loop()
|
void player_database_service::start_update_loop()
|
||||||
{
|
{
|
||||||
if (!g.player_db.update_player_online_states)
|
|
||||||
return;
|
|
||||||
|
|
||||||
g_thread_pool->push([this] {
|
g_thread_pool->push([this] {
|
||||||
|
while (!g_running)
|
||||||
|
std::this_thread::yield();
|
||||||
|
|
||||||
static auto last_update = std::chrono::high_resolution_clock::now() - 45s;
|
static auto last_update = std::chrono::high_resolution_clock::now() - 45s;
|
||||||
while (g_running && g.player_db.update_player_online_states)
|
|
||||||
|
while (g_running)
|
||||||
|
{
|
||||||
|
if (g.player_db.update_player_online_states)
|
||||||
{
|
{
|
||||||
const auto cur = std::chrono::high_resolution_clock::now();
|
const auto cur = std::chrono::high_resolution_clock::now();
|
||||||
if (cur - last_update > 45s && !updating)
|
if (cur - last_update > 45s && !updating)
|
||||||
@ -287,6 +293,7 @@ namespace big
|
|||||||
last_update = std::chrono::high_resolution_clock::now();
|
last_update = std::chrono::high_resolution_clock::now();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::this_thread::sleep_for(1s);
|
std::this_thread::sleep_for(1s);
|
||||||
}
|
}
|
||||||
|
@ -540,10 +540,12 @@ namespace big::ped
|
|||||||
|
|
||||||
inline void set_ped_random_component_variation(Ped 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;
|
return std::rand() % (upper_bound - lower_bound + 1) + lower_bound;
|
||||||
};
|
};
|
||||||
|
|
||||||
outfit::components_t components;
|
outfit::components_t components;
|
||||||
|
|
||||||
for (auto& item : components.items)
|
for (auto& item : components.items)
|
||||||
{
|
{
|
||||||
int drawable_id_max = PED::GET_NUMBER_OF_PED_DRAWABLE_VARIATIONS(ped, item.id) - 1;
|
int drawable_id_max = PED::GET_NUMBER_OF_PED_DRAWABLE_VARIATIONS(ped, item.id) - 1;
|
||||||
|
Reference in New Issue
Block a user