core-legacy/sourcehook/sourcehook_impl.h
author David Anderson <dvander@alliedmods.net>
Wed Oct 28 23:14:32 2009 -0700 (2009-10-28)
changeset 659 b35a5fdbc654
parent 545 07e5df11f9fc
permissions -rw-r--r--
Merged PM's unreleased SourceHook-legacy, getting us VP hooks (bug 4019, r=ds).
dvander@541
     1
/* ======== SourceHook ========
dvander@659
     2
* Copyright (C) 2004-2007 Metamod:Source Development Team
dvander@541
     3
* No warranties of any kind
dvander@541
     4
*
dvander@541
     5
* License: zlib/libpng
dvander@541
     6
*
dvander@541
     7
* Author(s): Pavol "PM OnoTo" Marko
dvander@541
     8
* ============================
dvander@541
     9
*/
dvander@541
    10
dvander@541
    11
#ifndef __SOURCEHOOK_IMPL_H__
dvander@541
    12
#define __SOURCEHOOK_IMPL_H__
dvander@541
    13
dvander@541
    14
#include "sourcehook.h"
dvander@541
    15
#include "sh_list.h"
dvander@541
    16
#include "sh_vector.h"
dvander@541
    17
#include "sh_tinyhash.h"
dvander@541
    18
#include "sh_stack.h"
dvander@659
    19
#include "sh_listcat.h"
dvander@541
    20
dvander@541
    21
/*
dvander@541
    22
dvander@541
    23
IMPLEMENTATION INFO
dvander@541
    24
dvander@541
    25
---------------------------------------
dvander@541
    26
Protos ("Prototypes")
dvander@541
    27
	The purpose of protos is to provide the amount of type information about a function
dvander@541
    28
	which is required to be able to execute a function call without corrupting the stack.
dvander@541
    29
	Our protos do not fully do this, but they provide the size of the return value, the number of
dvander@541
    30
	parameters, and the size of each parameter, which is enough for most situations.
dvander@541
    31
dvander@541
    32
	There are two version of protos:
dvander@541
    33
	OLD:
dvander@541
    34
		C-Style strings.
dvander@541
    35
dvander@541
    36
		0_void:
dvander@541
    37
		"attrib"
dvander@541
    38
		1_void:
dvander@541
    39
		"attrib|param1_type"
dvander@541
    40
		2_void:
dvander@541
    41
		"attrib|param1_type|param2_type
dvander@541
    42
		0:
dvander@541
    43
		"attrib|ret_type"
dvander@541
    44
		1:
dvander@541
    45
		"attrib|ret_type|param1_type"
dvander@541
    46
		2:
dvander@541
    47
		"attrib|ret_type|param2_type"
dvander@541
    48
dvander@541
    49
		Old protos are deprecated.
dvander@541
    50
dvander@541
    51
	NEW:
dvander@541
    52
		New protos are in fact pointers to the ProtoInfo structure (see sourcehook.h)
dvander@541
    53
dvander@541
    54
	Old protos begin with a non-zero byte, new protos begin with a zero byte.
dvander@541
    55
dvander@541
    56
	Protos are usually stored in a CProto instance.
dvander@541
    57
dvander@541
    58
---------------------------------------
dvander@541
    59
Hook managers and hook manager containers
dvander@541
    60
	Each hookman container is tied to _one_ proto/vtable index/vtable offset info.
dvander@541
    61
	Hookman containers then contain a list of hook managers provided by plugins, sorted by version.
dvander@541
    62
	(higher versions come first)
dvander@541
    63
dvander@541
    64
	Duplicate hook managers are ignored (ie. hook managers where proto, vtable index, vtable offset,
dvander@541
    65
	plugin, version are the same as in an already exisiting hook manager)
dvander@541
    66
dvander@541
    67
	A new hook manager is always added to the end of the version group in the corresponding
dvander@541
    68
	hook container. 
dvander@541
    69
dvander@541
    70
	If the new hook manager was added to the beginning of the container (which only happens if
dvander@541
    71
	it is the first one or if it has a higher version than the previously first hook manager),
dvander@541
    72
	the now second hook manager is shut down and the new hook manager takes its job.
dvander@541
    73
dvander@541
    74
	A "hook manager container id" (HMCI) consits of three values: proto, vtbl index, vtbl offset.
dvander@541
    75
---------------------------------------
dvander@541
    76
Hooks 
dvander@541
    77
	When adding a hook, first the proposed hook manager is added to the corresponding
dvander@541
    78
	hook manager container as described above.
dvander@541
    79
dvander@541
    80
	Then the first hook manager in the the manhook container is chosen to handle the function.
dvander@541
    81
dvander@541
    82
	Removing a hook does not neccessarily unreigster the plugin's hook manager. In order to do this,
dvander@541
    83
	use RemoveHookManager or UnloadPlugin/
dvander@541
    84
dvander@541
    85
	Hooks can be paused - they remain in memory but they are not called. In SH, the hook iterator
dvander@541
    86
	classes handle pausing transparently.
dvander@541
    87
dvander@541
    88
	The hook loop is supposed to call ShouldContinue before each iteration. This makes hook handlers
dvander@541
    89
	able to remove themselves.
dvander@541
    90
dvander@541
    91
---------------------------------------
dvander@541
    92
Call classes
dvander@659
    93
dvander@659
    94
	!! deprecated !!   - see below (new SH_CALL)
dvander@659
    95
dvander@541
    96
	Call classes are identified by a this pointer and an instance size
dvander@541
    97
dvander@541
    98
	We use the instance size because a derived class instance and a base class instance could
dvander@541
    99
	have the same this pointers, and we want to avoid that the derived class
dvander@541
   100
	(which could be bigger) gets the same callclass as the base class (mainly if the
dvander@541
   101
	base class' callclass was requested first).
dvander@541
   102
dvander@541
   103
	Call classes are reference counted.
dvander@541
   104
dvander@541
   105
	The original function pointers are stored in a vector (in order to allow fast random access).
dvander@541
   106
	These vectors are stored as the value type of a hash. The key type is int and represents the
dvander@541
   107
	vtable offset.
dvander@541
   108
dvander@541
   109
	If the hash key doesn't exist or the vtblidx is out of range or the corresponding element in the
dvander@541
   110
	vector is NULL, there is no hook on that function.
dvander@541
   111
dvander@541
   112
---------------------------------------
dvander@541
   113
Recalls
dvander@541
   114
	Recalls are used for the META_RETURN_(VALUE_)NEWPARAMS macros, ie. to change the parameters
dvander@541
   115
	in the hook loop on the fly.
dvander@541
   116
	
dvander@541
   117
	First, the macro calls DoRecall(), then it calls the function the hook is attached to -> it 
dvander@541
   118
	calls the hookfunc. SourceHook makes sure that the newly invoked hook loop starts where the last
dvander@541
   119
	one left off and that status variables like status, previous result, override return are kept.
dvander@541
   120
	When this recurisvely called hookfunc returns, the macro returns what it returned
dvander@541
   121
	(using MRES_SUPERCEDE). CSourceHookImpl returns false from ShouldContinue so the original hook loop
dvander@541
   122
	is abandonned.
dvander@541
   123
dvander@541
   124
Post Recalls
dvander@541
   125
	People wanted to be able to use META_RETURN_(VALUE_)NEWPARAMS from post hooks as well. Crazy people!
dvander@541
   126
	Anyway, for this, we have to know where a hook handler is. Is it executing pre or post hooks at the moment?
dvander@541
   127
	The only way we can know this is watching when it calls CHookList::GetIter(). So CHookList gets a new variable:
dvander@541
   128
	m_RequestedFlag. It also gets two new functions: RQFlagReset() and RQFlagGet().
dvander@541
   129
	HookLoopBegin() calls RQFlagReset on both hooklists of the iface; then DoRecall() checks whether the postlist's 
dvander@541
   130
	RQ flag is set. if yes, the hook loop is in post mode.
dvander@541
   131
dvander@541
   132
	So, what a about a recall in post mode? The first ShouldContinue returns false and sets Status to supercede. 
dvander@541
   133
	This way the pre hooks and the function call will be skipped. Then, then next ShouldContinue returns true, so we get
dvander@541
   134
	into the post hooks. HA!
dvander@541
   135
dvander@541
   136
Return Values in Post Recalls
dvander@541
   137
	The easy case is when we already have an override return value. In this case, the status register gets transferred,
dvander@541
   138
	and so does the override return pointer. So, basically, everything is ok.
dvander@541
   139
dvander@541
   140
	However, what happens if we don't? ie. the status register is on MRES_IGNORED? In this case we'd have to transfer the
dvander@541
   141
	orig ret value. But we can't: There's no way to tell the hookfunc: "Use this as orig ret pointer". It uses its own.
dvander@541
   142
	So, we emulate it. GetOrigRet will return the orig ret pointer from the old hook loop. If still no one overrides it,
dvander@541
   143
	we'd have to return it. BUT! HOW TO DO THIS? Check out SH_RETURN(). First calls HookLoopEnd(), then decides whether
dvander@541
   144
	to return the override retval or the orig retval. But it doesn't ask for a new override return. So we give the function
dvander@541
   145
	the last orig return value as its new override return value; but leave status where it is so everything works, and in
dvander@541
   146
	HookLoopEnd we make sure that status is high enough so that the override return will be returned. crazy.
dvander@541
   147
dvander@541
   148
	All this stuff could be much less complicated if I didn't try to preserve binary compatibility :)
dvander@659
   149
dvander@659
   150
VP Hooks
dvander@659
   151
	VP hooks are hooks which are called on a vfnptr, regardless of the this pointer with which it was called. They are
dvander@659
   152
	implemented as a special CIface instance with m_Ptr = NULL. All Hook Lists have a new "ListCatIterator" which
dvander@659
   153
	virtually concatenates the NULL-interface-hook-list with their normal hook list.
dvander@659
   154
dvander@659
   155
dvander@659
   156
	I'm afraid that with the addition of Recalls and VP Hooks, SourceHook is now a pretty complex and hacked-together
dvander@659
   157
	binary compatible beast which is pretty hard to maintain unless you've written it :)
dvander@659
   158
dvander@659
   159
New SH_CALL
dvander@659
   160
	The addition of VP hooks messed up the Call Classes concept (see above) - call classes are bound to an
dvander@659
   161
	instance pointer; they only work on one of the hooked instances. But VP hooks are called on all instances.
dvander@659
   162
	
dvander@659
   163
	That's why now, SH_CALL takes an instance pointer instead of a callclass pointer. It basically does this:
dvander@659
   164
	1) call SH_GLOB_PTR->SetIgnoreHooks(vfnptr)
dvander@659
   165
	2) call this->*mfp
dvander@659
   166
	3) call SH_GLOB_PTR->ResetIgnoreHooks(vfnptr)
dvander@659
   167
dvander@659
   168
	SourceHook stroes the "ignored vfnptr" and makes CVfnPtr::FindIface return NULL if the CVfnPtr instance
dvander@659
   169
	corresponds to the ignored vfnptr. This way the hook manager thinks that the instance isn't hooked, and calls
dvander@659
   170
	the original function. Everything works fine. This works even for VP hooks.
dvander@541
   171
*/
dvander@541
   172
dvander@541
   173
namespace SourceHook
dvander@541
   174
{
dvander@541
   175
	/**
dvander@541
   176
	*	@brief The SourceHook implementation class
dvander@541
   177
	*/
dvander@541
   178
	class CSourceHookImpl : public ISourceHook
dvander@541
   179
	{
dvander@541
   180
	private:
dvander@541
   181
		class CProto
dvander@541
   182
		{
dvander@541
   183
			char *m_Proto;
dvander@541
   184
dvander@541
   185
			static bool Equal(const char *p1, const char *p2);
dvander@541
   186
			char *DupProto(const char *src);
dvander@541
   187
			void FreeProto(char *prot);
dvander@541
   188
		public:
dvander@659
   189
			CProto() : m_Proto(NULL)
dvander@659
   190
			{
dvander@659
   191
			}
dvander@659
   192
dvander@541
   193
			CProto(const char *szProto) : m_Proto(DupProto(szProto))
dvander@541
   194
			{
dvander@541
   195
			}
dvander@541
   196
dvander@541
   197
			CProto(const CProto &other) : m_Proto(DupProto(other.m_Proto))
dvander@541
   198
			{
dvander@541
   199
			}
dvander@541
   200
dvander@541
   201
			~CProto()
dvander@541
   202
			{
dvander@541
   203
				FreeProto(m_Proto);
dvander@541
   204
				m_Proto = NULL;
dvander@541
   205
			}
dvander@541
   206
dvander@541
   207
			void operator = (const char *szProto)
dvander@541
   208
			{
dvander@541
   209
				m_Proto = DupProto(szProto);
dvander@541
   210
			}
dvander@541
   211
dvander@541
   212
			void operator = (const CProto &other)
dvander@541
   213
			{
dvander@541
   214
				m_Proto = DupProto(other.m_Proto);
dvander@541
   215
			}
dvander@541
   216
dvander@541
   217
			bool operator == (const char *szProto) const
dvander@541
   218
			{
dvander@541
   219
				return Equal(szProto, m_Proto);
dvander@541
   220
			}
dvander@541
   221
			bool operator == (const CProto &other) const
dvander@541
   222
			{
dvander@541
   223
				return Equal(other.m_Proto, m_Proto);
dvander@541
   224
			}
dvander@659
   225
dvander@659
   226
			const char *GetProto() const
dvander@659
   227
			{
dvander@659
   228
				return m_Proto;
dvander@659
   229
			}
dvander@541
   230
		};
dvander@541
   231
dvander@541
   232
dvander@541
   233
		/**
dvander@541
   234
		*	@brief A hook can be removed if you have this information
dvander@541
   235
		*/
dvander@541
   236
		struct RemoveHookInfo
dvander@541
   237
		{
dvander@541
   238
			RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman,
dvander@541
   239
				ISHDelegate *phandler, bool ppost)
dvander@541
   240
				: plug(pplug), iface(piface), thisptr_offs(tpo),
dvander@541
   241
				hookman(phookman), handler(phandler), post(ppost)
dvander@541
   242
			{
dvander@541
   243
			}
dvander@541
   244
dvander@541
   245
			Plugin plug;
dvander@541
   246
			void *iface;
dvander@541
   247
			int thisptr_offs;
dvander@541
   248
			HookManagerPubFunc hookman;
dvander@541
   249
			ISHDelegate *handler;
dvander@541
   250
			bool post;
dvander@541
   251
		};
dvander@541
   252
dvander@541
   253
		struct RemoveHookManInfo
dvander@541
   254
		{
dvander@541
   255
			RemoveHookManInfo(Plugin pplug, HookManagerPubFunc phookman)
dvander@541
   256
				: plug(pplug), hookman(phookman)
dvander@541
   257
			{
dvander@541
   258
			}
dvander@541
   259
dvander@541
   260
			Plugin plug;
dvander@541
   261
			HookManagerPubFunc hookman;
dvander@541
   262
		};
dvander@541
   263
dvander@659
   264
		// Associates hook ids with info about the hooks
dvander@659
   265
		// Also used to keep track of used hook ids
dvander@659
   266
		class CHookIDManager
dvander@659
   267
		{
dvander@659
   268
		public:
dvander@659
   269
			struct Entry
dvander@659
   270
			{
dvander@659
   271
				bool isfree;
dvander@659
   272
dvander@659
   273
				// hookman info
dvander@659
   274
				CProto proto;
dvander@659
   275
				int vtbl_offs;
dvander@659
   276
				int vtbl_idx;
dvander@659
   277
dvander@659
   278
				// vfnptr
dvander@659
   279
				void *vfnptr;
dvander@659
   280
dvander@659
   281
				// iface
dvander@659
   282
				void* adjustediface;
dvander@659
   283
dvander@659
   284
				// hook
dvander@659
   285
				Plugin plug;
dvander@659
   286
				int thisptr_offs;
dvander@659
   287
				ISHDelegate *handler;
dvander@659
   288
				bool post;
dvander@659
   289
dvander@659
   290
				Entry(const CProto &pprt, int pvo, int pvi, void *pvp, void *pai, Plugin pplug, int pto,
dvander@659
   291
					ISHDelegate *ph, bool ppost)
dvander@659
   292
					: isfree(false), proto(pprt), vtbl_offs(pvo), vtbl_idx(pvi), vfnptr(pvp), 
dvander@659
   293
					adjustediface(pai), plug(pplug), thisptr_offs(pto), handler(ph), post(ppost)
dvander@659
   294
				{
dvander@659
   295
				}
dvander@659
   296
				Entry()
dvander@659
   297
				{
dvander@659
   298
				}
dvander@659
   299
			};
dvander@659
   300
		private:
dvander@659
   301
			// Internally, hookid 1 is stored as m_Entries[0]
dvander@659
   302
dvander@659
   303
			CVector<Entry> m_Entries;
dvander@659
   304
		public:
dvander@659
   305
			CHookIDManager();
dvander@659
   306
			int New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr, void *adjustediface,
dvander@659
   307
				Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
dvander@659
   308
			bool Remove(int hookid);
dvander@659
   309
			const Entry * QueryHook(int hookid);
dvander@659
   310
dvander@659
   311
			// Finds all hooks with the given info, and fills the hookids into output.
dvander@659
   312
			void FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs, int vtbl_idx,
dvander@659
   313
				void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
dvander@659
   314
dvander@659
   315
			// Removes all hooks with a specified vfnptr
dvander@659
   316
			bool RemoveAll(void *vfnptr);
dvander@659
   317
		};
dvander@659
   318
dvander@541
   319
		struct HookInfo
dvander@541
   320
		{
dvander@541
   321
			ISHDelegate *handler;			//!< Pointer to the handler
dvander@541
   322
			bool paused;					//!< If true, the hook should not be executed
dvander@541
   323
			Plugin plug;					//!< The owner plugin
dvander@541
   324
			int thisptr_offs;				//!< This pointer offset
dvander@659
   325
			int hookid;						//!< Unique ID given by CHookIDManager
dvander@659
   326
dvander@659
   327
			bool operator==(int otherid)
dvander@659
   328
			{
dvander@659
   329
				return hookid == otherid;
dvander@659
   330
			}
dvander@541
   331
		};
dvander@541
   332
dvander@541
   333
		class CHookList : public IHookList
dvander@541
   334
		{
dvander@541
   335
		public:
dvander@659
   336
			List<HookInfo> *m_VPList;			// left-hand list for ListCatIterator -> for VP hooks
dvander@541
   337
			List<HookInfo> m_List;
dvander@541
   338
dvander@541
   339
			friend class CIter;
dvander@541
   340
dvander@541
   341
			class CIter : public IHookList::IIter
dvander@541
   342
			{
dvander@541
   343
				friend class CHookList;
dvander@541
   344
dvander@541
   345
				CHookList *m_pList;
dvander@541
   346
dvander@541
   347
				void SkipPaused();
dvander@541
   348
			public:
dvander@541
   349
dvander@659
   350
				ListCatIterator<HookInfo> m_Iter;
dvander@541
   351
dvander@541
   352
				CIter(CHookList *pList);
dvander@541
   353
dvander@541
   354
				virtual ~CIter();
dvander@541
   355
dvander@541
   356
				void GoToBegin();
dvander@541
   357
				void Set(CIter *pOther);
dvander@541
   358
dvander@541
   359
				bool End();
dvander@541
   360
				void Next();
dvander@541
   361
				ISHDelegate *Handler();
dvander@541
   362
				int ThisPtrOffs();
dvander@541
   363
dvander@541
   364
				void Clear();
dvander@541
   365
dvander@541
   366
				CIter *m_pNext;		// When stored in m_FreeIters and m_UsedIters
dvander@541
   367
				CIter *m_pPrev;		// Only used when stored in m_UsedIters
dvander@541
   368
			};
dvander@541
   369
dvander@541
   370
			CIter *m_FreeIters;
dvander@541
   371
			CIter *m_UsedIters;		// The last returned and not-yet-released iter is always m_UsedIters
dvander@541
   372
dvander@541
   373
			// For recalls
dvander@541
   374
			bool m_Recall;
dvander@541
   375
			bool m_RQFlag;
dvander@541
   376
dvander@541
   377
			void SetRecallState();	// Sets the list into a state where the next returned
dvander@541
   378
									// iterator (from GetIter) will be a copy of the last
dvander@541
   379
									// returned iterator, incremented by one. This is used in Recalls.
dvander@541
   380
									// The hook resets this state automatically on:
dvander@541
   381
									// GetIter, ReleaseIter
dvander@541
   382
dvander@659
   383
			void RQFlagReset() { m_RQFlag = false; }
dvander@541
   384
			bool RQFlagGet() { return m_RQFlag; }
dvander@541
   385
			CHookList();
dvander@541
   386
			CHookList(const CHookList &other);
dvander@541
   387
			virtual ~CHookList();
dvander@541
   388
dvander@541
   389
			void operator=(const CHookList &other);
dvander@541
   390
dvander@541
   391
			IIter *GetIter();
dvander@541
   392
			void ReleaseIter(IIter *pIter);
dvander@659
   393
dvander@659
   394
			void SetVPList(List<HookInfo> *newList);
dvander@659
   395
			void ClearVPList();
dvander@541
   396
		};
dvander@541
   397
dvander@541
   398
		// I know, data hiding... But I'm a lazy bastard!
dvander@541
   399
dvander@541
   400
		class CIface : public IIface
dvander@541
   401
		{
dvander@541
   402
		public:
dvander@541
   403
			void *m_Ptr;
dvander@541
   404
			CHookList m_PreHooks;
dvander@541
   405
			CHookList m_PostHooks;
dvander@541
   406
		public:
dvander@541
   407
			CIface(void *ptr);
dvander@541
   408
			virtual ~CIface();
dvander@541
   409
dvander@541
   410
			void *GetPtr();
dvander@541
   411
			IHookList *GetPreHooks();
dvander@541
   412
			IHookList *GetPostHooks();
dvander@541
   413
dvander@541
   414
			bool operator==(void *ptr)
dvander@541
   415
			{
dvander@541
   416
				return m_Ptr == ptr;
dvander@541
   417
			}
dvander@659
   418
			bool operator!=(void *ptr)
dvander@659
   419
			{
dvander@659
   420
				return m_Ptr != ptr;
dvander@659
   421
			}
dvander@541
   422
		};
dvander@541
   423
dvander@541
   424
		class CVfnPtr : public IVfnPtr
dvander@541
   425
		{
dvander@541
   426
		public:
dvander@541
   427
			typedef List<CIface> IfaceList;
dvander@541
   428
			typedef IfaceList::iterator IfaceListIter;
dvander@541
   429
dvander@541
   430
			void *m_Ptr;
dvander@541
   431
			void *m_OrigEntry;
dvander@541
   432
dvander@541
   433
			IfaceList m_Ifaces;
dvander@541
   434
dvander@659
   435
			void **m_pOneIgnore;
dvander@541
   436
		public:
dvander@659
   437
			CVfnPtr(void *ptr, void **pOneIgnore);
dvander@541
   438
			virtual ~CVfnPtr();
dvander@541
   439
dvander@541
   440
			void *GetVfnPtr();
dvander@541
   441
			void *GetOrigEntry();
dvander@541
   442
dvander@541
   443
			virtual IIface *FindIface(void *ptr);
dvander@541
   444
dvander@541
   445
			bool operator==(void *ptr)
dvander@541
   446
			{
dvander@541
   447
				return m_Ptr == ptr;
dvander@541
   448
			}
dvander@541
   449
		};
dvander@541
   450
dvander@541
   451
		class CHookManagerInfo : public IHookManagerInfo
dvander@541
   452
		{
dvander@541
   453
		public:
dvander@541
   454
			typedef List<CVfnPtr> VfnPtrList;
dvander@541
   455
			typedef VfnPtrList::iterator VfnPtrListIter;
dvander@541
   456
dvander@541
   457
			Plugin m_Plug;
dvander@541
   458
			HookManagerPubFunc m_Func;
dvander@541
   459
dvander@541
   460
			int m_VtblOffs;
dvander@541
   461
			int m_VtblIdx;
dvander@541
   462
			const char *m_Proto;
dvander@541
   463
			void *m_HookfuncVfnptr;
dvander@541
   464
dvander@541
   465
			VfnPtrList m_VfnPtrs;
dvander@541
   466
dvander@541
   467
			int m_HookManVersion;
dvander@541
   468
		public:
dvander@541
   469
			CHookManagerInfo();
dvander@541
   470
			virtual ~CHookManagerInfo();
dvander@541
   471
dvander@541
   472
			IVfnPtr *FindVfnPtr(void *vfnptr);
dvander@541
   473
dvander@541
   474
			void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
dvander@541
   475
			void SetHookfuncVfnptr(void *hookfunc_vfnptr);
dvander@541
   476
dvander@541
   477
			void SetVersion(int version);
dvander@541
   478
dvander@541
   479
			bool operator < (const CHookManagerInfo &other)
dvander@541
   480
			{
dvander@541
   481
				return m_HookManVersion < other.m_HookManVersion;
dvander@541
   482
			}
dvander@541
   483
dvander@541
   484
			struct Descriptor
dvander@541
   485
			{
dvander@541
   486
				Descriptor(Plugin pplug, HookManagerPubFunc ppubFunc) : plug(pplug), pubFunc(ppubFunc)
dvander@541
   487
				{
dvander@541
   488
				}
dvander@541
   489
dvander@541
   490
				Plugin plug;
dvander@541
   491
				HookManagerPubFunc pubFunc;
dvander@541
   492
			};
dvander@541
   493
dvander@541
   494
			bool operator == (const Descriptor desc)
dvander@541
   495
			{
dvander@541
   496
				return m_Func == desc.pubFunc && m_Plug == desc.plug;
dvander@541
   497
			}
dvander@541
   498
		};
dvander@541
   499
dvander@541
   500
		typedef List<CHookManagerInfo> HookManInfoList;
dvander@541
   501
dvander@541
   502
		class CHookManagerContainer : public HookManInfoList
dvander@541
   503
		{
dvander@541
   504
		public:
dvander@541
   505
			// HMCI (Hook Manager Container Identification)
dvander@541
   506
			class HMCI
dvander@541
   507
			{
dvander@541
   508
				CProto m_Proto;
dvander@541
   509
				int m_VtableOffset;
dvander@541
   510
				int m_VtableIndex;
dvander@541
   511
			public:
dvander@541
   512
				HMCI(const char *proto, int vtbloffs, int vtblidx) :
dvander@541
   513
				  m_Proto(proto), m_VtableOffset(vtbloffs), m_VtableIndex(vtblidx)
dvander@541
   514
				  {
dvander@541
   515
				  }
dvander@541
   516
				  ~HMCI()
dvander@541
   517
				  {
dvander@541
   518
				  }
dvander@541
   519
dvander@541
   520
				  bool operator==(const HMCI &other) const
dvander@541
   521
				  {
dvander@541
   522
					  return
dvander@541
   523
						  other.m_VtableIndex == m_VtableIndex &&
dvander@541
   524
						  other.m_Proto == m_Proto &&
dvander@541
   525
						  other.m_VtableOffset == m_VtableOffset;
dvander@541
   526
				  }
dvander@541
   527
dvander@541
   528
				  const CProto &GetProto() const
dvander@541
   529
				  {
dvander@541
   530
					  return m_Proto;
dvander@541
   531
				  }
dvander@541
   532
				  int GetVtableOffset() const
dvander@541
   533
				  {
dvander@541
   534
					  return m_VtableOffset;
dvander@541
   535
				  }
dvander@541
   536
				  int GetVtableIndex() const
dvander@541
   537
				  {
dvander@541
   538
					  return m_VtableIndex;
dvander@541
   539
				  }
dvander@541
   540
			};
dvander@541
   541
			HMCI m_HCMI;
dvander@541
   542
dvander@541
   543
		public:
dvander@541
   544
			CHookManagerContainer(const HMCI &hmci) : m_HCMI(hmci)
dvander@541
   545
			{
dvander@541
   546
			}
dvander@541
   547
			bool operator == (const HMCI &other) const
dvander@541
   548
			{
dvander@541
   549
				return m_HCMI == other;
dvander@541
   550
			}
dvander@541
   551
			void AddHookManager(Plugin plug, const CHookManagerInfo &hookman);
dvander@541
   552
		};
dvander@541
   553
dvander@659
   554
		class CCallClassImpl : public DeprecatedCallClass<void>
dvander@541
   555
		{
dvander@541
   556
		public:
dvander@541
   557
dvander@541
   558
			typedef SourceHook::CVector<void*> OrigFuncs;
dvander@541
   559
			typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
dvander@541
   560
dvander@541
   561
			void *m_Ptr;			//!< Pointer to the actual object
dvander@541
   562
			size_t m_ObjSize;		//!< Size of the instance
dvander@541
   563
			OrigVTables m_VT;		//!< Info about vtables & functions
dvander@541
   564
dvander@541
   565
			int m_RefCounter;
dvander@541
   566
dvander@541
   567
			CCallClassImpl(void *ptr, size_t size);
dvander@541
   568
			virtual ~CCallClassImpl();
dvander@541
   569
dvander@541
   570
			bool operator==(void *other)
dvander@541
   571
			{
dvander@541
   572
				return m_Ptr == other;
dvander@541
   573
			}
dvander@541
   574
dvander@541
   575
			void *GetThisPtr();
dvander@541
   576
			void *GetOrigFunc(int vtbloffs, int vtblidx);
dvander@541
   577
dvander@541
   578
			void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
dvander@541
   579
			void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
dvander@541
   580
		};
dvander@541
   581
dvander@541
   582
		/**
dvander@541
   583
		*	@brief A list of CallClass structures
dvander@541
   584
		*/
dvander@541
   585
		typedef List<CCallClassImpl> Impl_CallClassList;
dvander@541
   586
dvander@541
   587
		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
dvander@541
   588
dvander@541
   589
		/**
dvander@541
   590
		*	@brief A list of CHookManagerContainers
dvander@541
   591
		*/
dvander@541
   592
		typedef List<CHookManagerContainer> HookManContList;
dvander@541
   593
dvander@541
   594
		HookManContList m_HookMans;					//!< A list of hook managers
dvander@541
   595
dvander@541
   596
		struct HookLoopInfo
dvander@541
   597
		{
dvander@541
   598
			enum RecallType
dvander@541
   599
			{
dvander@541
   600
				Recall_No=0,
dvander@541
   601
				Recall_Pre,
dvander@541
   602
				Recall_Post1,
dvander@541
   603
				Recall_Post2
dvander@541
   604
			};
dvander@541
   605
dvander@541
   606
			META_RES *pStatus;
dvander@541
   607
			META_RES *pPrevRes;
dvander@541
   608
			META_RES *pCurRes;
dvander@541
   609
dvander@541
   610
			META_RES temporaryStatus;				//!< Stored during Post1 recall phase
dvander@541
   611
			bool shouldContinue;
dvander@541
   612
			RecallType recall;						//!< Specifies which kind of recall we're in.
dvander@541
   613
dvander@541
   614
			IIface *pCurIface;
dvander@541
   615
			const void *pOrigRet;
dvander@541
   616
			void *pOverrideRet;
dvander@541
   617
			void **pIfacePtrPtr;
dvander@541
   618
		};
dvander@541
   619
		typedef CStack<HookLoopInfo> HookLoopInfoStack;
dvander@541
   620
dvander@541
   621
		void ApplyCallClassPatches(CCallClassImpl &cc);
dvander@541
   622
		void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
dvander@541
   623
		void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
dvander@541
   624
dvander@541
   625
		void SetPluginPaused(Plugin plug, bool paused);
dvander@541
   626
dvander@541
   627
		HookLoopInfoStack m_HLIStack;
dvander@659
   628
		CHookIDManager m_HookIDMan;
dvander@659
   629
dvander@659
   630
		void *m_OneIgnore; //:TODO:
dvander@659
   631
		bool m_IgnoreActive;
dvander@541
   632
	public:
dvander@541
   633
		CSourceHookImpl();
dvander@541
   634
		virtual ~CSourceHookImpl();
dvander@541
   635
dvander@541
   636
		/**
dvander@541
   637
		*	@brief Returns the interface version
dvander@541
   638
		*/
dvander@541
   639
		int GetIfaceVersion();
dvander@541
   640
dvander@541
   641
		/**
dvander@541
   642
		*	@brief Returns the implemnetation version
dvander@541
   643
		*/
dvander@541
   644
		int GetImplVersion();
dvander@541
   645
dvander@541
   646
		/**
dvander@541
   647
		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
dvander@541
   648
		*/
dvander@541
   649
		void UnloadPlugin(Plugin plug);
dvander@541
   650
dvander@541
   651
		/**
dvander@541
   652
		*	@brief Shut down the whole system, unregister all hook managers
dvander@541
   653
		*/
dvander@541
   654
		void CompleteShutdown();
dvander@541
   655
dvander@541
   656
		/**
dvander@541
   657
		*	@brief Add a hook.
dvander@541
   658
		*
dvander@541
   659
		*	@return True if the function succeeded, false otherwise
dvander@541
   660
		*
dvander@541
   661
		*	@param plug The unique identifier of the plugin that calls this function
dvander@541
   662
		*	@param iface The interface pointer
dvander@541
   663
		*	@param ifacesize The size of the class iface points to
dvander@541
   664
		*	@param myHookMan A hook manager function that should be capable of handling the function
dvander@541
   665
		*	@param handler A pointer to a FastDelegate containing the hook handler
dvander@541
   666
		*	@param post Set to true if you want a post handler
dvander@541
   667
		*/
dvander@541
   668
		bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
dvander@541
   669
dvander@541
   670
		/**
dvander@541
   671
		*	@brief Removes a hook.
dvander@541
   672
		*
dvander@541
   673
		*	@return True if the function succeeded, false otherwise
dvander@541
   674
		*
dvander@541
   675
		*	@param plug The unique identifier of the plugin that calls this function
dvander@541
   676
		*	@param iface The interface pointer
dvander@541
   677
		*	@param thisptr_offs This pointer adjuster
dvander@541
   678
		*	@param myHookMan A hook manager function that should be capable of handling the function
dvander@541
   679
		*	@param handler A pointer to a FastDelegate containing the hook handler
dvander@541
   680
		*	@param post Set to true if you want a post handler
dvander@541
   681
		*/
dvander@541
   682
		bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
dvander@541
   683
dvander@541
   684
		/**
dvander@541
   685
		*	@brief Removes a hook.
dvander@541
   686
		*
dvander@541
   687
		*	@ return True if the function succeeded, false otherwise
dvander@541
   688
		*
dvander@541
   689
		*	@param info A RemoveHookInfo structure, describing the hook
dvander@541
   690
		*/
dvander@541
   691
		bool RemoveHook(RemoveHookInfo info);
dvander@541
   692
dvander@541
   693
		/**
dvander@541
   694
		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
dvander@541
   695
		*
dvander@541
   696
		*	@param plug The unique identifier of the plugin in question
dvander@541
   697
		*/
dvander@541
   698
		bool IsPluginInUse(Plugin plug);
dvander@541
   699
dvander@541
   700
		/**
dvander@541
   701
		*	@brief Pauses all hooks of a plugin
dvander@541
   702
		*
dvander@541
   703
		*	@param plug The unique identifier of the plugin
dvander@541
   704
		*/
dvander@541
   705
		void PausePlugin(Plugin plug);
dvander@541
   706
dvander@541
   707
		/**
dvander@541
   708
		*	@brief Unpauses all hooks of a plugin
dvander@541
   709
		*
dvander@541
   710
		*	@param plug The unique identifier of the plugin
dvander@541
   711
		*/
dvander@541
   712
		void UnpausePlugin(Plugin plug);
dvander@541
   713
dvander@541
   714
		/**
dvander@541
   715
		*	@brief Return a pointer to a callclass. Generate a new one if required.
dvander@541
   716
		*
dvander@541
   717
		*	@param iface The interface pointer
dvander@541
   718
		*	@param size Size of the class instance
dvander@541
   719
		*/
dvander@659
   720
		DeprecatedCallClass<void> *GetCallClass(void *iface, size_t size);
dvander@541
   721
dvander@541
   722
		/**
dvander@541
   723
		*	@brief Release a callclass
dvander@541
   724
		*
dvander@541
   725
		*	@param ptr Pointer to the callclass
dvander@541
   726
		*/
dvander@659
   727
		virtual void ReleaseCallClass(DeprecatedCallClass<void> *ptr);
dvander@541
   728
dvander@541
   729
		virtual void SetRes(META_RES res);				//!< Sets the meta result
dvander@541
   730
		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
dvander@541
   731
		virtual META_RES GetStatus();					//!< Gets the highest meta result
dvander@541
   732
		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
dvander@541
   733
		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
dvander@541
   734
		virtual void *GetIfacePtr();					//!< Gets the interface pointer
dvander@541
   735
dvander@541
   736
		//////////////////////////////////////////////////////////////////////////
dvander@541
   737
		// For hook managers
dvander@541
   738
		void HookLoopBegin(IIface *pIface);			//!< Should be called when a hook loop begins
dvander@541
   739
		void HookLoopEnd();							//!< Should be called when a hook loop exits
dvander@541
   740
		void SetCurResPtr(META_RES *mres);			//!< Sets pointer to the current meta result
dvander@541
   741
		void SetPrevResPtr(META_RES *mres);			//!< Sets pointer to previous meta result
dvander@541
   742
		void SetStatusPtr(META_RES *mres);			//!< Sets pointer to the status variable
dvander@541
   743
		void SetIfacePtrPtr(void **pp);				//!< Sets pointer to the interface this pointer
dvander@541
   744
		void SetOrigRetPtr(const void *ptr);		//!< Sets the original return pointer
dvander@541
   745
		void SetOverrideRetPtr(void *ptr);			//!< Sets the override result pointer
dvander@541
   746
		bool ShouldContinue();						//!< Returns false if the hook loop should exit
dvander@541
   747
dvander@541
   748
		/**
dvander@541
   749
		*	@brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
dvander@541
   750
		*
dvander@541
   751
		*	@param plug The owner of the hook manager
dvander@541
   752
		*   @param pubFunc The hook manager's info function
dvander@541
   753
		*/
dvander@541
   754
		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc);
dvander@541
   755
		virtual void RemoveHookManager(RemoveHookManInfo info);
dvander@541
   756
dvander@541
   757
		virtual void DoRecall();					//!< Initiates a recall sequence
dvander@541
   758
		virtual void *GetOverrideRetPtr();			//!< Returns the pointer set by SetOverrideRetPtr
dvander@541
   759
dvander@541
   760
		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
dvander@541
   761
			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr);
dvander@659
   762
dvander@659
   763
		/**
dvander@659
   764
		*	@brief Add a (VP) hook.
dvander@659
   765
		*
dvander@659
   766
		*	@return non-zero hook id on success, 0 otherwise
dvander@659
   767
		*
dvander@659
   768
		*	@param plug The unique identifier of the plugin that calls this function
dvander@659
   769
		*	@param mode	Can be either Hook_Normal or Hook_VP (vtable-wide hook)
dvander@659
   770
		*	@param iface The interface pointer
dvander@659
   771
		*				 The representative interface pointer for VP hooks
dvander@659
   772
		*				 The vtable pointer for direct VP hooks !!!
dvander@659
   773
		*	@param ifacesize The size of the class iface points to
dvander@659
   774
		*	@param myHookMan A hook manager function that should be capable of handling the function
dvander@659
   775
		*	@param handler A pointer to a FastDelegate containing the hook handler
dvander@659
   776
		*	@param post Set to true if you want a post handler
dvander@659
   777
		*/
dvander@659
   778
		virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
dvander@659
   779
			ISHDelegate *handler, bool post);
dvander@659
   780
dvander@659
   781
		/**
dvander@659
   782
		*	@brief Remove a VP hook by ID.
dvander@659
   783
		*
dvander@659
   784
		*	@return true on success, false otherwise
dvander@659
   785
		*
dvander@659
   786
		*	@param plug The unique identifier of the plugin that calls this function
dvander@659
   787
		*	@param hookid The hook id (returned by AddHookNew)
dvander@659
   788
		*/
dvander@659
   789
		virtual bool RemoveHookByID(Plugin plug, int hookid);
dvander@659
   790
dvander@659
   791
		/**
dvander@659
   792
		*	@brief Makes sure that hooks are going to be ignored on the next call of vfnptr
dvander@659
   793
		*
dvander@659
   794
		*	@param plug The unique identifier of the plugin that calls this function
dvander@659
   795
		*	@param vfnptr The virtual function pointer of the function in question
dvander@659
   796
		*/
dvander@659
   797
		virtual void SetIgnoreHooks(Plugin plug, void *vfnptr);
dvander@659
   798
dvander@659
   799
		/**
dvander@659
   800
		*	@brief Reverses SetIgnoreHooks' effect
dvander@659
   801
		*
dvander@659
   802
		*	@param plug The unique identifier of the plugin that calls this function
dvander@659
   803
		*	@param vfnptr The virtual function pointer of the function in question
dvander@659
   804
		*/
dvander@659
   805
		virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr);
dvander@659
   806
dvander@659
   807
		/**
dvander@659
   808
		*	@brief Finds the original entry of a virtual function pointer
dvander@659
   809
		*
dvander@659
   810
		*	@param vfnptr The virtual function pointer
dvander@659
   811
		*	@return The original entry if the virtual function pointer has been patched; NULL otherwise.
dvander@659
   812
		*/
dvander@659
   813
		virtual void *GetOrigVfnPtrEntry(void *vfnptr);
dvander@541
   814
	};
dvander@541
   815
}
dvander@541
   816
dvander@541
   817
#endif