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