mirror of
https://github.com/Mauler125/r5sdk.git
synced 2025-02-09 19:15:03 +01:00
Light pluginsystem cleanup
Light cleanup and improvements. Always load plugin dll's from "GAME" first before searching through mods to prevent mods from overriding them.
This commit is contained in:
parent
5af364f325
commit
feffcdfa94
@ -144,11 +144,11 @@ bool CModAppSystemGroup::StaticCreate(CModAppSystemGroup* pModAppSystemGroup)
|
||||
//-----------------------------------------------------------------------------
|
||||
void CModAppSystemGroup::InitPluginSystem(CModAppSystemGroup* pModAppSystemGroup)
|
||||
{
|
||||
// DEBUG CODE FOR PLUGINS
|
||||
g_pPluginSystem->PluginSystem_Init();
|
||||
for (auto& it : g_pPluginSystem->GetPluginInstances())
|
||||
g_pPluginSystem->Init();
|
||||
|
||||
for (auto& it : g_pPluginSystem->GetInstances())
|
||||
{
|
||||
if (g_pPluginSystem->LoadPluginInstance(it))
|
||||
if (g_pPluginSystem->LoadInstance(it))
|
||||
Msg(eDLL_T::ENGINE, "Loaded plugin: '%s'\n", it.m_Name.String());
|
||||
else
|
||||
Warning(eDLL_T::ENGINE, "Failed loading plugin: '%s'\n", it.m_Name.String());
|
||||
|
@ -7,36 +7,39 @@
|
||||
//=============================================================================//
|
||||
|
||||
#include "core/stdafx.h"
|
||||
#include "tier2/fileutils.h"
|
||||
#include "filesystem/filesystem.h"
|
||||
#include "pluginsystem.h"
|
||||
#include <filesystem/filesystem.h>
|
||||
#include <tier2/fileutils.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: initialize the plugin system
|
||||
// Input :
|
||||
//-----------------------------------------------------------------------------
|
||||
void CPluginSystem::PluginSystem_Init()
|
||||
void CPluginSystem::Init()
|
||||
{
|
||||
FileSystem()->CreateDirHierarchy(PLUGIN_INSTALL_DIR);
|
||||
if (!FileSystem()->IsDirectory(PLUGIN_INSTALL_DIR, "GAME"))
|
||||
return; // No plugins to load.
|
||||
|
||||
CUtlVector< CUtlString > pluginPaths;
|
||||
AddFilesToList(pluginPaths, PLUGIN_INSTALL_DIR, "dll");
|
||||
AddFilesToList(pluginPaths, PLUGIN_INSTALL_DIR, "dll", "GAME");
|
||||
|
||||
for (int i = 0; i < pluginPaths.Count(); ++i)
|
||||
{
|
||||
CUtlString& path = pluginPaths[i];
|
||||
|
||||
bool addInstance = true;
|
||||
for (auto& inst : pluginInstances)
|
||||
FOR_EACH_VEC(m_Instances, j)
|
||||
{
|
||||
if (inst.m_Path.IsEqual_CaseInsensitive(path.String()) == 0)
|
||||
const PluginInstance_t& instance = m_Instances[j];
|
||||
|
||||
if (instance.m_Path.IsEqual_CaseInsensitive(path.String()) == 0)
|
||||
addInstance = false;
|
||||
}
|
||||
|
||||
if (addInstance)
|
||||
{
|
||||
const char* baseFileName = V_UnqualifiedFileName(path.String());
|
||||
pluginInstances.push_back(PluginInstance_t(baseFileName, path.String()));
|
||||
m_Instances.AddToTail(PluginInstance_t(baseFileName, path.String()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -46,7 +49,7 @@ void CPluginSystem::PluginSystem_Init()
|
||||
// Input : pluginInst* -
|
||||
// Output : bool
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CPluginSystem::LoadPluginInstance(PluginInstance_t& pluginInst)
|
||||
bool CPluginSystem::LoadInstance(PluginInstance_t& pluginInst)
|
||||
{
|
||||
if (pluginInst.m_bIsLoaded)
|
||||
return false;
|
||||
@ -80,7 +83,7 @@ bool CPluginSystem::LoadPluginInstance(PluginInstance_t& pluginInst)
|
||||
// Input : pluginInst* -
|
||||
// Output : bool
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CPluginSystem::UnloadPluginInstance(PluginInstance_t& pluginInst)
|
||||
bool CPluginSystem::UnloadInstance(PluginInstance_t& pluginInst)
|
||||
{
|
||||
if (!pluginInst.m_bIsLoaded)
|
||||
return false;
|
||||
@ -107,27 +110,26 @@ bool CPluginSystem::UnloadPluginInstance(PluginInstance_t& pluginInst)
|
||||
// Input : pluginInst* -
|
||||
// Output : bool
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CPluginSystem::ReloadPluginInstance(PluginInstance_t& pluginInst)
|
||||
bool CPluginSystem::ReloadInstance(PluginInstance_t& pluginInst)
|
||||
{
|
||||
return UnloadPluginInstance(pluginInst) ? LoadPluginInstance(pluginInst) : false;
|
||||
return UnloadInstance(pluginInst) ? LoadInstance(pluginInst) : false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: get all plugin instances
|
||||
// Input :
|
||||
// Output : vector<CPluginSystem::PluginInstance>&
|
||||
// Output : CUtlVector<CPluginSystem::PluginInstance>&
|
||||
//-----------------------------------------------------------------------------
|
||||
vector<CPluginSystem::PluginInstance_t>& CPluginSystem::GetPluginInstances()
|
||||
CUtlVector<CPluginSystem::PluginInstance_t>& CPluginSystem::GetInstances()
|
||||
{
|
||||
return pluginInstances;
|
||||
return m_Instances;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: add plugin callback for function
|
||||
// Input : *help
|
||||
// Output : void
|
||||
//-----------------------------------------------------------------------------
|
||||
void CPluginSystem::AddPluginCallback(PluginHelpWithAnything_t* help)
|
||||
void CPluginSystem::AddCallback(PluginHelpWithAnything_t* help)
|
||||
{
|
||||
#define ADD_PLUGIN_CALLBACK(fn, callback, function) callback += reinterpret_cast<fn>(function)
|
||||
|
||||
@ -153,9 +155,8 @@ void CPluginSystem::AddPluginCallback(PluginHelpWithAnything_t* help)
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: remove plugin callback for function
|
||||
// Input : *help
|
||||
// Output : void
|
||||
//-----------------------------------------------------------------------------
|
||||
void CPluginSystem::RemovePluginCallback(PluginHelpWithAnything_t* help)
|
||||
void CPluginSystem::RemoveCallback(PluginHelpWithAnything_t* help)
|
||||
{
|
||||
#define REMOVE_PLUGIN_CALLBACK(fn, callback, function) callback -= reinterpret_cast<fn>(function)
|
||||
|
||||
@ -193,12 +194,12 @@ void* CPluginSystem::HelpWithAnything(PluginHelpWithAnything_t* help)
|
||||
}
|
||||
case PluginHelpWithAnything_t::ePluginHelp::PLUGIN_REGISTER_CALLBACK:
|
||||
{
|
||||
AddPluginCallback(help);
|
||||
AddCallback(help);
|
||||
break;
|
||||
}
|
||||
case PluginHelpWithAnything_t::ePluginHelp::PLUGIN_UNREGISTER_CALLBACK:
|
||||
{
|
||||
RemovePluginCallback(help);
|
||||
RemoveCallback(help);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@ -208,4 +209,4 @@ void* CPluginSystem::HelpWithAnything(PluginHelpWithAnything_t* help)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CPluginSystem* g_pPluginSystem = new CPluginSystem();
|
||||
CPluginSystem* g_pPluginSystem = new CPluginSystem();
|
||||
|
@ -12,17 +12,21 @@ template<typename T>
|
||||
class CPluginCallbackList
|
||||
{
|
||||
public:
|
||||
CPluginCallbackList() : m_vCallbacks() {}
|
||||
CPluginCallbackList(const vector<T>& cbs) : m_vCallbacks(cbs) {}
|
||||
CPluginCallbackList() {}
|
||||
CPluginCallbackList(const CUtlVector<T>& cbs)
|
||||
{
|
||||
for (auto it : cbs)
|
||||
m_vCallbacks.AddToTail(it);
|
||||
}
|
||||
|
||||
vector<T>& GetCallbacks() { return m_vCallbacks; }
|
||||
CUtlVector<T>& GetCallbacks() { return m_vCallbacks; }
|
||||
|
||||
operator bool()
|
||||
{
|
||||
return !this->m_vCallbacks.empty;
|
||||
return !this->m_vCallbacks.IsEmpty();
|
||||
}
|
||||
|
||||
vector<T>& operator!()
|
||||
CUtlVector<T>& operator!()
|
||||
{
|
||||
return this->m_vCallbacks;
|
||||
}
|
||||
@ -30,17 +34,17 @@ public:
|
||||
CPluginCallbackList<T>& operator+=(const T& rhs)
|
||||
{
|
||||
if (rhs)
|
||||
this->m_vCallbacks.push_back(rhs);
|
||||
this->m_vCallbacks.AddToTail(rhs);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
CPluginCallbackList<T>& operator+=(const vector<T>& rhs)
|
||||
CPluginCallbackList<T>& operator+=(const CUtlVector<T>& rhs)
|
||||
{
|
||||
for (auto it : rhs)
|
||||
{
|
||||
if (it)
|
||||
this->m_vCallbacks.push_back(it);
|
||||
this->m_vCallbacks.AddToTail(it);
|
||||
}
|
||||
|
||||
return *this;
|
||||
@ -48,23 +52,26 @@ public:
|
||||
|
||||
CPluginCallbackList<T>& operator-=(const T& rhs)
|
||||
{
|
||||
if (rhs) {
|
||||
auto it = std::find(m_vCallbacks.begin(), m_vCallbacks.end(), rhs);
|
||||
if (it != m_vCallbacks.end())
|
||||
m_vCallbacks.erase(it);
|
||||
if (rhs)
|
||||
{
|
||||
const int fnd = m_vCallbacks.Find(rhs);
|
||||
|
||||
if (fnd != m_vCallbacks.InvalidIndex())
|
||||
m_vCallbacks.Remove(fnd);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
CPluginCallbackList<T>& operator-=(const vector<T>& rhs)
|
||||
CPluginCallbackList<T>& operator-=(const CUtlVector<T>& rhs)
|
||||
{
|
||||
for (auto itc : rhs)
|
||||
{
|
||||
if (itc) {
|
||||
auto it = std::find(m_vCallbacks.begin(), m_vCallbacks.end(), itc);
|
||||
if (it != m_vCallbacks.end())
|
||||
m_vCallbacks.erase(it);
|
||||
const int fnd = m_vCallbacks.Find(itc);
|
||||
|
||||
if (fnd != m_vCallbacks.InvalidIndex())
|
||||
m_vCallbacks.Remove(fnd);
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,7 +79,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
vector<T> m_vCallbacks;
|
||||
CUtlVector<T> m_vCallbacks;
|
||||
};
|
||||
|
||||
class CPluginSystem : IPluginSystem
|
||||
@ -100,14 +107,16 @@ public:
|
||||
// I will make a module manager later which will grab all modules from the process and adds each module / removes module that passes through DLLMain.
|
||||
};
|
||||
|
||||
void PluginSystem_Init();
|
||||
bool ReloadPluginInstance(PluginInstance_t& pluginInst);
|
||||
bool LoadPluginInstance(PluginInstance_t& pluginInst);
|
||||
bool UnloadPluginInstance(PluginInstance_t& pluginInst);
|
||||
void AddPluginCallback(PluginHelpWithAnything_t* help);
|
||||
void RemovePluginCallback(PluginHelpWithAnything_t* help);
|
||||
void Init();
|
||||
|
||||
vector<PluginInstance_t>& GetPluginInstances();
|
||||
bool LoadInstance(PluginInstance_t& pluginInst);
|
||||
bool UnloadInstance(PluginInstance_t& pluginInst);
|
||||
bool ReloadInstance(PluginInstance_t& pluginInst);
|
||||
|
||||
void AddCallback(PluginHelpWithAnything_t* help);
|
||||
void RemoveCallback(PluginHelpWithAnything_t* help);
|
||||
|
||||
CUtlVector<PluginInstance_t>& GetInstances();
|
||||
|
||||
virtual void* HelpWithAnything(PluginHelpWithAnything_t* help);
|
||||
|
||||
@ -119,7 +128,7 @@ public:
|
||||
#undef CREATE_PLUGIN_CALLBACK
|
||||
|
||||
private:
|
||||
vector<PluginInstance_t> pluginInstances;
|
||||
CUtlVector<PluginInstance_t> m_Instances;
|
||||
};
|
||||
extern CPluginSystem* g_pPluginSystem;
|
||||
|
||||
@ -131,4 +140,4 @@ FORCEINLINE CPluginSystem* PluginSystem()
|
||||
// Monitor this and performance profile this if fps drops are detected.
|
||||
#define CALL_PLUGIN_CALLBACKS(callback, ...) \
|
||||
for (auto& cb : !callback) \
|
||||
cb(__VA_ARGS__)
|
||||
cb(__VA_ARGS__)
|
||||
|
Loading…
x
Reference in New Issue
Block a user