Code:
int Detour(lua_State* state, const char *buff, size_t sz, const char *name)
{
bool shouldRun = true;
if (client && client->IsConnected())
{
const char* SCRIPT;
const char* SOURCE;
ILuaBase* base = state->luabase;
std::string path;
std::string address = std::string(client->GetNetChannelInfo()->GetAddress());
address = ReplaceString(address, ":", "-");
path = "scripthook/" + address + "/";
std::string source = std::string(name);
if (source.substr(0, 1) == "@")
{
source.erase(0, 1);
}
std::string orig = source;
std::string fullpath = path + source;
if (hasEnding(source, ".lua"))
{
boost::filesystem::path p(fullpath);
boost::filesystem::path dir = p.parent_path();
boost::filesystem::create_directories(dir);
if (boost::filesystem::exists(fullpath))
{
std::ofstream out(fullpath);
out << std::string(buff);
} else {
std::ofstream out(fullpath, std::ios_base::app);
out << "\n-----------------------------------------------------\n" + std::string(buff);
}
} else {
std::string origsource = source;
source = path + source;
if (!boost::filesystem::exists(source + ".lua"))
{
std::ofstream out(source + ".lua");
out << std::string(buff);
} else {
std::ofstream out(source + ".lua", std::ios_base::app);
out << "\n-----------------------------------------------------\n" + std::string(buff);
}
}
if (!boost::filesystem::exists("scripthook/scripthook.lua"))
{
ConColorMsg(Color(255, 145, 0, 255), "[Warning] ");
Msg("scripthook.lua does not exist!\n");
} else {
base->PushSpecial(SPECIAL_GLOB);
base->PushString("_SCRIPT");
base->PushString(buff);
base->RawSet(-3);
base->Pop();
base->PushSpecial(SPECIAL_GLOB);
base->PushString("_SOURCE");
base->PushString(orig.c_str());
base->RawSet(-3);
base->Pop();
if (!luaL_loadfile(state, "scripthook/scripthook.lua"))
{
if (!lua_pcall(state, 0, 1, 0))
{
if (base->IsType(-1, Type::BOOL))
{
shouldRun = base->GetBool(-1);
}
base->Pop();
} else {
ConColorMsg(Color(255, 145, 0, 255), "[Warning] ");
Msg("%s\n", base->GetString(-1));
}
} else {
ConColorMsg(Color(255, 145, 0, 255), "[Warning] ");
Msg("%s\n", base->GetString(-1));
}
base->PushSpecial(SPECIAL_GLOB);
base->PushString("_SCRIPT");
base->RawGet(-2);
SCRIPT = base->GetString(-1);
base->Pop(2);
base->PushSpecial(SPECIAL_GLOB);
base->PushString("_SOURCE");
base->RawGet(-2);
SOURCE = base->GetString(-1);
base->Pop(2);
base->PushSpecial(SPECIAL_GLOB);
base->PushString("_SCRIPT");
base->PushNil();
base->RawSet(-3);
base->Pop();
base->PushSpecial(SPECIAL_GLOB);
base->PushString("_SOURCE");
base->PushNil();
base->RawSet(-3);
base->Pop();
}
if (!shouldRun)
{
ConColorMsg(Color(0, 154, 255, 255), "[scripthook] ");
Msg("Blocked %s\n", orig.c_str());
luaL_loadstring(state, "");
return 0;
} else {
std::string src = std::string(SOURCE);
return luaL_loadbuffer(state, SCRIPT, sz, ("@" + src).c_str());
}
}
return luaL_loadbuffer(state, buff, sz, name);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD Reason, LPVOID lpReserved)
{
if (Reason == DLL_PROCESS_ATTACH)
{
CreateInterfaceFn factory = Sys_GetFactory("engine.dll");
client = (IVEngineClient*)factory(VENGINE_CLIENT_INTERFACE_VERSION_13, NULL);
luaL_loadbuffer = (loadbuffer)GetProcAddress(GetModuleHandle("lua_shared.dll"), "luaL_loadbuffer");
if (!luaL_loadbuffer)
{
ConColorMsg(Color(255, 0, 0, 255), "[Error] ");
Msg("scripthook has failed to grab luaL_loadbuffer.\n");
return FALSE;
}
DisableThreadLibraryCalls(hModule);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)luaL_loadbuffer, Detour);
if (DetourTransactionCommit() == NO_ERROR)
{
ConColorMsg(Color(0, 255, 0, 255), "[Success] ");
Msg("scripthook has completed hooking successfully.\n");
Msg("This is not the final version scripthook!\n");
return TRUE;
} else {
ConColorMsg(Color(255, 0, 0, 255), "[Error] ");
Msg("scripthook was unsucessful.\n");
return FALSE;
}
return FALSE;
}
}