Draiget

Untitled

Oct 16th, 2016
25
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <datamap.h>
  2. #include <const.h>
  3. #include <tier1.h>
  4. #include <tier2.h>
  5. #include <tier3.h>
  6. #include <eiface.h>
  7. #include <igameevents.h>
  8. #include <iclient.h>
  9. #include <inetmessage.h>
  10. #include <shareddefs.h>
  11. #include <inetchannel.h>
  12. #include <igameevents.h>
  13. #include "igameserverdata.h"
  14. #include <iachievementmgr.h>
  15. #include <utllinkedlist.h>
  16. #include <game/server/iplayerinfo.h>
  17. #include <vphysics_interface.h>
  18. #include <vphysics/performance.h>
  19. #include "VTableUtil.h"
  20. #include "igameserverdata.h"
  21. #include <vstdlib/cvar.h>
  22.  
  23. // VTable stuff
  24. #define __thiscall
  25. typedef void *PVOID;
  26.  
  27. template< class T > inline T* makeptr(void* pObj, unsigned int offset) {
  28.     return reinterpret_cast< T* >(reinterpret_cast< unsigned char* >(pObj) + offset);
  29. }
  30.  
  31. inline void**& getvtable(void* inst, size_t offset = 0) {
  32.     return *reinterpret_cast<void***>(reinterpret_cast<size_t>(inst) + offset);
  33. }
  34.  
  35. inline const void** getvtable(const void* inst, size_t offset = 0) {
  36.     return *reinterpret_cast<const void***>(reinterpret_cast<size_t>(inst) + offset);
  37. }
  38.  
  39. template< typename Fn >
  40. inline Fn getvfunc(const void* inst, size_t index, size_t offset = 0) {
  41.     return reinterpret_cast<Fn>(getvtable(inst, offset)[index]);
  42. }
  43.  
  44. //------------------------------------------------------------------------
  45. // Forward declarations
  46. //------------------------------------------------------------------------
  47. class CTakeDamageInfo;
  48. class CNPC_VehicleDriver;
  49. class IVehicle;
  50. class IServerVehicle;
  51. class CBaseEntity;
  52. class CBaseAnimating;
  53. class CBaseAnimatingOverlay;
  54. class CBaseFlex;
  55. class CBaseCombatCharacter;
  56. class CBaseAnimating;
  57. class IEntitySaveUtils;
  58. class CBaseClient;
  59. class IServer;
  60. class CBaseServer;
  61. class CClientFrame;
  62. class CFrameSnapshot;
  63. class CGameServer;
  64. class CUserCmd;
  65. class IMoveHelper;
  66. class CMoveData;
  67. class CBaseCombatCharacter;
  68. class CBasePlayer;
  69. class CServerRemoteAccess;
  70. class CPhysicsProp;
  71. class CServerNetworkProperty;
  72.  
  73. //------------------------------------------------------------------------
  74. // CBaseServer
  75. //------------------------------------------------------------------------
  76. class CBaseServer
  77.     : public IServer
  78. {
  79. static const int m_vtOffset = 1;
  80. public:
  81.     virtual ~CBaseServer();
  82.  
  83.     const char* GetMapName( void ) {
  84.         typedef const char*(__thiscall* vtGetMapName)(PVOID);
  85.         return getvfunc< vtGetMapName >(this, 14 - m_vtOffset)(this);
  86.     }
  87.  
  88.     void BroadcastPrintf(const char *fmt, ...) {
  89.         va_list vl;
  90.         va_start(vl, fmt);
  91.         char buffer[1024];
  92.         vsprintf(buffer, fmt, vl);
  93.         va_end(vl);
  94.  
  95.         typedef void(__thiscall* vtBroadcastPrintf)(PVOID, const char *);
  96.         return getvfunc< vtBroadcastPrintf >(this, 36 - m_vtOffset)(this, buffer);
  97.     }
  98. };
  99.  
  100. ///////// Signature searching
  101. void* UTIL_FindSignatureInRunTime( const char* pSignature, const char* pModule ) {
  102.     if (!pSignature || pSignature[0] == 0) {
  103.         return nullptr;
  104.     }
  105.  
  106.     static char pSig[512];
  107.     auto siglen = UTIL_StringToSignature( pSignature, pSig, sizeof pSig );
  108.  
  109.     auto moduleHandle = dlopen(pModule, RTLD_NOW );
  110.     if ( !moduleHandle) {
  111.         return nullptr;
  112.     }
  113.  
  114.     auto moduleAddr = dlsym( moduleHandle, "CreateInterface" );
  115.     if ( !moduleAddr) {
  116.         printf("(UTIL_FindSignatureInRunTime) Cannot found module CreateInterface '%s'!\n", pModule);
  117.         return nullptr;
  118.     }
  119.  
  120.     Dl_info dlInfo;
  121.     if ( !dladdr(moduleAddr, &dlInfo) ) {
  122.         printf("(UTIL_FindSignatureInRunTime) Cannot exec dladdr[%p] on module '%s': %s!\n", moduleAddr, pModule, dlerror());
  123.         return nullptr;
  124.     }
  125.  
  126.     if ( !dlInfo.dli_fbase || !dlInfo.dli_fname ) {
  127.         return nullptr;
  128.     }
  129.  
  130.     struct stat buf;
  131.     if ( stat( dlInfo.dli_fname, &buf ) != 0 ) {
  132.         return nullptr;
  133.     }
  134.  
  135.     auto ptr = reinterpret_cast<char *>( dlInfo.dli_fbase );
  136.     auto end = ptr + buf.st_size - 1;
  137.     char* retn = nullptr;
  138.     bool found = false;
  139.  
  140.     // printf( "ptr: %p, end: %p, sz: %d [%d]\n", ptr, end, end - ptr, dlInfo.dli_saddr);
  141.     while (ptr < end) {
  142.         found = true;
  143.  
  144.         for (register unsigned int i = 0; i < siglen; i++) {
  145.             if (pSig[i] != '\x2A' && pSig[i] != ptr[i]) {
  146.                 found = false;
  147.                 break;
  148.             }
  149.         }
  150.  
  151.         if (found) {
  152.             if (retn) {
  153.                 return reinterpret_cast<void *>(-1);
  154.             }
  155.  
  156.             retn = ptr;
  157.         }
  158.         ptr++;
  159.     }
  160.  
  161.     return retn;
  162. }
  163.  
  164.  
  165. unsigned GetModuleSize( unsigned& hmod ) {
  166.     Dl_info info;
  167.     struct stat buf;
  168.  
  169.     if (!dladdr(reinterpret_cast< void* >( hmod ), &info))
  170.         return 0;
  171.  
  172.     if (!info.dli_fbase || !info.dli_fname)
  173.         return 0;
  174.  
  175.     if (stat(info.dli_fname, &buf) != 0)
  176.         return 0;
  177.  
  178.     hmod = (unsigned int)( info.dli_fbase );
  179.     return buf.st_size;
  180. }
  181.  
  182. void* SigScan( void* hmod, const char* pSignature ) {
  183.     if (!pSignature || pSignature[0] == 0) {
  184.         return nullptr;
  185.     }
  186.  
  187.     static char pSig[512];
  188.     auto siglen = UTIL_StringToSignature( pSignature, pSig, sizeof pSig );
  189.  
  190.     auto moduleSize = GetModuleSize(reinterpret_cast< unsigned int& >( hmod ));
  191.     if (moduleSize != 0) {
  192.         char* retn = nullptr;
  193.         auto found = false;
  194.         auto ptr = reinterpret_cast<char *>( hmod );
  195.         auto end = ptr + moduleSize - 1;
  196.  
  197.         while (hmod < end) {
  198.             found = true;
  199.  
  200.             for (register unsigned int i = 0; i < siglen; i++) {
  201.                 if (pSig[i] != '\x2A' && pSig[i] != ptr[i]) {
  202.                     found = false;
  203.                     break;
  204.                 }
  205.             }
  206.  
  207.             if (found) {
  208.                 if (retn) {
  209.                     return reinterpret_cast<void *>(-1);
  210.                 }
  211.  
  212.                 retn = ptr;
  213.                 break;
  214.             }
  215.             ptr++;
  216.         }
  217.  
  218.         // printf("sigscan retn [%p]\n", retn);
  219.         return retn;
  220.     }
  221.  
  222.     return nullptr;
  223. }
RAW Paste Data