Skip to content

Commit

Permalink
Do not store VmtLengthCalculator in VmtSwapper class
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Nov 21, 2023
1 parent c3598fe commit f2043de
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Source/Hooks/LoopModeGameHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class LoopModeGameHook : public RefCountedHook<LoopModeGameHook> {
public:
explicit LoopModeGameHook(const VmtLengthCalculator& vmtLengthCalculator) noexcept
: hook{ vmtLengthCalculator }
: vmtLengthCalculator{ vmtLengthCalculator }
{
}

Expand All @@ -28,14 +28,15 @@ class LoopModeGameHook : public RefCountedHook<LoopModeGameHook> {

void install() noexcept
{
if (loopModeGame && *loopModeGame && hook.install(*reinterpret_cast<std::uintptr_t**>(*loopModeGame))) {
if (loopModeGame && *loopModeGame && hook.install(vmtLengthCalculator, *reinterpret_cast<std::uintptr_t**>(*loopModeGame))) {
originalGetWorldSession = hook.hook(0, &getWorldSession);
}
}

friend class RefCountedHook;

cs2::CLoopModeGame** loopModeGame{ ClientPatterns::loopModeGame() };
VmtLengthCalculator vmtLengthCalculator;
VmtSwapper hook;
cs2::CLoopModeGame::getWorldSession originalGetWorldSession{ nullptr };
};
5 changes: 3 additions & 2 deletions Source/Hooks/ViewRenderHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ViewRenderHook : public RefCountedHook<ViewRenderHook> {
public:
explicit ViewRenderHook(const VmtLengthCalculator& vmtLengthCalculator) noexcept
: hook{ vmtLengthCalculator }
: vmtLengthCalculator{ vmtLengthCalculator }
{
}

Expand All @@ -33,14 +33,15 @@ class ViewRenderHook : public RefCountedHook<ViewRenderHook> {

void install() noexcept
{
if (viewRender && *viewRender && hook.install(*reinterpret_cast<std::uintptr_t**>(*viewRender))) {
if (viewRender && *viewRender && hook.install(vmtLengthCalculator, *reinterpret_cast<std::uintptr_t**>(*viewRender))) {
originalOnRenderStart = hook.hook(4, &onRenderStart);
}
}

friend class RefCountedHook;

cs2::CViewRender** viewRender{ ClientPatterns::viewRender() };
VmtLengthCalculator vmtLengthCalculator;
VmtSwapper hook;
cs2::CViewRender::OnRenderStart* originalOnRenderStart{ nullptr };
};
14 changes: 4 additions & 10 deletions Source/Vmt/VmtSwapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@

class VmtSwapper {
public:
explicit VmtSwapper(const VmtLengthCalculator& lengthCalculator) noexcept
: lengthCalculator{ lengthCalculator }
{
}

[[nodiscard]] bool isInstalled(const std::uintptr_t* vmt) const noexcept
{
return vmtCopy.has_value() && vmt == vmtCopy->getReplacementVmt();
}

bool install(std::uintptr_t*& vmt) noexcept
bool install(const VmtLengthCalculator& vmtLengthCalculator, std::uintptr_t*& vmt) noexcept
{
const auto justInitialized = initializeVmtCopy(vmt);
const auto justInitialized = initializeVmtCopy(vmtLengthCalculator, vmt);
vmt = vmtCopy->getReplacementVmt();
return justInitialized;
}
Expand All @@ -43,15 +38,14 @@ class VmtSwapper {
}

private:
[[nodiscard]] bool initializeVmtCopy(std::uintptr_t* vmt) noexcept
[[nodiscard]] bool initializeVmtCopy(const VmtLengthCalculator& vmtLengthCalculator, std::uintptr_t* vmt) noexcept
{
if (!vmtCopy.has_value()) {
vmtCopy.emplace(vmt, lengthCalculator(vmt));
vmtCopy.emplace(vmt, vmtLengthCalculator(vmt));
return true;
}
return false;
}

VmtLengthCalculator lengthCalculator;
std::optional<VmtCopy> vmtCopy;
};

0 comments on commit f2043de

Please sign in to comment.