From 1a02a20fa68d6de90322d64c6a431ac31da548cd Mon Sep 17 00:00:00 2001 From: def Date: Mon, 20 Dec 2021 15:00:21 +0100 Subject: [PATCH] Add IStorage::FormatTmpPath static method Not sure I prefer it, but now it's done. Any opinions? --- src/engine/client/client.cpp | 4 ++-- src/engine/client/updater.cpp | 4 ++-- src/engine/shared/config.cpp | 6 ++---- src/engine/shared/storage.cpp | 6 ++++++ src/engine/storage.h | 1 + src/game/client/components/skins.cpp | 5 +++-- src/game/server/gamecontext.cpp | 11 +++++------ src/test/test.cpp | 5 +++-- 8 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 4d20299e6..3abe402fa 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -338,7 +338,7 @@ CClient::CClient() : m_MapDetailsSha256 = SHA256_ZEROED; m_MapDetailsCrc = 0; - str_format(m_aDDNetInfoTmp, sizeof(m_aDDNetInfoTmp), DDNET_INFO ".%d.tmp", pid()); + IStorage::FormatTmpPath(m_aDDNetInfoTmp, sizeof(m_aDDNetInfoTmp), DDNET_INFO); m_pDDNetInfoTask = NULL; m_aNews[0] = '\0'; m_aMapDownloadUrl[0] = '\0'; @@ -1232,7 +1232,7 @@ static void FormatMapDownloadFilename(const char *pName, const SHA256_DIGEST *pS char aSuffix[32]; if(Temp) { - str_format(aSuffix, sizeof(aSuffix), ".%d.tmp", pid()); + IStorage::FormatTmpPath(aSuffix, sizeof(aSuffix), ""); } else { diff --git a/src/engine/client/updater.cpp b/src/engine/client/updater.cpp index 4709efa9a..a99196598 100644 --- a/src/engine/client/updater.cpp +++ b/src/engine/client/updater.cpp @@ -93,8 +93,8 @@ CUpdater::CUpdater() m_Percent = 0; m_Lock = lock_create(); - str_format(m_aClientExecTmp, sizeof(m_aClientExecTmp), CLIENT_EXEC ".%d.tmp", pid()); - str_format(m_aServerExecTmp, sizeof(m_aServerExecTmp), SERVER_EXEC ".%d.tmp", pid()); + IStorage::FormatTmpPath(m_aClientExecTmp, sizeof(m_aClientExecTmp), CLIENT_EXEC); + IStorage::FormatTmpPath(m_aServerExecTmp, sizeof(m_aServerExecTmp), SERVER_EXEC); } void CUpdater::Init() diff --git a/src/engine/shared/config.cpp b/src/engine/shared/config.cpp index abebe0481..f00b196ee 100644 --- a/src/engine/shared/config.cpp +++ b/src/engine/shared/config.cpp @@ -44,10 +44,8 @@ bool CConfigManager::Save() if(!m_pStorage || !g_Config.m_ClSaveSettings) return true; - char aConfigFileTmp[64]; - str_format(aConfigFileTmp, sizeof(aConfigFileTmp), CONFIG_FILE ".%d.tmp", pid()); - - m_ConfigFile = m_pStorage->OpenFile(aConfigFileTmp, IOFLAG_WRITE, IStorage::TYPE_SAVE); + char aConfigFileTmp[IO_MAX_PATH_LENGTH]; + m_ConfigFile = m_pStorage->OpenFile(IStorage::FormatTmpPath(aConfigFileTmp, sizeof(aConfigFileTmp), CONFIG_FILE), IOFLAG_WRITE, IStorage::TYPE_SAVE); if(!m_ConfigFile) { diff --git a/src/engine/shared/storage.cpp b/src/engine/shared/storage.cpp index 110bbe68d..8de83e6a5 100644 --- a/src/engine/shared/storage.cpp +++ b/src/engine/shared/storage.cpp @@ -594,6 +594,12 @@ void IStorage::StripPathAndExtension(const char *pFilename, char *pBuffer, int B str_copy(pBuffer, pExtractedName, Length); } +const char *IStorage::FormatTmpPath(char *aBuf, unsigned BufSize, const char *pPath) +{ + str_format(aBuf, BufSize, "%s.%d.tmp", pPath, pid()); + return aBuf; +} + IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments) { return CStorage::Create(pApplicationName, StorageType, NumArgs, ppArguments); diff --git a/src/engine/storage.h b/src/engine/storage.h index 1feb88a36..23bc7c308 100644 --- a/src/engine/storage.h +++ b/src/engine/storage.h @@ -39,6 +39,7 @@ public: virtual const char *GetBinaryPath(const char *pFilename, char *pBuffer, unsigned BufferSize) = 0; static void StripPathAndExtension(const char *pFilename, char *pBuffer, int BufferSize); + static const char *FormatTmpPath(char *aBuf, unsigned BufSize, const char *pPath); }; extern IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments); diff --git a/src/game/client/components/skins.cpp b/src/game/client/components/skins.cpp index 46e85c7e4..9eeda2925 100644 --- a/src/game/client/components/skins.cpp +++ b/src/game/client/components/skins.cpp @@ -421,11 +421,12 @@ int CSkins::FindImpl(const char *pName) CDownloadSkin Skin; str_copy(Skin.m_aName, pName, sizeof(Skin.m_aName)); - char aUrl[256]; + char aUrl[IO_MAX_PATH_LENGTH]; char aEscapedName[256]; EscapeUrl(aEscapedName, sizeof(aEscapedName), pName); str_format(aUrl, sizeof(aUrl), "%s%s.png", g_Config.m_ClSkinDownloadUrl, aEscapedName); - str_format(Skin.m_aPath, sizeof(Skin.m_aPath), "downloadedskins/%s.%d.tmp", pName, pid()); + char aBuf[IO_MAX_PATH_LENGTH]; + str_format(Skin.m_aPath, sizeof(Skin.m_aPath), "downloadedskins/%s", IStorage::FormatTmpPath(aBuf, sizeof(aBuf), pName)); Skin.m_pTask = std::make_shared(this, Storage(), aUrl, Skin.m_aPath, IStorage::TYPE_SAVE, CTimeout{0, 0, 0}, HTTPLOG::NONE); m_pClient->Engine()->AddJob(Skin.m_pTask); m_aDownloadSkins.add(Skin); diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index d9f746c2b..d69d863bc 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -3125,7 +3125,7 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/) m_Layers.Init(Kernel()); m_Collision.Init(&m_Layers); - char aMapName[128]; + char aMapName[IO_MAX_PATH_LENGTH]; int MapSize; SHA256_DIGEST MapSha256; int MapCrc; @@ -3421,10 +3421,8 @@ void CGameContext::DeleteTempfile() void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) { - char aConfig[128]; - char aTemp[128]; + char aConfig[IO_MAX_PATH_LENGTH]; str_format(aConfig, sizeof(aConfig), "maps/%s.cfg", g_Config.m_SvMap); - str_format(aTemp, sizeof(aTemp), "%s.%d.tmp", pNewMapName, pid()); IOHANDLE File = Storage()->OpenFile(aConfig, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL); if(!File) @@ -3539,7 +3537,8 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) dbg_msg("mapchange", "imported settings"); free(pSettings); Reader.Close(); - Writer.OpenFile(Storage(), aTemp); + char aTemp[IO_MAX_PATH_LENGTH]; + Writer.OpenFile(Storage(), IStorage::FormatTmpPath(aTemp, sizeof(aTemp), pNewMapName)); Writer.Finish(); str_copy(pNewMapName, aTemp, MapNameSize); @@ -3603,7 +3602,7 @@ void CGameContext::LoadMapSettings() break; } - char aBuf[128]; + char aBuf[IO_MAX_PATH_LENGTH]; str_format(aBuf, sizeof(aBuf), "maps/%s.map.cfg", g_Config.m_SvMap); Console()->ExecuteFile(aBuf, IConsole::CLIENT_ID_NO_GAME); } diff --git a/src/test/test.cpp b/src/test/test.cpp index 66d93190d..c47d6f5ef 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -10,8 +10,9 @@ CTestInfo::CTestInfo() { const ::testing::TestInfo *pTestInfo = ::testing::UnitTest::GetInstance()->current_test_info(); - str_format(m_aFilename, sizeof(m_aFilename), "%s.%s-%d.tmp", - pTestInfo->test_case_name(), pTestInfo->name(), pid()); + char aBuf[IO_MAX_PATH_LENGTH]; + str_format(aBuf, sizeof(aBuf), "%s.%s", pTestInfo->test_case_name(), pTestInfo->name()); + IStorage::FormatTmpPath(m_aFilename, sizeof(m_aFilename), aBuf); } IStorage *CTestInfo::CreateTestStorage()