diff --git a/src/game_api/bucket.hpp b/src/game_api/bucket.hpp index f3cdf37a3..697a745ff 100644 --- a/src/game_api/bucket.hpp +++ b/src/game_api/bucket.hpp @@ -9,6 +9,20 @@ using BucketItem = std::variant; using KEY = int64_t; +struct RecoverableMemory +{ + size_t address; + char* old_data; + size_t size; + bool prot_used; +}; + +struct EditedMemory +{ + std::vector mem; + bool dirty; +}; + struct Overlunky { /// Current Overlunky options. Read only. @@ -48,4 +62,6 @@ class Bucket bool patches_applied{false}; // Used by Overlunky to restart adventure runs with same seed, updated by PRE_LOAD_SCREEN std::pair adventure_seed{0, 0}; + // Used by memory for recoverable memory interoperability + std::unordered_map original_memory; }; diff --git a/src/game_api/memory.cpp b/src/game_api/memory.cpp index 960f0b4bb..af6ba062f 100644 --- a/src/game_api/memory.cpp +++ b/src/game_api/memory.cpp @@ -8,6 +8,8 @@ #include // for min, max #include // for vector, _Vector_iterator, _Vector_const_ite... +#include "bucket.hpp" + ExecutableMemory::ExecutableMemory(std::string_view raw_code) { SYSTEM_INFO system_info; @@ -102,18 +104,16 @@ LPVOID alloc_mem_rel32(size_t addr, size_t size) return new_array; } -std::unordered_map original_memory; - void write_mem_recoverable(std::string name, size_t addr, std::string_view payload, bool prot) { - auto map_it = original_memory.find(name); - if (map_it == original_memory.end()) + auto map_it = Bucket::get()->original_memory.find(name); + if (map_it == Bucket::get()->original_memory.end()) { char* old_data = (char*)VirtualAlloc(0, payload.size(), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (old_data) { memcpy(old_data, (char*)addr, payload.size()); - original_memory.emplace(name, EditedMemory{{{addr, old_data, payload.size(), prot}}, true}); + Bucket::get()->original_memory.emplace(name, EditedMemory{{{addr, old_data, payload.size(), prot}}, true}); } } else @@ -137,22 +137,22 @@ void write_mem_recoverable(std::string name, size_t addr, std::string_view paylo } } } - original_memory[name].dirty = true; + Bucket::get()->original_memory[name].dirty = true; write_mem_prot(addr, payload, prot); } void recover_mem(std::string name, size_t addr) { - if (original_memory.contains(name)) + if (Bucket::get()->original_memory.contains(name)) { size_t fixed = 0; - for (auto& it : original_memory[name].mem) + for (auto& it : Bucket::get()->original_memory[name].mem) { if (!addr || addr == it.address) { write_mem_prot(it.address, std::string_view{it.old_data, it.size}, it.prot_used); - if (++fixed == original_memory[name].mem.size()) - original_memory[name].dirty = false; + if (++fixed == Bucket::get()->original_memory[name].mem.size()) + Bucket::get()->original_memory[name].dirty = false; } } } @@ -160,7 +160,7 @@ void recover_mem(std::string name, size_t addr) bool mem_written(std::string name) { - return original_memory.contains(name) && original_memory[name].dirty; + return Bucket::get()->original_memory.contains(name) && Bucket::get()->original_memory[name].dirty; } size_t patch_and_redirect(size_t addr, size_t replace_size, const std::string_view payload, bool just_nop, size_t return_to_addr, bool game_code_first) diff --git a/src/game_api/memory.hpp b/src/game_api/memory.hpp index b0ccf6f37..e9ed97986 100644 --- a/src/game_api/memory.hpp +++ b/src/game_api/memory.hpp @@ -81,20 +81,6 @@ struct Memory } }; -struct RecoverableMemory -{ - size_t address; - char* old_data; - size_t size; - bool prot_used; -}; - -struct EditedMemory -{ - std::vector mem; - bool dirty; -}; - LPVOID alloc_mem_rel32(size_t addr, size_t size); void write_mem_prot(size_t addr, std::string_view payload, bool prot); void write_mem_prot(size_t addr, std::string payload, bool prot);