From 107c92c7a0eabfdaf4bbc210c4b221a1a2d867c7 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Sun, 15 Nov 2015 00:35:29 +0600 Subject: [PATCH] Implemented unload plugin Implemented log Hide fake cvar from cvar_vars --- msvc/rechecker.vcxproj | 16 ----- msvc/rechecker.vcxproj.filters | 4 -- src/cmdexec.cpp | 2 + src/config.cpp | 2 - src/config.h | 2 - src/dllapi.cpp | 1 + src/main.cpp | 122 ++++++++++++++++----------------- src/meta_api.cpp | 4 +- src/precompiled.h | 1 + src/resource.cpp | 108 +++++++++++++++++++++++++---- src/resource.h | 9 ++- 11 files changed, 170 insertions(+), 101 deletions(-) diff --git a/msvc/rechecker.vcxproj b/msvc/rechecker.vcxproj index 76c8656..5c51048 100644 --- a/msvc/rechecker.vcxproj +++ b/msvc/rechecker.vcxproj @@ -164,18 +164,10 @@ - - true - true - - - true - true - @@ -184,10 +176,6 @@ - - true - true - @@ -203,10 +191,6 @@ Create - - true - true - diff --git a/msvc/rechecker.vcxproj.filters b/msvc/rechecker.vcxproj.filters index e3f00be..216e9fe 100644 --- a/msvc/rechecker.vcxproj.filters +++ b/msvc/rechecker.vcxproj.filters @@ -462,8 +462,6 @@ - - @@ -482,8 +480,6 @@ - - diff --git a/src/cmdexec.cpp b/src/cmdexec.cpp index 82d11fa..f88f39e 100644 --- a/src/cmdexec.cpp +++ b/src/cmdexec.cpp @@ -101,6 +101,8 @@ void CExecMngr::CommandExecute(IGameClient *pClient) if (!bBreak // erase all cmdexec because have flag is break && cmdExec != NULL && cmdExec[0] != '\0') { + Resource.Log(" -> ExecuteCMD: (%s), for (%s)", cmdExec, pClient->GetName()); + // execute cmdexec SERVER_COMMAND(cmdExec); } diff --git a/src/config.cpp b/src/config.cpp index f3369f7..585bfc8 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -73,7 +73,6 @@ void CConfig::Load() if (fp == NULL) { - m_ConfigFailed = true; UTIL_Printf(__FUNCTION__ ": can't find path to " FILE_INI_CONFIG "\n"); return; } @@ -97,5 +96,4 @@ void CConfig::Load() } fclose(fp); - m_ConfigFailed = false; } diff --git a/src/config.h b/src/config.h index 8bc8a38..5091e04 100644 --- a/src/config.h +++ b/src/config.h @@ -9,13 +9,11 @@ public: void Load(); float GetDelay() const { return m_DelayExec; }; - bool IsConfigLoaded() const { return !m_ConfigFailed; }; private: void ResetValues(); private: - bool m_ConfigFailed; char m_PathDir[MAX_PATH_LENGTH]; // settings diff --git a/src/dllapi.cpp b/src/dllapi.cpp index 44b9921..133e299 100644 --- a/src/dllapi.cpp +++ b/src/dllapi.cpp @@ -4,6 +4,7 @@ DLL_FUNCTIONS *g_pFunctionTable; extern void ServerDeactivate_Post(); extern void ClientPutInServer_Post(edict_t *pEntity); +extern void StartFrame(); static DLL_FUNCTIONS gFunctionTable = { diff --git a/src/main.cpp b/src/main.cpp index e7dfb98..eb77399 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "precompiled.h" -cvar_t mp_consistency = { "mp_consistency", "0", 0, 0.0f, NULL }; +cvar_t cv_mp_consistency = { "mp_consistency", "0", 0, 0.0f, NULL }; +cvar_t *pcv_consistency_old = NULL; void (*SV_AddResource)(resourcetype_t type, const char *name, int size, unsigned char flags, int index); @@ -9,6 +10,37 @@ bool OnMetaAttach() if (RehldsApi_Init() != RETURN_LOAD) return false; + // initialize resource config + Resource.Init(); + + // set force cvar on own value and replacement of original + // NOTE: in gamedll used this cvar not through a pointer thus we create own cvar for gamedll with default values + // so for engine set it the cvar values is 1. + pcv_consistency_old = g_engfuncs.pfnCVarGetPointer("mp_consistency"); + + cv_mp_consistency.value = pcv_consistency_old->value; + cv_mp_consistency.string = pcv_consistency_old->string; + cv_mp_consistency.flags = pcv_consistency_old->flags; + cv_mp_consistency.name = pcv_consistency_old->name; + pcv_consistency_old->name = "mp_consistency_"; + + g_engfuncs.pfnCVarRegister(&cv_mp_consistency); + g_engfuncs.pfnCvar_DirectSet(pcv_consistency_old, "1"); + + // to remove the old cvar of cvars list + cvar_t *cvar_vars = g_RehldsApi->GetFuncs()->GetCvarVars(); + for (cvar_t *var = cvar_vars, *prev = NULL; var != NULL; prev = var, var = var->next) + { + if (var == pcv_consistency_old) + { + if (prev != NULL) + prev->next = var->next; + else + cvar_vars = cvar_vars->next; + break; + } + } + // register function from ReHLDS API g_RehldsApi->GetHookchains()->SV_DropClient()->registerHook(&SV_DropClient); g_RehldsApi->GetHookchains()->SV_ActivateServer()->registerHook(&SV_ActivateServer); @@ -16,48 +48,46 @@ bool OnMetaAttach() SV_AddResource = g_RehldsApi->GetFuncs()->SV_AddResource; - // initialize resource config - Resource.Init(); - - /* - // initialize config - Config.Init(); - */ - - // set force cvar on own value and replacement of original - // NOTE: in gamedll used this cvar not through a pointer thus we create own cvar for gamedll with default values - // so for engine set it the cvar values is 1. - cvar_t *mp_consistency_old = g_engfuncs.pfnCVarGetPointer("mp_consistency"); - - mp_consistency.value = mp_consistency_old->value; - mp_consistency.string = mp_consistency_old->string; - mp_consistency.flags = mp_consistency_old->flags; - mp_consistency_old->name = "mp_consistency_orig"; - - g_engfuncs.pfnCVarRegister(&mp_consistency); - g_engfuncs.pfnCvar_DirectSet(mp_consistency_old, "1"); - - // if all config's is OK go to attach - return (Resource.IsConfigLoaded()/* && Config.IsConfigLoaded()*/); + // go to attach + return true; } void OnMetaDetach() { - g_RehldsApi->GetHookchains()->SV_DropClient()->unregisterHook(&SV_DropClient); - g_RehldsApi->GetHookchains()->SV_ActivateServer()->unregisterHook(&SV_ActivateServer); - g_RehldsApi->GetHookchains()->SV_CheckConsistencyResponce()->unregisterHook(&SV_CheckConsistencyResponce); + cvar_t *pcv_mp_consistency = g_engfuncs.pfnCVarGetPointer("mp_consistency"); + + // to restore the pointer address of a string + pcv_consistency_old->name = cv_mp_consistency.name; + g_engfuncs.pfnCvar_DirectSet(pcv_consistency_old, pcv_mp_consistency->string); + + // restore old cvar mp_consistency + cvar_t *cvar_vars = g_RehldsApi->GetFuncs()->GetCvarVars(); + + for (cvar_t *var = cvar_vars, *prev = NULL; var != NULL; prev = var, var = var->next) + { + if (var == pcv_mp_consistency) + { + if (prev != NULL) + prev->next = pcv_consistency_old; + else + cvar_vars = pcv_consistency_old; + break; + } + } // clear Exec.Clear(); - /*Task.Clear();*/ Resource.Clear(); + + g_RehldsApi->GetHookchains()->SV_DropClient()->unregisterHook(&SV_DropClient); + g_RehldsApi->GetHookchains()->SV_ActivateServer()->unregisterHook(&SV_ActivateServer); + g_RehldsApi->GetHookchains()->SV_CheckConsistencyResponce()->unregisterHook(&SV_CheckConsistencyResponce); } void ServerDeactivate_Post() { // clear Exec.Clear(); - /*Task.Clear();*/ Resource.Clear(); SET_META_RESULT(MRES_IGNORED); @@ -67,23 +97,13 @@ void SV_DropClient(IRehldsHook_SV_DropClient *chain, IGameClient *pClient, bool { // clear buffer cmdexec the client when was disconnected up to perform cmdexec Exec.Clear(pClient); - /*Task.Clear(pClient);*/ // call next hook chain->callNext(pClient, crash, string); } -/* -void StartFrame() -{ - Task.StartFrame(); - SET_META_RESULT(MRES_IGNORED); -} -*/ - void SV_ActivateServer(IRehldsHook_SV_ActivateServer *chain, int runPhysics) { - /*Config.Load();*/ Resource.LoadResources(); chain->callNext(runPhysics); @@ -92,17 +112,6 @@ void SV_ActivateServer(IRehldsHook_SV_ActivateServer *chain, int runPhysics) Resource.CreateResourceList(); } -/* -void TaskHandler(IGameClient *pClient) -{ - if (!pClient->IsConnected()) - return; - - // client is connected to putinserver, go execute cmd out buffer - Exec.CommandExecute(pClient); -} -*/ - void ClientPutInServer_Post(edict_t *pEntity) { int nIndex = ENTINDEX(pEntity) - 1; @@ -112,17 +121,8 @@ void ClientPutInServer_Post(edict_t *pEntity) IGameClient *pClient = g_RehldsApi->GetServerStatic()->GetClient(nIndex); - /* - float time = Config.GetDelay(); - if (time <= 0.0f) - */ - // client is connected to putinserver, go execute cmd out buffer - Exec.CommandExecute(pClient); - /* - else - Task.AddTask(pClient, time, (xtask_t)TaskHandler); - */ - + // client is connected to putinserver, go execute cmd out buffer + Exec.CommandExecute(pClient); SET_META_RESULT(MRES_IGNORED); } diff --git a/src/meta_api.cpp b/src/meta_api.cpp index be98b89..f45ff63 100644 --- a/src/meta_api.cpp +++ b/src/meta_api.cpp @@ -9,8 +9,8 @@ plugin_info_t Plugin_info = "s1lent", "http://www.dedicated-server.ru/", "Rechecker", - PT_STARTUP, - PT_NEVER, + PT_CHANGELEVEL, + PT_ANYTIME, }; meta_globals_t *gpMetaGlobals; diff --git a/src/precompiled.h b/src/precompiled.h index 6adb1bf..e9f2f32 100644 --- a/src/precompiled.h +++ b/src/precompiled.h @@ -14,6 +14,7 @@ #include #include // strrchr +#include // time, localtime etc #include #include // ALERT() diff --git a/src/resource.cpp b/src/resource.cpp index 2f0ae25..cdd0144 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -3,6 +3,9 @@ CResourceFile Resource; std::vector StringsCache; +cvar_t cv_rch_log = { "rch_log", "0", 0, 0.0f, NULL }; +cvar_t *pcv_rch_log = NULL; + void CResourceFile::CreateResourceList() { int nConsistency = g_RehldsServerData->GetConsistencyNum(); @@ -23,9 +26,7 @@ void CResourceFile::CreateResourceList() break; } -#ifdef _DEBUG - UTIL_Printf(__FUNCTION__ " :: (%s)(%s)(%x)\n", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetFileHash()); -#endif // _DEBUG + Log(__FUNCTION__ " -> file: (%s), cmdexc: (%s), hash: (%x)", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetFileHash()); SV_AddResource(t_decal, pRes->GetFileName(), 0, RES_CHECKFILE, m_DecalsNum++); nConsistency++; } @@ -46,6 +47,60 @@ void CResourceFile::Clear() ClearStringsCache(); } +void CResourceFile::Log(const char *fmt, ...) +{ + if (pcv_rch_log->string[0] != '1') + return; + + static char string[2048]; + + FILE *fp; + char *file; + bool bFirst = false; + + fp = fopen(m_LogFilePath, "r"); + + if (fp != NULL) + { + bFirst = true; + fclose(fp); + } + + fp = fopen(m_LogFilePath, "a"); + + if (fp == NULL) + { + return; + } + + va_list argptr; + va_start(argptr, fmt); + vsnprintf(string, sizeof(string), fmt, argptr); + va_end(argptr); + + strcat(string, "\n"); + if (!bFirst) + { + file = strrchr(m_LogFilePath, '/'); + if (file == NULL) + file = "null"; + + fprintf(fp, "L %s: Log file started (file \"%s\") (version \"%s\")\n", m_LogDate, file, Plugin_info.version); + } + + fprintf(fp, "L %s: %s", m_LogDate, string); + fclose(fp); +} + +void CreateDirectory(const char *path) +{ + _mkdir(path +#ifndef _WIN32 + ,0755 +#endif // _WIN32 + ); +} + void CResourceFile::Init() { char *pos; @@ -61,8 +116,16 @@ void CResourceFile::Init() *(pos + 1) = '\0'; + strncpy(m_LogFilePath, path, sizeof(m_LogFilePath) - 1); + m_LogFilePath[sizeof(m_LogFilePath) - 1] = '\0'; + strcat(m_LogFilePath, "logs/"); + CreateDirectory(m_LogFilePath); + // resources.ini snprintf(m_PathDir, sizeof(m_PathDir), "%s" FILE_INI_RESOURCES, path); + + g_engfuncs.pfnCvar_RegisterVariable(&cv_rch_log); + pcv_rch_log = g_engfuncs.pfnCVarGetPointer(cv_rch_log.name); } inline uint8 hexbyte(uint8 *hex) @@ -114,6 +177,27 @@ bool IsFileHasExtension(char *psrc) return strchr(pch, '/') == NULL; } +void CResourceFile::LogPrepare() +{ + char dateFile[64]; + char *pos; + time_t td; + tm *lt; + + td = time(NULL); + lt = localtime(&td); + + // remove path to log file + if ((pos = strrchr(m_LogFilePath, '/')) != NULL) + { + *(pos + 1) = '\0'; + } + + strftime(dateFile, sizeof(dateFile), "L_%d_%m_%Y.log", lt); + strftime(m_LogDate, sizeof(m_LogDate), "%m/%d/%Y - %H:%M:%S", lt); + strcat(m_LogFilePath, dateFile); +} + void CResourceFile::LoadResources() { char *pos; @@ -131,7 +215,6 @@ void CResourceFile::LoadResources() if (fp == NULL) { - m_ConfigFailed = true; UTIL_Printf(__FUNCTION__ ": can't find path to " FILE_INI_RESOURCES "\n"); return; } @@ -271,7 +354,7 @@ void CResourceFile::LoadResources() } fclose(fp); - m_ConfigFailed = false; + LogPrepare(); } const char *CResourceFile::GetNextToken(char **pbuf) @@ -367,7 +450,7 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource m_PrevHash = hash; return true; } - + // strange thing // if this happened when missing all the files from client if (!m_PrevHash) @@ -433,12 +516,13 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource if (typeFind != FLAG_TYPE_NONE) { - // push exec cmd - Exec.AddElement(pSenderClient, pRes, hash); - -#ifdef _DEBUG - UTIL_Printf(" -> filename: (%s), exphash: (%x), resphash: (%x), typeFind: (%d), prevhash: (%x)\n", pRes->GetFileName(), pRes->GetFileHash(), hash, typeFind, m_PrevHash); -#endif // _DEBUG + // TODO: what is? + if (hash != 0x0) + { + // push exec cmd + Exec.AddElement(pSenderClient, pRes, hash); + } + Log(" -> file: (%s), exphash: (%x), got: (%x), typeFind: (%d), prevhash: (%x), (%s)", pRes->GetFileName(), pRes->GetFileHash(), hash, typeFind, m_PrevHash, pSenderClient->GetName()); } bHandled = true; diff --git a/src/resource.h b/src/resource.h index 709d4b5..9dafea3 100644 --- a/src/resource.h +++ b/src/resource.h @@ -58,12 +58,13 @@ public: void Clear(); void LoadResources(); void CreateResourceList(); + void Log(const char *fmt, ...); bool FileConsistencyResponce(IGameClient *pSenderClient, resource_t *resource, uint32 hash); - bool IsConfigLoaded() const { return !m_ConfigFailed; }; private: void AddElement(char *filename, char *cmdExec, flag_type_resources flag, uint32 hash, int line); + void LogPrepare(); // parse const char *GetNextToken(char **pbuf); @@ -74,8 +75,12 @@ private: int m_DecalsNum; uint32 m_PrevHash; - bool m_ConfigFailed; + char m_PathDir[MAX_PATH_LENGTH]; + + // log data + char m_LogFilePath[MAX_PATH_LENGTH]; + char m_LogDate[64]; }; extern CResourceFile Resource;