Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * OnConsoleOutput.cpp
- */
- #define _CRT_SECURE_NO_WARNINGS
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <time.h>
- #include <stdarg.h>
- #include <malloc.h>
- #include <string.h>
- using namespace
- std
- ;
- #include "SDK/amx/amx.h"
- #include "SDK/plugincommon.h"
- #ifdef LINUX
- #include <sys/mman.h>
- #include <time.h>
- #else
- #define VC_EXTRALEAN
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #endif
- #include "OnConsoleOutput.h"
- struct s_AMX {
- int iIdx;
- AMX *pAMX;
- };
- std::vector <s_AMX>
- g_pAMX
- ;
- std::fstream
- g_fsServerLog
- ;
- std::string
- g_szLogTimeFormat
- ;
- logprintf_t
- logprintf
- ;
- extern void
- *pAMXFunctions
- ;
- bool
- g_bInPublic
- ;
- char
- g_szBuffer[1024],
- g_szTime[32]
- ;
- int logprintf__(char *szStr, ...) {
- va_list
- vaParams
- ;
- va_start(vaParams, szStr);
- vsnprintf(g_szBuffer, sizeof(g_szBuffer), szStr, vaParams);
- va_end(vaParams);
- printf("%s\n", g_szBuffer);
- if(g_fsServerLog.is_open()) {
- time_t
- tTime
- ;
- time(&tTime);
- strftime(g_szTime, sizeof(g_szTime), g_szLogTimeFormat.c_str(), localtime(&tTime));
- g_fsServerLog << g_szTime << " " << g_szBuffer << std::endl;
- }
- if(!g_bInPublic) {
- g_bInPublic = true;
- for(std::vector <s_AMX>::iterator i = g_pAMX.begin(); i != g_pAMX.end(); ++i) {
- cell
- iRet,
- iAddr
- ;
- amx_PushString(i->pAMX, &iAddr, 0, g_szBuffer, 0, 0);
- amx_Exec(i->pAMX, &iRet, i->iIdx);
- amx_Release(i->pAMX, iAddr);
- if(iRet == 1) {
- g_bInPublic = false;
- return 1;
- }
- }
- g_bInPublic = false;
- }
- return 1;
- }
- static void installHook(void *pFrom, void *pTo) {
- #ifdef WIN32
- DWORD
- dwOld
- ;
- VirtualProtect(pFrom, 5, PAGE_EXECUTE_READWRITE, &dwOld);
- #else
- size_t iPageSize = getpagesize();
- size_t iAddr = ((reinterpret_cast <uint32_t>(pFrom) / iPageSize) * iPageSize);
- size_t iCount = (5 / iPageSize) * iPageSize + iPageSize * 2;
- mprotect(reinterpret_cast <void*>(iAddr), iCount, PROT_READ | PROT_WRITE | PROT_EXEC);
- #endif
- *((unsigned char*)pFrom) = 0xE9;
- *((int*)((int)pFrom + 1)) = ((int)pTo - ((int)pFrom + 5));
- }
- PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() {
- return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
- }
- PLUGIN_EXPORT bool PLUGIN_CALL Load(void ** ppData) {
- pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
- logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
- installHook((void*)logprintf, (void*)logprintf__);
- setlocale(LC_CTYPE, "");
- g_fsServerLog.open("server_log.txt", fstream::app | fstream::in | fstream::out);
- std::ifstream
- ifsCfg("server.cfg")
- ;
- if(ifsCfg.is_open()) {
- std::string
- szLine
- ;
- while(std::getline(ifsCfg, szLine, '\n')) {
- if(szLine.find("logtimeformat") != -1) {
- g_szLogTimeFormat = szLine.substr(14, 14);
- break;
- }
- }
- }
- ifsCfg.close();
- return true;
- }
- PLUGIN_EXPORT void PLUGIN_CALL Unload() {
- g_pAMX.clear();
- g_fsServerLog.close();
- }
- PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx) {
- int
- iIdx
- ;
- if(!amx_FindPublic(amx, "OnConsoleOutput", &iIdx)) {
- s_AMX
- AMX
- ;
- AMX.pAMX = amx;
- AMX.iIdx = iIdx;
- g_pAMX.push_back(AMX);
- }
- return 1;
- }
- PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx) {
- for(std::vector <s_AMX>::iterator i = g_pAMX.begin(); i != g_pAMX.end(); ++i) {
- if(i->pAMX == amx) {
- g_pAMX.erase(i);
- break;
- }
- }
- return AMX_ERR_NONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement