CPluginSystem init, for native mods.

* Not finished yet.
* Need to make a proper ModuleManager
* Handle PluginSystem_Reload()
This commit is contained in:
Marvin D 2022-08-20 01:59:36 +02:00
parent 52e19e9ad6
commit 55a620aa4d
9 changed files with 184 additions and 1 deletions

View File

@ -0,0 +1,116 @@
//=============================================================================//
//
// Purpose: Manager that manages Plugins!
//
//-----------------------------------------------------------------------------
//
//=============================================================================//
#include "core/stdafx.h"
#include "pluginsystem.h"
//-----------------------------------------------------------------------------
// Purpose: initialize the plugin system
// Input :
//-----------------------------------------------------------------------------
void CPluginSystem::PluginSystem_Init()
{
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)
{
pluginInstances.push_back(PluginInstance(path.filename().u8string(), path.u8string()));
}
}
}
//-----------------------------------------------------------------------------
// Purpose: reload the plugin system
// Input :
//-----------------------------------------------------------------------------
void CPluginSystem::PluginSystem_Reload()
{
}
//-----------------------------------------------------------------------------
// Purpose: load a plugin instance
// Input : pluginInst* -
// Output : bool
//-----------------------------------------------------------------------------
bool CPluginSystem::LoadPluginInstance(PluginInstance& pluginInst)
{
if (pluginInst.m_bIsLoaded) // Ugh need to make proper module manager.
return false;
HMODULE loadedPlugin = LoadLibraryA(pluginInst.m_svPluginFullPath.c_str());
if (!loadedPlugin)
return false;
pluginInst.m_hModule = CModule(pluginInst.m_svPluginName);
auto onLoadFn = pluginInst.m_hModule.GetExportedFunction("PluginInstance_OnLoad").RCast<PluginInstance::OnLoad>();
if (!onLoadFn)
return false;
onLoadFn(pluginInst.m_hModule, g_GameDll);
auto getVersFn = pluginInst.m_hModule.GetExportedFunction("PluginInstance_GetVersion").RCast<PluginInstance::GetVersion>();
if (getVersFn)
pluginInst.m_nVersion = getVersFn();
auto getDescFn = pluginInst.m_hModule.GetExportedFunction("PluginInstance_GetDescription").RCast<PluginInstance::GetDescription>();
if (getDescFn)
pluginInst.m_svDescription = getDescFn();
return pluginInst.m_bIsLoaded = true; // Change this sooooooon.
}
//-----------------------------------------------------------------------------
// Purpose: unload a plugin instance
// Input : pluginInst* -
// Output : bool
//-----------------------------------------------------------------------------
bool CPluginSystem::UnloadPluginInstance(PluginInstance& pluginInst)
{
if (!pluginInst.m_bIsLoaded)
return false;
if (!pluginInst.m_hModule.GetModuleBase())
return false;
auto onUnloadFn = pluginInst.m_hModule.GetExportedFunction("PluginInstance_OnUnload").RCast<PluginInstance::OnUnload>();
if (onUnloadFn)
onUnloadFn(g_GameDll);
bool unloadOk = FreeLibrary((HMODULE)pluginInst.m_hModule.GetModuleBase());
if (unloadOk)
pluginInst.m_bIsLoaded = false;
return unloadOk;
}
//-----------------------------------------------------------------------------
// Purpose: reload a plugin instance
// Input : pluginInst* -
// Output : bool
//-----------------------------------------------------------------------------
bool CPluginSystem::ReloadPluginInstance(PluginInstance& pluginInst)
{
return UnloadPluginInstance(pluginInst) ? LoadPluginInstance(pluginInst) : false;
}
//-----------------------------------------------------------------------------
// Purpose: get all plugin instances
// Input :
// Output : vector<CPluginSystem::PluginInstance>&
//-----------------------------------------------------------------------------
vector<CPluginSystem::PluginInstance>& CPluginSystem::GetPluginInstances()
{
return pluginInstances;
}

View File

@ -0,0 +1,34 @@
#pragma once
class CPluginSystem
{
public:
struct PluginInstance
{
PluginInstance(string svPluginName, string svPluginFullPath) : m_svPluginName(svPluginName), m_svPluginFullPath(svPluginFullPath), m_svDescription(std::string()), m_nVersion(0), m_hModule(""), m_bIsLoaded(false) {};
// Might wanna make a status code system.
typedef void(*OnLoad)(CModule, CModule);
typedef void(*OnUnload)(CModule);
typedef int16_t (*GetVersion)();
typedef const char* (*GetDescription)();
CModule m_hModule;
string m_svPluginName;
string m_svPluginFullPath;
string m_svDescription;
int16_t m_nVersion;
bool m_bIsLoaded; // [ PIXIE ]: I don't like this and it's bad.
// I will make a module manager later which will grab all modules from the processand adds each module / removes module that passes through DLLMain.
};
void PluginSystem_Init();
void PluginSystem_Reload();
bool ReloadPluginInstance(PluginInstance& pluginInst);
bool LoadPluginInstance(PluginInstance& pluginInst);
bool UnloadPluginInstance(PluginInstance& pluginInst);
vector<PluginInstance>& GetPluginInstances();
private:
vector<PluginInstance> pluginInstances;
};

View File

@ -118,7 +118,7 @@ CMemory CModule::FindPatternSIMD(const uint8_t* szPattern, const char* szMask, c
//-----------------------------------------------------------------------------
// Purpose: find array of bytes in process memory using SIMD instructions
// Input : *szPattern
// Input : *svPattern
// Output : CMemory
//-----------------------------------------------------------------------------
CMemory CModule::FindPatternSIMD(const string& svPattern, const ModuleSections_t& moduleSection) const

View File

@ -79,6 +79,7 @@
<ClCompile Include="..\mathlib\vmatrix.cpp" />
<ClCompile Include="..\networksystem\listmanager.cpp" />
<ClCompile Include="..\networksystem\pylon.cpp" />
<ClCompile Include="..\pluginsystem\pluginsystem.cpp" />
<ClCompile Include="..\protoc\cl_rcon.pb.cc">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
@ -236,6 +237,7 @@
<ClInclude Include="..\networksystem\pylon.h" />
<ClInclude Include="..\networksystem\serverlisting.h" />
<ClInclude Include="..\networksystem\sm_protocol.h" />
<ClInclude Include="..\pluginsystem\pluginsystem.h" />
<ClInclude Include="..\protoc\cl_rcon.pb.h" />
<ClInclude Include="..\protoc\sv_rcon.pb.h" />
<ClInclude Include="..\public\avi\iavi.h" />

View File

@ -220,6 +220,9 @@
<Filter Include="sdk\public\utility">
<UniqueIdentifier>{91e5c68f-327d-446c-840d-5e7a53d9a631}</UniqueIdentifier>
</Filter>
<Filter Include="sdk\pluginsystem">
<UniqueIdentifier>{b22e88ea-1ee0-4431-82a4-e877a6798f23}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\client\cdll_engine_int.cpp">
@ -585,6 +588,9 @@
<ClCompile Include="..\tier0\frametask.cpp">
<Filter>sdk\tier0</Filter>
</ClCompile>
<ClCompile Include="..\pluginsystem\pluginsystem.cpp">
<Filter>sdk\pluginsystem</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\client\cdll_engine_int.h">
@ -1739,6 +1745,9 @@
<ClInclude Include="..\tier0\frametask.h">
<Filter>sdk\tier0</Filter>
</ClInclude>
<ClInclude Include="..\pluginsystem\pluginsystem.h">
<Filter>sdk\pluginsystem</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="..\shared\resource\lockedserver.png">

View File

@ -209,6 +209,7 @@
<ClInclude Include="..\networksystem\pylon.h" />
<ClInclude Include="..\networksystem\serverlisting.h" />
<ClInclude Include="..\networksystem\sm_protocol.h" />
<ClInclude Include="..\pluginsystem\pluginsystem.h" />
<ClInclude Include="..\protoc\cl_rcon.pb.h" />
<ClInclude Include="..\protoc\sv_rcon.pb.h" />
<ClInclude Include="..\public\iengine.h" />
@ -531,6 +532,7 @@
<ClCompile Include="..\mathlib\vmatrix.cpp" />
<ClCompile Include="..\networksystem\bansystem.cpp" />
<ClCompile Include="..\networksystem\pylon.cpp" />
<ClCompile Include="..\pluginsystem\pluginsystem.cpp" />
<ClCompile Include="..\protoc\cl_rcon.pb.cc">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>

View File

@ -193,6 +193,9 @@
<Filter Include="sdk\public\utility">
<UniqueIdentifier>{e0155821-8fb2-4fb6-aa6a-d3e6d980b64c}</UniqueIdentifier>
</Filter>
<Filter Include="sdk\pluginsystem">
<UniqueIdentifier>{d0650f0e-6359-4322-82c2-97f2e40f141f}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\common\opcodes.h">
@ -1239,6 +1242,9 @@
<ClInclude Include="..\tier0\frametask.h">
<Filter>sdk\tier0</Filter>
</ClInclude>
<ClInclude Include="..\pluginsystem\pluginsystem.h">
<Filter>sdk\pluginsystem</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\common\opcodes.cpp">
@ -1550,6 +1556,9 @@
<ClCompile Include="..\tier0\frametask.cpp">
<Filter>sdk\tier0</Filter>
</ClCompile>
<ClCompile Include="..\pluginsystem\pluginsystem.cpp">
<Filter>sdk\pluginsystem</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\Dedicated.def" />

View File

@ -87,6 +87,7 @@
<ClCompile Include="..\networksystem\bansystem.cpp" />
<ClCompile Include="..\networksystem\listmanager.cpp" />
<ClCompile Include="..\networksystem\pylon.cpp" />
<ClCompile Include="..\pluginsystem\pluginsystem.cpp" />
<ClCompile Include="..\protoc\cl_rcon.pb.cc">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
@ -260,6 +261,7 @@
<ClInclude Include="..\networksystem\pylon.h" />
<ClInclude Include="..\networksystem\serverlisting.h" />
<ClInclude Include="..\networksystem\sm_protocol.h" />
<ClInclude Include="..\pluginsystem\pluginsystem.h" />
<ClInclude Include="..\protoc\cl_rcon.pb.h" />
<ClInclude Include="..\protoc\sv_rcon.pb.h" />
<ClInclude Include="..\public\avi\iavi.h" />

View File

@ -229,6 +229,9 @@
<Filter Include="sdk\public\utility">
<UniqueIdentifier>{2828835b-d972-4793-83f7-8eb1741b78ba}</UniqueIdentifier>
</Filter>
<Filter Include="sdk\pluginsystem">
<UniqueIdentifier>{cd7b0538-e28c-4181-9e19-588f77d41d36}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\client\vengineclient_impl.cpp">
@ -624,6 +627,9 @@
<ClCompile Include="..\engine\host.cpp">
<Filter>sdk\engine</Filter>
</ClCompile>
<ClCompile Include="..\pluginsystem\pluginsystem.cpp">
<Filter>sdk\pluginsystem</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\client\cdll_engine_int.h">
@ -1826,6 +1832,9 @@
<ClInclude Include="..\public\iframetask.h">
<Filter>sdk\public</Filter>
</ClInclude>
<ClInclude Include="..\pluginsystem\pluginsystem.h">
<Filter>sdk\pluginsystem</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="..\shared\resource\lockedserver.png">