From e3813cf027c9fb6d086db087f13557f86eb4730a Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sat, 12 Oct 2024 22:51:50 +0200 Subject: [PATCH] Settings: semi-automatic callback cleanup --- src/client/clientlauncher.cpp | 5 ++-- src/client/clientmap.cpp | 3 +-- src/client/clouds.cpp | 5 +--- src/client/fontengine.cpp | 3 +-- src/client/game.cpp | 47 +++------------------------------- src/client/hud.cpp | 4 +-- src/client/localplayer.cpp | 5 +--- src/client/renderingengine.cpp | 3 +-- src/gui/guiEngine.cpp | 2 +- src/settings.cpp | 23 +++++++++-------- src/settings.h | 3 +-- 11 files changed, 25 insertions(+), 78 deletions(-) diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index 13b87f2ef..60d4fb6c8 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -71,9 +71,8 @@ static void dump_start_data(const GameStartData &data) ClientLauncher::~ClientLauncher() { delete input; - g_settings->deregisterChangedCallback("dpi_change_notifier", setting_changed_callback, this); - g_settings->deregisterChangedCallback("display_density_factor", setting_changed_callback, this); - g_settings->deregisterChangedCallback("gui_scaling", setting_changed_callback, this); + + g_settings->deregisterAllChangedCallbacks(this); delete g_fontengine; g_fontengine = nullptr; diff --git a/src/client/clientmap.cpp b/src/client/clientmap.cpp index ab826c775..b62268bea 100644 --- a/src/client/clientmap.cpp +++ b/src/client/clientmap.cpp @@ -140,8 +140,7 @@ void ClientMap::onSettingChanged(std::string_view name, bool all) ClientMap::~ClientMap() { - for (const auto &name : ClientMap_settings) - g_settings->deregisterChangedCallback(name, on_settings_changed, this); + g_settings->deregisterAllChangedCallbacks(this); } void ClientMap::updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset, video::SColor light_color) diff --git a/src/client/clouds.cpp b/src/client/clouds.cpp index 96926f3e0..b18f70c37 100644 --- a/src/client/clouds.cpp +++ b/src/client/clouds.cpp @@ -76,10 +76,7 @@ Clouds::Clouds(scene::ISceneManager* mgr, IShaderSource *ssrc, Clouds::~Clouds() { - g_settings->deregisterChangedCallback("enable_3d_clouds", - &cloud_3d_setting_changed, this); - g_settings->deregisterChangedCallback("soft_clouds", - &cloud_3d_setting_changed, this); + g_settings->deregisterAllChangedCallbacks(this); } void Clouds::OnRegisterSceneNode() diff --git a/src/client/fontengine.cpp b/src/client/fontengine.cpp index 5475e9768..2d00f3cf4 100644 --- a/src/client/fontengine.cpp +++ b/src/client/fontengine.cpp @@ -68,8 +68,7 @@ FontEngine::FontEngine(gui::IGUIEnvironment* env) : /******************************************************************************/ FontEngine::~FontEngine() { - for (auto name : settings) - g_settings->deregisterChangedCallback(name, font_setting_changed, this); + g_settings->deregisterAllChangedCallbacks(this); cleanCache(); } diff --git a/src/client/game.cpp b/src/client/game.cpp index 9619a4a8a..8e01c40da 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -451,8 +451,7 @@ public: ~GameGlobalShaderConstantSetter() { - for (auto &name : SETTING_CALLBACKS) - g_settings->deregisterChangedCallback(name, settingsCallback, this); + g_settings->deregisterAllChangedCallbacks(this); } void onSetConstants(video::IMaterialRendererServices *services) override @@ -1046,48 +1045,8 @@ Game::~Game() clearTextureNameCache(); - g_settings->deregisterChangedCallback("chat_log_level", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("disable_escape_sequences", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("doubletap_jump", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("enable_clouds", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("enable_joysticks", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("enable_particles", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("enable_fog", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("mouse_sensitivity", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("joystick_frustum_sensitivity", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("repeat_place_time", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("repeat_dig_time", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("noclip", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("free_move", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("fog_start", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("cinematic", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("cinematic_camera_smoothing", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("camera_smoothing", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("invert_mouse", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("enable_hotbar_mouse_wheel", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("invert_hotbar_mouse_wheel", - &settingChangedCallback, this); - g_settings->deregisterChangedCallback("pause_on_lost_focus", - &settingChangedCallback, this); + g_settings->deregisterAllChangedCallbacks(this); + if (m_rendering_engine) m_rendering_engine->finalize(); } diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 6fd6b7d03..dcb38c9b6 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -170,9 +170,7 @@ void Hud::readScalingSetting() Hud::~Hud() { - g_settings->deregisterChangedCallback("dpi_change_notifier", setting_changed_callback, this); - g_settings->deregisterChangedCallback("display_density_factor", setting_changed_callback, this); - g_settings->deregisterChangedCallback("hud_scaling", setting_changed_callback, this); + g_settings->deregisterAllChangedCallbacks(this); if (m_selection_mesh) m_selection_mesh->drop(); diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp index 75f755578..cf2e63698 100644 --- a/src/client/localplayer.cpp +++ b/src/client/localplayer.cpp @@ -60,10 +60,7 @@ void PlayerSettings::registerSettingsCallback() void PlayerSettings::deregisterSettingsCallback() { - for (auto &name : PlayerSettings_names) { - g_settings->deregisterChangedCallback(name, - &PlayerSettings::settingsChangedCallback, this); - } + g_settings->deregisterAllChangedCallbacks(this); } void PlayerSettings::settingsChangedCallback(const std::string &name, void *data) diff --git a/src/client/renderingengine.cpp b/src/client/renderingengine.cpp index 2f714e020..522383f22 100644 --- a/src/client/renderingengine.cpp +++ b/src/client/renderingengine.cpp @@ -238,8 +238,7 @@ RenderingEngine::~RenderingEngine() { sanity_check(s_singleton == this); - g_settings->deregisterChangedCallback("fullscreen", settingChangedCallback, this); - g_settings->deregisterChangedCallback("window_maximized", settingChangedCallback, this); + g_settings->deregisterAllChangedCallbacks(this); core.reset(); m_device->closeDevice(); diff --git a/src/gui/guiEngine.cpp b/src/gui/guiEngine.cpp index 200c26fa0..8cdbb97fb 100644 --- a/src/gui/guiEngine.cpp +++ b/src/gui/guiEngine.cpp @@ -404,7 +404,7 @@ void GUIEngine::run() /******************************************************************************/ GUIEngine::~GUIEngine() { - g_settings->deregisterChangedCallback("fullscreen", fullscreenChangedCallback, this); + g_settings->deregisterAllChangedCallbacks(this); // deinitialize script first. gc destructors might depend on other stuff infostream << "GUIEngine: Deinitializing scripting" << std::endl; diff --git a/src/settings.cpp b/src/settings.cpp index e017b3323..867801e06 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1048,21 +1048,22 @@ void Settings::registerChangedCallback(const std::string &name, m_callbacks[name].emplace_back(cbf, userdata); } -void Settings::deregisterChangedCallback(const std::string &name, - SettingsChangedCallback cbf, void *userdata) +size_t Settings::deregisterAllChangedCallbacks(const void *userdata) { MutexAutoLock lock(m_callback_mutex); - SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name); - if (it_cbks != m_callbacks.end()) { - SettingsCallbackList &cbks = it_cbks->second; - - SettingsCallbackList::iterator position = - std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata)); - - if (position != cbks.end()) - cbks.erase(position); + size_t n_removed = 0; + for (auto &settings : m_callbacks) { + for (auto cb = settings.second.begin(); cb != settings.second.end();) { + if (cb->second == userdata) { + cb = settings.second.erase(cb); + n_removed++; + } else { + ++cb; + } + } } + return n_removed; } void Settings::removeSecureSettings() diff --git a/src/settings.h b/src/settings.h index 692001c59..8fc5b8258 100644 --- a/src/settings.h +++ b/src/settings.h @@ -238,8 +238,7 @@ public: void registerChangedCallback(const std::string &name, SettingsChangedCallback cbf, void *userdata = NULL); - void deregisterChangedCallback(const std::string &name, - SettingsChangedCallback cbf, void *userdata = NULL); + size_t deregisterAllChangedCallbacks(const void *userdata); void removeSecureSettings();