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