Advertisement
RyDeR_

OnConsoleOutput.cpp

Jan 6th, 2013
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. /*
  2.  *      OnConsoleOutput.cpp
  3. */
  4.  
  5. #define _CRT_SECURE_NO_WARNINGS
  6.  
  7. #include <fstream>
  8. #include <sstream>
  9. #include <vector>
  10. #include <time.h>
  11. #include <stdarg.h>
  12. #include <malloc.h>
  13. #include <string.h>
  14.  
  15. using namespace
  16.     std
  17. ;
  18.  
  19. #include "SDK/amx/amx.h"
  20. #include "SDK/plugincommon.h"
  21.  
  22. #ifdef LINUX
  23.     #include <sys/mman.h>
  24.     #include <time.h>
  25. #else
  26.     #define VC_EXTRALEAN
  27.     #define WIN32_LEAN_AND_MEAN
  28.    
  29.     #include <windows.h>
  30. #endif
  31.  
  32. #include "OnConsoleOutput.h"
  33.  
  34. struct s_AMX {
  35.     int iIdx;
  36.     AMX *pAMX;
  37. };
  38.  
  39. std::vector <s_AMX>
  40.     g_pAMX
  41. ;
  42.  
  43. std::fstream
  44.     g_fsServerLog
  45. ;
  46.  
  47. std::string
  48.     g_szLogTimeFormat
  49. ;
  50.  
  51. logprintf_t
  52.     logprintf
  53. ;
  54.  
  55. extern void
  56.     *pAMXFunctions
  57. ;
  58.  
  59. bool
  60.     g_bInPublic
  61. ;
  62.  
  63. char
  64.     g_szBuffer[1024],
  65.     g_szTime[32]
  66. ;
  67.  
  68. int logprintf__(char *szStr, ...) {
  69.     va_list
  70.         vaParams
  71.     ;
  72.     va_start(vaParams, szStr);
  73.     vsnprintf(g_szBuffer, sizeof(g_szBuffer), szStr, vaParams);
  74.     va_end(vaParams);
  75.  
  76.     printf("%s\n", g_szBuffer);
  77.  
  78.     if(g_fsServerLog.is_open()) {
  79.         time_t
  80.             tTime
  81.         ;
  82.         time(&tTime);
  83.  
  84.         strftime(g_szTime, sizeof(g_szTime), g_szLogTimeFormat.c_str(), localtime(&tTime));
  85.         g_fsServerLog << g_szTime << " " << g_szBuffer << std::endl;
  86.     }
  87.     if(!g_bInPublic) {
  88.         g_bInPublic = true;
  89.  
  90.         for(std::vector <s_AMX>::iterator i = g_pAMX.begin(); i != g_pAMX.end(); ++i) {
  91.             cell
  92.                 iRet,
  93.                 iAddr
  94.             ;
  95.             amx_PushString(i->pAMX, &iAddr, 0, g_szBuffer, 0, 0);
  96.             amx_Exec(i->pAMX, &iRet, i->iIdx);
  97.             amx_Release(i->pAMX, iAddr);
  98.  
  99.             if(iRet == 1) {
  100.                 g_bInPublic = false;
  101.                 return 1;
  102.             }
  103.         }
  104.         g_bInPublic = false;
  105.     }
  106.     return 1;
  107. }
  108.  
  109. static void installHook(void *pFrom, void *pTo) {
  110.     #ifdef WIN32
  111.         DWORD
  112.             dwOld
  113.         ;
  114.         VirtualProtect(pFrom, 5, PAGE_EXECUTE_READWRITE, &dwOld);
  115.     #else
  116.         size_t iPageSize = getpagesize();
  117.         size_t iAddr = ((reinterpret_cast <uint32_t>(pFrom) / iPageSize) * iPageSize);
  118.         size_t iCount = (5 / iPageSize) * iPageSize + iPageSize * 2;
  119.         mprotect(reinterpret_cast <void*>(iAddr), iCount, PROT_READ | PROT_WRITE | PROT_EXEC);
  120.     #endif
  121.  
  122.     *((unsigned char*)pFrom) = 0xE9;
  123.     *((int*)((int)pFrom + 1)) = ((int)pTo - ((int)pFrom + 5));
  124. }
  125.  
  126. PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() {
  127.     return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
  128. }
  129.  
  130. PLUGIN_EXPORT bool PLUGIN_CALL Load(void ** ppData) {
  131.     pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
  132.     logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
  133.  
  134.     installHook((void*)logprintf, (void*)logprintf__);
  135.  
  136.     setlocale(LC_CTYPE, "");
  137.  
  138.     g_fsServerLog.open("server_log.txt", fstream::app | fstream::in | fstream::out);
  139.  
  140.     std::ifstream
  141.         ifsCfg("server.cfg")
  142.     ;
  143.     if(ifsCfg.is_open()) {
  144.         std::string
  145.             szLine
  146.         ;
  147.         while(std::getline(ifsCfg, szLine, '\n')) {
  148.             if(szLine.find("logtimeformat") != -1) {
  149.                 g_szLogTimeFormat = szLine.substr(14, 14);
  150.                 break;
  151.             }
  152.  
  153.         }
  154.     }
  155.     ifsCfg.close();
  156.  
  157.     return true;
  158. }
  159.  
  160. PLUGIN_EXPORT void PLUGIN_CALL Unload() {
  161.     g_pAMX.clear();
  162.     g_fsServerLog.close();
  163. }
  164.  
  165. PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)  {
  166.     int
  167.         iIdx
  168.     ;
  169.     if(!amx_FindPublic(amx, "OnConsoleOutput", &iIdx)) {
  170.         s_AMX
  171.             AMX
  172.         ;
  173.         AMX.pAMX = amx;
  174.         AMX.iIdx = iIdx;
  175.  
  176.         g_pAMX.push_back(AMX);
  177.     }
  178.     return 1;
  179. }
  180.  
  181. PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)  {
  182.     for(std::vector <s_AMX>::iterator i = g_pAMX.begin(); i != g_pAMX.end(); ++i) {
  183.         if(i->pAMX == amx) {
  184.             g_pAMX.erase(i);
  185.             break;
  186.         }
  187.     }
  188.     return AMX_ERR_NONE;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement