Skip to content

Commit c92619b

Browse files
committed
Correct CVTFMHook fucntion callback type
1 parent 674534e commit c92619b

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

include/dynlibutils/virtual.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class CVirtualTable
136136

137137
public: // Constructors.
138138
CVirtualTable() : m_pVTFs(nullptr) {}
139+
CVirtualTable(const CVirtualTable &) = default;
140+
CVirtualTable(CVirtualTable &&) = default;
139141
CVirtualTable(void* pClass) : m_pVTFs(*reinterpret_cast<void***>(pClass)) {} // Interprets the object’s first memory slot as a pointer to its vtable.
140142
CVirtualTable(CMemory pVTFs) : m_pVTFs(pVTFs.RCast<void**>()) {}
141143

include/dynlibutils/vthook.hpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ class CVTFMHook : public CVTMHook<R, T, Args...>
432432
{
433433
public:
434434
using Base_t = CVTMHook<R, T, Args...>;
435-
using Function_t = R (*)(T, Args...);
436-
using Callback_t = std::function<R (T, Args...)>;
435+
using Function_t = std::function<R (T, Args...)>;
436+
using Functions_t = std::vector<std::function<R (T, Args...)>>;
437437

438438
// AddHook (by index):
439439
// Installs or appends a callback for a given vtable index.
@@ -452,9 +452,20 @@ class CVTFMHook : public CVTMHook<R, T, Args...>
452452
// b) Retrieves `callbacks`, which is a std::vector<Function_t> stored in sm_vcallbacks.
453453
// c) Iterates through each callback in the vector and invokes it with (pClass, args...).
454454
template<auto METHOD>
455-
void AddHook(CVirtualTable pVTable, Callback_t funcCallback) { return AddHook(pVTable, GetVirtualIndex<METHOD>(), funcCallback); }
456-
void AddHook(CVirtualTable pVTable, std::ptrdiff_t nIndex, Callback_t funcCallback)
455+
void AddHook(CVirtualTable pVTable, Function_t funcCallback) { return AddHook(pVTable, GetVirtualIndex<METHOD>(), funcCallback); }
456+
void AddHook(CVirtualTable pVTable, std::ptrdiff_t nIndex, Function_t funcCallback)
457457
{
458+
auto found = sm_vcallbacks.find(pVTable);
459+
460+
if(found != sm_vcallbacks.cend())
461+
{
462+
found->second.push_back(std::move(funcCallback));
463+
}
464+
else
465+
{
466+
sm_vcallbacks.emplace(pVTable, Functions_t{std::move(funcCallback)});
467+
}
468+
458469
Base_t::AddHook(pVTable, nIndex,
459470
+[](T pClass, Args... args) -> R
460471
{
@@ -490,7 +501,7 @@ class CVTFMHook : public CVTMHook<R, T, Args...>
490501
}
491502

492503
protected:
493-
inline static std::map<CVirtualTable, std::vector<Function_t>> sm_vcallbacks;
504+
inline static std::map<CVirtualTable, Functions_t> sm_vcallbacks;
494505
}; // class CVTFHookSet<R, T, Args...>
495506

496507
// ========================================================================================

0 commit comments

Comments
 (0)