TmpMenu/src/memory/byte_patch.cpp
2023-07-15 22:37:20 +02:00

40 lines
731 B
C++

#include "byte_patch.hpp"
namespace memory
{
byte_patch::~byte_patch()
{
restore();
}
void byte_patch::apply() const
{
VirtualProtect(m_address, m_size, PAGE_EXECUTE_READWRITE, (PDWORD)&m_old_protect);
memcpy(m_address, m_value.get(), m_size);
}
void byte_patch::restore() const
{
DWORD temp;
VirtualProtect(m_address, m_size, m_old_protect, &temp);
memcpy(m_address, m_original_bytes.get(), m_size);
}
void byte_patch::remove() const
{
std::erase_if(m_patches, [this](auto& el) {
return el.get() == this;
});
}
void byte_patch::restore_all()
{
m_patches.clear();
}
bool operator==(const std::unique_ptr<byte_patch>& a, const byte_patch* b)
{
return a->m_address == b->m_address;
}
}