Skip to content

Commit 7bd161d

Browse files
Fernando-A-Rochabotder
authored andcommitted
Refactor mtaserver.conf.template (#3857)
Refer to PR #3857 for details on what this change entails.
1 parent 186ffa8 commit 7bd161d

File tree

6 files changed

+73
-372
lines changed

6 files changed

+73
-372
lines changed

Server/mods/deathmatch/logic/CMainConfig.cpp

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

26-
#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template"
26+
#define SETTINGS_TEMPLATE_PATH "mtaserver.conf.template"
2727

2828
extern CGame* g_pGame;
2929

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

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

873-
CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName);
874-
CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr;
872+
std::unique_ptr<CXMLFile> templateFile(g_pServerInterface->GetXML()->CreateXML(templateFileName.c_str()));
873+
if (!templateFile || !templateFile->Parse())
874+
{
875+
CLogger::ErrorPrintf("Failed to parse template file: '%s'\n", templateFileName.c_str());
876+
return false;
877+
}
878+
879+
CXMLNode* templateRootNode = templateFile->GetRootNode();
875880
if (!templateRootNode)
876881
{
877-
CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName);
882+
CLogger::ErrorPrintf("Template file '%s' has no root node\n", templateFileName.c_str());
878883
return false;
879884
}
880885

881886
// Check that each item in the template also exists in the server config
882-
bool hasConfigChanged = false;
887+
bool configChanged = false;
883888
CXMLNode* previousNode = nullptr;
889+
884890
for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it)
885891
{
886892
CXMLNode* templateNode = *it;
887-
SString templateNodeTagName = templateNode->GetTagName();
893+
const std::string& templateNodeName = templateNode->GetTagName();
888894

895+
// Skip certain optional nodes
896+
if (templateNodeName == "resource" || templateNodeName == "module")
897+
continue;
898+
889899
// Find node with exact same attributes
890900
CXMLAttributes& templateAttributes = templateNode->GetAttributes();
891901
CXMLNode* foundNode = nullptr;
892902
for (auto it2 = m_pRootNode->ChildrenBegin(); it2 != m_pRootNode->ChildrenEnd(); ++it2)
893903
{
894904
CXMLNode* tempNode = *it2;
895-
if (tempNode->GetTagName() != templateNodeTagName)
896-
{
905+
if (tempNode->GetTagName() != templateNodeName)
897906
continue;
898-
}
907+
899908
CXMLAttributes& attributes = tempNode->GetAttributes();
900-
bool attributesMatch = true;
901-
909+
bool attributesMatch = true;
910+
902911
for (auto it3 = templateAttributes.ListBegin(); it3 != templateAttributes.ListEnd(); ++it3)
903912
{
904913
CXMLAttribute* templateAttribute = *it3;
905-
const SString& strKey = templateAttribute->GetName();
906-
const SString& strValue = templateAttribute->GetValue();
907-
908-
CXMLAttribute* foundAttribute = attributes.Find(strKey);
909-
if (!foundAttribute || foundAttribute->GetValue() != strValue)
914+
const SString& attrName = templateAttribute->GetName();
915+
916+
// Don't check value attribute which is intended to be different
917+
if (attrName == "value")
918+
continue;
919+
920+
const SString& attrValue = templateAttribute->GetValue();
921+
922+
CXMLAttribute* foundAttribute = attributes.Find(attrName);
923+
if (!foundAttribute || foundAttribute->GetValue() != attrValue)
910924
{
911925
attributesMatch = false;
912926
break;
913927
}
914928
}
915-
929+
916930
if (attributesMatch)
917931
{
918932
foundNode = tempNode;
919933
break;
920934
}
921935
}
922-
// Create missing node if not found
936+
923937
if (!foundNode)
924938
{
925-
CLogger::LogPrintf("Adding missing '%s' to mtaserver.conf\n", *templateNodeTagName);
926-
SString value = templateNode->GetTagContent();
927-
SString commentText = templateNode->GetCommentText();
928-
foundNode = m_pRootNode->CreateSubNode(templateNodeTagName, previousNode);
929-
foundNode->SetTagContent(value);
930-
foundNode->SetCommentText(commentText, true);
931-
932-
// Copy attributes from template node
933-
CXMLAttributes& templateAttributes = templateNode->GetAttributes();
934-
for (auto it = templateAttributes.ListBegin(); it != templateAttributes.ListEnd(); ++it)
935-
{
936-
CXMLAttribute* templateAttribute = *it;
937-
const SString& attributeName = templateAttribute->GetName();
938-
const SString& attributeValue = templateAttribute->GetValue();
939+
const std::string templateNodeValue = templateNode->GetTagContent();
940+
const SString templateNodeComment = templateNode->GetCommentText();
939941

940-
CXMLAttribute* newAttribute = foundNode->GetAttributes().Create(attributeName);
941-
if (newAttribute)
942-
newAttribute->SetValue(attributeValue);
943-
}
944-
hasConfigChanged = true;
942+
foundNode = m_pRootNode->CreateSubNode(templateNodeName.c_str(), previousNode);
943+
foundNode->SetTagContent(templateNodeValue.c_str());
944+
foundNode->SetCommentText(templateNodeComment.c_str(), true);
945+
946+
CLogger::LogPrintf("Added missing '%s' setting to mtaserver.conf\n", templateNodeName.c_str());
947+
configChanged = true;
945948
}
946949
previousNode = foundNode;
947950
}
948-
949-
// Clean up
950-
g_pServerInterface->GetXML()->DeleteXML(templateFile);
951-
FileDelete(templateFileName);
952-
return hasConfigChanged;
951+
return configChanged;
953952
}
954953

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

0 commit comments

Comments
 (0)