From b099c1c9ba2f6680a686e2f0cefde1e33dda5100 Mon Sep 17 00:00:00 2001 From: MiranDMC Date: Wed, 11 Sep 2024 17:43:41 +0200 Subject: [PATCH] Unloading CLEO plugins. (#185) Unloading CLEO plugins. Log improvements --- cleo_plugins/Audio/CSoundSystem.cpp | 1 + source/CCustomOpcodeSystem.h | 6 ++++-- source/CDebug.h | 1 + source/CPluginSystem.cpp | 21 +++++++++++++++++++-- source/CPluginSystem.h | 13 +++++++++++-- source/CScriptEngine.cpp | 1 + source/CleoBase.cpp | 2 ++ 7 files changed, 39 insertions(+), 6 deletions(-) diff --git a/cleo_plugins/Audio/CSoundSystem.cpp b/cleo_plugins/Audio/CSoundSystem.cpp index 08179106..719ed529 100644 --- a/cleo_plugins/Audio/CSoundSystem.cpp +++ b/cleo_plugins/Audio/CSoundSystem.cpp @@ -46,6 +46,7 @@ namespace CLEO CSoundSystem::~CSoundSystem() { + TRACE(""); // seaprator TRACE("Finalizing SoundSystem..."); Clear(); diff --git a/source/CCustomOpcodeSystem.h b/source/CCustomOpcodeSystem.h index 9059bdc2..fd450c7c 100644 --- a/source/CCustomOpcodeSystem.h +++ b/source/CCustomOpcodeSystem.h @@ -38,8 +38,10 @@ namespace CLEO void Init(); ~CCustomOpcodeSystem() { - TRACE("Last opcode executed: %04X", lastOpcode); - TRACE("Previous opcode executed: %04X", prevOpcode); + TRACE(""); // separator + TRACE("Custom Opcode System finalized:"); + TRACE(" Last opcode executed: %04X", lastOpcode); + TRACE(" Previous opcode executed: %04X", prevOpcode); } static bool RegisterOpcode(WORD opcode, CustomOpcodeHandler callback); diff --git a/source/CDebug.h b/source/CDebug.h index c1da6c0e..c6223d54 100644 --- a/source/CDebug.h +++ b/source/CDebug.h @@ -24,6 +24,7 @@ class CDebug ~CDebug() { + CLEO::Trace(CLEO::eLogLevel::Default, ""); // separator CLEO::Trace(CLEO::eLogLevel::Default, "Log finished."); } diff --git a/source/CPluginSystem.cpp b/source/CPluginSystem.cpp index 3c8b2e5f..1a6a6dcc 100644 --- a/source/CPluginSystem.cpp +++ b/source/CPluginSystem.cpp @@ -7,7 +7,7 @@ using namespace CLEO; CPluginSystem::~CPluginSystem() { - std::for_each(plugins.begin(), plugins.end(), FreeLibrary); + UnloadPlugins(); } void CPluginSystem::LoadPlugins() @@ -79,7 +79,7 @@ void CPluginSystem::LoadPlugins() continue; } - plugins.push_back(hlib); + plugins.emplace_back(filename, hlib); } TRACE(""); // separator } @@ -91,6 +91,23 @@ void CPluginSystem::LoadPlugins() pluginsLoaded = true; } +void CPluginSystem::UnloadPlugins() +{ + if (!pluginsLoaded) return; + + TRACE(""); // separator + TRACE("Unloading CLEO plugins:"); + for (const auto& plugin : plugins) + { + TRACE(" - Unloading '%s' at 0x%08X", plugin.name.c_str(), plugin.handle); + FreeLibrary(plugin.handle); + } + TRACE("CLEO plugins unloaded"); + + plugins.clear(); + pluginsLoaded = false; +} + size_t CPluginSystem::GetNumPlugins() const { return plugins.size(); diff --git a/source/CPluginSystem.h b/source/CPluginSystem.h index a1b67287..7b99d4a5 100644 --- a/source/CPluginSystem.h +++ b/source/CPluginSystem.h @@ -2,8 +2,8 @@ #include "FileEnumerator.h" #include "CDebug.h" #include -#include #include +#include #include @@ -11,7 +11,15 @@ namespace CLEO { class CPluginSystem { - std::list plugins; + struct PluginEntry + { + std::string name; + HMODULE handle = nullptr; + + PluginEntry() = default; + PluginEntry(std::string name, HMODULE handle) : name(name), handle(handle) {} + }; + std::list plugins; bool pluginsLoaded = false; public: @@ -20,6 +28,7 @@ namespace CLEO ~CPluginSystem(); void LoadPlugins(); + void UnloadPlugins(); size_t GetNumPlugins() const; }; } diff --git a/source/CScriptEngine.cpp b/source/CScriptEngine.cpp index 17882522..fe15d471 100644 --- a/source/CScriptEngine.cpp +++ b/source/CScriptEngine.cpp @@ -994,6 +994,7 @@ namespace CLEO void CScriptEngine::LoadCustomScripts() { + TRACE(""); // separator TRACE("Listing CLEO scripts:"); std::set found; diff --git a/source/CleoBase.cpp b/source/CleoBase.cpp index 8b598c0d..96bf2752 100644 --- a/source/CleoBase.cpp +++ b/source/CleoBase.cpp @@ -155,6 +155,8 @@ namespace CLEO m_bStarted = false; ScriptEngine.GameEnd(); + + PluginSystem.UnloadPlugins(); } void CCleoInstance::GameBegin()