diff --git a/src/memory/byte_patch.hpp b/src/memory/byte_patch.hpp index 7bcad2e4..f1bcb49f 100644 --- a/src/memory/byte_patch.hpp +++ b/src/memory/byte_patch.hpp @@ -1,7 +1,14 @@ #pragma once +#include namespace memory { + template + concept SpanCompatibleType = requires(T a) + { + std::span{ a }; + }; + class byte_patch { public: @@ -20,6 +27,13 @@ namespace memory std::unique_ptr(new byte_patch(address, value))); } + template requires SpanCompatibleType + static const std::unique_ptr& make(TAddr address, T span_compatible) + { + return m_patches.emplace_back( + std::unique_ptr(new byte_patch(address, std::span{ span_compatible }))); + } + static void restore_all(); private: @@ -28,20 +42,34 @@ namespace memory : m_address(address) { m_size = sizeof(std::remove_pointer_t>); - m_original_bytes = std::make_unique(m_size); - m_value = std::make_unique(m_size); + m_original_bytes = std::make_unique(m_size); memcpy(m_original_bytes.get(), m_address, m_size); + + m_value = std::make_unique(m_size); memcpy(m_value.get(), &value, m_size); } + template + byte_patch(TAddr address, std::span span) + : m_address((void*)address) + { + m_size = span.size_bytes(); + + m_original_bytes = std::make_unique(m_size); + memcpy(m_original_bytes.get(), m_address, m_size); + + m_value = std::make_unique(m_size); + memcpy(m_value.get(), span.data(), m_size); + } + protected: static inline std::vector> m_patches; private: void* m_address; - std::unique_ptr m_value; - std::unique_ptr m_original_bytes; + std::unique_ptr m_value; + std::unique_ptr m_original_bytes; std::size_t m_size; friend bool operator== (const std::unique_ptr& a, const byte_patch* b);