CPluginCallbackList impl

* CPluginSystem allows for function callbacks now.
This commit is contained in:
Marvin D 2022-12-12 17:16:39 +01:00
parent 651b4b9c33
commit b13c542ed1
4 changed files with 148 additions and 15 deletions

View File

@ -64,14 +64,9 @@ bool CModAppSystemGroup::Create(CModAppSystemGroup* pModAppSystemGroup)
g_pFactory->GetFactoriesFromRegister();
g_pFactory->AddFactory(FACTORY_INTERFACE_VERSION, g_pFactory);
g_pFactory->AddFactory(INTERFACEVERSION_PLUGINSYSTEM, g_pPluginSystem);
// DEBUG CODE FOR PLUGINS
//g_pPluginSystem->PluginSystem_Init();
//for (auto& it : g_pPluginSystem->GetPluginInstances())
//{
// if (g_pPluginSystem->LoadPluginInstance(it))
// spdlog::info("Load PLUGIN SUCCESS\n");
//}
//InitPluginSystem(pModAppSystemGroup);
//CALL_PLUGIN_CALLBACKS(g_pPluginSystem->GetCreateCallbacks(), pModAppSystemGroup);
#ifndef DEDICATED
g_pClientEntityList = g_pFactory->GetFactoryPtr("VClientEntityList003", false).RCast<IClientEntityList*>();
@ -99,6 +94,19 @@ bool CModAppSystemGroup::Create(CModAppSystemGroup* pModAppSystemGroup)
return CModAppSystemGroup_Create(pModAppSystemGroup);
}
//-----------------------------------------------------------------------------
// Purpose: Initialize plugin system
//-----------------------------------------------------------------------------
void CModAppSystemGroup::InitPluginSystem(CModAppSystemGroup* pModAppSystemGroup)
{
// DEBUG CODE FOR PLUGINS
g_pPluginSystem->PluginSystem_Init();
for (auto& it : g_pPluginSystem->GetPluginInstances())
{
if (g_pPluginSystem->LoadPluginInstance(it))
spdlog::info("Load PLUGIN SUCCESS\n");
}
}
///////////////////////////////////////////////////////////////////////////////
void IApplication_Attach()

View File

@ -9,6 +9,7 @@ class CModAppSystemGroup
public:
static int Main(CModAppSystemGroup* pModAppSystemGroup);
static bool Create(CModAppSystemGroup* pModAppSystemGroup);
static void InitPluginSystem(CModAppSystemGroup* pModAppSystemGroup);
bool IsServerOnly(void) const
{

View File

@ -112,13 +112,49 @@ vector<CPluginSystem::PluginInstance_t>& CPluginSystem::GetPluginInstances()
}
//-----------------------------------------------------------------------------
// Purpose: get all plugin callbacks
// Input :
// Output : unordered_map<string, vector<pair<string, void*>>>&
// Purpose: add plugin callback for function
// Input : *help
// Output : void
//-----------------------------------------------------------------------------
unordered_map<string, vector<pair<string, void*>>>& CPluginSystem::GetPluginCallbacks()
void CPluginSystem::AddPluginCallback(PluginHelpWithAnything_t* help)
{
return pluginCallbacks;
#define ADD_PLUGIN_CALLBACK(fn, callback, function) callback += reinterpret_cast<fn>(function)
switch (help->m_nCallbackID)
{
case PluginHelpWithAnything_t::ePluginCallback::CModAppSystemGroup_Create:
{
ADD_PLUGIN_CALLBACK(CreateFn, GetCreateCallbacks(), help->m_pFunction);
break;
}
default:
break;
}
#undef ADD_PLUGIN_CALLBACK
}
//-----------------------------------------------------------------------------
// Purpose: remove plugin callback for function
// Input : *help
// Output : void
//-----------------------------------------------------------------------------
void CPluginSystem::RemovePluginCallback(PluginHelpWithAnything_t* help)
{
#define REMOVE_PLUGIN_CALLBACK(fn, callback, function) callback -= reinterpret_cast<fn>(function)
switch (help->m_nCallbackID)
{
case PluginHelpWithAnything_t::ePluginCallback::CModAppSystemGroup_Create:
{
REMOVE_PLUGIN_CALLBACK(CreateFn, GetCreateCallbacks(), help->m_pFunction);
break;
}
default:
break;
}
#undef REMOVE_PLUGIN_CALLBACK
}
//-----------------------------------------------------------------------------
@ -136,10 +172,12 @@ void* CPluginSystem::HelpWithAnything(PluginHelpWithAnything_t* help)
}
case PluginHelpWithAnything_t::ePluginHelp::PLUGIN_REGISTER_CALLBACK:
{
AddPluginCallback(help);
break;
}
case PluginHelpWithAnything_t::ePluginHelp::PLUGIN_UNREGISTER_CALLBACK:
{
RemovePluginCallback(help);
break;
}
default:

View File

@ -1,6 +1,8 @@
#pragma once
#include "ipluginsystem.h"
class CModAppSystemGroup;
struct PluginHelpWithAnything_t
{
enum class ePluginHelp : int16_t
@ -10,11 +12,84 @@ struct PluginHelpWithAnything_t
PLUGIN_UNREGISTER_CALLBACK
};
enum ePluginCallback : int16_t
{
CModAppSystemGroup_Create = 0
};
ePluginHelp m_nHelpID;
ePluginCallback m_nCallbackID;
const char* m_pszName;
void* m_pFunction;
};
template<typename T>
class CPluginCallbackList
{
public:
CPluginCallbackList() : m_vCallbacks() {}
CPluginCallbackList(const vector<T>& cbs) : m_vCallbacks(cbs) {}
vector<T>& GetCallbacks() { return m_vCallbacks; }
operator bool()
{
return !this->m_vCallbacks.empty;
}
vector<T>& operator!()
{
return this->m_vCallbacks;
}
CPluginCallbackList<T>& operator+=(const T& rhs)
{
if (rhs)
this->m_vCallbacks.push_back(rhs);
return *this;
}
CPluginCallbackList<T>& operator+=(const vector<T>& rhs)
{
for (auto it : rhs)
{
if (it)
this->m_vCallbacks.push_back(it);
}
return *this;
}
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);
}
return *this;
}
CPluginCallbackList<T>& operator-=(const vector<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);
}
}
return *this;
}
private:
vector<T> m_vCallbacks;
};
class CPluginSystem : IPluginSystem
{
public:
@ -38,14 +113,25 @@ public:
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);
vector<PluginInstance_t>& GetPluginInstances();
unordered_map<string, vector<pair<string, void*>>>& GetPluginCallbacks();
virtual void* HelpWithAnything(PluginHelpWithAnything_t* help);
#define CREATE_PLUGIN_CALLBACK(typeName, type, funcName, varName) public: using typeName = type; CPluginCallbackList<typeName>& funcName() { return varName; } private: CPluginCallbackList<typeName> varName;
CREATE_PLUGIN_CALLBACK(CreateFn, bool(*)(CModAppSystemGroup*), GetCreateCallbacks, createCallbacks);
#undef CREATE_PLUGIN_CALLBACK
private:
vector<PluginInstance_t> pluginInstances;
unordered_map<string, vector<pair<string, void*>>> pluginCallbacks;
};
extern CPluginSystem* g_pPluginSystem;
// Monitor this and performance profile this if fps drops are detected.
#define CALL_PLUGIN_CALLBACKS(callback, ...) \
for (auto& cb : !callback) \
cb(__VA_ARGS__)