Skip to content

Commit

Permalink
byte patch manager (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rxann authored Mar 21, 2024
1 parent 41b81ed commit d28e2d9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 60 deletions.
25 changes: 25 additions & 0 deletions src/core/byte_patch_manager/byte_patch_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "byte_patch_manager.hpp"

namespace YimMenu
{
void Byte_Patch_Manager::InitImpl()
{
// put patches here
// example usage:
//
//BytePatch::Make(Pointers.GetLocalPed,0).get()

LOG(INFO) << "Byte patch manager initialized";
}

void Byte_Patch_Manager::Init()
{
GetInstance().InitImpl();
}

Byte_Patch_Manager::~Byte_Patch_Manager()
{
BytePatch::RestoreAll();
}

}
22 changes: 22 additions & 0 deletions src/core/byte_patch_manager/byte_patch_manager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "core/memory/BytePatch.hpp"

namespace YimMenu
{
class Byte_Patch_Manager
{
public:
~Byte_Patch_Manager();

static void Init();

private:
static Byte_Patch_Manager& GetInstance()
{
static Byte_Patch_Manager i{};
return i;
}

void InitImpl();
};
}
116 changes: 56 additions & 60 deletions src/core/memory/BytePatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,45 @@

namespace YimMenu
{
template<typename T>
concept SpanCompatibleType = requires(T a)
{
std::span{a};
};
template<typename T>
concept SpanCompatibleType = requires(T a) { std::span{a}; };

class BytePatch
{
private:
std::byte* m_Address;
std::unique_ptr<std::byte[]> m_Patch;
std::unique_ptr<std::byte[]> m_Original;
std::size_t m_Size;

protected:
static inline std::vector<std::unique_ptr<BytePatch>> m_Patches;
class BytePatch
{
private:
std::byte* m_Address;
std::unique_ptr<std::byte[]> m_Patch;
std::unique_ptr<std::byte[]> m_Original;
std::size_t m_Size;

public:
virtual ~BytePatch();
protected:
static inline std::vector<std::unique_ptr<BytePatch>> m_Patches;

void Apply() const;
void Restore() const;
void Remove() const;
public:
virtual ~BytePatch();

static void RestoreAll();
void Apply() const;
void Restore() const;
void Remove() const;

template<typename TAddr>
static const std::unique_ptr<BytePatch>& Make(TAddr addr, std::remove_pointer_t<std::remove_reference_t<TAddr>> value);
static void RestoreAll();

template<typename TAddr, typename T>
requires SpanCompatibleType<T>
static const std::unique_ptr<BytePatch>& Make(TAddr addr, T spanCompatible);
template<typename TAddr>
static const std::unique_ptr<BytePatch>& Make(TAddr addr, std::remove_pointer_t<std::remove_reference_t<TAddr>> value);

private:
template<typename TAddr>
BytePatch(TAddr addr, std::remove_pointer_t<std::remove_reference_t<TAddr>> value);
template<typename TAddr, typename T>
requires SpanCompatibleType<T>
static const std::unique_ptr<BytePatch>& Make(TAddr addr, T spanCompatible);

template<typename TAddr, typename T, std::size_t N>
BytePatch(TAddr addr, std::span<T, N> span);
private:
template<typename TAddr>
BytePatch(TAddr addr, std::remove_pointer_t<std::remove_reference_t<TAddr>> value);

friend bool operator==(const std::unique_ptr<BytePatch>& a, const BytePatch* b);
template<typename TAddr, typename T, std::size_t N>
BytePatch(TAddr addr, std::span<T, N> span);

};
friend bool operator==(const std::unique_ptr<BytePatch>& a, const BytePatch* b);
};

template<typename TAddr>
inline const std::unique_ptr<BytePatch>& BytePatch::Make(TAddr addr, std::remove_pointer_t<std::remove_reference_t<TAddr>> value)
Expand All @@ -56,35 +52,35 @@ namespace YimMenu
}

template<typename TAddr, typename T>
requires SpanCompatibleType<T>
requires SpanCompatibleType<T>
inline const std::unique_ptr<BytePatch>& BytePatch::Make(TAddr addr, T spanCompatible)
{
return m_Patches.emplace_back(std::unique_ptr<BytePatch>(new BytePatch(addr, std::span{spanCompatible})));
}
template<typename TAddr>
inline BytePatch::BytePatch(TAddr addr, std::remove_pointer_t<std::remove_reference_t<TAddr>> value) :
m_Address(addr)
{
m_Size = sizeof(std::remove_pointer_t<std::remove_reference_t<TAddr>>);

m_Original = std::make_unique<std::byte[]>(m_Size);
std::copy_n(m_Address, m_Size, m_Original.get());

m_Patch = std::make_unique<std::byte[]>(m_Size);
std::copy_n(&value, m_Size, m_Patch.get());
}

template<typename TAddr, typename T, std::size_t N>
inline BytePatch::BytePatch(TAddr addr, std::span<T, N> span) :
m_Address((std::byte*)addr)
{
m_Size = span.size();

m_Original = std::make_unique<std::byte[]>(m_Size);
std::copy_n(m_Address, m_Size, m_Original.get());

m_Patch = std::make_unique<std::byte[]>(m_Size);
std::copy(span.begin(), span.end(), m_Patch.get());
}

template<typename TAddr>
inline BytePatch::BytePatch(TAddr addr, std::remove_pointer_t<std::remove_reference_t<TAddr>> value) :
m_Address(addr)
{
m_Size = sizeof(std::remove_pointer_t<std::remove_reference_t<TAddr>>);

m_Original = std::make_unique<std::byte[]>(m_Size);
std::copy_n(m_Address, m_Size, m_Original.get());

m_Patch = std::make_unique<std::byte[]>(m_Size);
std::copy_n(&value, m_Size, m_Patch.get());
}

template<typename TAddr, typename T, std::size_t N>
inline BytePatch::BytePatch(TAddr addr, std::span<T, N> span) :
m_Address((std::byte*)addr)
{
m_Size = span.size();

m_Original = std::make_unique<std::byte[]>(m_Size);
std::copy_n(m_Address, m_Size, m_Original.get());

m_Patch = std::make_unique<std::byte[]>(m_Size);
std::copy(span.begin(), span.end(), m_Patch.get());
}
}
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common.hpp"
#include "core/byte_patch_manager/byte_patch_manager.hpp"
#include "core/commands/HotkeySystem.hpp"
#include "core/filemgr/FileMgr.hpp"
#include "core/frontend/Notifications.hpp"
Expand Down Expand Up @@ -34,6 +35,8 @@ namespace YimMenu
if (!Renderer::Init())
goto unload;

Byte_Patch_Manager::Init();

Hooking::Init();

ScriptMgr::Init();
Expand Down

0 comments on commit d28e2d9

Please sign in to comment.