Skip to content

Commit

Permalink
make it better
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Nov 13, 2023
1 parent 0236cde commit 3254446
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/game_api/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ LPVOID alloc_mem_rel32(size_t addr, size_t size)

void write_mem_recoverable(std::string name, size_t addr, std::string_view payload, bool prot)
{
auto map_it = Bucket::get()->original_memory.find(name);
if (map_it == Bucket::get()->original_memory.end())
static const auto bucket = Bucket::get();
auto map_it = bucket->original_memory.find(name);
if (map_it == bucket->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());
Bucket::get()->original_memory.emplace(name, EditedMemory{{{addr, old_data, payload.size(), prot}}, true});
bucket->original_memory.emplace(name, EditedMemory{{{addr, old_data, payload.size(), prot}}, true});
}
}
else
Expand All @@ -137,30 +138,32 @@ void write_mem_recoverable(std::string name, size_t addr, std::string_view paylo
}
}
}
Bucket::get()->original_memory[name].dirty = true;
bucket->original_memory[name].dirty = true;
write_mem_prot(addr, payload, prot);
}

void recover_mem(std::string name, size_t addr)
{
if (Bucket::get()->original_memory.contains(name))
static const auto bucket = Bucket::get();
if (bucket->original_memory.contains(name))
{
size_t fixed = 0;
for (auto& it : Bucket::get()->original_memory[name].mem)
for (auto& it : bucket->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 == Bucket::get()->original_memory[name].mem.size())
Bucket::get()->original_memory[name].dirty = false;
if (++fixed == bucket->original_memory[name].mem.size())
bucket->original_memory[name].dirty = false;
}
}
}
}

bool mem_written(std::string name)
{
return Bucket::get()->original_memory.contains(name) && Bucket::get()->original_memory[name].dirty;
static const auto bucket = Bucket::get();
return bucket->original_memory.contains(name) && bucket->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)
Expand Down

0 comments on commit 3254446

Please sign in to comment.