@@ -184,6 +184,20 @@ namespace SourceHook
184184 */
185185 struct PassInfo
186186 {
187+ PassInfo ()
188+ : size(0 )
189+ , type(0 )
190+ , flags(0 )
191+ {
192+ }
193+
194+ PassInfo (size_t typeSize, int typeId, unsigned int typeFlags)
195+ : size(typeSize)
196+ , type(typeId)
197+ , flags(typeFlags)
198+ {
199+ }
200+
187201 enum PassType
188202 {
189203 PassType_Unknown=0 , /* *< Unknown -- no extra info available */
@@ -217,6 +231,22 @@ namespace SourceHook
217231
218232 struct V2Info
219233 {
234+ V2Info ()
235+ : pNormalCtor(nullptr )
236+ , pCopyCtor(nullptr )
237+ , pDtor(nullptr )
238+ , pAssignOperator(nullptr )
239+ {
240+ }
241+
242+ V2Info (void *normalCtor, void *copyCtor, void *dtor, void *assignOperator)
243+ : pNormalCtor(normalCtor)
244+ , pCopyCtor(copyCtor)
245+ , pDtor(dtor)
246+ , pAssignOperator(assignOperator)
247+ {
248+ }
249+
220250 void *pNormalCtor;
221251 void *pCopyCtor;
222252 void *pDtor;
@@ -252,6 +282,19 @@ namespace SourceHook
252282 const PassInfo::V2Info *paramsPassInfo2;
253283 };
254284
285+ class IProtoInfo
286+ {
287+ public:
288+ virtual ~IProtoInfo () = default ;
289+ virtual int GetNumOfParams () const = 0;
290+ virtual const PassInfo &GetRetPassInfo () const = 0;
291+ virtual const PassInfo *GetParamsPassInfo () const = 0;
292+ virtual int GetConvention () const = 0;
293+ virtual int GetVersion () const = 0;
294+ virtual const PassInfo::V2Info &GetRetPassInfo2 () const = 0;
295+ virtual const PassInfo::V2Info *GetParamsPassInfo2 () const = 0;
296+ };
297+
255298 struct IHookManagerInfo ;
256299
257300 /* *
@@ -409,6 +452,9 @@ namespace SourceHook
409452 {
410453 virtual void SetInfo (int hookman_version, int vtbloffs, int vtblidx,
411454 ProtoInfo *proto, void *hookfunc_vfnptr) = 0;
455+
456+ virtual void SetInfo (int hookman_version, int vtbloffs, int vtblidx,
457+ IProtoInfo *proto, void *hookfunc_vfnptr) = 0;
412458 };
413459
414460 // I'm adding support for functions which return references.
@@ -5130,12 +5176,17 @@ class PassInfoInitializer
51305176 InitializePassInfo<sizeof ...(ArgsType), 0 , ArgsType...>();
51315177 }
51325178
5133- const PassInfo *ParamsPassInfo () const
5179+ const PassInfo *GetParamsPassInfo () const
51345180 {
51355181 return params_;
51365182 }
51375183
5138- const size_t ParamsPassInfoSize () const
5184+ const PassInfo::V2Info *GetParamsPassInfoV2 () const
5185+ {
5186+ return paramsV2_;
5187+ }
5188+
5189+ const size_t GetParamsPassInfoSize () const
51395190 {
51405191 return sizeof ...(ArgsType);
51415192 }
@@ -5155,6 +5206,32 @@ class PassInfoInitializer
51555206 }
51565207
51575208 PassInfo params_[sizeof ...(ArgsType)];
5209+ PassInfo::V2Info paramsV2_[sizeof ...(ArgsType)];
5210+ };
5211+
5212+ // For zero arguments
5213+ template <>
5214+ class PassInfoInitializer <>
5215+ {
5216+ public:
5217+ constexpr PassInfoInitializer ()
5218+ {
5219+ }
5220+
5221+ const PassInfo *GetParamsPassInfo () const
5222+ {
5223+ return nullptr ;
5224+ }
5225+
5226+ const PassInfo::V2Info *GetParamsPassInfoV2 () const
5227+ {
5228+ return nullptr ;
5229+ }
5230+
5231+ const size_t GetParamsPassInfoSize () const
5232+ {
5233+ return 0 ;
5234+ }
51585235};
51595236
51605237template <typename T>
@@ -5181,7 +5258,7 @@ struct ReturnTypeInfo<void>
51815258{
51825259 static const size_t size ()
51835260 {
5184- return 0 ; // why isn't it sizeof(void) like in TypeInfo<T>?
5261+ return 0 ;
51855262 }
51865263
51875264 static const int type ()
@@ -5222,6 +5299,65 @@ class CHookManagerMemberFuncHandler : public IHookManagerMemberFunc
52225299 HookManagerMemberFunc func_;
52235300};
52245301
5302+ template <typename ReturnType, class ... Params>
5303+ class CProtoInfo : public IProtoInfo
5304+ {
5305+ public:
5306+ constexpr CProtoInfo ()
5307+ : retPassInfo(ReturnTypeInfo<ReturnType>::size(),
5308+ ReturnTypeInfo<ReturnType>::type(),
5309+ ReturnTypeInfo<ReturnType>::flags())
5310+ {
5311+ }
5312+
5313+ virtual ~CProtoInfo () override
5314+ {
5315+ }
5316+
5317+ virtual int GetNumOfParams () const override
5318+ {
5319+ return paramsPassInfo.GetParamsPassInfoSize ();
5320+ }
5321+
5322+ virtual const PassInfo &GetRetPassInfo () const override
5323+ {
5324+ return retPassInfo;
5325+ }
5326+
5327+ virtual const PassInfo *GetParamsPassInfo () const override
5328+ {
5329+ return paramsPassInfo.GetParamsPassInfo ();
5330+ }
5331+
5332+ virtual int GetConvention () const override
5333+ {
5334+ return 0 ;
5335+ }
5336+
5337+ // version of the ProtoInfo structure.
5338+ virtual int GetVersion () const override
5339+ {
5340+ // 1 for Version2
5341+ return 1 ;
5342+ }
5343+
5344+ virtual const PassInfo::V2Info &GetRetPassInfo2 () const override
5345+ {
5346+ return retPassInfo2;
5347+ }
5348+
5349+ virtual const PassInfo::V2Info *GetParamsPassInfo2 () const override
5350+ {
5351+ return paramsPassInfo.GetParamsPassInfoV2 ();
5352+ }
5353+
5354+ private:
5355+ int numOfParams; // !< number of parameters
5356+ PassInfo retPassInfo; // !< PassInfo for the return value. size=0 -> no retval
5357+ PassInfo::V2Info retPassInfo2; // !< Version2 only
5358+ PassInfoInitializer<Params...> paramsPassInfo; // !< PassInfos for the parameters
5359+ };
5360+
52255361template <typename ReturnType, class ... Params>
52265362class ManualHookHandler : public CHookManagerMemberFuncHandler <ManualHookHandler<ReturnType, Params...>>
52275363{
@@ -5237,40 +5373,18 @@ class ManualHookHandler : public CHookManagerMemberFuncHandler<ManualHookHandler
52375373 typedef ReturnType (T::*HookFunc)(Params...);
52385374 };
52395375
5240- ManualHookHandler ()
5376+ constexpr ManualHookHandler ()
52415377 : CHookManagerMemberFuncHandler<ThisType>(this , &ThisType::HookManPubFunc)
5242- , thisPointerOffset_(0 )
5243- , vTableIndex_(0 )
5244- , vtableOffset_(0 )
52455378 , msMFI_{false , 0 , 0 , 0 }
52465379 , msHI_(nullptr )
5247- , msProto_{sizeof ...(Params),
5248- {ReturnTypeInfo<ReturnType>::size (), ReturnTypeInfo<ReturnType>::type (), ReturnTypeInfo<ReturnType>::flags ()},
5249- paramInfosM_.ParamsPassInfo (),
5250- 0 ,
5251- __SH_EPI,
5252- paramInfos2M_}
52535380 {
5254- for (PassInfo::V2Info& paramInfo : paramInfos2M_)
5255- {
5256- paramInfo = __SH_EPI;
5257- }
52585381 }
52595382
52605383 virtual ~ManualHookHandler ()
52615384 {
52625385 // TODO apply RAII, cleanup all related hooks here
52635386 }
52645387
5265- // TODO probably not needed for manual hooks
5266- void Reconfigure ()
5267- {
5268- msMFI_.isVirtual = true ;
5269- msMFI_.thisptroffs = thisPointerOffset_;
5270- msMFI_.vtblindex = vTableIndex_;
5271- msMFI_.vtbloffs = vtableOffset_;
5272- }
5273-
52745388 void Reconfigure (int vtblindex, int vtbloffs = 0 , int thisptroffs = 0 )
52755389 {
52765390 g_SHPtr->RemoveHookManager (g_PLID, this );
@@ -5285,7 +5399,7 @@ class ManualHookHandler : public CHookManagerMemberFuncHandler<ManualHookHandler
52855399 typename ManualHookHandler::FD handler (callbackInstPtr, callbackFuncPtr);
52865400 typename ThisType::CMyDelegateImpl* tmp = new typename ThisType::CMyDelegateImpl (handler); // TODO use unique_ptr here
52875401
5288- return g_SHPtr->AddHook (g_PLID, mode, iface, thisPointerOffset_ , this , tmp, post );
5402+ return g_SHPtr->AddHook (g_PLID, mode, iface, 0 , this , tmp, post );
52895403 }
52905404
52915405 template <typename T>
@@ -5294,7 +5408,7 @@ class ManualHookHandler : public CHookManagerMemberFuncHandler<ManualHookHandler
52945408 typename ManualHookHandler::FD handler (callbackInstPtr, callbackFuncPtr);
52955409 typename ThisType::CMyDelegateImpl tmp (handler);
52965410
5297- return g_SHPtr->RemoveHook (g_PLID, iface, thisPointerOffset_ , this , &tmp, post );
5411+ return g_SHPtr->RemoveHook (g_PLID, iface, 0 , this , &tmp, post );
52985412 }
52995413
53005414 // For void return type only
@@ -5557,16 +5671,10 @@ class ManualHookHandler : public CHookManagerMemberFuncHandler<ManualHookHandler
55575671 }
55585672
55595673private:
5560- int thisPointerOffset_; // thisptroffs
5561- int vTableIndex_; // vtblindex
5562- int vtableOffset_; // vtbloffs
5563-
55645674 MemFuncInfo msMFI_; // ms_MFI
55655675 IHookManagerInfo *msHI_; // ms_HI
55665676
5567- PassInfoInitializer<void , Params...> paramInfosM_; // ParamInfosM
5568- PassInfo::V2Info paramInfos2M_[sizeof ...(Params) + 1 ]; // ParamInfos2M
5569- ProtoInfo msProto_; // ms_Proto
5677+ CProtoInfo<ReturnType, Params...> msProto_; // ms_Proto
55705678};
55715679
55725680} // SourceHook
0 commit comments