From df102c7ae2289357024be2da90074f0f35774968 Mon Sep 17 00:00:00 2001 From: gir489 <100792176+gir489returns@users.noreply.github.com> Date: Wed, 17 Jul 2024 08:13:10 -0400 Subject: [PATCH] Refactor no_idle_kick to not store the address of the tunables. (#3363) * Refactor no_idle_kick to not store the address of the tunables. * Added sanity checks to the tunable pointers. --- src/backend/looped/tunables/no_idle_kick.cpp | 49 ++++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/backend/looped/tunables/no_idle_kick.cpp b/src/backend/looped/tunables/no_idle_kick.cpp index 6f9365f5..38171ce2 100644 --- a/src/backend/looped/tunables/no_idle_kick.cpp +++ b/src/backend/looped/tunables/no_idle_kick.cpp @@ -1,7 +1,7 @@ #include "backend/looped_command.hpp" -#include "services/tunables/tunables_service.hpp" #include "core/scr_globals.hpp" #include "natives.hpp" +#include "services/tunables/tunables_service.hpp" namespace big { @@ -9,48 +9,47 @@ namespace big { using looped_command::looped_command; - std::array m_tunables = {nullptr}; std::array m_restore; - bool m_ready_to_use; + bool m_backed_up; + + const std::vector m_tunable_hashes = {"IDLEKICK_WARNING1"_J, "IDLEKICK_WARNING2"_J, "IDLEKICK_WARNING3"_J, "IDLEKICK_KICK"_J, "ConstrainedKick_Warning1"_J, "ConstrainedKick_Warning2"_J, "ConstrainedKick_Warning3"_J, "ConstrainedKick_Kick"_J}; virtual void on_tick() override { - if (!m_ready_to_use) + if (!m_backed_up) [[unlikely]] { - m_tunables[0] = g_tunables_service->get_tunable("IDLEKICK_WARNING1"_J); - m_tunables[1] = g_tunables_service->get_tunable("IDLEKICK_WARNING2"_J); - m_tunables[2] = g_tunables_service->get_tunable("IDLEKICK_WARNING3"_J); - m_tunables[3] = g_tunables_service->get_tunable("IDLEKICK_KICK"_J); - m_tunables[4] = g_tunables_service->get_tunable("ConstrainedKick_Warning1"_J); - m_tunables[5] = g_tunables_service->get_tunable("ConstrainedKick_Warning2"_J); - m_tunables[6] = g_tunables_service->get_tunable("ConstrainedKick_Warning3"_J); - m_tunables[7] = g_tunables_service->get_tunable("ConstrainedKick_Kick"_J); - - // create backup of tunables - m_ready_to_use = true; - for (int i = 0; i < m_restore.size(); ++i) + bool did_fail = false; + for (int i = 0; i < m_restore.size(); i++) { - if (m_ready_to_use = m_tunables[i]; !m_ready_to_use) - break; - m_restore[i] = *m_tunables[i]; + if (auto tunable = g_tunables_service->get_tunable(m_tunable_hashes[i])) + { + m_restore[i] = *tunable; + } + else + { + did_fail = true; + } } + m_backed_up = !did_fail; } else { - for (const auto& tunable : m_tunables) + for (Hash hash_iter : m_tunable_hashes) { - if (tunable) - *tunable = INT_MAX; + if (auto tunable_ptr = g_tunables_service->get_tunable(hash_iter)) + { + *tunable_ptr = INT_MAX; + } } } } virtual void on_disable() override { - for (int i = 0; m_ready_to_use && i < m_restore.size(); ++i) + if (m_backed_up) { - if (m_tunables[i]) - *m_tunables[i] = m_restore[i]; + for (int i = 0; i < m_restore.size(); ++i) + *g_tunables_service->get_tunable(m_tunable_hashes[i]) = m_restore[i]; } } };