From 1d4763bc94c9887bc4171374202a5d12be1fc10f Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Fri, 26 Oct 2018 21:43:17 -0700 Subject: Way better caching. --- WitnessRandomizer/Memory.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'WitnessRandomizer/Memory.cpp') diff --git a/WitnessRandomizer/Memory.cpp b/WitnessRandomizer/Memory.cpp index 92a5136..6077691 100644 --- a/WitnessRandomizer/Memory.cpp +++ b/WitnessRandomizer/Memory.cpp @@ -62,18 +62,21 @@ uintptr_t Memory::ComputeOffset(std::vector offsets) int final_offset = offsets.back(); offsets.pop_back(); - auto search = _computedOffsets.find(offsets); - if (search != std::end(_computedOffsets)) { - return search->second + final_offset; - } - uintptr_t cumulativeAddress = _baseAddress; for (int offset : offsets) { cumulativeAddress += offset; - if (!ReadProcessMemory(_handle, (LPVOID)cumulativeAddress, &cumulativeAddress, sizeof(uintptr_t), NULL)) { - ThrowError(); + + auto search = _computedAddresses.find(cumulativeAddress); + if (search == std::end(_computedAddresses)) { + // If the address is not yet computed, then compute it. + uintptr_t computedAddress = 0; + if (!ReadProcessMemory(_handle, (LPVOID)cumulativeAddress, &computedAddress, sizeof(uintptr_t), NULL)) { + ThrowError(); + } + _computedAddresses[cumulativeAddress] = computedAddress; } + + cumulativeAddress = _computedAddresses[cumulativeAddress]; } - _computedOffsets[offsets] = cumulativeAddress; return cumulativeAddress + final_offset; } -- cgit 1.4.1