From 92c1719982834d010de5f3c087f29dd824f47ef7 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Wed, 11 Nov 2015 21:48:48 +0300 Subject: [PATCH] Fixes --- src/cmdexec.cpp | 6 ++-- src/cmdexec.h | 6 ++-- src/main.cpp | 2 +- src/precompiled.h | 1 + src/resource.cpp | 88 +++++++++++++++++++++++++---------------------- src/resource.h | 22 ++++++------ 6 files changed, 66 insertions(+), 59 deletions(-) diff --git a/src/cmdexec.cpp b/src/cmdexec.cpp index 707c055..ef2984a 100644 --- a/src/cmdexec.cpp +++ b/src/cmdexec.cpp @@ -6,7 +6,7 @@ CBufExec::CBufExec(IGameClient *pClient, CResourceBuffer *pResource, uint32 resp { m_pClient = pClient; m_pResource = pResource; - m_Hash = responseHash; + m_ClientHash = responseHash; } CBufExec::~CBufExec() @@ -92,7 +92,7 @@ void CExecManager::CommandExecute(IGameClient *pClient) break; } - char *cmdExec = GetExecCmdPrepare(pClient, pRes, pExec->GetHash()); + char *cmdExec = GetExecCmdPrepare(pClient, pRes, pExec->GetClientHash()); // erase all cmdexec because have flag is break if (bBreak) { @@ -113,7 +113,7 @@ void CExecManager::CommandExecute(IGameClient *pClient) else iter++; - bBreak = (pRes->GetFlagsFile() & FLAG_TYPE_BREAK) == FLAG_TYPE_BREAK; + bBreak = (pRes->GetFileFlags() & FLAG_TYPE_BREAK) == FLAG_TYPE_BREAK; } } diff --git a/src/cmdexec.h b/src/cmdexec.h index 0f207d3..8541d20 100644 --- a/src/cmdexec.h +++ b/src/cmdexec.h @@ -8,12 +8,12 @@ public: IGameClient *GetGameClient() const { return m_pClient; }; CResourceBuffer *GetResource() const { return m_pResource; }; - uint32 GetHash() const { return m_Hash; }; + uint32 GetClientHash() const { return m_ClientHash; }; private: IGameClient *m_pClient; CResourceBuffer *m_pResource; - uint32 m_Hash; + uint32 m_ClientHash; }; class CExecManager @@ -24,7 +24,7 @@ public: void Clear(IGameClient *pClient = NULL); private: - typedef std::vector CBufExecList; + typedef std::list CBufExecList; CBufExecList m_execList; }; diff --git a/src/main.cpp b/src/main.cpp index 8f57013..ce26af4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ bool OnMetaAttach() g_RehldsApi->GetHookchains()->SV_CheckConsistencyResponce()->registerHook(&SV_CheckConsistencyResponce); g_RehldsApi->GetHookchains()->SV_DropClient()->registerHook(&SV_DropClient); - SV_AddResource = reinterpret_cast(g_RehldsApi->GetFuncs()->SV_AddResource); + SV_AddResource = g_RehldsApi->GetFuncs()->SV_AddResource; // initialize resource config Resource.Init(); diff --git a/src/precompiled.h b/src/precompiled.h index 8f710e8..912ba14 100644 --- a/src/precompiled.h +++ b/src/precompiled.h @@ -8,6 +8,7 @@ #undef __FUNCTION__ #endif // _WIN32 +#include #include #include // strrchr diff --git a/src/resource.cpp b/src/resource.cpp index f3aa46b..ac38b57 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -1,6 +1,7 @@ #include "precompiled.h" CResourceFile Resource; +std::vector StringsCache; void CResourceFile::Add() { @@ -12,10 +13,10 @@ void CResourceFile::Add() // prevent duplicate of filenames // check if filename is been marked so do not add the resource again - if (pRes->GetMark() != true) { + if (!pRes->IsDuplicate()) { //#ifdef _DEBUG if (CVAR_GET_FLOAT("developer") == 1.0f) { - UTIL_Printf(__FUNCTION__ " :: (%s)(%s)(%x)\n", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetHashFile()); + UTIL_Printf(__FUNCTION__ " :: (%s)(%s)(%x)\n", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetFileHash()); } //#endif // _DEBUG SV_AddResource(t_decal, pRes->GetFileName(), 0, RES_CHECKFILE, 4095); @@ -32,6 +33,8 @@ void CResourceFile::Clear() // clear resources m_resourceList.clear(); + + ClearStringsCache(); } void CResourceFile::Init() @@ -50,7 +53,7 @@ void CResourceFile::Init() *(pos + 1) = '\0'; // resources.ini - snprintf(m_PathDir, sizeof(m_PathDir) - 1, "%s" FILE_INI_RESOURCES, path); + snprintf(m_PathDir, sizeof(m_PathDir), "%s" FILE_INI_RESOURCES, path); } inline uint8 hexbyte(uint8 *hex) @@ -86,7 +89,7 @@ bool IsValidFilename(char *psrc, char &pchar) { return true; } -bool FileIsExtension(char *psrc) { +bool IsFileHasExtension(char *psrc) { // find the extension filename char *pch = strrchr(psrc, '.'); @@ -120,12 +123,12 @@ void CResourceFile::Load() if (fp == NULL) { - m_ConfigFailed = false; + m_ConfigFailed = true; UTIL_Printf(__FUNCTION__ ": can't find path to " FILE_INI_RESOURCES "\n"); return; } - while (!feof(fp) && fgets(buffer, sizeof(buffer) - 1, fp)) + while (!feof(fp) && fgets(buffer, sizeof(buffer), fp)) { pos = buffer; @@ -222,7 +225,7 @@ void CResourceFile::Load() continue; } - else if (!FileIsExtension(filename)) { + else if (!IsFileHasExtension(filename)) { UTIL_Printf(__FUNCTION__ ": Failed to load \"" FILE_INI_RESOURCES "\"; filename has no extension on line %d\n", cline); continue; } @@ -251,6 +254,7 @@ void CResourceFile::Load() } fclose(fp); + m_ConfigFailed = false; } const char *CResourceFile::GetNextToken(char **pbuf) @@ -347,27 +351,26 @@ void TrimSpace(char *pbuf) void CResourceFile::AddElement(char *filename, char *cmdExec, int flags, uint32 hash) { - m_resourceList.push_back(new CResourceBuffer(filename, cmdExec, flags, hash)); + auto nRes = new CResourceBuffer(filename, cmdExec, flags, hash); // to mark files which are not required to add to the resource again for (auto iter = m_resourceList.cbegin(); iter != m_resourceList.cend(); ++iter) { CResourceBuffer *pRes = (*iter); - // do not check the last element - if (pRes == m_resourceList.back()) - continue; - if (_stricmp(pRes->GetFileName(), filename) == 0) { - // set be marked - pRes->SetMark(); + // resource name already registered + nRes->SetDuplicate(); + break; } } + + m_resourceList.push_back(nRes); } bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource_t *resource, uint32 hash) { - bool bCheckeFiles = false; + bool bHandled = false; find_type_e typeFind = FIND_TYPE_NONE; std::vector tempResourceList; @@ -378,9 +381,9 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource if (strcmp(resource->szFileName, pRes->GetFileName()) != 0) continue; - bCheckeFiles = true; + bHandled = true; - int flags = pRes->GetFlagsFile(); + int flags = pRes->GetFileFlags(); if (flags & FLAG_TYPE_IGNORE) { @@ -391,7 +394,7 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource } else if (flags & FLAG_TYPE_EXISTS) { - if (m_PrevHash != hash && pRes->GetHashFile() == hash) { + if (m_PrevHash != hash && pRes->GetFileHash() == hash) { typeFind = FIND_TYPE_ON_HASH; } } @@ -414,7 +417,7 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource continue; } - if (pTemp->GetHashFile() == hash) { + if (pTemp->GetFileHash() == hash) { typeFind = FIND_TYPE_NONE; break; } @@ -431,39 +434,42 @@ bool CResourceFile::FileConsistencyResponce(IGameClient *pSenderClient, resource //#ifdef _DEBUG if (CVAR_GET_FLOAT("developer") == 1.0f) { - UTIL_Printf(" -> filename: (%s), cmdexec: (%s), hash: (%x)\n", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetHashFile()); + UTIL_Printf(" -> filename: (%s), cmdexec: (%s), hash: (%x)\n", pRes->GetFileName(), pRes->GetCmdExec(), pRes->GetFileHash()); } //#endif // _DEBUG } } m_PrevHash = hash; - return !bCheckeFiles; + return !bHandled; +} + +const char* DuplicateString(const char* str) +{ + for (auto it = StringsCache.begin(), end = StringsCache.end(); it != end; ++it) + { + if (!strcmp(*it, str)) + return *it; + } + + const char* s = strcpy(new char[strlen(str) + 1], str); + StringsCache.push_back(s); + return s; +} + +void ClearStringsCache() +{ + for (auto it = StringsCache.begin(), end = StringsCache.end(); it != end; ++it) + delete *it; } CResourceBuffer::CResourceBuffer(char *filename, char *cmdExec, int flags, uint32 hash) { - int lenFile = strlen(filename); - int lenExec = strlen(cmdExec); + m_FileName = DuplicateString(filename); + m_CmdExec = DuplicateString(cmdExec); - m_FileName = new char[lenFile + 1]; - m_CmdExec = new char[lenExec + 1]; - - strncpy(m_FileName, filename, lenFile); - strncpy(m_CmdExec, cmdExec, lenExec); - - m_FileName[lenFile] = '\0'; - m_CmdExec[lenExec] = '\0'; - - m_Mark = false; + m_Duplicate = false; m_Flags = flags; - m_HashFile = hash; -} - -CResourceBuffer::~CResourceBuffer() -{ - // free me - delete[] m_FileName, - m_CmdExec; + m_FileHash = hash; } diff --git a/src/resource.h b/src/resource.h index 7457ddf..b48c8da 100644 --- a/src/resource.h +++ b/src/resource.h @@ -39,28 +39,27 @@ class CResourceBuffer { public: CResourceBuffer(char *filename, char *cmdExec, int flags, uint32 hash); - ~CResourceBuffer(); - uint32 GetHashFile() const { return m_HashFile; }; - int GetFlagsFile() const { return m_Flags; }; + uint32 GetFileHash() const { return m_FileHash; }; + int GetFileFlags() const { return m_Flags; }; - char *GetFileName() const { return m_FileName; }; - char *GetCmdExec() const { return m_CmdExec; }; + const char *GetFileName() const { return m_FileName; }; + const char *GetCmdExec() const { return m_CmdExec; }; bool GetBreak() const { return m_Break; }; void SetBreak() { m_Break = true; }; - bool GetMark() const { return m_Mark; }; - void SetMark() { m_Mark = true; }; + bool IsDuplicate() const { return m_Duplicate; }; + void SetDuplicate() { m_Duplicate = true; }; private: - uint32 m_HashFile; + uint32 m_FileHash; int m_Flags; - char *m_FileName; - char *m_CmdExec; - bool m_Mark; + const char *m_FileName; + const char *m_CmdExec; + bool m_Duplicate; bool m_Break; }; @@ -93,3 +92,4 @@ private: extern CResourceFile Resource; +void ClearStringsCache();