1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include "winlite.h"
#undef _UNICODE
#include <interface>
#include "icvar.h"
#include "convar.h"
#include "detours.h"
#include "GMLuaModule.h"
extern "C"
{
#include "lua.h"
}
GMOD_MODULE(Init, Shutdown);
static ICvar *g_pCVar = NULL;
lua_State *(lua_newstateOriginal)(lua_Alloc, void *);
lua_State *(* lua_newstateDetour)(lua_Alloc, void *);
lua_State *(* lua_newstateTramp)(lua_Alloc, void *);
DETOUR_TRAMPOLINE_EMPTY(lua_State *(lua_newstateOriginal)(lua_Alloc, void *));
static lua_State *currentState = NULL;
lua_State *lua_newstateNew(lua_Alloc f, void *ud)
{
lua_State* L = lua_newstateOriginal(f, ud);
currentState = L;
return L;
}
typedef struct LoadS {
const char *s;
size_t size;
} LoadS;
static const char *getS (lua_State *L, void *ud, size_t *size) {
LoadS *ls = (LoadS *)ud;
(void)L;
if (ls->size == 0) return NULL;
*size = ls->size;
ls->size = 0;
return ls->s;
}
void LoadLua(const CCommand &command)
{
if(currentState == NULL)
return;
// util.RelativePathToFull( path )
lua_getglobal(currentState, "util");
lua_pushstring(currentState, "RelativePathToFull");
lua_gettable(currentState, -2);
lua_pushstring(currentState, command.ArgS());
if(lua_pcall(currentState, 1, 1, 0) != 0)
{
lua_pop(currentState, 1);
return;
}
const char *fullpath = lua_tostring(currentState, -1);
lua_pop(currentState, 2);
FILE *f = fopen(fullpath, "rb");
if(f == NULL)
return;
fseek(f, 0, SEEK_END);
long size = ftell(f);
rewind(f);
char *data = (char *)malloc(size);
fread(data, 1, size, f);
fclose(f);
// The game will think any calls from this code are coming from the base gamemodes cl_init.lua
//luaL_loadbuffer(currentState, data, size, "gamemodes\\base\\gamemode\\cl_init.lua");
LoadS ls;
ls.s = data;
ls.size = size;
lua_load(currentState, getS, &ls, "gamemodes\\base\\gamemode\\cl_init.lua");
lua_pcall(currentState, 0, 0, 0);
free(data);
}
static ConCommand *cmd;
int Init(void)
{
lua_newstateDetour = &lua_newstateNew;
lua_newstateTramp = &lua_newstateOriginal;
// Detour the lua_newstate function from lua_shared.dll so we can capture the lua_State when its created.
DetourFunctionWithEmptyTrampoline(*(PBYTE *) &lua_newstateTramp, (PBYTE) &lua_newstate, *(PBYTE *) &lua_newstateDetour);
g_pCVar = *(ICvar **)GetProcAddress(GetModuleHandleA("client.dll"), "cvar");
cmd = new ConCommand("lua_se2_load", LoadLua, "", FCVAR_UNREGISTERED);
g_pCVar->RegisterConCommand(cmd);
return 0;
}
int Shutdown(void)
{
DetourRemove(*(PBYTE*) &lua_newstateOriginal, *(PBYTE*) &lua_newstateDetour);
g_pCVar->UnregisterConCommand(cmd);
delete cmd;
return 0;
}
|