Only try to restore a function if it's actually patched

This commit is contained in:
Maschell 2023-01-06 14:27:12 +01:00
parent 5adf6c3688
commit ecc109af50

View File

@ -60,6 +60,10 @@ FunctionPatcherStatus FPRemoveFunctionPatch(PatchedFunctionHandle handle) {
if (cur->getHandle() == handle) { if (cur->getHandle() == handle) {
toRemoved = cur; toRemoved = cur;
found = true; found = true;
if (!cur->isPatched) {
// Early return if the function is not patched.
break;
}
continue; continue;
} }
// Check if something else patched the same function afterwards. // Check if something else patched the same function afterwards.
@ -76,6 +80,7 @@ FunctionPatcherStatus FPRemoveFunctionPatch(PatchedFunctionHandle handle) {
return FUNCTION_PATCHER_RESULT_PATCH_NOT_FOUND; return FUNCTION_PATCHER_RESULT_PATCH_NOT_FOUND;
} }
if (toRemoved->isPatched) {
// Restore function patches that were done after the patch we actually want to restore. // Restore function patches that were done after the patch we actually want to restore.
for (auto &cur : std::ranges::reverse_view(toBeTempRestored)) { for (auto &cur : std::ranges::reverse_view(toBeTempRestored)) {
RestoreFunction(cur); RestoreFunction(cur);
@ -83,13 +88,16 @@ FunctionPatcherStatus FPRemoveFunctionPatch(PatchedFunctionHandle handle) {
// Restore the function we actually want to restore // Restore the function we actually want to restore
RestoreFunction(toRemoved); RestoreFunction(toRemoved);
}
gPatchedFunctions.erase(gPatchedFunctions.begin() + erasePosition); gPatchedFunctions.erase(gPatchedFunctions.begin() + erasePosition);
if (toRemoved->isPatched) {
// Apply the other patches again // Apply the other patches again
for (auto &cur : toBeTempRestored) { for (auto &cur : toBeTempRestored) {
PatchFunction(cur); PatchFunction(cur);
} }
}
OSMemoryBarrier(); OSMemoryBarrier();
return FUNCTION_PATCHER_RESULT_SUCCESS; return FUNCTION_PATCHER_RESULT_SUCCESS;