summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZephiles <cae222@yahoo.com>2021-04-20 12:14:20 -0500
committerZephiles <cae222@yahoo.com>2021-04-20 12:14:20 -0500
commit2735bb35773600bb18647484d3640e3067c59ab6 (patch)
tree29f48c61d71e6bc6a73b4014568d54cc0a19c497
parentc165bb407d08b9afee4aa75146ad0f3ab58b2793 (diff)
Add unhookFunction, for undoing hookFunction
-rw-r--r--include/patch.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/include/patch.h b/include/patch.h
index 4dff3d3..e67447d 100644
--- a/include/patch.h
+++ b/include/patch.h
@@ -36,5 +36,40 @@ namespace libtp::patch
return reinterpret_cast<Func>( trampoline );
}
+
+ template<typename Func>
+ Func unhookFunction( Func trampoline )
+ {
+ uint32_t* instructions = reinterpret_cast<uint32_t*>( trampoline );
+
+ // Restore the original instruction
+ uint32_t instructionAddress = reinterpret_cast<uint32_t>( &instructions[1] );
+ uint32_t branchInstruction = instructions[1];
+ uint32_t branchLength;
+ uint32_t* address;
+
+ if ( branchInstruction >= 0x4A000000 )
+ {
+ // Branch is negative
+ branchLength = 0x4C000000 - branchInstruction;
+ address = reinterpret_cast<uint32_t*>( instructionAddress - branchLength - 0x4 );
+ }
+ else
+ {
+ // Branch is positive
+ branchLength = branchInstruction - 0x48000000;
+ address = reinterpret_cast<uint32_t*>( instructionAddress + branchLength - 0x4 );
+ }
+
+ *address = instructions[0];
+
+ // Clear the cache for both the address and where the instructions were stored
+ memory::clear_DC_IC_Cache( address, sizeof( uint32_t ) );
+ memory::clear_DC_IC_Cache( instructions, sizeof( uint32_t ) );
+
+ // Free the memory used by the trampoline
+ delete[] instructions;
+ return nullptr;
+ }
} // namespace libtp::patch
#endif \ No newline at end of file