Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef __HEADER_CLUA__
  2. #define __HEADER_CLUA__
  3. #pragma once
  4.  
  5. #if defined(GAME_GMOD)
  6.  
  7. #include "Multigame/InterfaceBruteforcer.h"
  8.  
  9. #include "CBaseEntity.h"
  10.  
  11. namespace Source
  12. {
  13.     struct lua_State;
  14.     struct UserData;
  15.  
  16.     class ILuaInterface;
  17.     class ILuaShared;
  18.  
  19.     namespace GarrysMod
  20.     {
  21.         enum
  22.         {
  23.             SPECIAL_GLOB,       // Global table
  24.             SPECIAL_ENV,        // Environment table
  25.             SPECIAL_REG,        // Registry table
  26.         };
  27.  
  28.         enum
  29.         {
  30.             CLIENT_STATE = 0,
  31.             SERVER_STATE,
  32.             MENU_STATE
  33.         };
  34.  
  35.         struct Type
  36.         {
  37.             enum
  38.             {
  39.                 INVALID = -1,
  40.                 NIL,
  41.                 BOOL,
  42.                 LIGHTUSERDATA,
  43.                 NUMBER,
  44.                 STRING,
  45.                 TABLE,
  46.                 FUNCTION,
  47.                 USERDATA,
  48.                 THREAD,
  49.  
  50.                 // UserData
  51.                 ENTITY,
  52.                 VECTOR,
  53.                 ANGLE,
  54.                 PHYSOBJ,
  55.                 SAVE,
  56.                 RESTORE,
  57.                 DAMAGEINFO,
  58.                 EFFECTDATA,
  59.                 MOVEDATA,
  60.                 RECIPIENTFILTER,
  61.                 USERCMD,
  62.                 SCRIPTEDVEHICLE,
  63.  
  64.                 // Client Only
  65.                 MATERIAL,
  66.                 PANEL,
  67.                 PARTICLE,
  68.                 PARTICLEEMITTER,
  69.                 TEXTURE,
  70.                 USERMSG,
  71.  
  72.                 CONVAR,
  73.                 IMESH,
  74.                 MATRIX,
  75.                 SOUND,
  76.                 PIXELVISHANDLE,
  77.                 DLIGHT,
  78.                 VIDEO,
  79.                 FILE,
  80.  
  81.                 COUNT
  82.             };
  83.         };
  84.  
  85.         typedef int(*CFunc) (lua_State *L);
  86.  
  87.  
  88.     }
  89.  
  90.     namespace Interfaces
  91.     {
  92.         extern ILuaShared* Lua;
  93.     }
  94. }
  95.  
  96. struct Source::lua_State
  97. {
  98.     unsigned char _ignore_this_common_lua_header_[69];
  99.     ILuaInterface* luabase;
  100. };
  101.  
  102. struct Source::UserData
  103. {
  104.     void* data;
  105.     unsigned char type;
  106. };
  107.  
  108. class Source::ILuaInterface
  109. {
  110. public:
  111.     virtual int         Top(void) = 0;
  112.     virtual void        Push(int iStackPos) = 0;
  113.     virtual void        Pop(int iAmt = 1) = 0;
  114.     virtual void        GetTable(int iStackPos) = 0;
  115.     virtual void        GetField(int iStackPos, const char* strName) = 0;
  116.     virtual void        SetField(int iStackPos, const char* strName) = 0;
  117.     virtual void        CreateTable() = 0;
  118.     virtual void        SetTable(int i) = 0;
  119.     virtual void        SetMetaTable(int i) = 0;
  120.     virtual bool        GetMetaTable(int i) = 0;
  121.     virtual void        Call(int iArgs, int iResults) = 0;
  122.     virtual int         PCall(int iArgs, int iResults, int iErrorFunc) = 0;
  123.     virtual int         Equal(int iA, int iB) = 0;
  124.     virtual int         RawEqual(int iA, int iB) = 0;
  125.     virtual void        Insert(int iStackPos) = 0;
  126.     virtual void        Remove(int iStackPos) = 0;
  127.     virtual int         Next(int iStackPos) = 0;
  128.     virtual void*       NewUserdata(unsigned int iSize) = 0;
  129.     virtual void        ThrowError(const char* strError) = 0;
  130.     virtual void        CheckType(int iStackPos, int iType) = 0;
  131.     virtual void        ArgError(int iArgNum, const char* strMessage) = 0;
  132.     virtual void        RawGet(int iStackPos) = 0;
  133.     virtual void        RawSet(int iStackPos) = 0;
  134.     virtual const char*     GetString(int iStackPos = -1, unsigned int* iOutLen = NULL) = 0;
  135.     virtual double          GetNumber(int iStackPos = -1) = 0;
  136.     virtual bool            GetBool(int iStackPos = -1) = 0;
  137.     virtual GarrysMod::CFunc            GetCFunction(int iStackPos = -1) = 0;
  138.     virtual void*           GetUserdata(int iStackPos = -1) = 0;
  139.     virtual void        PushNil() = 0;
  140.     virtual void        PushString(const char* val, unsigned int iLen = 0) = 0;
  141.     virtual void        PushNumber(double val) = 0;
  142.     virtual void        PushBool(bool val) = 0;
  143.     virtual void        PushCFunction(GarrysMod::CFunc val) = 0;
  144.     virtual void        PushCClosure(GarrysMod::CFunc val, int iVars) = 0;
  145.     virtual void        PushUserdata(void*) = 0;
  146.     virtual int         ReferenceCreate() = 0;
  147.     virtual void        ReferenceFree(int i) = 0;
  148.     virtual void        ReferencePush(int i) = 0;
  149.     virtual void        PushSpecial(int iType) = 0;
  150.     virtual bool            IsType(int iStackPos, int iType) = 0;
  151.     virtual int             GetType(int iStackPos) = 0;
  152.     virtual const char*     GetTypeName(int iType) = 0;
  153.     virtual void            CreateMetaTableType(const char* strName, int iType) = 0;
  154.     virtual const char* CheckString(int iStackPos = -1) = 0;
  155.     virtual double  CheckNumber(int iStackPos = -1) = 0;
  156.  
  157.     inline double GetNumberFromEntity(Source::CBaseEntity* pEntity, const char* index)
  158.     {
  159.         PushSpecial(GarrysMod::SPECIAL_GLOB);
  160.             GetField(-1, "Entity");
  161.             PushNumber(pEntity->GetNetworkable()->EntIndex());
  162.             Call(1, 1);
  163.             GetField(-1, index);
  164.             if (!IsType(-1, GarrysMod::Type::NUMBER))
  165.             {
  166.                 Pop(3);
  167.                 return 0.0;
  168.             }
  169.  
  170.         double ret = GetNumber(-1);
  171.         Pop(3);
  172.  
  173.         return ret;
  174.     }
  175.  
  176.     inline Vector GetVectorFromEntity(Source::CBaseEntity* pEntity, const char* index)
  177.     {
  178.         PushSpecial(GarrysMod::SPECIAL_GLOB);
  179.         GetField(-1, "Entity");
  180.         PushNumber(pEntity->GetNetworkable()->EntIndex());
  181.         Call(1, 1);
  182.         GetField(-1, index);
  183.         if (!IsType(-1, GarrysMod::Type::VECTOR))
  184.         {
  185.             Pop(3);
  186.             return Vector(0.f, 0.f, 0.f);
  187.         }
  188.         Vector ret;
  189.        
  190.         GetField(-1, "x");
  191.         ret.x = GetNumber();
  192.         Pop();
  193.  
  194.         GetField(-1, "y");
  195.         ret.y = GetNumber();
  196.         Pop();
  197.  
  198.         GetField(-1, "z");
  199.         ret.z = GetNumber();
  200.         Pop();
  201.  
  202.         Pop(3);
  203.  
  204.         return ret;
  205.     }
  206. };
  207.  
  208. class Source::ILuaShared
  209. {
  210. public:
  211.     static ILuaShared* GetPtr();
  212.  
  213.     ILuaInterface* GetLuaInterface(unsigned char state);
  214. };
  215.  
  216. #endif
  217.  
  218. #endif
  219.  
  220. //////////////////////////////////////////////
  221.  
  222. #include "ILua.h"
  223.  
  224. #if defined(GAME_GMOD)
  225.  
  226. namespace Source
  227. {
  228.     namespace Interfaces
  229.     {
  230.         ILuaShared* Lua;
  231.     }
  232. }
  233.  
  234. using namespace Source;
  235.  
  236. ILuaShared* ILuaShared::GetPtr()
  237. {
  238.     return (ILuaShared*)::Util::BruteforceInterface("LUASHARED", "lua_shared.dll");
  239. }
  240.  
  241. ILuaInterface* ILuaShared::GetLuaInterface(unsigned char state)
  242. {
  243.     return GET_VFUNC(ILuaInterface*(__thiscall*)(ILuaShared*, unsigned char), this, ILUASHARED_GETLUAINTERFACE)(this, state);
  244. }
  245.  
  246. #endif
  247.  
  248. /////////////////////////////////////
  249.  
  250. #include "CreateInterface.h"
  251.  
  252. void* Source::CreateInterface(const char* pszInterfaceName, const char* pszModule)
  253. {
  254.     using CreateInterfaceFn = void*(*)(const char*, int*);
  255.  
  256.     CreateInterfaceFn moduleFactory = (CreateInterfaceFn)GetProcAddress(GetModuleHandleA(pszModule), "CreateInterface");
  257.     if (moduleFactory == nullptr)
  258.     {
  259.         return nullptr;
  260.     }
  261.  
  262.     return moduleFactory(pszInterfaceName, nullptr);
  263. }
  264.  
  265. //////////////////////////////////////////
  266.  
  267.     auto pMenuState = Source::Interfaces::Lua->GetLuaInterface(Source::GarrysMod::MENU_STATE);
  268.     auto pClientState = Source::Interfaces::Lua->GetLuaInterface(Source::GarrysMod::CLIENT_STATE);
  269.  
  270.     if (pMenuState == nullptr || pClientState == nullptr)
  271.     {
  272.         return;
  273.     }
  274.  
  275.     double CurCone = 0.09 + pClientState->GetNumberFromEntity(pWeapon, "MaxSpreadInc");
  276.  
  277.     pMenuState->PushSpecial(Source::GarrysMod::SPECIAL_GLOB);
  278.     pMenuState->GetField(-1, "math");
  279.     pMenuState->GetField(-1, "randomseed");
  280.    
  281.     {
  282.         pClientState->PushSpecial(Source::GarrysMod::SPECIAL_GLOB);
  283.         pClientState->GetField(-1, "CurTime");
  284.  
  285.         pClientState->Call(0, 1);
  286.         pMenuState->PushNumber(pClientState->GetNumber(-1));
  287.  
  288.         pClientState->Pop(2);
  289.     }
  290.  
  291.     pMenuState->Call(1, 0);
  292.     pMenuState->Pop(2);
  293.  
  294.     Vector vSpread;
  295.  
  296.     for (int i = 0; i < 2; i++)
  297.     {
  298.         pMenuState->PushSpecial(Source::GarrysMod::SPECIAL_GLOB);
  299.         pMenuState->GetField(-1, "math");
  300.         pMenuState->GetField(-1, "Rand");
  301.         pMenuState->PushNumber(-CurCone);
  302.         pMenuState->PushNumber(CurCone);
  303.         pMenuState->Call(2, 1);
  304.         vSpread[i] = (float)(pMenuState->GetNumber(-1) * -25.0);
  305.         pMenuState->Pop(3);
  306.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement