VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
MemoryAccess.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6namespace VoltMod
7{
8
9/**
10 * @file MemoryAccess.hpp
11 * @brief Typed read/write of engine fields at a base pointer + byte offset.
12 *
13 * Engine objects have no nameable C++ type, so fields are reached by offset.
14 * These helpers keep that one `reinterpret_cast` in a single audited place; the
15 * caller owns @p base and @p offset. Note this aliases live storage - not a
16 * `std::bit_cast`, which copies bits into a new object.
17 */
18
19/** Pointer to the field of type T located @p offset bytes into @p base. */
20template <typename T>
21T* MemberPtr(void* base, std::ptrdiff_t offset) noexcept
22{
23 return reinterpret_cast<T*>(static_cast<uint8_t*>(base) + offset);
24}
25
26/** Read the value of type T located @p offset bytes into @p base. */
27template <typename T>
28T ReadAt(void* base, std::ptrdiff_t offset) noexcept
29{
30 return *MemberPtr<T>(base, offset);
31}
32
33/** Write @p value to the field of type T located @p offset bytes into @p base. */
34template <typename T>
35void WriteAt(void* base, std::ptrdiff_t offset, const T& value) noexcept
36{
37 *MemberPtr<T>(base, offset) = value;
38}
39
40} // namespace VoltMod
T * MemberPtr(void *base, std::ptrdiff_t offset) noexcept
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
void WriteAt(void *base, std::ptrdiff_t offset, const T &value) noexcept
T ReadAt(void *base, std::ptrdiff_t offset) noexcept