public/vtable_hook_helper.h
author Kyle Sanderson <kyle.leet@gmail.com>
Sat Mar 01 14:25:35 2014 -0700 (2014-03-01)
changeset 4268 3c383c5b1aaa
child 4297 c05238822c32
permissions -rw-r--r--
Move PlayerRunCmd to a global VTable Hook (bug 6051, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_VTABLE_HOOK_HELPER_H_
    33 #define _INCLUDE_VTABLE_HOOK_HELPER_H_
    34 
    35 class CVTableHook
    36 {
    37 public:
    38 	CVTableHook(void *takenclass)
    39 	{
    40 		this->vtableptr = *reinterpret_cast<void ***>(takenclass);
    41 		this->hookid = 0;
    42 	}
    43 
    44 	CVTableHook(void *vtable, int hook)
    45 	{
    46 		this->vtableptr = vtable;
    47 		this->hookid = hook;
    48 	}
    49 
    50 	CVTableHook(CVTableHook &other)
    51 	{
    52 		this->vtableptr = other.vtableptr;
    53 		this->hookid = other.hookid;
    54 
    55 		other.hookid = 0;
    56 	}
    57 
    58 	CVTableHook(CVTableHook *other)
    59 	{
    60 		this->vtableptr = other->vtableptr;
    61 		this->hookid = other->hookid;
    62 
    63 		other->hookid = 0;
    64 	}
    65 
    66 	~CVTableHook()
    67 	{
    68 		if (this->hookid)
    69 		{
    70 			SH_REMOVE_HOOK_ID(this->hookid);
    71 			this->hookid = 0;
    72 		}
    73 	}
    74 public:
    75 	void *GetVTablePtr(void)
    76 	{
    77 		return this->vtableptr;
    78 	}
    79 
    80 	void SetHookID(int hook)
    81 	{
    82 		this->hookid = hook;
    83 	}
    84 
    85 	bool IsHooked(void)
    86 	{
    87 		return (this->hookid != 0);
    88 	}
    89 public:
    90 	bool operator == (CVTableHook &other)
    91 	{
    92 		return (this->GetVTablePtr() == other.GetVTablePtr());
    93 	}
    94 
    95 	bool operator == (CVTableHook *other)
    96 	{
    97 		return (this->GetVTablePtr() == other->GetVTablePtr());
    98 	}
    99 private:
   100 	void *vtableptr;
   101 	int hookid;
   102 };
   103 
   104 #endif //_INCLUDE_VTABLE_HOOK_HELPER_H_