Skip to content

Commit 3e83841

Browse files
Synchronize changes from 1.6 master branch [ci skip]
5c3b988 Revert "Refactor mtaserver.conf.template (#3857)" This reverts commit bc431d8. b4bfd3e Visual Studio Update 1307ddd Update client en_US pot
2 parents 5820085 + 5c3b988 commit 3e83841

File tree

6 files changed

+372
-73
lines changed

6 files changed

+372
-73
lines changed

Server/mods/deathmatch/logic/CMainConfig.cpp

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "CHTTPD.h"
2323
#include "CStaticFunctionDefinitions.h"
2424

25-
#define SETTINGS_TEMPLATE_PATH "mtaserver.conf.template"
25+
#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template"
2626

2727
extern CGame* g_pGame;
2828

@@ -864,90 +864,91 @@ bool CMainConfig::AddMissingSettings()
864864
if (!g_pGame->IsUsingMtaServerConf())
865865
return false;
866866

867-
const std::string templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), SETTINGS_TEMPLATE_PATH);
868-
if (!FileExists(templateFileName))
869-
return false;
867+
const SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), "mtaserver.conf.template");
870868

871-
std::unique_ptr<CXMLFile> templateFile(g_pServerInterface->GetXML()->CreateXML(templateFileName.c_str()));
872-
if (!templateFile || !templateFile->Parse())
873-
{
874-
CLogger::ErrorPrintf("Failed to parse template file: '%s'\n", templateFileName.c_str());
869+
if (!FileExists(templateFileName))
875870
return false;
876-
}
877871

878-
CXMLNode* templateRootNode = templateFile->GetRootNode();
872+
CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName);
873+
CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr;
879874
if (!templateRootNode)
880875
{
881-
CLogger::ErrorPrintf("Template file '%s' has no root node\n", templateFileName.c_str());
876+
CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName);
882877
return false;
883878
}
884879

885880
// Check that each item in the template also exists in the server config
886-
bool configChanged = false;
881+
bool hasConfigChanged = false;
887882
CXMLNode* previousNode = nullptr;
888-
889883
for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it)
890884
{
891885
CXMLNode* templateNode = *it;
892-
const std::string& templateNodeName = templateNode->GetTagName();
886+
SString templateNodeTagName = templateNode->GetTagName();
893887

894-
// Skip certain optional nodes
895-
if (templateNodeName == "resource" || templateNodeName == "module")
896-
continue;
897-
898888
// Find node with exact same attributes
899889
CXMLAttributes& templateAttributes = templateNode->GetAttributes();
900890
CXMLNode* foundNode = nullptr;
901891
for (auto it2 = m_pRootNode->ChildrenBegin(); it2 != m_pRootNode->ChildrenEnd(); ++it2)
902892
{
903893
CXMLNode* tempNode = *it2;
904-
if (tempNode->GetTagName() != templateNodeName)
894+
if (tempNode->GetTagName() != templateNodeTagName)
895+
{
905896
continue;
906-
897+
}
907898
CXMLAttributes& attributes = tempNode->GetAttributes();
908-
bool attributesMatch = true;
909-
899+
bool attributesMatch = true;
900+
910901
for (auto it3 = templateAttributes.ListBegin(); it3 != templateAttributes.ListEnd(); ++it3)
911902
{
912903
CXMLAttribute* templateAttribute = *it3;
913-
const SString& attrName = templateAttribute->GetName();
914-
915-
// Don't check value attribute which is intended to be different
916-
if (attrName == "value")
917-
continue;
918-
919-
const SString& attrValue = templateAttribute->GetValue();
920-
921-
CXMLAttribute* foundAttribute = attributes.Find(attrName);
922-
if (!foundAttribute || foundAttribute->GetValue() != attrValue)
904+
const SString& strKey = templateAttribute->GetName();
905+
const SString& strValue = templateAttribute->GetValue();
906+
907+
CXMLAttribute* foundAttribute = attributes.Find(strKey);
908+
if (!foundAttribute || foundAttribute->GetValue() != strValue)
923909
{
924910
attributesMatch = false;
925911
break;
926912
}
927913
}
928-
914+
929915
if (attributesMatch)
930916
{
931917
foundNode = tempNode;
932918
break;
933919
}
934920
}
935-
921+
// Create missing node if not found
936922
if (!foundNode)
937923
{
938-
const std::string templateNodeValue = templateNode->GetTagContent();
939-
const SString templateNodeComment = templateNode->GetCommentText();
940-
941-
foundNode = m_pRootNode->CreateSubNode(templateNodeName.c_str(), previousNode);
942-
foundNode->SetTagContent(templateNodeValue.c_str());
943-
foundNode->SetCommentText(templateNodeComment.c_str(), true);
924+
CLogger::LogPrintf("Adding missing '%s' to mtaserver.conf\n", *templateNodeTagName);
925+
SString value = templateNode->GetTagContent();
926+
SString commentText = templateNode->GetCommentText();
927+
foundNode = m_pRootNode->CreateSubNode(templateNodeTagName, previousNode);
928+
foundNode->SetTagContent(value);
929+
foundNode->SetCommentText(commentText, true);
930+
931+
// Copy attributes from template node
932+
CXMLAttributes& templateAttributes = templateNode->GetAttributes();
933+
for (auto it = templateAttributes.ListBegin(); it != templateAttributes.ListEnd(); ++it)
934+
{
935+
CXMLAttribute* templateAttribute = *it;
936+
const SString& attributeName = templateAttribute->GetName();
937+
const SString& attributeValue = templateAttribute->GetValue();
944938

945-
CLogger::LogPrintf("Added missing '%s' setting to mtaserver.conf\n", templateNodeName.c_str());
946-
configChanged = true;
939+
CXMLAttribute* newAttribute = foundNode->GetAttributes().Create(attributeName);
940+
if (newAttribute)
941+
newAttribute->SetValue(attributeValue);
942+
}
943+
hasConfigChanged = true;
947944
}
948945
previousNode = foundNode;
949946
}
950-
return configChanged;
947+
948+
// Clean up
949+
g_pServerInterface->GetXML()->DeleteXML(templateFile);
950+
FileDelete(templateFileName);
951+
return hasConfigChanged;
951952
}
952953

953954
bool CMainConfig::IsValidPassword(const char* szPassword)

0 commit comments

Comments
 (0)