Advertisement
captmicro

Gmod Lua Module - LuaInterface->Init hook

Jul 24th, 2011
1,448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. #gm_runlua.cpp(main)
  2. #define _RETAIL  
  3. #define GAME_DLL  
  4.  
  5. //hey let's just include everything possible
  6. #include "GMLuaModule.h"
  7. #include "windows.h"
  8. #include <convar.h>
  9. #include <public/icvar.h>
  10. #include <tier1/tier1.h>
  11. #include <tier2/tier2.h>
  12. #include <tier3/tier3.h>
  13. #include <eiface.h>  
  14. #include <interface.h>  
  15. #include <detours.h>
  16. #include <vfnhook.h>
  17.  
  18.  
  19. GMOD_MODULE( Init, Shutdown );
  20. ILuaInterface *gLua = NULL;
  21. ICvar *g_pCVar = NULL;
  22. //(ICvar*)interfaceFactory(CVAR_INTERFACE_VERSION, NULL);
  23. //ICvar *icvar = NULL;
  24.  
  25.  
  26. DEFVFUNC(LuaInterfaceInit, bool, (ILuaInterface* pInterface, NULL));
  27.  
  28. bool VFUNC luaInit(ILuaInterface* pInterface, NULL) {
  29.     gLua->Msg("Hook called!");
  30.  
  31.     return LuaInterfaceInit(pInterface);
  32. }
  33.  
  34.  
  35. const char *runString;
  36. const char *filePath;
  37. bool doRun;
  38. bool showErrors;
  39.  
  40.  
  41. void runscript( const char *p, bool dor=true, bool showerr=true ) {
  42.     gLua->FindAndRunScript(p, dor, showerr);
  43. }
  44.  
  45.  
  46.  
  47. void runscript_concmd( const CCommand &args ) {
  48.  
  49.     gLua->Msg("Args 0-3: %s, %s, %s, %s",args.Arg(0), args.Arg(1), args.Arg(2), args.Arg(3));
  50.     const char *p = args.Arg(1);
  51.     bool dor = true;
  52.     bool showerr = true;
  53.  
  54.     gLua->FindAndRunScript(p, dor, showerr);
  55.  
  56. }
  57.  
  58. void runstring_concmd( const CCommand &args ) {
  59.    
  60.     const char *strtorun = args.Arg(1);
  61.  
  62.     gLua->RunString("tempstrcontainerconcmd.txt", "./", strtorun, true, true);
  63. }
  64.  
  65. LUA_FUNCTION ( runFile ) {
  66.     gLua->CheckType( 1, GLua::TYPE_STRING );
  67.     gLua->CheckType( 2, GLua::TYPE_BOOL );
  68.     gLua->CheckType( 3, GLua::TYPE_BOOL );
  69.  
  70.     filePath = gLua->GetString(1);
  71.     doRun = gLua->GetBool(1);
  72.     showErrors = gLua->GetBool(2);
  73.    
  74.     runscript(filePath, doRun, showErrors);
  75.  
  76.     return 0;
  77. }
  78.  
  79.  
  80. LUA_FUNCTION ( runLua ) {
  81.     gLua->CheckType( 1, GLua::TYPE_STRING );
  82.  
  83.  
  84.     runString = gLua->GetString(1);
  85.    
  86.     gLua->RunString("tempstrcontainer.txt", "./", runString, true, true);
  87.  
  88.     return 0;
  89. }
  90.  
  91.  
  92.  
  93. class CBase : public IConCommandBaseAccessor
  94. {
  95. public:
  96.     bool RegisterConCommandBase(ConCommandBase *pVar)
  97.     {
  98.         gLua->Msg( "RegisterConCommandBase called for: %s.\n", pVar->GetName() );
  99.         g_pCVar->RegisterConCommand(pVar);
  100.         return 0;
  101.     }
  102. } s_CBase;
  103.  
  104.  
  105. /*
  106. CON_COMMAND(runfile, "Runs a file.") {
  107.     gLua->FindAndRunScript("cockfishing.lua", true, true);
  108. }
  109. */
  110.  
  111. static ConCommand runfile_command( "runfile", runscript_concmd, "Runs a given lua file." );
  112. static ConCommand runstring_command( "runlua", runstring_concmd, "Runs a given lua string." );
  113.  
  114.  
  115. int Init( lua_State *L ) {
  116.     gLua = Lua();
  117.  
  118.  
  119.     //Extract g_pCVar
  120.     CreateInterfaceFn vstdFactory = Sys_GetFactory("vstdlib.dll");  
  121.     if(!vstdFactory) {
  122.         gLua->Msg("yeah sys_getfactory did not work");
  123.        
  124.         return 0;
  125.     }
  126.     g_pCVar = (ICvar*)vstdFactory(CVAR_INTERFACE_VERSION, NULL);  
  127.  
  128.  
  129.    
  130.        
  131.  
  132. //  ConCommandBase* CMD = g_pCVar->GetCommands();
  133.  
  134.     gLua->Msg( "gmcl_runlua loaded.\n" );
  135.  
  136.     gLua->SetGlobal( "runLua", runLua );
  137.     gLua->SetGlobal( "runFile", runFile );
  138.  
  139. //  ConVar_Register(0, &fuckfuckfuck);
  140. //  ConCommand *runfile = new ConCommand( "runfile", runscript_concmd, "runs a script", 0, 0);
  141.     ConVar_Register(0, &s_CBase);
  142.  
  143.     HOOKVFUNC(gLua, 2, LuaInterfaceInit, luaInit);
  144.     return 0;
  145. }
  146.  
  147.  
  148.  
  149. int Shutdown( lua_State *L ) {
  150.     ConVar_Unregister();
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement