core/sourcehook/sourcehook_impl_cvfnptr.h
changeset 539 db914129ae01
parent 523 6f1630519b81
child 733 2365240861a2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sourcehook/sourcehook_impl_cvfnptr.h	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* ======== SourceHook ========
     1.5 +* Copyright (C) 2004-2008 Metamod:Source Development Team
     1.6 +* No warranties of any kind
     1.7 +*
     1.8 +* License: zlib/libpng
     1.9 +*
    1.10 +* Author(s): Pavol "PM OnoTo" Marko
    1.11 +* ============================
    1.12 +*/
    1.13 +
    1.14 +#ifndef __SOURCEHOOK_IMPL_CVFNPTR_H__
    1.15 +#define __SOURCEHOOK_IMPL_CVFNPTR_H__
    1.16 +
    1.17 +#include "sh_list.h"
    1.18 +#include "sh_memory.h"
    1.19 +
    1.20 +namespace SourceHook
    1.21 +{
    1.22 +	namespace Impl
    1.23 +	{
    1.24 +		class CVfnPtr
    1.25 +		{
    1.26 +			// *** Data ***
    1.27 +			void *m_Ptr;
    1.28 +			void *m_OrigEntry;
    1.29 +
    1.30 +			List<CHookManager*> m_HookMans;
    1.31 +			List<CIface> m_IfaceList;
    1.32 +		public:
    1.33 +			// *** Descriptor ***
    1.34 +			typedef void* Descriptor;
    1.35 +
    1.36 +			// *** Interface ***
    1.37 +			inline CVfnPtr(void *ptr);
    1.38 +			inline ~CVfnPtr();
    1.39 +			inline bool operator==(const Descriptor &other);
    1.40 +			inline void *GetPtr() const;
    1.41 +			inline void *GetOrigEntry() const;
    1.42 +			inline List<CIface> &GetIfaceList();
    1.43 +			inline const List<CIface> &GetIfaceList() const;
    1.44 +			inline CIface *FindIface(void *iface);
    1.45 +			CIface &GetIface(void *iface);
    1.46 +			bool Patch(void *newValue);
    1.47 +			inline bool Revert();
    1.48 +
    1.49 +			void AddHookMan(CHookManager *pHookMan);
    1.50 +			// If this returns false, it means that there is no hook manager left
    1.51 +			// to use and that this vfnptr should be removed.
    1.52 +			bool HookManRemoved(CHookManager *pHookMan);
    1.53 +		};
    1.54 +
    1.55 +		// *** Implementation ***
    1.56 +		inline CVfnPtr::CVfnPtr(void *ptr)
    1.57 +			: m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(m_Ptr))
    1.58 +		{
    1.59 +		}
    1.60 +
    1.61 +		inline CVfnPtr::~CVfnPtr()
    1.62 +		{
    1.63 +			if (!m_HookMans.empty())
    1.64 +				m_HookMans.front()->DecrRef(this);
    1.65 +		}
    1.66 +
    1.67 +		inline bool CVfnPtr::operator==(const Descriptor &other)
    1.68 +		{
    1.69 +			return m_Ptr == other;
    1.70 +		}
    1.71 +
    1.72 +		inline void *CVfnPtr::GetPtr() const
    1.73 +		{
    1.74 +			return m_Ptr;
    1.75 +		}
    1.76 +
    1.77 +		inline void *CVfnPtr::GetOrigEntry() const
    1.78 +		{
    1.79 +			return m_OrigEntry;
    1.80 +		}
    1.81 +
    1.82 +		inline List<CIface> &CVfnPtr::GetIfaceList()
    1.83 +		{
    1.84 +			return m_IfaceList;
    1.85 +		}
    1.86 +
    1.87 +		inline const List<CIface> &CVfnPtr::GetIfaceList() const
    1.88 +		{
    1.89 +			return m_IfaceList;
    1.90 +		}
    1.91 +
    1.92 +		inline bool CVfnPtr::Revert()
    1.93 +		{
    1.94 +			// Only patch the vfnptr back if the module is still in memory
    1.95 +			// If it's not, do not remove stuff like we did before
    1.96 +			// First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
    1.97 +			// confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
    1.98 +			if (ModuleInMemory(reinterpret_cast<char*>(m_Ptr), SH_PTRSIZE))
    1.99 +			{
   1.100 +				return Patch(m_OrigEntry);
   1.101 +			}
   1.102 +			else
   1.103 +			{
   1.104 +				return true;
   1.105 +			}
   1.106 +		}
   1.107 +
   1.108 +		inline CIface *CVfnPtr::FindIface(void *iface)
   1.109 +		{
   1.110 +			List<CIface>::iterator iter = m_IfaceList.find(iface);
   1.111 +			if (iter == m_IfaceList.end())
   1.112 +				return NULL;
   1.113 +			else
   1.114 +				return &(*iter);
   1.115 +		}
   1.116 +	}
   1.117 +}
   1.118 +
   1.119 +#endif
   1.120 +