From 0a8a46324503b52b068d2f0c16c776c27a35d08f Mon Sep 17 00:00:00 2001 From: Marvin D <41352111+IcePixelx@users.noreply.github.com> Date: Sat, 20 Aug 2022 11:28:28 +0200 Subject: [PATCH] PluginSystem_Reload logic --- r5dev/pluginsystem/pluginsystem.cpp | 36 +++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/r5dev/pluginsystem/pluginsystem.cpp b/r5dev/pluginsystem/pluginsystem.cpp index 5f22ccbd..f42c8dcd 100644 --- a/r5dev/pluginsystem/pluginsystem.cpp +++ b/r5dev/pluginsystem/pluginsystem.cpp @@ -35,7 +35,26 @@ void CPluginSystem::PluginSystem_Init() //----------------------------------------------------------------------------- void CPluginSystem::PluginSystem_Reload() { + CreateDirectories("bin\\x64_plugins\\."); + for (auto& it : fs::directory_iterator("bin\\x64_plugins")) + { + if (!it.is_regular_file()) + continue; + + if (auto path = it.path(); path.has_filename() && path.has_extension() && path.extension().compare(".dll") == 0) + { + bool addInstance = true; + for (auto& inst : pluginInstances) + { + if (inst.m_svPluginFullPath.compare(path.u8string()) == 0) + addInstance = false; + } + + if (addInstance) + pluginInstances.push_back(PluginInstance(path.filename().u8string(), path.u8string())); + } + } } //----------------------------------------------------------------------------- @@ -45,18 +64,17 @@ void CPluginSystem::PluginSystem_Reload() //----------------------------------------------------------------------------- bool CPluginSystem::LoadPluginInstance(PluginInstance& pluginInst) { - if (pluginInst.m_bIsLoaded) // Ugh need to make proper module manager. + if (pluginInst.m_bIsLoaded) return false; HMODULE loadedPlugin = LoadLibraryA(pluginInst.m_svPluginFullPath.c_str()); - if (!loadedPlugin) + if (loadedPlugin == INVALID_HANDLE_VALUE) return false; pluginInst.m_hModule = CModule(pluginInst.m_svPluginName); auto onLoadFn = pluginInst.m_hModule.GetExportedFunction("PluginInstance_OnLoad").RCast(); - if (!onLoadFn) - return false; + Assert(onLoadFn); onLoadFn(pluginInst.m_hModule, g_GameDll); @@ -68,7 +86,7 @@ bool CPluginSystem::LoadPluginInstance(PluginInstance& pluginInst) if (getDescFn) pluginInst.m_svDescription = getDescFn(); - return pluginInst.m_bIsLoaded = true; // Change this sooooooon. + return pluginInst.m_bIsLoaded = true; } //----------------------------------------------------------------------------- @@ -81,16 +99,16 @@ bool CPluginSystem::UnloadPluginInstance(PluginInstance& pluginInst) if (!pluginInst.m_bIsLoaded) return false; - if (!pluginInst.m_hModule.GetModuleBase()) - return false; + Assert(pluginInst.m_hModule.GetModuleBase()); auto onUnloadFn = pluginInst.m_hModule.GetExportedFunction("PluginInstance_OnUnload").RCast(); if (onUnloadFn) onUnloadFn(g_GameDll); bool unloadOk = FreeLibrary((HMODULE)pluginInst.m_hModule.GetModuleBase()); - if (unloadOk) - pluginInst.m_bIsLoaded = false; + Assert(unloadOk); + + pluginInst.m_bIsLoaded = false; return unloadOk; }