**THIS CODE IS A SIMPLE HOOK FUNCTION AND SHOULD NOT BE USED FOR PASTE**
I admit it this code is pretty ugly but i did it in a few hours quite easy (This code is for Roblox)
I admit it this code is pretty ugly but i did it in a few hours quite easy (This code is for Roblox)
C++:
#define sizeCclosure(n) (offsetof(Closure, c.upvalue) + sizeof(TValue) * (n)) //thanks Roblox for the definition is always nice to reverse you!
std::uint32_t hookfunction(std::uintptr_t rL) {
if (lua_type(rL, 1) != lua::types::LUA_TFUNCTION || lua_type(rL, 2) != lua::types::LUA_TFUNCTION) return error(rL, "Error, both of the arguments must be functions!");
auto firstClosure = *reinterpret_cast<uintptr_t*>(rl_topointer(rL, 1)); //get the first function (Function that we want to be replaced)
auto secondClosure = *reinterpret_cast<uintptr_t*>(rl_topointer(rL, 2)); //get the second function (Function that we use for replace the first function)
auto firstClosureNups = *reinterpret_cast<std::uint8_t *>(firstClosure + closure_nups_off); //gets the first closure nups (you can find the cclosure_nups in luaf_newcclosure lol, easy to reverse)
auto secondClosureNups = *reinterpret_cast<std::uint8_t *>(secondClosure + closure_nups_off); //gets the second closure nups (you can find the cclosure_nups in luaf_newcclosure lol, easy to reverse)
void* newClosure = reinterpret_cast<void*>(malloc(sizeCclosure(firstClosureNups))); //allocate memory (this is the function that we will return from the hookfunction).
std::memcpy(newClosure, (void*)firstClosure, sizeCclosure(firstClosureNups)); //copy the firstClosure bytes and insert them in newClosure
std::memcpy(firstClosure, secondClosure, sizeCclosure(firstClosureNups));//replace the firstClosure with the secondClosure (basically the hook part is this one lol)
/* Push the newClosure and make the hookfunction returns it */
TValue* top = *reinterpret_cast<TValue**>(rL + top);
top->value.p = reinterpret_cast<void*>(newClosure);
top->tt = lua::types::LUA_TFUNCTION;
*reinterpret_cast<uintptr_t*>(rL + top) += 16; //Increase the top obv.
return 1;
}
Hope you enjoyed this thread and hope it was useful for you.
Last edited: