TmpMenu/src/memory/byte_patch.cpp
Andreas Maerten 8b415d5186 fix(byte_patch): sometimes the page protections get modified between apply and restore: make sure the protections are fine for us to memcpy. (#1831)
* feat(BytePatch): add is_active member function
* fix(byte_patch): sometimes the page protections get modified between the apply and restore of the byte_patch, make sure the protections are fine for us to memcpy

Co-authored-by: Quentin E. / iDeath <xiaoxiao921@hotmail.fr>
2023-07-24 16:13:09 +02:00

45 lines
882 B
C++

#include "byte_patch.hpp"
namespace memory
{
byte_patch::~byte_patch()
{
restore();
}
void byte_patch::apply() const
{
DWORD temp;
VirtualProtect(m_address, m_size, PAGE_EXECUTE_READWRITE, (PDWORD)&m_old_protect);
memcpy(m_address, m_value.get(), m_size);
VirtualProtect(m_address, m_size, m_old_protect, &temp);
}
void byte_patch::restore() const
{
DWORD temp;
VirtualProtect(m_address, m_size, PAGE_EXECUTE_READWRITE, (PDWORD)&temp);
memcpy(m_address, m_original_bytes.get(), m_size);
VirtualProtect(m_address, m_size, m_old_protect, &temp);
}
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;
}
}