2013-08-16 05:53:38 +02:00
|
|
|
#include "extension.h"
|
2013-08-19 17:07:25 +02:00
|
|
|
#include "listeners.h"
|
2016-12-12 06:02:10 +01:00
|
|
|
#include "dynhooks_sourcepawn.h"
|
2018-01-25 17:18:30 +01:00
|
|
|
#include "signatures.h"
|
2013-08-16 05:53:38 +02:00
|
|
|
|
2013-08-19 17:07:25 +02:00
|
|
|
DHooks g_DHooksIface; /**< Global singleton for extension's main interface */
|
|
|
|
SMEXT_LINK(&g_DHooksIface);
|
2013-08-16 05:53:38 +02:00
|
|
|
|
|
|
|
IBinTools *g_pBinTools;
|
|
|
|
ISDKHooks *g_pSDKHooks;
|
|
|
|
ISDKTools *g_pSDKTools;
|
2013-08-19 17:07:25 +02:00
|
|
|
DHooksEntityListener *g_pEntityListener = NULL;
|
2013-08-16 05:53:38 +02:00
|
|
|
|
2013-08-19 17:07:25 +02:00
|
|
|
HandleType_t g_HookSetupHandle = 0;
|
|
|
|
HandleType_t g_HookParamsHandle = 0;
|
|
|
|
HandleType_t g_HookReturnHandle = 0;
|
2013-08-16 05:53:38 +02:00
|
|
|
|
2018-05-02 13:37:41 +02:00
|
|
|
ke::ThreadId g_MainThreadId;
|
|
|
|
|
2013-08-16 05:53:38 +02:00
|
|
|
bool DHooks::SDK_OnLoad(char *error, size_t maxlength, bool late)
|
|
|
|
{
|
2013-08-19 17:07:25 +02:00
|
|
|
HandleError err;
|
|
|
|
g_HookSetupHandle = handlesys->CreateType("HookSetup", this, 0, NULL, NULL, myself->GetIdentity(), &err);
|
|
|
|
if(g_HookSetupHandle == 0)
|
|
|
|
{
|
|
|
|
snprintf(error, maxlength, "Could not create hook setup handle type (err: %d)", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
g_HookParamsHandle = handlesys->CreateType("HookParams", this, 0, NULL, NULL, myself->GetIdentity(), &err);
|
|
|
|
if(g_HookParamsHandle == 0)
|
|
|
|
{
|
|
|
|
snprintf(error, maxlength, "Could not create hook params handle type (err: %d)", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
g_HookReturnHandle = handlesys->CreateType("HookReturn", this, 0, NULL, NULL, myself->GetIdentity(), &err);
|
|
|
|
if(g_HookReturnHandle == 0)
|
|
|
|
{
|
|
|
|
snprintf(error, maxlength, "Could not create hook return handle type (err: %d)", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-12 06:02:10 +01:00
|
|
|
if (!g_pPreDetours.init())
|
|
|
|
{
|
|
|
|
snprintf(error, maxlength, "Could not initialize pre hook detours hashmap.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_pPostDetours.init())
|
|
|
|
{
|
|
|
|
snprintf(error, maxlength, "Could not initialize post hook detours hashmap.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-16 05:53:38 +02:00
|
|
|
sharesys->AddDependency(myself, "bintools.ext", true, true);
|
|
|
|
sharesys->AddDependency(myself, "sdktools.ext", true, true);
|
|
|
|
sharesys->AddDependency(myself, "sdkhooks.ext", true, true);
|
|
|
|
|
|
|
|
sharesys->RegisterLibrary(myself, "dhooks");
|
2013-08-19 17:07:25 +02:00
|
|
|
plsys->AddPluginsListener(this);
|
|
|
|
sharesys->AddNatives(myself, g_Natives);
|
2013-08-16 05:53:38 +02:00
|
|
|
|
2013-08-22 04:18:52 +02:00
|
|
|
g_pEntityListener = new DHooksEntityListener();
|
2018-01-25 17:18:30 +01:00
|
|
|
g_pSignatures = new SignatureGameConfig();
|
2018-05-02 13:37:41 +02:00
|
|
|
g_MainThreadId = ke::GetCurrentThreadId();
|
2013-08-22 04:18:52 +02:00
|
|
|
|
2013-08-16 05:53:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-19 17:07:25 +02:00
|
|
|
|
|
|
|
void DHooks::OnHandleDestroy(HandleType_t type, void *object)
|
2013-08-16 05:53:38 +02:00
|
|
|
{
|
2013-08-19 17:07:25 +02:00
|
|
|
if(type == g_HookSetupHandle)
|
2013-08-16 05:53:38 +02:00
|
|
|
{
|
2013-08-19 17:07:25 +02:00
|
|
|
delete (HookSetup *)object;
|
2013-08-16 05:53:38 +02:00
|
|
|
}
|
2013-08-19 17:07:25 +02:00
|
|
|
else if(type == g_HookParamsHandle)
|
|
|
|
{
|
|
|
|
delete (HookParamsStruct *)object;
|
|
|
|
}
|
|
|
|
else if(type == g_HookReturnHandle)
|
|
|
|
{
|
|
|
|
delete (HookReturnStruct *)object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DHooks::SDK_OnAllLoaded()
|
|
|
|
{
|
|
|
|
SM_GET_LATE_IFACE(SDKTOOLS, g_pSDKTools);
|
2013-08-16 05:53:38 +02:00
|
|
|
SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
|
|
|
|
SM_GET_LATE_IFACE(SDKHOOKS, g_pSDKHooks);
|
2013-08-19 17:07:25 +02:00
|
|
|
|
|
|
|
g_pSDKHooks->AddEntityListener(g_pEntityListener);
|
2018-01-25 17:18:30 +01:00
|
|
|
gameconfs->AddUserConfigHook("Functions", g_pSignatures);
|
2013-08-16 05:53:38 +02:00
|
|
|
}
|
2013-08-19 17:07:25 +02:00
|
|
|
|
|
|
|
void DHooks::SDK_OnUnload()
|
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
CleanupHooks();
|
2016-12-12 06:18:53 +01:00
|
|
|
CleanupDetours();
|
2013-08-19 17:07:25 +02:00
|
|
|
if(g_pEntityListener)
|
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
g_pEntityListener->CleanupListeners();
|
2013-08-19 17:07:25 +02:00
|
|
|
g_pSDKHooks->RemoveEntityListener(g_pEntityListener);
|
|
|
|
delete g_pEntityListener;
|
|
|
|
}
|
|
|
|
plsys->RemovePluginsListener(this);
|
2014-08-19 19:14:48 +02:00
|
|
|
|
|
|
|
handlesys->RemoveType(g_HookSetupHandle, myself->GetIdentity());
|
|
|
|
handlesys->RemoveType(g_HookParamsHandle, myself->GetIdentity());
|
|
|
|
handlesys->RemoveType(g_HookReturnHandle, myself->GetIdentity());
|
2018-01-25 17:18:30 +01:00
|
|
|
|
|
|
|
gameconfs->RemoveUserConfigHook("Functions", g_pSignatures);
|
2013-08-19 17:07:25 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 05:53:38 +02:00
|
|
|
bool DHooks::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
|
|
|
|
{
|
|
|
|
if(!SetupHookManager(ismm))
|
|
|
|
{
|
|
|
|
snprintf(error, maxlength, "Failed to get IHookManagerAutoGen iface");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:07:25 +02:00
|
|
|
void DHooks::OnPluginUnloaded(IPlugin *plugin)
|
|
|
|
{
|
|
|
|
CleanupHooks(plugin->GetBaseContext());
|
2016-12-12 06:02:10 +01:00
|
|
|
RemoveAllCallbacksForContext(plugin->GetBaseContext());
|
2013-08-19 17:07:25 +02:00
|
|
|
if(g_pEntityListener)
|
2013-08-16 05:53:38 +02:00
|
|
|
{
|
2013-08-19 17:07:25 +02:00
|
|
|
g_pEntityListener->CleanupListeners(plugin->GetBaseContext());
|
2013-08-16 05:53:38 +02:00
|
|
|
}
|
2013-08-19 17:07:25 +02:00
|
|
|
}
|
2014-06-04 21:46:31 +02:00
|
|
|
// The next 3 functions handle cleanup if our interfaces are going to be unloaded
|
|
|
|
bool DHooks::QueryRunning(char *error, size_t maxlength)
|
|
|
|
{
|
|
|
|
SM_CHECK_IFACE(SDKTOOLS, g_pSDKTools);
|
|
|
|
SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
|
|
|
|
SM_CHECK_IFACE(SDKHOOKS, g_pSDKHooks);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DHooks::NotifyInterfaceDrop(SMInterface *pInterface)
|
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
if(strcmp(pInterface->GetInterfaceName(), SMINTERFACE_SDKHOOKS_NAME) == 0)
|
2014-06-04 21:46:31 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
if(g_pEntityListener)
|
2014-06-04 21:46:31 +02:00
|
|
|
{
|
|
|
|
// If this fails, remove this line and just delete the ent listener instead
|
2014-08-19 19:14:48 +02:00
|
|
|
g_pSDKHooks->RemoveEntityListener(g_pEntityListener);
|
|
|
|
|
|
|
|
g_pEntityListener->CleanupListeners();
|
2014-06-04 21:46:31 +02:00
|
|
|
delete g_pEntityListener;
|
|
|
|
g_pEntityListener = NULL;
|
|
|
|
}
|
|
|
|
g_pSDKHooks = NULL;
|
|
|
|
}
|
2014-08-19 19:14:48 +02:00
|
|
|
else if(strcmp(pInterface->GetInterfaceName(), SMINTERFACE_BINTOOLS_NAME) == 0)
|
2014-06-04 21:46:31 +02:00
|
|
|
{
|
|
|
|
g_pBinTools = NULL;
|
|
|
|
}
|
2014-08-19 19:14:48 +02:00
|
|
|
else if(strcmp(pInterface->GetInterfaceName(), SMINTERFACE_SDKTOOLS_NAME) == 0)
|
2014-06-04 21:46:31 +02:00
|
|
|
{
|
|
|
|
g_pSDKTools = NULL;
|
|
|
|
}
|
|
|
|
}
|