[ create a new paste ] login | about

Link: http://codepad.org/P5HBiuP4    [ raw code | fork ]

C++, pasted on Jan 5:
/*
	gm_preproc - lua preprocessor

	written by yakahughes on 1/3/2011

	updated on 1/5/2011 thanks to chrisaster's advice to unreference ILuaObject*s

	linux binary thanks to chrisaster
*/

#define NO_SDK

#include <GMLuaModule.h>
#include <vfnhook.h>

GMOD_MODULE(Init, Shutdown);

DEFVFUNC_(RunStringReal, bool, (ILuaInterface* liface, const char* filename, const char* path, const char* lua, bool run, bool showErrors));

bool VFUNC RunStringDetour(ILuaInterface* liface, const char* filename, const char* path, const char* lua, bool run, bool showErrors) {
	ILuaObject* hookTable = liface->GetGlobal("hook");
	ILuaObject* hookCall = NULL;

	char bl = -1;

	if (hookTable && hookTable->isTable()) {
		hookCall = hookTable->GetMember("Call");

		if (hookCall && hookCall->isFunction()) {
			liface->Push(hookCall);
			liface->Push("Lua_Preprocess");
			liface->PushNil();
			liface->Push(filename);
			liface->Push(path);
			liface->Push(lua);
			liface->Call(5, 1);

			ILuaObject* retrn = liface->GetReturn(0);

			if (retrn && retrn->GetType() == GLua::TYPE_STRING)
				lua = retrn->GetString();
			else if (retrn && retrn->GetType() == GLua::TYPE_BOOL)
				bl = (char)retrn->GetBool();
		}
	}

	if (hookTable)
		hookTable->UnReference();

	if (hookCall)
		hookCall->UnReference();

	if (bl != -1)
		return bl;

	return RunStringReal(liface, filename, path, lua, run, showErrors);
}

LUA_FUNCTION(RawRunString) {
	ILuaInterface* gLua = Lua();

	gLua->CheckType(1, GLua::TYPE_STRING);
	gLua->CheckType(2, GLua::TYPE_STRING);
	gLua->CheckType(3, GLua::TYPE_STRING);

	const char* file = gLua->GetString(1);
	const char* path = gLua->GetString(2);
	const char* lua = gLua->GetString(3);

	gLua->Push(RunStringReal(gLua, file, path, lua, true, true));

	return 1;
}

int Init(lua_State* L) {
	HOOKVFUNC(Lua(), 68, RunStringReal, RunStringDetour);

	Lua()->SetGlobal("RawRunString", RawRunString);

	return 0;
}

int Shutdown(lua_State* L) {
	UNHOOKVFUNC(Lua(), 68, RunStringReal);

	return 0;
}


Create a new paste based on this one


Comments: