#include size_t c_vfunc_hook::estimate_vftbl_length(std::uintptr_t* vftbl_start) { MEMORY_BASIC_INFORMATION memInfo = {}; int m_nSize = -1; do { m_nSize++; win_api::rebuilt::virtual_query(reinterpret_cast(vftbl_start[m_nSize]), &memInfo, sizeof(memInfo)); } while (memInfo.Protect == PAGE_EXECUTE_READ || memInfo.Protect == PAGE_EXECUTE_READWRITE); return m_nSize; } bool c_vfunc_hook::setup(void* base) { if (base != nullptr) class_base = base; if (class_base == nullptr) return false; old_vftbl = *static_cast(class_base); vftbl_len = estimate_vftbl_length(old_vftbl) * sizeof(std::uintptr_t); if (vftbl_len == 0) return false; new_vftb1 = stl::mem_alloc((sizeof(uintptr_t) * (vftbl_len + 1))); if (new_vftb1 == nullptr) return false; memcpy(&new_vftb1[1], old_vftbl, vftbl_len); auto guard = detail::protect_guard{ class_base, sizeof(std::uintptr_t), PAGE_READWRITE }; new_vftb1[0] = old_vftbl[-1]; *static_cast(class_base) = &new_vftb1[1]; wasAllocated = true; return true; }