feat(BytePatch): added std::array/vector support (#626)
Co-authored-by: xiaoxiao921 <xiaoxiao921@hotmail.fr>
This commit is contained in:
parent
ab49103171
commit
abad615531
@ -1,7 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <span>
|
||||||
|
|
||||||
namespace memory
|
namespace memory
|
||||||
{
|
{
|
||||||
|
template<typename T>
|
||||||
|
concept SpanCompatibleType = requires(T a)
|
||||||
|
{
|
||||||
|
std::span{ a };
|
||||||
|
};
|
||||||
|
|
||||||
class byte_patch
|
class byte_patch
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -20,6 +27,13 @@ namespace memory
|
|||||||
std::unique_ptr<byte_patch>(new byte_patch(address, value)));
|
std::unique_ptr<byte_patch>(new byte_patch(address, value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename TAddr, typename T> requires SpanCompatibleType<T>
|
||||||
|
static const std::unique_ptr<byte_patch>& make(TAddr address, T span_compatible)
|
||||||
|
{
|
||||||
|
return m_patches.emplace_back(
|
||||||
|
std::unique_ptr<byte_patch>(new byte_patch(address, std::span{ span_compatible })));
|
||||||
|
}
|
||||||
|
|
||||||
static void restore_all();
|
static void restore_all();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -28,20 +42,34 @@ namespace memory
|
|||||||
: m_address(address)
|
: m_address(address)
|
||||||
{
|
{
|
||||||
m_size = sizeof(std::remove_pointer_t<std::remove_reference_t<TAddr>>);
|
m_size = sizeof(std::remove_pointer_t<std::remove_reference_t<TAddr>>);
|
||||||
m_original_bytes = std::make_unique<uint8_t[]>(m_size);
|
|
||||||
m_value = std::make_unique<uint8_t[]>(m_size);
|
|
||||||
|
|
||||||
|
m_original_bytes = std::make_unique<byte[]>(m_size);
|
||||||
memcpy(m_original_bytes.get(), m_address, m_size);
|
memcpy(m_original_bytes.get(), m_address, m_size);
|
||||||
|
|
||||||
|
m_value = std::make_unique<byte[]>(m_size);
|
||||||
memcpy(m_value.get(), &value, m_size);
|
memcpy(m_value.get(), &value, m_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename TAddr, typename T, std::size_t N>
|
||||||
|
byte_patch(TAddr address, std::span<T, N> span)
|
||||||
|
: m_address((void*)address)
|
||||||
|
{
|
||||||
|
m_size = span.size_bytes();
|
||||||
|
|
||||||
|
m_original_bytes = std::make_unique<byte[]>(m_size);
|
||||||
|
memcpy(m_original_bytes.get(), m_address, m_size);
|
||||||
|
|
||||||
|
m_value = std::make_unique<byte[]>(m_size);
|
||||||
|
memcpy(m_value.get(), span.data(), m_size);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static inline std::vector<std::unique_ptr<byte_patch>> m_patches;
|
static inline std::vector<std::unique_ptr<byte_patch>> m_patches;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void* m_address;
|
void* m_address;
|
||||||
std::unique_ptr<uint8_t[]> m_value;
|
std::unique_ptr<byte[]> m_value;
|
||||||
std::unique_ptr<uint8_t[]> m_original_bytes;
|
std::unique_ptr<byte[]> m_original_bytes;
|
||||||
std::size_t m_size;
|
std::size_t m_size;
|
||||||
|
|
||||||
friend bool operator== (const std::unique_ptr<byte_patch>& a, const byte_patch* b);
|
friend bool operator== (const std::unique_ptr<byte_patch>& a, const byte_patch* b);
|
||||||
|
Reference in New Issue
Block a user