Advertisement
Guest User

Untitled

a guest
Jan 11th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include "extension.h"
  2. #include "variant_t.h"
  3.  
  4. Sample g_Sample;        /**< Global singleton for extension's main interface */
  5. SMEXT_LINK(&g_Sample);
  6.  
  7. int g_hookEntCreated = 0;
  8. int g_hookEntRemoved = 0;
  9. int g_hookInput[MAX_EDICTS] = {0};
  10.  
  11. SH_DECL_HOOK2_void(CEntListHack, OnAddEntity, SH_NOATTRIB, 0, CBaseEntity *, CBaseHandle);
  12. SH_DECL_HOOK2_void(CEntListHack, OnRemoveEntity, SH_NOATTRIB, 0, CBaseEntity *, CBaseHandle);
  13. SH_DECL_MANUALHOOK5(AcceptInput, 0, 0, 0, bool, const char *, CBaseEntity *, CBaseEntity *, variant_t, int);
  14.  
  15. bool Sample::SDK_OnLoad(char *error, size_t maxlength, bool late)
  16. {
  17.     // Get AcceptInput offset from gamedata. (from SDK Tools)
  18.     IGameConfig *pGameConf;
  19.     char sError[255];
  20.     int offset;
  21.  
  22.     if (!gameconfs->LoadGameConfigFile("sdktools.games", &pGameConf, sError, sizeof(sError)))
  23.     {
  24.         snprintf(error, maxlength, "Failed to read sdktools.games gamedata: %s", sError);
  25.         return false;
  26.     }
  27.     if (!pGameConf->GetOffset("AcceptInput", &offset))
  28.     {
  29.         snprintf(error, maxlength, "Failed to find AcceptInput offset.");
  30.         return false;
  31.     }
  32.  
  33.     SH_MANUALHOOK_RECONFIGURE(AcceptInput, offset, 0, 0);
  34.     gameconfs->CloseGameConfigFile(pGameConf);
  35.  
  36.     // Prepare entity hooks. (from SDK Hooks)
  37.     void *gEntList = gamehelpers->GetGlobalEntityList();
  38.     if (!gEntList)
  39.     {
  40.         g_pSM->Format(error, maxlength, "Failed to find gEntList pointer.");
  41.         return false;
  42.     }
  43.  
  44.     g_hookEntCreated = SH_ADD_HOOK(CEntListHack, OnAddEntity, (CEntListHack *)gEntList, SH_MEMBER(this, &Sample::OnEntityCreated), true);
  45.     g_hookEntRemoved = SH_ADD_HOOK(CEntListHack, OnRemoveEntity, (CEntListHack *)gEntList, SH_MEMBER(this, &Sample::OnEntityDeleted), false);
  46.  
  47.     return true;
  48. }
  49.  
  50. void Sample::SDK_OnUnload()
  51. {
  52.     // Unhook everything.
  53.     SH_REMOVE_HOOK_ID(g_hookEntCreated);
  54.     SH_REMOVE_HOOK_ID(g_hookEntRemoved);
  55.  
  56.     for (int i = 0; i < MAX_EDICTS; i++)
  57.     {
  58.         if (g_hookInput[i] != 0)
  59.         {
  60.             SH_REMOVE_HOOK_ID(g_hookInput[i]);
  61.         }
  62.     }
  63. }
  64.  
  65. void Sample::OnEntityCreated(CBaseEntity *pEntity, CBaseHandle hndl)
  66. {
  67.     int entity = hndl.GetEntryIndex();
  68.  
  69.     if (entity > playerhelpers->GetMaxClients() && entity < MAX_EDICTS)
  70.     {
  71.         const char *pName = gamehelpers->GetEntityClassname(pEntity);
  72.  
  73.         if (strcmp(pName, "point_servercommand") == 0 ||
  74.             strcmp(pName, "point_clientcommand") == 0 ||
  75.             strcmp(pName, "point_broadcastclientcommand") == 0)
  76.         {
  77.             g_hookInput[entity] = SH_ADD_MANUALHOOK(AcceptInput, pEntity, SH_MEMBER(this, &Sample::Hook_AcceptInput), false);
  78.         }
  79.     }
  80. }
  81.  
  82. void Sample::OnEntityDeleted(CBaseEntity *pEntity, CBaseHandle hndl)
  83. {
  84.     int entity = hndl.GetEntryIndex();
  85.  
  86.     if (entity > playerhelpers->GetMaxClients() && entity < MAX_EDICTS && g_hookInput[entity] != 0)
  87.     {
  88.         SH_REMOVE_HOOK_ID(g_hookInput[entity]);
  89.         g_hookInput[entity] = 0;
  90.     }
  91. }
  92.  
  93. bool Sample::Hook_AcceptInput(const char *szInputName, CBaseEntity *pActivator, CBaseEntity *pCaller, variant_t Value, int outputID)
  94. {
  95.     // We only care if a command is being sent.
  96.     if (strcmp(szInputName, "Command") != 0)
  97.         RETURN_META_VALUE(MRES_IGNORED, true);
  98.  
  99.     // Print command string to console and block.
  100.     META_CONPRINTF("Value: %s\n", Value.String());
  101.     RETURN_META_VALUE(MRES_SUPERCEDE, false);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement