commit 1f956817fa189cf3389cf22e4a2be8386ab3f22f Author: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun Nov 6 02:12:24 2016 +0200 Initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f39a31e --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + g++ luaspy.cpp -shared -o gmcl_luaspy_win32.dll -I D:\gmbase\lua -lpsapi \ No newline at end of file diff --git a/gmcl_luaspy_win32.dll b/gmcl_luaspy_win32.dll new file mode 100644 index 0000000..31b3bce Binary files /dev/null and b/gmcl_luaspy_win32.dll differ diff --git a/luaspy.cpp b/luaspy.cpp new file mode 100644 index 0000000..b05fca3 --- /dev/null +++ b/luaspy.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include "GarrysMod/Lua/Interface.h" +#include "GarrysMod/Lua/LuaBase.h" + +using namespace GarrysMod; +using namespace GarrysMod::Lua; + +typedef struct +{ + HMODULE hMod; + MODULEINFO iMod; + uintptr_t ptr; + uintptr_t offset; +} DLLFunction; + +uintptr_t VPhysicalAddress(MODULEINFO iMod,uintptr_t func) +{ + uintptr_t base = (uintptr_t)iMod.lpBaseOfDll; + return (uintptr_t)(func - base); +} + +bool VFindModule(DLLFunction* func) +{ + HMODULE hMods[1024]; + DWORD cbNeeded; + + if( EnumProcessModules(GetCurrentProcess(), hMods, sizeof(hMods), &cbNeeded)) + { + for ( int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ ) + { + GetModuleInformation(GetCurrentProcess(),hMods[i],&func->iMod, sizeof(MODULEINFO)); + //Check if function in module space + if( (func->offset = VPhysicalAddress(func->iMod,func->ptr) ) <= func->iMod.SizeOfImage && func->ptr >= (uintptr_t)func->iMod.lpBaseOfDll) + { + func->hMod = hMods[i]; + return true; + } + } + } + return false; +} + +int FuncPtr(lua_State* state) +{ + LUA->CheckType(1,Type::FUNCTION); + + DLLFunction func; + char dllpath[256]; + + func.ptr = (uintptr_t)LUA->GetCFunction(1); + + if(VFindModule(&func)) + { + LUA->CreateTable(); + LUA->PushUserdata((void*)func.ptr); + LUA->SetField(-2,"ptr"); + + LUA->PushUserdata((void*)func.offset); + LUA->SetField(-2,"offset"); + + LUA->CreateTable(); + LUA->PushUserdata((void*)func.iMod.lpBaseOfDll); + LUA->SetField(-2,"base"); + + LUA->PushUserdata((void*)func.iMod.SizeOfImage); + LUA->SetField(-2,"size"); + + GetModuleFileName(func.hMod,dllpath,256); + LUA->PushString(dllpath); + LUA->SetField(-2,"path"); + LUA->SetField(-2,"mod"); + } + else + { + LUA->PushNil(); + } + + return 1; +} + +GMOD_MODULE_OPEN() +{ + LUA->PushSpecial(0); + LUA->CreateTable(); + LUA->PushCFunction(FuncPtr); + LUA->SetField(-2,"FuncPtr"); + LUA->SetField(-2,"luaspy"); + LUA->Pop(2); + return 0; +} + +GMOD_MODULE_CLOSE() +{ + LUA->PushNil(); + LUA->SetField(INDEX_GLOBAL,"luaspy"); + return 0; +} \ No newline at end of file