2022-10-30 19:14:19 +01:00
|
|
|
#include "byte_patch.hpp"
|
|
|
|
|
|
|
|
namespace memory
|
|
|
|
{
|
2023-03-01 21:27:15 +00:00
|
|
|
byte_patch::~byte_patch()
|
|
|
|
{
|
|
|
|
restore();
|
|
|
|
}
|
2022-10-30 19:14:19 +01:00
|
|
|
|
2023-03-01 21:27:15 +00:00
|
|
|
void byte_patch::apply() const
|
|
|
|
{
|
2023-07-24 16:13:09 +02:00
|
|
|
DWORD temp;
|
|
|
|
|
2023-07-15 20:37:20 +00:00
|
|
|
VirtualProtect(m_address, m_size, PAGE_EXECUTE_READWRITE, (PDWORD)&m_old_protect);
|
2023-03-01 21:27:15 +00:00
|
|
|
memcpy(m_address, m_value.get(), m_size);
|
2023-07-24 16:13:09 +02:00
|
|
|
VirtualProtect(m_address, m_size, m_old_protect, &temp);
|
2023-03-01 21:27:15 +00:00
|
|
|
}
|
2022-10-30 19:14:19 +01:00
|
|
|
|
2023-03-01 21:27:15 +00:00
|
|
|
void byte_patch::restore() const
|
|
|
|
{
|
2023-07-15 20:37:20 +00:00
|
|
|
DWORD temp;
|
2023-07-24 16:13:09 +02:00
|
|
|
|
|
|
|
VirtualProtect(m_address, m_size, PAGE_EXECUTE_READWRITE, (PDWORD)&temp);
|
2023-03-01 21:27:15 +00:00
|
|
|
memcpy(m_address, m_original_bytes.get(), m_size);
|
2023-07-24 16:13:09 +02:00
|
|
|
VirtualProtect(m_address, m_size, m_old_protect, &temp);
|
2023-03-01 21:27:15 +00:00
|
|
|
}
|
2022-10-30 19:14:19 +01:00
|
|
|
|
2023-03-01 21:27:15 +00:00
|
|
|
void byte_patch::remove() const
|
|
|
|
{
|
2023-06-06 07:40:40 +00:00
|
|
|
std::erase_if(m_patches, [this](auto& el) {
|
|
|
|
return el.get() == this;
|
|
|
|
});
|
2023-03-01 21:27:15 +00:00
|
|
|
}
|
2022-10-30 19:14:19 +01:00
|
|
|
|
2023-03-01 21:27:15 +00:00
|
|
|
void byte_patch::restore_all()
|
|
|
|
{
|
|
|
|
m_patches.clear();
|
|
|
|
}
|
2022-10-30 19:14:19 +01:00
|
|
|
|
2023-03-01 21:27:15 +00:00
|
|
|
bool operator==(const std::unique_ptr<byte_patch>& a, const byte_patch* b)
|
|
|
|
{
|
|
|
|
return a->m_address == b->m_address;
|
|
|
|
}
|
2022-10-30 19:14:19 +01:00
|
|
|
}
|