core/sourcehook/sourcehook_impl_cvfnptr.h
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 01 02:43:22 2010 -0500 (2010-07-01)
changeset 722 78f730f91439
parent 539 db914129ae01
child 733 2365240861a2
permissions -rw-r--r--
Fixed problem on OBV where alternately loaded plugins (CreateInterface_MMS) could be incorrectly told they were on the OB engine (bug 4491, r=dvander).

This only happened when there was more than one plugin that loaded this way. And the first plugin to load had to fail on OBV first so that OB could be tried. It was being permanently set to OB for future plugin loads and this caused problems.
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2008 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_CVFNPTR_H__
    12 #define __SOURCEHOOK_IMPL_CVFNPTR_H__
    13 
    14 #include "sh_list.h"
    15 #include "sh_memory.h"
    16 
    17 namespace SourceHook
    18 {
    19 	namespace Impl
    20 	{
    21 		class CVfnPtr
    22 		{
    23 			// *** Data ***
    24 			void *m_Ptr;
    25 			void *m_OrigEntry;
    26 
    27 			List<CHookManager*> m_HookMans;
    28 			List<CIface> m_IfaceList;
    29 		public:
    30 			// *** Descriptor ***
    31 			typedef void* Descriptor;
    32 
    33 			// *** Interface ***
    34 			inline CVfnPtr(void *ptr);
    35 			inline ~CVfnPtr();
    36 			inline bool operator==(const Descriptor &other);
    37 			inline void *GetPtr() const;
    38 			inline void *GetOrigEntry() const;
    39 			inline List<CIface> &GetIfaceList();
    40 			inline const List<CIface> &GetIfaceList() const;
    41 			inline CIface *FindIface(void *iface);
    42 			CIface &GetIface(void *iface);
    43 			bool Patch(void *newValue);
    44 			inline bool Revert();
    45 
    46 			void AddHookMan(CHookManager *pHookMan);
    47 			// If this returns false, it means that there is no hook manager left
    48 			// to use and that this vfnptr should be removed.
    49 			bool HookManRemoved(CHookManager *pHookMan);
    50 		};
    51 
    52 		// *** Implementation ***
    53 		inline CVfnPtr::CVfnPtr(void *ptr)
    54 			: m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(m_Ptr))
    55 		{
    56 		}
    57 
    58 		inline CVfnPtr::~CVfnPtr()
    59 		{
    60 			if (!m_HookMans.empty())
    61 				m_HookMans.front()->DecrRef(this);
    62 		}
    63 
    64 		inline bool CVfnPtr::operator==(const Descriptor &other)
    65 		{
    66 			return m_Ptr == other;
    67 		}
    68 
    69 		inline void *CVfnPtr::GetPtr() const
    70 		{
    71 			return m_Ptr;
    72 		}
    73 
    74 		inline void *CVfnPtr::GetOrigEntry() const
    75 		{
    76 			return m_OrigEntry;
    77 		}
    78 
    79 		inline List<CIface> &CVfnPtr::GetIfaceList()
    80 		{
    81 			return m_IfaceList;
    82 		}
    83 
    84 		inline const List<CIface> &CVfnPtr::GetIfaceList() const
    85 		{
    86 			return m_IfaceList;
    87 		}
    88 
    89 		inline bool CVfnPtr::Revert()
    90 		{
    91 			// Only patch the vfnptr back if the module is still in memory
    92 			// If it's not, do not remove stuff like we did before
    93 			// First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
    94 			// confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
    95 			if (ModuleInMemory(reinterpret_cast<char*>(m_Ptr), SH_PTRSIZE))
    96 			{
    97 				return Patch(m_OrigEntry);
    98 			}
    99 			else
   100 			{
   101 				return true;
   102 			}
   103 		}
   104 
   105 		inline CIface *CVfnPtr::FindIface(void *iface)
   106 		{
   107 			List<CIface>::iterator iter = m_IfaceList.find(iface);
   108 			if (iter == m_IfaceList.end())
   109 				return NULL;
   110 			else
   111 				return &(*iter);
   112 		}
   113 	}
   114 }
   115 
   116 #endif
   117