From 06635403c0dfbcc73f976e6bb032b2f86fd5df5e Mon Sep 17 00:00:00 2001 From: Tracer <43095317+TracerDS@users.noreply.github.com> Date: Thu, 29 Feb 2024 16:51:34 +0100 Subject: [PATCH 1/3] Added `onResourceStateChange` event Syntax: `onResourceStateChange ( resource changedResource, string oldState, string newState )` --- Server/mods/deathmatch/logic/CGame.cpp | 1 + Server/mods/deathmatch/logic/CResource.cpp | 37 ++ Server/mods/deathmatch/logic/CResource.h | 2 + .../deathmatch/logic/CResourceManager.cpp | 358 ++++++++---------- 4 files changed, 200 insertions(+), 198 deletions(-) diff --git a/Server/mods/deathmatch/logic/CGame.cpp b/Server/mods/deathmatch/logic/CGame.cpp index c2c8485562..4ed400fb1b 100644 --- a/Server/mods/deathmatch/logic/CGame.cpp +++ b/Server/mods/deathmatch/logic/CGame.cpp @@ -1533,6 +1533,7 @@ void CGame::AddBuiltInEvents() m_Events.AddEvent("onResourcePreStart", "resource", NULL, false); m_Events.AddEvent("onResourceStart", "resource", NULL, false); m_Events.AddEvent("onResourceStop", "resource, deleted", NULL, false); + m_Events.AddEvent("onResourceStateChange", "resource, oldState, newState", nullptr, false); m_Events.AddEvent("onResourceLoadStateChange", "resource, oldState, newState", NULL, false); // Blip events diff --git a/Server/mods/deathmatch/logic/CResource.cpp b/Server/mods/deathmatch/logic/CResource.cpp index 500edf254c..72a85902f6 100644 --- a/Server/mods/deathmatch/logic/CResource.cpp +++ b/Server/mods/deathmatch/logic/CResource.cpp @@ -338,10 +338,13 @@ bool CResource::Unload() m_pNodeSettings = nullptr; } + OnResourceStateChange("unloaded"); + m_strResourceZip = ""; m_strResourceCachePath = ""; m_strResourceDirectoryPath = ""; m_eState = EResourceState::None; + return true; } @@ -741,6 +744,8 @@ bool CResource::Start(std::list* pDependents, bool bManualStart, con if (m_bDestroyed) return false; + OnResourceStateChange("starting"); + m_eState = EResourceState::Starting; CLuaArguments PreStartArguments; @@ -974,6 +979,8 @@ bool CResource::Start(std::list* pDependents, bool bManualStart, con AddDependent(pDependent); } + OnResourceStateChange("running"); + m_eState = EResourceState::Running; // Call the onResourceStart event. If it returns false, cancel this script again @@ -1016,6 +1023,33 @@ bool CResource::Start(std::list* pDependents, bool bManualStart, con return true; } +void CResource::OnResourceStateChange(const char* state) noexcept +{ + CLuaArguments stateArgs; + stateArgs.PushResource(this); + switch (m_eState) + { + case EResourceState::Loaded: // When resource is stopped + stateArgs.PushString("loaded"); + break; + case EResourceState::Running: // When resource is running + stateArgs.PushString("running"); + break; + case EResourceState::Starting: // When resource is starting + stateArgs.PushString("starting"); + break; + case EResourceState::Stopping: // When resource is stopping + stateArgs.PushString("stopping"); + break; + case EResourceState::None: // When resource is not loaded + default: + stateArgs.PushString("unloaded"); + break; + } + stateArgs.PushString(state); + m_pResourceElement->CallEvent("onResourceStateChange", stateArgs); +} + bool CResource::Stop(bool bManualStop) { if (m_eState == EResourceState::Loaded) @@ -1027,6 +1061,8 @@ bool CResource::Stop(bool bManualStop) if (m_bStartedManually && !bManualStop) return false; + OnResourceStateChange("stopping"); + m_eState = EResourceState::Stopping; m_pResourceManager->RemoveMinClientRequirement(this); m_pResourceManager->RemoveSyncMapElementDataOption(this); @@ -1113,6 +1149,7 @@ bool CResource::Stop(bool bManualStop) // Broadcast the packet to joined players g_pGame->GetPlayerManager()->BroadcastOnlyJoined(removePacket); + OnResourceStateChange("loaded"); m_eState = EResourceState::Loaded; return true; } diff --git a/Server/mods/deathmatch/logic/CResource.h b/Server/mods/deathmatch/logic/CResource.h index 30b6698148..0b0c40347c 100644 --- a/Server/mods/deathmatch/logic/CResource.h +++ b/Server/mods/deathmatch/logic/CResource.h @@ -268,6 +268,8 @@ class CResource : public EHS void OnPlayerJoin(CPlayer& Player); void SendNoClientCacheScripts(CPlayer* pPlayer = nullptr); + void OnResourceStateChange(const char* state) noexcept; + CDummy* GetResourceRootElement() { return m_pResourceElement; } const CDummy* GetResourceRootElement() const noexcept { return m_pResourceElement; } diff --git a/Server/mods/deathmatch/logic/CResourceManager.cpp b/Server/mods/deathmatch/logic/CResourceManager.cpp index b3b2d2f9c1..e7dfc7c966 100644 --- a/Server/mods/deathmatch/logic/CResourceManager.cpp +++ b/Server/mods/deathmatch/logic/CResourceManager.cpp @@ -79,18 +79,15 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour resourcesPathList.push_back("resources"); SString strModPath = g_pServerInterface->GetModManager()->GetModPath(); - for (uint i = 0; i < resourcesPathList.size(); i++) + for (const auto& strResourcesRelPath : resourcesPathList) { // Enumerate all files and directories - SString strResourcesRelPath = resourcesPathList[i]; SString strResourcesAbsPath = PathJoin(strModPath, strResourcesRelPath, "/"); std::vector itemList = FindFiles(strResourcesAbsPath, true, true); // Check each item - for (uint i = 0; i < itemList.size(); i++) + for (auto& strName : itemList) { - SString strName = itemList[i]; - // Ignore items that start with a dot if (strName[0] == '.') continue; @@ -143,19 +140,17 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour assert(!pDup->bIsDir); *pDup = newInfo; } + continue; } - else - { - // Don't load resource if there are duplicates on different paths - pDup->bPathIssue = true; - pDup->strAbsPathDup = newInfo.strAbsPath; - } - } - else - { - // No duplicate found - MapSet(resInfoMap, strName, newInfo); + + // Don't load resource if there are duplicates on different paths + pDup->bPathIssue = true; + pDup->strAbsPathDup = newInfo.strAbsPath; + continue; } + + // No duplicate found + MapSet(resInfoMap, strName, newInfo); } } @@ -166,25 +161,24 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour marker.Set("UnloadRemoved"); // Process potential resource list - for (std::map::const_iterator iter = resInfoMap.begin(); iter != resInfoMap.end(); ++iter) + for (const auto& [first, info] : resInfoMap) { - const SResInfo& info = iter->second; if (!strJustThisResource.empty() && strJustThisResource != info.strName) continue; - if (!info.bPathIssue) - { - CResource* pResource = GetResource(info.strName); + if (info.bPathIssue) + continue; - if (bRefreshAll || !pResource || !pResource->CheckIfStartable()) - { - if (g_pServerInterface->IsRequestingExit()) - return false; + CResource* pResource = GetResource(info.strName); - // Add the resource - Load(!info.bIsDir, info.strAbsPath, info.strName); - } - } + if (!bRefreshAll && pResource && pResource->CheckIfStartable()) + continue; + + if (g_pServerInterface->IsRequestingExit()) + return false; + + // Add the resource + Load(!info.bIsDir, info.strAbsPath, info.strName); } marker.Set("AddNew"); @@ -194,9 +188,8 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour marker.Set("CheckDep"); // Print important errors - for (std::map::const_iterator iter = resInfoMap.begin(); iter != resInfoMap.end(); ++iter) + for (const auto& [first,info] : resInfoMap) { - const SResInfo& info = iter->second; if (!strJustThisResource.empty() && strJustThisResource != info.strName) continue; @@ -205,15 +198,14 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour CLogger::ErrorPrintf("Not processing resource '%s' as it has duplicates on different paths:\n", *info.strName); CLogger::LogPrintfNoStamp(" Path #1: \"%s\"\n", *PathJoin(PathMakeRelative(strModPath, info.strAbsPath), info.strName)); CLogger::LogPrintfNoStamp(" Path #2: \"%s\"\n", *PathJoin(PathMakeRelative(strModPath, info.strAbsPathDup), info.strName)); + continue; } - else - { - CResource* pResource = GetResource(info.strName); - if (pResource && !pResource->CheckIfStartable()) - { - CLogger::ErrorPrintf("Problem with resource: %s; %s\n", *info.strName, pResource->GetFailureReason().c_str()); - } - } + + CResource* pResource = GetResource(info.strName); + if (!pResource || pResource->CheckIfStartable()) + continue; + + CLogger::ErrorPrintf("Problem with resource: %s; %s\n", *info.strName, pResource->GetFailureReason().c_str()); } marker.Set("CheckErrors"); @@ -246,12 +238,11 @@ void CResourceManager::UpgradeResources(CResource* pResource) if (pResource) { pResource->ApplyUpgradeModifications(); + return; } - else - { - for (list::const_iterator iter = m_resources.begin(); iter != m_resources.end(); iter++) - (*iter)->ApplyUpgradeModifications(); - } + + for (const auto& pResource : m_resources) + pResource->ApplyUpgradeModifications(); } void CResourceManager::CheckResources(CResource* pResource) @@ -260,12 +251,11 @@ void CResourceManager::CheckResources(CResource* pResource) if (pResource) { pResource->LogUpgradeWarnings(); + return; } - else - { - for (list::const_iterator iter = m_resources.begin(); iter != m_resources.end(); iter++) - (*iter)->LogUpgradeWarnings(); - } + + for (const auto& pResource : m_resources) + pResource->LogUpgradeWarnings(); } void CResourceManager::OnResourceLoadStateChange(CResource* pResource, const char* szOldState, const char* szNewState) const @@ -300,16 +290,11 @@ void CResourceManager::CheckResourceDependencies() { m_uiResourceLoadedCount = 0; m_uiResourceFailedCount = 0; - list::const_iterator iter = m_resources.begin(); - for (; iter != m_resources.end(); iter++) + for (const auto& pResource : m_resources) { - (*iter)->LinkToIncludedResources(); - } + pResource->LinkToIncludedResources(); - iter = m_resources.begin(); - for (; iter != m_resources.end(); iter++) - { - if ((*iter)->CheckIfStartable()) + if (pResource->CheckIfStartable()) m_uiResourceLoadedCount++; else m_uiResourceFailedCount++; @@ -318,37 +303,34 @@ void CResourceManager::CheckResourceDependencies() void CResourceManager::ListResourcesLoaded(const SString& strListType) { - unsigned int uiCount = 0; - unsigned int uiFailedCount = 0; - unsigned int uiRunningCount = 0; + std::uint32_t uiCount = 0; + std::uint32_t uiFailedCount = 0; + std::uint32_t uiRunningCount = 0; CLogger::LogPrintf("== Resource list ==\n"); - list::const_iterator iter = m_resources.begin(); - for (; iter != m_resources.end(); iter++) - { - CResource* res = (*iter); - if (res->IsLoaded()) - { - if (res->IsActive()) - { - if (strListType == "running" || strListType == "all") - CLogger::LogPrintf("%-20.20s RUNNING (%d dependents)\n", res->GetName().c_str(), res->GetDependentCount()); - uiRunningCount++; - } - else - { - if (strListType == "stopped" || strListType == "all") - CLogger::LogPrintf("%-20.20s STOPPED (%d files)\n", res->GetName().c_str(), res->GetFileCount()); - } - uiCount++; - } - else + for (const auto& res : m_resources) + { + if (!res->IsLoaded()) { if (strListType == "failed" || strListType == "all") CLogger::LogPrintf("%-20.20s FAILED (see info command for reason)\n", res->GetName().c_str()); uiFailedCount++; + continue; } + if (res->IsActive()) + { + if (strListType == "running" || strListType == "all") + CLogger::LogPrintf("%-20.20s RUNNING (%d dependents)\n", res->GetName().c_str(), res->GetDependentCount()); + + uiRunningCount++; + } + else + { + if (strListType == "stopped" || strListType == "all") + CLogger::LogPrintf("%-20.20s STOPPED (%d files)\n", res->GetName().c_str(), res->GetFileCount()); + } + uiCount++; } CLogger::LogPrintf("Resources: %d loaded, %d failed, %d running\n", uiCount, uiFailedCount, uiRunningCount); } @@ -360,7 +342,7 @@ void CResourceManager::UnloadRemovedResources() // the 'm_resources' member variable while we iterate over it std::list resourcesToDelete; - for (CResource* pResource : m_resources) + for (const auto& pResource : m_resources) { if (!pResource->HasGoneAway()) continue; @@ -438,19 +420,18 @@ CResource* CResourceManager::Load(bool bIsZipped, const char* szAbsPath, const c if (!pLoadedResource->IsLoaded()) { CLogger::LogPrintf("Loading of resource '%s' failed\n", szResourceName); + return pLoadedResource; } - else + + // Don't log new resources during server startup + if (g_pGame->IsServerFullyUp()) { - // Don't log new resources during server startup - if (g_pGame->IsServerFullyUp()) - { - if (!bPreviouslyLoaded) - OnResourceLoadStateChange(pLoadedResource, nullptr, "loaded"); - else - OnResourceLoadStateChange(pLoadedResource, "loaded", "loaded"); + if (!bPreviouslyLoaded) + OnResourceLoadStateChange(pLoadedResource, nullptr, "loaded"); + else + OnResourceLoadStateChange(pLoadedResource, "loaded", "loaded"); - CLogger::LogPrintf("New resource '%s' loaded\n", pLoadedResource->GetName().c_str()); - } + CLogger::LogPrintf("New resource '%s' loaded\n", pLoadedResource->GetName().c_str()); } return pLoadedResource; @@ -458,15 +439,13 @@ CResource* CResourceManager::Load(bool bIsZipped, const char* szAbsPath, const c CResource* CResourceManager::GetResource(const char* szResourceName) { - CResource** ppResource = MapFind(m_NameResourceMap, SStringX(szResourceName).ToUpper()); - if (ppResource) - return *ppResource; - return NULL; + auto pResource = MapFind(m_NameResourceMap, SStringX(szResourceName).ToUpper()); + return pResource ? *pResource : nullptr; } CResource* CResourceManager::GetResourceFromScriptID(uint uiScriptID) { - CResource* pResource = (CResource*)CIdArray::FindEntry(uiScriptID, EIdClass::RESOURCE); + auto pResource = (CResource*)CIdArray::FindEntry(uiScriptID, EIdClass::RESOURCE); dassert(!pResource || ListContains(m_resources, pResource)); return pResource; } @@ -488,12 +467,12 @@ unsigned short CResourceManager::GenerateID() return m_usNextNetId; // Find an unused ID - for (unsigned short i = 0; i < 0xFFFE; i++) + for (auto i = 0; i < 0xFFFE; i++) { bool bFound = false; - for (list::const_iterator iter = m_resources.begin(); iter != m_resources.end(); iter++) + for (const auto& pResource : m_resources) { - if ((*iter)->GetNetID() == m_usNextNetId) + if (pResource->GetNetID() == m_usNextNetId) { bFound = true; break; @@ -514,32 +493,30 @@ unsigned short CResourceManager::GenerateID() CResource* CResourceManager::GetResourceFromNetID(unsigned short usNetID) { - CResource* pResource = MapFindRef(m_NetIdResourceMap, usNetID); + auto pResource = MapFindRef(m_NetIdResourceMap, usNetID); if (pResource) { assert(pResource->GetNetID() == usNetID); return pResource; } - list::const_iterator iter = m_resources.begin(); - for (; iter != m_resources.end(); iter++) + for (const auto& pResource : m_resources) { - if ((*iter)->GetNetID() == usNetID) + if (pResource->GetNetID() == usNetID) { assert(0); // Should be in map - return (*iter); + return pResource; } } - return NULL; + return nullptr; } void CResourceManager::OnPlayerJoin(CPlayer& Player) { // Loop through our started resources so they start in the correct order clientside - list::iterator iter = CResource::m_StartedResources.begin(); - for (; iter != CResource::m_StartedResources.end(); iter++) + for (const auto& pResource : CResource::m_StartedResources) { - (*iter)->OnPlayerJoin(Player); + pResource->OnPlayerJoin(Player); } } @@ -611,51 +588,44 @@ CResource* CResourceManager::GetResourceFromLuaState(lua_State* luaVM) luaVM = lua_getmainstate(luaVM); // Use lookup map - CResource** ppResource = MapFind(m_LuaStateResourceMap, luaVM); - if (ppResource) - { - CResource* pResource = *ppResource; - CLuaMain* pLuaMain = pResource->GetVirtualMachine(); - if (pLuaMain) - { - assert(luaVM == pLuaMain->GetVirtualMachine()); - return pResource; - } - } - return NULL; + auto ppResource = MapFind(m_LuaStateResourceMap, luaVM); + if (!ppResource) + return nullptr; + + CResource* pResource = *ppResource; + CLuaMain* pLuaMain = pResource->GetVirtualMachine(); + if (!pLuaMain) + return nullptr; + + assert(luaVM == pLuaMain->GetVirtualMachine()); + return pResource; } SString CResourceManager::GetResourceName(lua_State* luaVM) { CResource* pResource = GetResourceFromLuaState(luaVM); - if (pResource) - return pResource->GetName(); - return ""; + return pResource ? pResource->GetName() : ""; } bool CResourceManager::IsAResourceElement(CElement* pElement) { - list::const_iterator iter = m_resources.begin(); - for (; iter != m_resources.end(); iter++) + for (const auto& pResource : m_resources) { - CResource* pResource = *iter; - if (pResource->IsActive()) + if (!pResource->IsActive()) + continue; + + if (pResource->GetResourceRootElement() == pElement || pResource->GetDynamicElementRoot() == pElement) + return true; + + std::list::iterator fiter = pResource->IterBegin(); + for (; fiter != pResource->IterEnd(); fiter++) { - if (pResource->GetResourceRootElement() == pElement || pResource->GetDynamicElementRoot() == pElement) + if ((*fiter)->GetType() != CResourceFile::RESOURCE_FILE_TYPE_MAP) + continue; + + CResourceMapItem* pMap = static_cast(*fiter); + if (pMap->GetMapRootElement() == pElement) return true; - else - { - list::iterator fiter = pResource->IterBegin(); - for (; fiter != pResource->IterEnd(); fiter++) - { - if ((*fiter)->GetType() == CResourceFile::RESOURCE_FILE_TYPE_MAP) - { - CResourceMapItem* pMap = static_cast(*fiter); - if (pMap->GetMapRootElement() == pElement) - return true; - } - } - } } } return false; @@ -704,7 +674,7 @@ bool CResourceManager::Reload(CResource* pResource) return false; } - // Call the onResourceStateChange event + // Call the onResourceLoadStateChange event OnResourceLoadStateChange(pResource, "loaded", "loaded"); // Success @@ -717,19 +687,17 @@ bool CResourceManager::StopAllResources() CLogger::LogPrint("Stopping resources..."); CLogger::ProgressDotsBegin(); - list::const_iterator iter = m_resources.begin(); - for (; iter != m_resources.end(); iter++) + for (const auto& pResource : m_resources) { - CResource* pResource = *iter; - if (pResource->IsActive()) - { - if (pResource->IsPersistent()) - pResource->SetPersistent(false); + if (!pResource->IsActive()) + continue; - pResource->Stop(true); + if (pResource->IsPersistent()) + pResource->SetPersistent(false); - CLogger::ProgressDotsUpdate(); - } + pResource->Stop(true); + + CLogger::ProgressDotsUpdate(); } CLogger::ProgressDotsEnd(); @@ -878,17 +846,17 @@ CResource* CResourceManager::CreateResource(const SString& strNewResourceName, c SString strRelResourceLocation = PathJoin(strNewOrganizationalPath, strNewResourceName); // Does the resource name already exist? - if (GetResource(strNewResourceName) != NULL) + if (GetResource(strNewResourceName)) { strOutStatus = SString("CreateResource - Could not create '%s' as the resource already exists\n", *strNewResourceName); - return NULL; + return nullptr; } // Is it a valid path? if (!IsValidFilePath(strRelResourceLocation) || !IsValidOrganizationPath(strNewOrganizationalPath)) { strOutStatus = SString("CreateResource - Could not create '%s' as the provided path is invalid", *strNewResourceName); - return NULL; + return nullptr; } // Create destination folder @@ -900,24 +868,21 @@ CResource* CResourceManager::CreateResource(const SString& strNewResourceName, c if (!pXML) { strOutStatus = SString("CreateResource - Could not create '%s'\n", *strMetaPath); - return NULL; + return nullptr; } - else + // If we got the rootnode created, write the XML + pXML->CreateRootNode("meta"); + if (!pXML->Write()) { - // If we got the rootnode created, write the XML - pXML->CreateRootNode("meta"); - if (!pXML->Write()) - { - delete pXML; - strOutStatus = SString("CreateResource - Could not save '%s'\n", *strMetaPath); - return NULL; - } - - // Delete the XML delete pXML; - pXML = NULL; + strOutStatus = SString("CreateResource - Could not save '%s'\n", *strMetaPath); + return nullptr; } + // Delete the XML + delete pXML; + pXML = nullptr; + // Add the resource and load it CResource* pResource = new CResource(this, false, strDstAbsPath, strNewResourceName); pResource->SetNetID(GenerateID()); @@ -950,28 +915,28 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!pSourceResource->IsLoaded()) { strOutStatus = SString("Could not copy '%s' as the resource is not loaded\n", *strSrcResourceName); - return NULL; + return nullptr; } // Does the resource name already exist? - if (GetResource(strNewResourceName) != NULL) + if (GetResource(strNewResourceName)) { strOutStatus = SString("Could not copy '%s' as the resource '%s' already exists\n", *strSrcResourceName, *strNewResourceName); - return NULL; + return nullptr; } // Does the destination directory already exist? if (FileExists(strDstResourceLocation) || DirectoryExists(strDstResourceLocation)) { strOutStatus = SString("Could not copy '%s' as the file/directory '%s' already exists\n", *strSrcResourceName, *strNewResourceName); - return NULL; + return nullptr; } // Is it a valid path? if (!IsValidFilePath(strRelResourceLocation) || !IsValidOrganizationPath(strDstOrganizationalPath)) { strOutStatus = SString("Could not copy '%s' as the provided path is invalid", *strSrcResourceName); - return NULL; + return nullptr; } // Clear temp directory @@ -995,7 +960,7 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!FileCopy(strSrcFileName, strDstFileName)) { strOutStatus = SString("Could not copy '%s' to '%s'\n", *strSrcFileName, *strDstFileName); - return NULL; + return nullptr; } } @@ -1013,7 +978,7 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!FileCopy(strSrcFileName, strDstFileName)) { strOutStatus = SString("Could not copy '%s' to '%s'\n", *strSrcFileName, *strDstFileName); - return NULL; + return nullptr; } } @@ -1022,7 +987,7 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!FileRename(strDstTemp, strDstResourceLocation)) { strOutStatus = SString("Could not rename '%s' to '%s'\n", *strDstTemp, *strDstResourceLocation); - return NULL; + return nullptr; } // Add the resource and load it @@ -1062,28 +1027,28 @@ CResource* CResourceManager::RenameResource(CResource* pSourceResource, const SS if (pSourceResource->IsActive()) { strOutStatus = SString("Could not rename '%s' as the resource is running\n", *strSrcResourceName); - return NULL; + return nullptr; } // Is the source resource loaded if (!pSourceResource->IsLoaded()) { strOutStatus = SString("Could not rename '%s' as the resource is not loaded\n", *strSrcResourceName); - return NULL; + return nullptr; } // Does the destination already exist? if (FileExists(strDstResourceLocation) || DirectoryExists(strDstResourceLocation)) { strOutStatus = SString("Could not rename to '%s' as the file/directory name already exists\n", *strNewResourceName); - return NULL; + return nullptr; } // Is it a valid path? if (!IsValidFilePath(strRelResourceLocation) || !IsValidOrganizationPath(strDstOrganizationalPath)) { strOutStatus = SString("Could not rename to '%s' as the provided path is invalid", *strNewResourceName); - return NULL; + return nullptr; } UnloadAndDelete(pSourceResource); @@ -1277,9 +1242,9 @@ void CResourceManager::ReevaluateMinClientRequirement() { // Calc highest requirement m_strMinClientRequirement = ""; - for (auto iter = m_MinClientRequirementMap.begin(); iter != m_MinClientRequirementMap.end(); ++iter) - if (iter->second > m_strMinClientRequirement) - m_strMinClientRequirement = iter->second; + for (const auto& [first,requirement] : m_MinClientRequirementMap) + if (requirement > m_strMinClientRequirement) + m_strMinClientRequirement = requirement; g_pGame->CalculateMinClientRequirement(); } @@ -1324,15 +1289,15 @@ void CResourceManager::RemoveSyncMapElementDataOption(CResource* pResource) void CResourceManager::ReevaluateSyncMapElementDataOption() { bool bSyncMapElementData = true; - for (CFastHashMap::iterator iter = m_SyncMapElementDataOptionMap.begin(); iter != m_SyncMapElementDataOptionMap.end(); ++iter) + for (const auto& pDataOption : m_SyncMapElementDataOptionMap) { - if (iter->second) + if (pDataOption.second) { bSyncMapElementData = true; // Any 'true' will stop the set break; } - else - bSyncMapElementData = false; // Need at least one 'false' to set + + bSyncMapElementData = false; // Need at least one 'false' to set } // Apply @@ -1365,11 +1330,10 @@ void CResourceManager::LoadBlockedFileReasons() if (result->nRows > 0 && result->nColumns >= 2) { m_BlockedFileReasonMap.clear(); - for (CRegistryResultIterator iter = result->begin(); iter != result->end(); ++iter) + for (const auto& row : *result.operator->()) // thank CAutoRefedPointer for that { - const CRegistryResultRow& row = *iter; - SString strFileHash = (const char*)row[0].pVal; - SString strReason = (const char*)row[1].pVal; + SString strFileHash = (const char*)row[0].pVal; + SString strReason = (const char*)row[1].pVal; MapSet(m_BlockedFileReasonMap, strFileHash, strReason); } } @@ -1395,10 +1359,11 @@ void CResourceManager::SaveBlockedFileReasons() pDatabaseManager->Execf(hDbConnection, "DROP TABLE " BLOCKED_DB_TABLE_NAME); pDatabaseManager->Execf(hDbConnection, "CREATE TABLE IF NOT EXISTS " BLOCKED_DB_TABLE_NAME " (`hash` TEXT,`reason` TEXT)"); - for (std::map::iterator iter = m_BlockedFileReasonMap.begin(); iter != m_BlockedFileReasonMap.end(); iter++) + for (const auto& [hash,reason] : m_BlockedFileReasonMap) { - pDatabaseManager->Execf(hDbConnection, "INSERT INTO " BLOCKED_DB_TABLE_NAME " (`hash`,`reason`) VALUES (?,?)", SQLITE_TEXT, *iter->first, SQLITE_TEXT, - *iter->second); + pDatabaseManager->Execf(hDbConnection, + "INSERT INTO " BLOCKED_DB_TABLE_NAME " (`hash`,`reason`) VALUES (?,?)", + SQLITE_TEXT, *hash, SQLITE_TEXT, *reason); } pDatabaseManager->Disconnect(hDbConnection); } @@ -1442,8 +1407,5 @@ void CResourceManager::AddBlockedFileReason(const SString& strFileHash, const SS SString CResourceManager::GetBlockedFileReason(const SString& strFileHash) { SString* pstrReason = MapFind(m_BlockedFileReasonMap, strFileHash); - if (pstrReason) - return *pstrReason; - - return ""; + return pstrReason ? *pstrReason : ""; } From f18eb703e51b832e7788af8e5eb14beda4396d0b Mon Sep 17 00:00:00 2001 From: Tracer <43095317+TracerDS@users.noreply.github.com> Date: Thu, 29 Feb 2024 17:32:18 +0100 Subject: [PATCH 2/3] Moved condition check to top --- Server/mods/deathmatch/logic/CResource.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Server/mods/deathmatch/logic/CResource.cpp b/Server/mods/deathmatch/logic/CResource.cpp index 72a85902f6..ccc7bb69a4 100644 --- a/Server/mods/deathmatch/logic/CResource.cpp +++ b/Server/mods/deathmatch/logic/CResource.cpp @@ -1025,6 +1025,9 @@ bool CResource::Start(std::list* pDependents, bool bManualStart, con void CResource::OnResourceStateChange(const char* state) noexcept { + if (!m_pResourceElement) + return; + CLuaArguments stateArgs; stateArgs.PushResource(this); switch (m_eState) @@ -1047,6 +1050,7 @@ void CResource::OnResourceStateChange(const char* state) noexcept break; } stateArgs.PushString(state); + m_pResourceElement->CallEvent("onResourceStateChange", stateArgs); } From a175ee5073eebe8c08228ef8c3240f2c5082692f Mon Sep 17 00:00:00 2001 From: Tracer <43095317+TracerDS@users.noreply.github.com> Date: Thu, 29 Feb 2024 17:39:24 +0100 Subject: [PATCH 3/3] Update Refactoring is a separate issue that will be discussed in another PR --- .../deathmatch/logic/CResourceManager.cpp | 358 ++++++++++-------- 1 file changed, 198 insertions(+), 160 deletions(-) diff --git a/Server/mods/deathmatch/logic/CResourceManager.cpp b/Server/mods/deathmatch/logic/CResourceManager.cpp index e7dfc7c966..b3b2d2f9c1 100644 --- a/Server/mods/deathmatch/logic/CResourceManager.cpp +++ b/Server/mods/deathmatch/logic/CResourceManager.cpp @@ -79,15 +79,18 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour resourcesPathList.push_back("resources"); SString strModPath = g_pServerInterface->GetModManager()->GetModPath(); - for (const auto& strResourcesRelPath : resourcesPathList) + for (uint i = 0; i < resourcesPathList.size(); i++) { // Enumerate all files and directories + SString strResourcesRelPath = resourcesPathList[i]; SString strResourcesAbsPath = PathJoin(strModPath, strResourcesRelPath, "/"); std::vector itemList = FindFiles(strResourcesAbsPath, true, true); // Check each item - for (auto& strName : itemList) + for (uint i = 0; i < itemList.size(); i++) { + SString strName = itemList[i]; + // Ignore items that start with a dot if (strName[0] == '.') continue; @@ -140,17 +143,19 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour assert(!pDup->bIsDir); *pDup = newInfo; } - continue; } - - // Don't load resource if there are duplicates on different paths - pDup->bPathIssue = true; - pDup->strAbsPathDup = newInfo.strAbsPath; - continue; + else + { + // Don't load resource if there are duplicates on different paths + pDup->bPathIssue = true; + pDup->strAbsPathDup = newInfo.strAbsPath; + } + } + else + { + // No duplicate found + MapSet(resInfoMap, strName, newInfo); } - - // No duplicate found - MapSet(resInfoMap, strName, newInfo); } } @@ -161,24 +166,25 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour marker.Set("UnloadRemoved"); // Process potential resource list - for (const auto& [first, info] : resInfoMap) + for (std::map::const_iterator iter = resInfoMap.begin(); iter != resInfoMap.end(); ++iter) { + const SResInfo& info = iter->second; if (!strJustThisResource.empty() && strJustThisResource != info.strName) continue; - if (info.bPathIssue) - continue; - - CResource* pResource = GetResource(info.strName); - - if (!bRefreshAll && pResource && pResource->CheckIfStartable()) - continue; + if (!info.bPathIssue) + { + CResource* pResource = GetResource(info.strName); - if (g_pServerInterface->IsRequestingExit()) - return false; + if (bRefreshAll || !pResource || !pResource->CheckIfStartable()) + { + if (g_pServerInterface->IsRequestingExit()) + return false; - // Add the resource - Load(!info.bIsDir, info.strAbsPath, info.strName); + // Add the resource + Load(!info.bIsDir, info.strAbsPath, info.strName); + } + } } marker.Set("AddNew"); @@ -188,8 +194,9 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour marker.Set("CheckDep"); // Print important errors - for (const auto& [first,info] : resInfoMap) + for (std::map::const_iterator iter = resInfoMap.begin(); iter != resInfoMap.end(); ++iter) { + const SResInfo& info = iter->second; if (!strJustThisResource.empty() && strJustThisResource != info.strName) continue; @@ -198,14 +205,15 @@ bool CResourceManager::Refresh(bool bRefreshAll, const SString strJustThisResour CLogger::ErrorPrintf("Not processing resource '%s' as it has duplicates on different paths:\n", *info.strName); CLogger::LogPrintfNoStamp(" Path #1: \"%s\"\n", *PathJoin(PathMakeRelative(strModPath, info.strAbsPath), info.strName)); CLogger::LogPrintfNoStamp(" Path #2: \"%s\"\n", *PathJoin(PathMakeRelative(strModPath, info.strAbsPathDup), info.strName)); - continue; } - - CResource* pResource = GetResource(info.strName); - if (!pResource || pResource->CheckIfStartable()) - continue; - - CLogger::ErrorPrintf("Problem with resource: %s; %s\n", *info.strName, pResource->GetFailureReason().c_str()); + else + { + CResource* pResource = GetResource(info.strName); + if (pResource && !pResource->CheckIfStartable()) + { + CLogger::ErrorPrintf("Problem with resource: %s; %s\n", *info.strName, pResource->GetFailureReason().c_str()); + } + } } marker.Set("CheckErrors"); @@ -238,11 +246,12 @@ void CResourceManager::UpgradeResources(CResource* pResource) if (pResource) { pResource->ApplyUpgradeModifications(); - return; } - - for (const auto& pResource : m_resources) - pResource->ApplyUpgradeModifications(); + else + { + for (list::const_iterator iter = m_resources.begin(); iter != m_resources.end(); iter++) + (*iter)->ApplyUpgradeModifications(); + } } void CResourceManager::CheckResources(CResource* pResource) @@ -251,11 +260,12 @@ void CResourceManager::CheckResources(CResource* pResource) if (pResource) { pResource->LogUpgradeWarnings(); - return; } - - for (const auto& pResource : m_resources) - pResource->LogUpgradeWarnings(); + else + { + for (list::const_iterator iter = m_resources.begin(); iter != m_resources.end(); iter++) + (*iter)->LogUpgradeWarnings(); + } } void CResourceManager::OnResourceLoadStateChange(CResource* pResource, const char* szOldState, const char* szNewState) const @@ -290,11 +300,16 @@ void CResourceManager::CheckResourceDependencies() { m_uiResourceLoadedCount = 0; m_uiResourceFailedCount = 0; - for (const auto& pResource : m_resources) + list::const_iterator iter = m_resources.begin(); + for (; iter != m_resources.end(); iter++) { - pResource->LinkToIncludedResources(); + (*iter)->LinkToIncludedResources(); + } - if (pResource->CheckIfStartable()) + iter = m_resources.begin(); + for (; iter != m_resources.end(); iter++) + { + if ((*iter)->CheckIfStartable()) m_uiResourceLoadedCount++; else m_uiResourceFailedCount++; @@ -303,34 +318,37 @@ void CResourceManager::CheckResourceDependencies() void CResourceManager::ListResourcesLoaded(const SString& strListType) { - std::uint32_t uiCount = 0; - std::uint32_t uiFailedCount = 0; - std::uint32_t uiRunningCount = 0; + unsigned int uiCount = 0; + unsigned int uiFailedCount = 0; + unsigned int uiRunningCount = 0; CLogger::LogPrintf("== Resource list ==\n"); - - for (const auto& res : m_resources) + list::const_iterator iter = m_resources.begin(); + for (; iter != m_resources.end(); iter++) { - if (!res->IsLoaded()) + CResource* res = (*iter); + if (res->IsLoaded()) { - if (strListType == "failed" || strListType == "all") - CLogger::LogPrintf("%-20.20s FAILED (see info command for reason)\n", res->GetName().c_str()); - - uiFailedCount++; - continue; - } - if (res->IsActive()) - { - if (strListType == "running" || strListType == "all") - CLogger::LogPrintf("%-20.20s RUNNING (%d dependents)\n", res->GetName().c_str(), res->GetDependentCount()); + if (res->IsActive()) + { + if (strListType == "running" || strListType == "all") + CLogger::LogPrintf("%-20.20s RUNNING (%d dependents)\n", res->GetName().c_str(), res->GetDependentCount()); - uiRunningCount++; + uiRunningCount++; + } + else + { + if (strListType == "stopped" || strListType == "all") + CLogger::LogPrintf("%-20.20s STOPPED (%d files)\n", res->GetName().c_str(), res->GetFileCount()); + } + uiCount++; } else { - if (strListType == "stopped" || strListType == "all") - CLogger::LogPrintf("%-20.20s STOPPED (%d files)\n", res->GetName().c_str(), res->GetFileCount()); + if (strListType == "failed" || strListType == "all") + CLogger::LogPrintf("%-20.20s FAILED (see info command for reason)\n", res->GetName().c_str()); + + uiFailedCount++; } - uiCount++; } CLogger::LogPrintf("Resources: %d loaded, %d failed, %d running\n", uiCount, uiFailedCount, uiRunningCount); } @@ -342,7 +360,7 @@ void CResourceManager::UnloadRemovedResources() // the 'm_resources' member variable while we iterate over it std::list resourcesToDelete; - for (const auto& pResource : m_resources) + for (CResource* pResource : m_resources) { if (!pResource->HasGoneAway()) continue; @@ -420,18 +438,19 @@ CResource* CResourceManager::Load(bool bIsZipped, const char* szAbsPath, const c if (!pLoadedResource->IsLoaded()) { CLogger::LogPrintf("Loading of resource '%s' failed\n", szResourceName); - return pLoadedResource; } - - // Don't log new resources during server startup - if (g_pGame->IsServerFullyUp()) + else { - if (!bPreviouslyLoaded) - OnResourceLoadStateChange(pLoadedResource, nullptr, "loaded"); - else - OnResourceLoadStateChange(pLoadedResource, "loaded", "loaded"); + // Don't log new resources during server startup + if (g_pGame->IsServerFullyUp()) + { + if (!bPreviouslyLoaded) + OnResourceLoadStateChange(pLoadedResource, nullptr, "loaded"); + else + OnResourceLoadStateChange(pLoadedResource, "loaded", "loaded"); - CLogger::LogPrintf("New resource '%s' loaded\n", pLoadedResource->GetName().c_str()); + CLogger::LogPrintf("New resource '%s' loaded\n", pLoadedResource->GetName().c_str()); + } } return pLoadedResource; @@ -439,13 +458,15 @@ CResource* CResourceManager::Load(bool bIsZipped, const char* szAbsPath, const c CResource* CResourceManager::GetResource(const char* szResourceName) { - auto pResource = MapFind(m_NameResourceMap, SStringX(szResourceName).ToUpper()); - return pResource ? *pResource : nullptr; + CResource** ppResource = MapFind(m_NameResourceMap, SStringX(szResourceName).ToUpper()); + if (ppResource) + return *ppResource; + return NULL; } CResource* CResourceManager::GetResourceFromScriptID(uint uiScriptID) { - auto pResource = (CResource*)CIdArray::FindEntry(uiScriptID, EIdClass::RESOURCE); + CResource* pResource = (CResource*)CIdArray::FindEntry(uiScriptID, EIdClass::RESOURCE); dassert(!pResource || ListContains(m_resources, pResource)); return pResource; } @@ -467,12 +488,12 @@ unsigned short CResourceManager::GenerateID() return m_usNextNetId; // Find an unused ID - for (auto i = 0; i < 0xFFFE; i++) + for (unsigned short i = 0; i < 0xFFFE; i++) { bool bFound = false; - for (const auto& pResource : m_resources) + for (list::const_iterator iter = m_resources.begin(); iter != m_resources.end(); iter++) { - if (pResource->GetNetID() == m_usNextNetId) + if ((*iter)->GetNetID() == m_usNextNetId) { bFound = true; break; @@ -493,30 +514,32 @@ unsigned short CResourceManager::GenerateID() CResource* CResourceManager::GetResourceFromNetID(unsigned short usNetID) { - auto pResource = MapFindRef(m_NetIdResourceMap, usNetID); + CResource* pResource = MapFindRef(m_NetIdResourceMap, usNetID); if (pResource) { assert(pResource->GetNetID() == usNetID); return pResource; } - for (const auto& pResource : m_resources) + list::const_iterator iter = m_resources.begin(); + for (; iter != m_resources.end(); iter++) { - if (pResource->GetNetID() == usNetID) + if ((*iter)->GetNetID() == usNetID) { assert(0); // Should be in map - return pResource; + return (*iter); } } - return nullptr; + return NULL; } void CResourceManager::OnPlayerJoin(CPlayer& Player) { // Loop through our started resources so they start in the correct order clientside - for (const auto& pResource : CResource::m_StartedResources) + list::iterator iter = CResource::m_StartedResources.begin(); + for (; iter != CResource::m_StartedResources.end(); iter++) { - pResource->OnPlayerJoin(Player); + (*iter)->OnPlayerJoin(Player); } } @@ -588,44 +611,51 @@ CResource* CResourceManager::GetResourceFromLuaState(lua_State* luaVM) luaVM = lua_getmainstate(luaVM); // Use lookup map - auto ppResource = MapFind(m_LuaStateResourceMap, luaVM); - if (!ppResource) - return nullptr; - - CResource* pResource = *ppResource; - CLuaMain* pLuaMain = pResource->GetVirtualMachine(); - if (!pLuaMain) - return nullptr; - - assert(luaVM == pLuaMain->GetVirtualMachine()); - return pResource; + CResource** ppResource = MapFind(m_LuaStateResourceMap, luaVM); + if (ppResource) + { + CResource* pResource = *ppResource; + CLuaMain* pLuaMain = pResource->GetVirtualMachine(); + if (pLuaMain) + { + assert(luaVM == pLuaMain->GetVirtualMachine()); + return pResource; + } + } + return NULL; } SString CResourceManager::GetResourceName(lua_State* luaVM) { CResource* pResource = GetResourceFromLuaState(luaVM); - return pResource ? pResource->GetName() : ""; + if (pResource) + return pResource->GetName(); + return ""; } bool CResourceManager::IsAResourceElement(CElement* pElement) { - for (const auto& pResource : m_resources) + list::const_iterator iter = m_resources.begin(); + for (; iter != m_resources.end(); iter++) { - if (!pResource->IsActive()) - continue; - - if (pResource->GetResourceRootElement() == pElement || pResource->GetDynamicElementRoot() == pElement) - return true; - - std::list::iterator fiter = pResource->IterBegin(); - for (; fiter != pResource->IterEnd(); fiter++) + CResource* pResource = *iter; + if (pResource->IsActive()) { - if ((*fiter)->GetType() != CResourceFile::RESOURCE_FILE_TYPE_MAP) - continue; - - CResourceMapItem* pMap = static_cast(*fiter); - if (pMap->GetMapRootElement() == pElement) + if (pResource->GetResourceRootElement() == pElement || pResource->GetDynamicElementRoot() == pElement) return true; + else + { + list::iterator fiter = pResource->IterBegin(); + for (; fiter != pResource->IterEnd(); fiter++) + { + if ((*fiter)->GetType() == CResourceFile::RESOURCE_FILE_TYPE_MAP) + { + CResourceMapItem* pMap = static_cast(*fiter); + if (pMap->GetMapRootElement() == pElement) + return true; + } + } + } } } return false; @@ -674,7 +704,7 @@ bool CResourceManager::Reload(CResource* pResource) return false; } - // Call the onResourceLoadStateChange event + // Call the onResourceStateChange event OnResourceLoadStateChange(pResource, "loaded", "loaded"); // Success @@ -687,17 +717,19 @@ bool CResourceManager::StopAllResources() CLogger::LogPrint("Stopping resources..."); CLogger::ProgressDotsBegin(); - for (const auto& pResource : m_resources) + list::const_iterator iter = m_resources.begin(); + for (; iter != m_resources.end(); iter++) { - if (!pResource->IsActive()) - continue; - - if (pResource->IsPersistent()) - pResource->SetPersistent(false); + CResource* pResource = *iter; + if (pResource->IsActive()) + { + if (pResource->IsPersistent()) + pResource->SetPersistent(false); - pResource->Stop(true); + pResource->Stop(true); - CLogger::ProgressDotsUpdate(); + CLogger::ProgressDotsUpdate(); + } } CLogger::ProgressDotsEnd(); @@ -846,17 +878,17 @@ CResource* CResourceManager::CreateResource(const SString& strNewResourceName, c SString strRelResourceLocation = PathJoin(strNewOrganizationalPath, strNewResourceName); // Does the resource name already exist? - if (GetResource(strNewResourceName)) + if (GetResource(strNewResourceName) != NULL) { strOutStatus = SString("CreateResource - Could not create '%s' as the resource already exists\n", *strNewResourceName); - return nullptr; + return NULL; } // Is it a valid path? if (!IsValidFilePath(strRelResourceLocation) || !IsValidOrganizationPath(strNewOrganizationalPath)) { strOutStatus = SString("CreateResource - Could not create '%s' as the provided path is invalid", *strNewResourceName); - return nullptr; + return NULL; } // Create destination folder @@ -868,21 +900,24 @@ CResource* CResourceManager::CreateResource(const SString& strNewResourceName, c if (!pXML) { strOutStatus = SString("CreateResource - Could not create '%s'\n", *strMetaPath); - return nullptr; + return NULL; } - // If we got the rootnode created, write the XML - pXML->CreateRootNode("meta"); - if (!pXML->Write()) + else { + // If we got the rootnode created, write the XML + pXML->CreateRootNode("meta"); + if (!pXML->Write()) + { + delete pXML; + strOutStatus = SString("CreateResource - Could not save '%s'\n", *strMetaPath); + return NULL; + } + + // Delete the XML delete pXML; - strOutStatus = SString("CreateResource - Could not save '%s'\n", *strMetaPath); - return nullptr; + pXML = NULL; } - // Delete the XML - delete pXML; - pXML = nullptr; - // Add the resource and load it CResource* pResource = new CResource(this, false, strDstAbsPath, strNewResourceName); pResource->SetNetID(GenerateID()); @@ -915,28 +950,28 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!pSourceResource->IsLoaded()) { strOutStatus = SString("Could not copy '%s' as the resource is not loaded\n", *strSrcResourceName); - return nullptr; + return NULL; } // Does the resource name already exist? - if (GetResource(strNewResourceName)) + if (GetResource(strNewResourceName) != NULL) { strOutStatus = SString("Could not copy '%s' as the resource '%s' already exists\n", *strSrcResourceName, *strNewResourceName); - return nullptr; + return NULL; } // Does the destination directory already exist? if (FileExists(strDstResourceLocation) || DirectoryExists(strDstResourceLocation)) { strOutStatus = SString("Could not copy '%s' as the file/directory '%s' already exists\n", *strSrcResourceName, *strNewResourceName); - return nullptr; + return NULL; } // Is it a valid path? if (!IsValidFilePath(strRelResourceLocation) || !IsValidOrganizationPath(strDstOrganizationalPath)) { strOutStatus = SString("Could not copy '%s' as the provided path is invalid", *strSrcResourceName); - return nullptr; + return NULL; } // Clear temp directory @@ -960,7 +995,7 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!FileCopy(strSrcFileName, strDstFileName)) { strOutStatus = SString("Could not copy '%s' to '%s'\n", *strSrcFileName, *strDstFileName); - return nullptr; + return NULL; } } @@ -978,7 +1013,7 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!FileCopy(strSrcFileName, strDstFileName)) { strOutStatus = SString("Could not copy '%s' to '%s'\n", *strSrcFileName, *strDstFileName); - return nullptr; + return NULL; } } @@ -987,7 +1022,7 @@ CResource* CResourceManager::CopyResource(CResource* pSourceResource, const SStr if (!FileRename(strDstTemp, strDstResourceLocation)) { strOutStatus = SString("Could not rename '%s' to '%s'\n", *strDstTemp, *strDstResourceLocation); - return nullptr; + return NULL; } // Add the resource and load it @@ -1027,28 +1062,28 @@ CResource* CResourceManager::RenameResource(CResource* pSourceResource, const SS if (pSourceResource->IsActive()) { strOutStatus = SString("Could not rename '%s' as the resource is running\n", *strSrcResourceName); - return nullptr; + return NULL; } // Is the source resource loaded if (!pSourceResource->IsLoaded()) { strOutStatus = SString("Could not rename '%s' as the resource is not loaded\n", *strSrcResourceName); - return nullptr; + return NULL; } // Does the destination already exist? if (FileExists(strDstResourceLocation) || DirectoryExists(strDstResourceLocation)) { strOutStatus = SString("Could not rename to '%s' as the file/directory name already exists\n", *strNewResourceName); - return nullptr; + return NULL; } // Is it a valid path? if (!IsValidFilePath(strRelResourceLocation) || !IsValidOrganizationPath(strDstOrganizationalPath)) { strOutStatus = SString("Could not rename to '%s' as the provided path is invalid", *strNewResourceName); - return nullptr; + return NULL; } UnloadAndDelete(pSourceResource); @@ -1242,9 +1277,9 @@ void CResourceManager::ReevaluateMinClientRequirement() { // Calc highest requirement m_strMinClientRequirement = ""; - for (const auto& [first,requirement] : m_MinClientRequirementMap) - if (requirement > m_strMinClientRequirement) - m_strMinClientRequirement = requirement; + for (auto iter = m_MinClientRequirementMap.begin(); iter != m_MinClientRequirementMap.end(); ++iter) + if (iter->second > m_strMinClientRequirement) + m_strMinClientRequirement = iter->second; g_pGame->CalculateMinClientRequirement(); } @@ -1289,15 +1324,15 @@ void CResourceManager::RemoveSyncMapElementDataOption(CResource* pResource) void CResourceManager::ReevaluateSyncMapElementDataOption() { bool bSyncMapElementData = true; - for (const auto& pDataOption : m_SyncMapElementDataOptionMap) + for (CFastHashMap::iterator iter = m_SyncMapElementDataOptionMap.begin(); iter != m_SyncMapElementDataOptionMap.end(); ++iter) { - if (pDataOption.second) + if (iter->second) { bSyncMapElementData = true; // Any 'true' will stop the set break; } - - bSyncMapElementData = false; // Need at least one 'false' to set + else + bSyncMapElementData = false; // Need at least one 'false' to set } // Apply @@ -1330,10 +1365,11 @@ void CResourceManager::LoadBlockedFileReasons() if (result->nRows > 0 && result->nColumns >= 2) { m_BlockedFileReasonMap.clear(); - for (const auto& row : *result.operator->()) // thank CAutoRefedPointer for that + for (CRegistryResultIterator iter = result->begin(); iter != result->end(); ++iter) { - SString strFileHash = (const char*)row[0].pVal; - SString strReason = (const char*)row[1].pVal; + const CRegistryResultRow& row = *iter; + SString strFileHash = (const char*)row[0].pVal; + SString strReason = (const char*)row[1].pVal; MapSet(m_BlockedFileReasonMap, strFileHash, strReason); } } @@ -1359,11 +1395,10 @@ void CResourceManager::SaveBlockedFileReasons() pDatabaseManager->Execf(hDbConnection, "DROP TABLE " BLOCKED_DB_TABLE_NAME); pDatabaseManager->Execf(hDbConnection, "CREATE TABLE IF NOT EXISTS " BLOCKED_DB_TABLE_NAME " (`hash` TEXT,`reason` TEXT)"); - for (const auto& [hash,reason] : m_BlockedFileReasonMap) + for (std::map::iterator iter = m_BlockedFileReasonMap.begin(); iter != m_BlockedFileReasonMap.end(); iter++) { - pDatabaseManager->Execf(hDbConnection, - "INSERT INTO " BLOCKED_DB_TABLE_NAME " (`hash`,`reason`) VALUES (?,?)", - SQLITE_TEXT, *hash, SQLITE_TEXT, *reason); + pDatabaseManager->Execf(hDbConnection, "INSERT INTO " BLOCKED_DB_TABLE_NAME " (`hash`,`reason`) VALUES (?,?)", SQLITE_TEXT, *iter->first, SQLITE_TEXT, + *iter->second); } pDatabaseManager->Disconnect(hDbConnection); } @@ -1407,5 +1442,8 @@ void CResourceManager::AddBlockedFileReason(const SString& strFileHash, const SS SString CResourceManager::GetBlockedFileReason(const SString& strFileHash) { SString* pstrReason = MapFind(m_BlockedFileReasonMap, strFileHash); - return pstrReason ? *pstrReason : ""; + if (pstrReason) + return *pstrReason; + + return ""; }