Implemented unload plugin

Implemented log
Hide fake cvar from cvar_vars
This commit is contained in:
s1lentq 2015-11-15 00:35:29 +06:00
parent e93951b651
commit 107c92c7a0
11 changed files with 170 additions and 101 deletions

View File

@ -164,18 +164,10 @@
<ClInclude Include="..\public\FileSystem.h" /> <ClInclude Include="..\public\FileSystem.h" />
<ClInclude Include="..\public\interface.h" /> <ClInclude Include="..\public\interface.h" />
<ClInclude Include="..\src\cmdexec.h" /> <ClInclude Include="..\src\cmdexec.h" />
<ClInclude Include="..\src\config.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="..\src\resource.h" /> <ClInclude Include="..\src\resource.h" />
<ClInclude Include="..\src\engine_rehlds.h" /> <ClInclude Include="..\src\engine_rehlds.h" />
<ClInclude Include="..\src\main.h" /> <ClInclude Include="..\src\main.h" />
<ClInclude Include="..\src\precompiled.h" /> <ClInclude Include="..\src\precompiled.h" />
<ClInclude Include="..\src\task.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\common\parsemsg.cpp"> <ClCompile Include="..\common\parsemsg.cpp">
@ -184,10 +176,6 @@
</ClCompile> </ClCompile>
<ClCompile Include="..\public\interface.cpp" /> <ClCompile Include="..\public\interface.cpp" />
<ClCompile Include="..\src\cmdexec.cpp" /> <ClCompile Include="..\src\cmdexec.cpp" />
<ClCompile Include="..\src\config.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\src\resource.cpp" /> <ClCompile Include="..\src\resource.cpp" />
<ClCompile Include="..\src\dllapi.cpp" /> <ClCompile Include="..\src\dllapi.cpp" />
<ClCompile Include="..\src\engine_api.cpp"> <ClCompile Include="..\src\engine_api.cpp">
@ -203,10 +191,6 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\sdk_util.cpp" /> <ClCompile Include="..\src\sdk_util.cpp" />
<ClCompile Include="..\src\task.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\dist\config.ini" /> <None Include="..\dist\config.ini" />

View File

@ -462,8 +462,6 @@
<ClInclude Include="..\src\engine_rehlds.h" /> <ClInclude Include="..\src\engine_rehlds.h" />
<ClInclude Include="..\src\cmdexec.h" /> <ClInclude Include="..\src\cmdexec.h" />
<ClInclude Include="..\src\resource.h" /> <ClInclude Include="..\src\resource.h" />
<ClInclude Include="..\src\config.h" />
<ClInclude Include="..\src\task.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\common\parsemsg.cpp"> <ClCompile Include="..\common\parsemsg.cpp">
@ -482,8 +480,6 @@
<ClCompile Include="..\src\sdk_util.cpp" /> <ClCompile Include="..\src\sdk_util.cpp" />
<ClCompile Include="..\src\cmdexec.cpp" /> <ClCompile Include="..\src\cmdexec.cpp" />
<ClCompile Include="..\src\resource.cpp" /> <ClCompile Include="..\src\resource.cpp" />
<ClCompile Include="..\src\config.cpp" />
<ClCompile Include="..\src\task.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="sdk"> <Filter Include="sdk">

View File

@ -101,6 +101,8 @@ void CExecMngr::CommandExecute(IGameClient *pClient)
if (!bBreak // erase all cmdexec because have flag is break if (!bBreak // erase all cmdexec because have flag is break
&& cmdExec != NULL && cmdExec[0] != '\0') && cmdExec != NULL && cmdExec[0] != '\0')
{ {
Resource.Log(" -> ExecuteCMD: (%s), for (%s)", cmdExec, pClient->GetName());
// execute cmdexec // execute cmdexec
SERVER_COMMAND(cmdExec); SERVER_COMMAND(cmdExec);
} }

View File

@ -73,7 +73,6 @@ void CConfig::Load()
if (fp == NULL) if (fp == NULL)
{ {
m_ConfigFailed = true;
UTIL_Printf(__FUNCTION__ ": can't find path to " FILE_INI_CONFIG "\n"); UTIL_Printf(__FUNCTION__ ": can't find path to " FILE_INI_CONFIG "\n");
return; return;
} }
@ -97,5 +96,4 @@ void CConfig::Load()
} }
fclose(fp); fclose(fp);
m_ConfigFailed = false;
} }

View File

@ -9,13 +9,11 @@ public:
void Load(); void Load();
float GetDelay() const { return m_DelayExec; }; float GetDelay() const { return m_DelayExec; };
bool IsConfigLoaded() const { return !m_ConfigFailed; };
private: private:
void ResetValues(); void ResetValues();
private: private:
bool m_ConfigFailed;
char m_PathDir[MAX_PATH_LENGTH]; char m_PathDir[MAX_PATH_LENGTH];
// settings // settings

View File

@ -4,6 +4,7 @@ DLL_FUNCTIONS *g_pFunctionTable;
extern void ServerDeactivate_Post(); extern void ServerDeactivate_Post();
extern void ClientPutInServer_Post(edict_t *pEntity); extern void ClientPutInServer_Post(edict_t *pEntity);
extern void StartFrame();
static DLL_FUNCTIONS gFunctionTable = static DLL_FUNCTIONS gFunctionTable =
{ {

View File

@ -1,6 +1,7 @@
#include "precompiled.h" #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); 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) if (RehldsApi_Init() != RETURN_LOAD)
return false; 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 // register function from ReHLDS API
g_RehldsApi->GetHookchains()->SV_DropClient()->registerHook(&SV_DropClient); g_RehldsApi->GetHookchains()->SV_DropClient()->registerHook(&SV_DropClient);
g_RehldsApi->GetHookchains()->SV_ActivateServer()->registerHook(&SV_ActivateServer); g_RehldsApi->GetHookchains()->SV_ActivateServer()->registerHook(&SV_ActivateServer);
@ -16,48 +48,46 @@ bool OnMetaAttach()
SV_AddResource = g_RehldsApi->GetFuncs()->SV_AddResource; SV_AddResource = g_RehldsApi->GetFuncs()->SV_AddResource;
// initialize resource config // go to attach
Resource.Init(); return true;
/*
// 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()*/);
} }
void OnMetaDetach() void OnMetaDetach()
{ {
g_RehldsApi->GetHookchains()->SV_DropClient()->unregisterHook(&SV_DropClient); cvar_t *pcv_mp_consistency = g_engfuncs.pfnCVarGetPointer("mp_consistency");
g_RehldsApi->GetHookchains()->SV_ActivateServer()->unregisterHook(&SV_ActivateServer);
g_RehldsApi->GetHookchains()->SV_CheckConsistencyResponce()->unregisterHook(&SV_CheckConsistencyResponce); // 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 // clear
Exec.Clear(); Exec.Clear();
/*Task.Clear();*/
Resource.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() void ServerDeactivate_Post()
{ {
// clear // clear
Exec.Clear(); Exec.Clear();
/*Task.Clear();*/
Resource.Clear(); Resource.Clear();
SET_META_RESULT(MRES_IGNORED); 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 // clear buffer cmdexec the client when was disconnected up to perform cmdexec
Exec.Clear(pClient); Exec.Clear(pClient);
/*Task.Clear(pClient);*/
// call next hook // call next hook
chain->callNext(pClient, crash, string); chain->callNext(pClient, crash, string);
} }
/*
void StartFrame()
{
Task.StartFrame();
SET_META_RESULT(MRES_IGNORED);
}
*/
void SV_ActivateServer(IRehldsHook_SV_ActivateServer *chain, int runPhysics) void SV_ActivateServer(IRehldsHook_SV_ActivateServer *chain, int runPhysics)
{ {
/*Config.Load();*/
Resource.LoadResources(); Resource.LoadResources();
chain->callNext(runPhysics); chain->callNext(runPhysics);
@ -92,17 +112,6 @@ void SV_ActivateServer(IRehldsHook_SV_ActivateServer *chain, int runPhysics)
Resource.CreateResourceList(); 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) void ClientPutInServer_Post(edict_t *pEntity)
{ {
int nIndex = ENTINDEX(pEntity) - 1; int nIndex = ENTINDEX(pEntity) - 1;
@ -112,17 +121,8 @@ void ClientPutInServer_Post(edict_t *pEntity)
IGameClient *pClient = g_RehldsApi->GetServerStatic()->GetClient(nIndex); IGameClient *pClient = g_RehldsApi->GetServerStatic()->GetClient(nIndex);
/* // client is connected to putinserver, go execute cmd out buffer
float time = Config.GetDelay(); Exec.CommandExecute(pClient);
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);
*/
SET_META_RESULT(MRES_IGNORED); SET_META_RESULT(MRES_IGNORED);
} }

View File

@ -9,8 +9,8 @@ plugin_info_t Plugin_info =
"s1lent", "s1lent",
"http://www.dedicated-server.ru/", "http://www.dedicated-server.ru/",
"Rechecker", "Rechecker",
PT_STARTUP, PT_CHANGELEVEL,
PT_NEVER, PT_ANYTIME,
}; };
meta_globals_t *gpMetaGlobals; meta_globals_t *gpMetaGlobals;

View File

@ -14,6 +14,7 @@
#include <vector> #include <vector>
#include <cstring> // strrchr #include <cstring> // strrchr
#include <time.h> // time, localtime etc
#include <extdll.h> #include <extdll.h>
#include <enginecallback.h> // ALERT() #include <enginecallback.h> // ALERT()

View File

@ -3,6 +3,9 @@
CResourceFile Resource; CResourceFile Resource;
std::vector<const char *> StringsCache; std::vector<const char *> StringsCache;
cvar_t cv_rch_log = { "rch_log", "0", 0, 0.0f, NULL };
cvar_t *pcv_rch_log = NULL;
void CResourceFile::CreateResourceList() void CResourceFile::CreateResourceList()
{ {
int nConsistency = g_RehldsServerData->GetConsistencyNum(); int nConsistency = g_RehldsServerData->GetConsistencyNum();
@ -23,9 +26,7 @@ void CResourceFile::CreateResourceList()
break; break;
} }
#ifdef _DEBUG Log(__FUNCTION__ " -> file: (%s), cmdexc: (%s), hash: (%x)", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetFileHash());
UTIL_Printf(__FUNCTION__ " :: (%s)(%s)(%x)\n", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetFileHash());
#endif // _DEBUG
SV_AddResource(t_decal, pRes->GetFileName(), 0, RES_CHECKFILE, m_DecalsNum++); SV_AddResource(t_decal, pRes->GetFileName(), 0, RES_CHECKFILE, m_DecalsNum++);
nConsistency++; nConsistency++;
} }
@ -46,6 +47,60 @@ void CResourceFile::Clear()
ClearStringsCache(); 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() void CResourceFile::Init()
{ {
char *pos; char *pos;
@ -61,8 +116,16 @@ void CResourceFile::Init()
*(pos + 1) = '\0'; *(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 // resources.ini
snprintf(m_PathDir, sizeof(m_PathDir), "%s" FILE_INI_RESOURCES, path); 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) inline uint8 hexbyte(uint8 *hex)
@ -114,6 +177,27 @@ bool IsFileHasExtension(char *psrc)
return strchr(pch, '/') == NULL; 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() void CResourceFile::LoadResources()
{ {
char *pos; char *pos;
@ -131,7 +215,6 @@ void CResourceFile::LoadResources()
if (fp == NULL) if (fp == NULL)
{ {
m_ConfigFailed = true;
UTIL_Printf(__FUNCTION__ ": can't find path to " FILE_INI_RESOURCES "\n"); UTIL_Printf(__FUNCTION__ ": can't find path to " FILE_INI_RESOURCES "\n");
return; return;
} }
@ -271,7 +354,7 @@ void CResourceFile::LoadResources()
} }
fclose(fp); fclose(fp);
m_ConfigFailed = false; LogPrepare();
} }
const char *CResourceFile::GetNextToken(char **pbuf) const char *CResourceFile::GetNextToken(char **pbuf)
@ -367,7 +450,7 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource
m_PrevHash = hash; m_PrevHash = hash;
return true; return true;
} }
// strange thing // strange thing
// if this happened when missing all the files from client // if this happened when missing all the files from client
if (!m_PrevHash) if (!m_PrevHash)
@ -433,12 +516,13 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource
if (typeFind != FLAG_TYPE_NONE) if (typeFind != FLAG_TYPE_NONE)
{ {
// push exec cmd // TODO: what is?
Exec.AddElement(pSenderClient, pRes, hash); if (hash != 0x0)
{
#ifdef _DEBUG // push exec cmd
UTIL_Printf(" -> filename: (%s), exphash: (%x), resphash: (%x), typeFind: (%d), prevhash: (%x)\n", pRes->GetFileName(), pRes->GetFileHash(), hash, typeFind, m_PrevHash); Exec.AddElement(pSenderClient, pRes, hash);
#endif // _DEBUG }
Log(" -> file: (%s), exphash: (%x), got: (%x), typeFind: (%d), prevhash: (%x), (%s)", pRes->GetFileName(), pRes->GetFileHash(), hash, typeFind, m_PrevHash, pSenderClient->GetName());
} }
bHandled = true; bHandled = true;

View File

@ -58,12 +58,13 @@ public:
void Clear(); void Clear();
void LoadResources(); void LoadResources();
void CreateResourceList(); void CreateResourceList();
void Log(const char *fmt, ...);
bool FileConsistencyResponce(IGameClient *pSenderClient, resource_t *resource, uint32 hash); bool FileConsistencyResponce(IGameClient *pSenderClient, resource_t *resource, uint32 hash);
bool IsConfigLoaded() const { return !m_ConfigFailed; };
private: private:
void AddElement(char *filename, char *cmdExec, flag_type_resources flag, uint32 hash, int line); void AddElement(char *filename, char *cmdExec, flag_type_resources flag, uint32 hash, int line);
void LogPrepare();
// parse // parse
const char *GetNextToken(char **pbuf); const char *GetNextToken(char **pbuf);
@ -74,8 +75,12 @@ private:
int m_DecalsNum; int m_DecalsNum;
uint32 m_PrevHash; uint32 m_PrevHash;
bool m_ConfigFailed;
char m_PathDir[MAX_PATH_LENGTH]; char m_PathDir[MAX_PATH_LENGTH];
// log data
char m_LogFilePath[MAX_PATH_LENGTH];
char m_LogDate[64];
}; };
extern CResourceFile Resource; extern CResourceFile Resource;