Skip to content

Commit

Permalink
Add VmtLengthCalculator class replacing Helpers::calculateVmtLength()…
Browse files Browse the repository at this point in the history
… function
  • Loading branch information
danielkrupinski committed Jul 11, 2023
1 parent 9540285 commit a250ea8
Show file tree
Hide file tree
Showing 26 changed files with 173 additions and 30 deletions.
14 changes: 0 additions & 14 deletions Source/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,6 @@ std::vector<char> Helpers::loadBinaryFile(const std::string& path) noexcept
return result;
}

std::size_t Helpers::calculateVmtLength(const std::uintptr_t* vmt) noexcept
{
std::size_t length = 0;
#if IS_WIN32()
MEMORY_BASIC_INFORMATION memoryInfo;
while (VirtualQuery(LPCVOID(vmt[length]), &memoryInfo, sizeof(memoryInfo)) && memoryInfo.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY))
++length;
#else
while (vmt[length])
++length;
#endif
return length;
}

static bool transformWorldPositionToScreenPosition(const csgo::Matrix4x4& matrix, const csgo::Vector& worldPosition, ImVec2& screenPosition) noexcept
{
const auto w = matrix._41 * worldPosition.x + matrix._42 * worldPosition.y + matrix._43 * worldPosition.z + matrix._44;
Expand Down
2 changes: 0 additions & 2 deletions Source/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ namespace Helpers
template <typename T> constexpr auto deg2rad(T degrees) noexcept { return degrees * (std::numbers::pi_v<T> / static_cast<T>(180)); }
template <typename T> constexpr auto rad2deg(T radians) noexcept { return radians * (static_cast<T>(180) / std::numbers::pi_v<T>); }

[[nodiscard]] std::size_t calculateVmtLength(const std::uintptr_t* vmt) noexcept;

constexpr auto isKnife(WeaponId id) noexcept
{
return (id >= WeaponId::Bayonet && id <= WeaponId::SkeletonKnife) || id == WeaponId::KnifeT || id == WeaponId::Knife;
Expand Down
9 changes: 0 additions & 9 deletions Source/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ void swapWindow(SDL_Window* window) noexcept;

#endif

#if IS_WIN32() || IS_WIN64()

Hooks::Hooks(HMODULE moduleHandle) noexcept
: windowProcedureHook{ FindWindowW(L"Valve001", nullptr) }, moduleHandle{ moduleHandle }
{
}

#endif

void Hooks::install(csgo::ClientPOD* clientInterface, const EngineInterfaces& engineInterfaces, const OtherInterfaces& interfaces, const Memory& memory) noexcept
{
#if IS_WIN32()
Expand Down
36 changes: 34 additions & 2 deletions Source/Hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,47 @@ int pollEvent(SDL_Event* event) noexcept;
class Hooks {
public:
#if IS_WIN32() || IS_WIN64()
Hooks(HMODULE moduleHandle) noexcept;
template <typename PlatformApi>
Hooks(HMODULE moduleHandle, DynamicLibrary<PlatformApi> clientDll, DynamicLibrary<PlatformApi> engineDll, DynamicLibrary<PlatformApi> vstdlibDll, DynamicLibrary<PlatformApi> vguiMatSurfaceDll) noexcept
: windowProcedureHook{ FindWindowW(L"Valve001", nullptr) }
, keyValuesSystemHooks{ vstdlibDll.getCodeSection() }
, engineHooks{ engineDll.getCodeSection() }
, clientHooks{ clientDll.getCodeSection() }
, clientModeHooks{ clientDll.getCodeSection() }
, clientStateHooks{ engineDll.getCodeSection() }
, playerInventoryHooks{ clientDll.getCodeSection() }
, panoramaMarshallHelperHooks{ clientDll.getCodeSection() }
, viewRenderHooks{ clientDll.getCodeSection() }
, inventoryManagerHooks{ clientDll.getCodeSection() }
, bspQueryHooks{ engineDll.getCodeSection() }
, engineSoundHooks{ engineDll.getCodeSection() }
, svCheatsHooks{ engineDll.getCodeSection() }
, modelRenderHooks{ engineDll.getCodeSection() }
, surfaceHooks{ vguiMatSurfaceDll.getCodeSection() }
, moduleHandle{ moduleHandle }
{
}

WindowProcedureHook windowProcedureHook;
std::add_pointer_t<HRESULT __stdcall(IDirect3DDevice9*, const RECT*, const RECT*, HWND, const RGNDATA*)> originalPresent;
std::add_pointer_t<HRESULT __stdcall(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*)> originalReset;
#elif IS_LINUX()
template <typename PlatformApi>
Hooks(PlatformApi) noexcept
Hooks(DynamicLibrary<PlatformApi> clientDll, DynamicLibrary<PlatformApi> engineDll, DynamicLibrary<PlatformApi> vstdlibDll, DynamicLibrary<PlatformApi> vguiMatSurfaceDll) noexcept
: sdlFunctions{ DynamicLibrary<PlatformApi>{ "libSDL2-2.0.so.0" } }
, engineHooks{ engineDll.getCodeSection() }
, clientHooks{ clientDll.getCodeSection() }
, clientModeHooks{ clientDll.getCodeSection() }
, clientStateHooks{ engineDll.getCodeSection() }
, playerInventoryHooks{ clientDll.getCodeSection() }
, panoramaMarshallHelperHooks{ clientDll.getCodeSection() }
, viewRenderHooks{ clientDll.getCodeSection() }
, inventoryManagerHooks{ clientDll.getCodeSection() }
, bspQueryHooks{ engineDll.getCodeSection() }
, engineSoundHooks{ engineDll.getCodeSection() }
, svCheatsHooks{ engineDll.getCodeSection() }
, modelRenderHooks{ engineDll.getCodeSection() }
, surfaceHooks{ vguiMatSurfaceDll.getCodeSection() }
{
pollEvent = *reinterpret_cast<decltype(pollEvent)*>(sdlFunctions.pollEvent);
*reinterpret_cast<decltype(::pollEvent)**>(sdlFunctions.pollEvent) = ::pollEvent;
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/BspQueryHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct Vector; }

class BspQueryHooks {
public:
explicit BspQueryHooks(std::span<const std::byte> engineCodeSection)
: hookImpl{ VmtLengthCalculator{ engineCodeSection } }
{
}

void install(void* engineSpatialQuery)
{
hookImpl.init(engineSpatialQuery);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/CSPlayerInventoryHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo
{
Expand All @@ -14,6 +15,11 @@ namespace csgo

class CSPlayerInventoryHooks {
public:
explicit CSPlayerInventoryHooks(std::span<const std::byte> clientCodeSection)
: hookImpl{ VmtLengthCalculator{ clientCodeSection } }
{
}

void install(csgo::CSPlayerInventoryPOD* inventory)
{
hookImpl.init(inventory);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/ClientHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo
{
Expand All @@ -14,6 +15,11 @@ namespace csgo

class ClientHooks {
public:
explicit ClientHooks(std::span<const std::byte> clientCodeSection)
: hookImpl{ VmtLengthCalculator{ clientCodeSection } }
{
}

void install(csgo::ClientPOD* client)
{
hookImpl.init(client);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/ClientModeHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo
{
Expand All @@ -14,6 +15,11 @@ namespace csgo

class ClientModeHooks {
public:
explicit ClientModeHooks(std::span<const std::byte> clientCodeSection)
: hookImpl{ VmtLengthCalculator{ clientCodeSection } }
{
}

void install(csgo::ClientMode* clientMode)
{
hookImpl.init(clientMode);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/ClientStateHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct ClientState; }

class ClientStateHooks {
public:
explicit ClientStateHooks(std::span<const std::byte> engineCodeSection)
: hookImpl{ VmtLengthCalculator{ engineCodeSection } }
{
}

void install(csgo::ClientState* clientState)
{
hookImpl.init(clientState);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/EngineHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
#include <HookType.h>
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct DemoPlaybackParameters; }
namespace csgo { struct EnginePOD; }

class EngineHooks {
public:
explicit EngineHooks(std::span<const std::byte> engineCodeSection)
: hookImpl{ VmtLengthCalculator{ engineCodeSection } }
{
}

void install(csgo::EnginePOD* engine)
{
hookImpl.init(engine);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/EngineSoundHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo
{
Expand All @@ -13,6 +14,11 @@ namespace csgo

class EngineSoundHooks {
public:
explicit EngineSoundHooks(std::span<const std::byte> engineCodeSection)
: hookImpl{ VmtLengthCalculator{ engineCodeSection } }
{
}

void install(csgo::EngineSoundPOD* engineSound)
{
hookImpl.init(engineSound);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/InventoryManagerHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <HookType.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo
{
Expand All @@ -13,6 +14,11 @@ namespace csgo

class InventoryManagerHooks {
public:
explicit InventoryManagerHooks(std::span<const std::byte> clientCodeSection)
: hookImpl{ VmtLengthCalculator{ clientCodeSection } }
{
}

void install(csgo::InventoryManagerPOD* inventoryManager)
{
hookImpl.init(inventoryManager);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/KeyValuesSystemHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct KeyValuesSystemPOD; }

class KeyValuesSystemHooks {
public:
explicit KeyValuesSystemHooks(std::span<const std::byte> vstdlibCodeSection)
: hookImpl{ VmtLengthCalculator{ vstdlibCodeSection } }
{
}

void install(csgo::KeyValuesSystemPOD* keyValuesSystem)
{
hookImpl.init(keyValuesSystem);
Expand Down
5 changes: 5 additions & 0 deletions Source/Hooks/MinHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@

#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/IsPlatform.h>
#include <Vmt/VmtLengthCalculator.h>

class MinHook {
public:
explicit MinHook(VmtLengthCalculator)
{
}

void init(void* base) noexcept;
void restore() noexcept {}
std::uintptr_t hookAt(std::size_t index, void* fun) noexcept;
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/ModelRenderHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo
{
Expand All @@ -14,6 +15,11 @@ namespace csgo

class ModelRenderHooks {
public:
explicit ModelRenderHooks(std::span<const std::byte> engineCodeSection)
: hookImpl{ VmtLengthCalculator{ engineCodeSection } }
{
}

void install(csgo::ModelRenderPOD* modelRender)
{
hookImpl.init(modelRender);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/PanoramaMarshallHelperHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct PanoramaMarshallHelperPOD; }

class PanoramaMarshallHelperHooks {
public:
explicit PanoramaMarshallHelperHooks(std::span<const std::byte> clientCodeSection)
: hookImpl{ VmtLengthCalculator{ clientCodeSection } }
{
}

void install(csgo::PanoramaMarshallHelperPOD* panoramaMarshallHelper)
{
hookImpl.init(panoramaMarshallHelper);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/SurfaceHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
#include <Platform/Macros/IsPlatform.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct SurfacePOD; }

class SurfaceHooks {
public:
explicit SurfaceHooks(std::span<const std::byte> vguiMatSurfaceCodeSection)
: hookImpl{ VmtLengthCalculator{ vguiMatSurfaceCodeSection } }
{
}

void install(csgo::SurfacePOD* surface)
{
hookImpl.init(surface);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/SvCheatsHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct ConVarPOD; }

class SvCheatsHooks {
public:
explicit SvCheatsHooks(std::span<const std::byte> engineCodeSection)
: hookImpl{ VmtLengthCalculator{ engineCodeSection } }
{
}

void install(csgo::ConVarPOD* svCheats)
{
hookImpl.init(svCheats);
Expand Down
6 changes: 6 additions & 0 deletions Source/Hooks/ViewRenderHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
#include <Platform/Macros/CallingConventions.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <RetSpoof/FunctionInvoker.h>
#include <Vmt/VmtLengthCalculator.h>

namespace csgo { struct ViewRender; }

class ViewRenderHooks {
public:
explicit ViewRenderHooks(std::span<const std::byte> clientCodeSection)
: hookImpl{ VmtLengthCalculator{ clientCodeSection } }
{
}

void install(csgo::ViewRender* viewRender)
{
hookImpl.init(viewRender);
Expand Down
2 changes: 1 addition & 1 deletion Source/Hooks/VmtSwap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void VmtSwap::init(void* base) noexcept
{
this->base = base;
oldVmt = *reinterpret_cast<std::uintptr_t**>(base);
std::size_t length = Helpers::calculateVmtLength(oldVmt) + dynamicCastInfoLength;
length = static_cast<std::size_t>(vmtLengthCalculator(oldVmt)) + dynamicCastInfoLength;
newVmt = std::make_unique<std::uintptr_t[]>(length);
std::copy(oldVmt - dynamicCastInfoLength, oldVmt - dynamicCastInfoLength + length, newVmt.get());
*reinterpret_cast<std::uintptr_t**>(base) = newVmt.get() + dynamicCastInfoLength;
Expand Down
Loading

0 comments on commit a250ea8

Please sign in to comment.