Skip to content

Refactor mtaserver.conf.template #3857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
525f43d
remove mtaserver.conf.template
Fernando-A-Rocha Nov 14, 2024
85292af
Merge branch 'master' into conf-template
Fernando-A-Rocha Nov 14, 2024
899dd52
.
Fernando-A-Rocha Nov 14, 2024
ce676de
Merge branch 'conf-template' of github.com:Fernando-A-Rocha/mtasa-blu…
Fernando-A-Rocha Nov 14, 2024
f0195c7
dont remove the template
Fernando-A-Rocha Nov 15, 2024
d3bb1ea
include mtaserver.conf.template in installer
Fernando-A-Rocha Nov 15, 2024
2796ea0
compose_files
Fernando-A-Rocha Nov 15, 2024
6c98bd3
Merge branch 'master' into conf-template
Fernando-A-Rocha Nov 18, 2024
8d3da29
more refactoring
Fernando-A-Rocha Nov 18, 2024
5748061
Merge branch 'master' into conf-template
Fernando-A-Rocha Nov 21, 2024
620e1d7
resolve merge conflict
Fernando-A-Rocha Nov 22, 2024
d3d5740
mtaserver.conf.template add header comment
Fernando-A-Rocha Nov 22, 2024
37e9254
rename func
Fernando-A-Rocha Nov 22, 2024
7e67221
Merge branch 'master' into conf-template
Fernando-A-Rocha Nov 22, 2024
e0c698e
Merge branch 'master' into conf-template
TheNormalnij Dec 13, 2024
1c8bb13
fix merge conflicts
Fernando-A-Rocha Jan 5, 2025
8d4947a
del mtaserverconf.template
Fernando-A-Rocha Jan 5, 2025
010923a
Merge branch 'master' into conf-template
Fernando-A-Rocha Jan 5, 2025
d9fa84b
reviews
Fernando-A-Rocha Jan 5, 2025
bd5db2c
Update CMainConfig.cpp
Fernando-A-Rocha Jan 8, 2025
55855bd
Merge branch 'master' into conf-template
Fernando-A-Rocha Jan 8, 2025
4adfb9e
Merge branch 'master' into conf-template
Fernando-A-Rocha Mar 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 43 additions & 44 deletions Server/mods/deathmatch/logic/CMainConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "CHTTPD.h"
#include "CStaticFunctionDefinitions.h"

#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template"
#define SETTINGS_TEMPLATE_PATH "mtaserver.conf.template"

extern CGame* g_pGame;

Expand Down Expand Up @@ -864,91 +864,90 @@ bool CMainConfig::AddMissingSettings()
if (!g_pGame->IsUsingMtaServerConf())
return false;

const SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), "mtaserver.conf.template");

const std::string templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), SETTINGS_TEMPLATE_PATH);
if (!FileExists(templateFileName))
return false;

CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName);
CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr;
std::unique_ptr<CXMLFile> templateFile(g_pServerInterface->GetXML()->CreateXML(templateFileName.c_str()));
if (!templateFile || !templateFile->Parse())
{
CLogger::ErrorPrintf("Failed to parse template file: '%s'\n", templateFileName.c_str());
return false;
}

CXMLNode* templateRootNode = templateFile->GetRootNode();
if (!templateRootNode)
{
CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName);
CLogger::ErrorPrintf("Template file '%s' has no root node\n", templateFileName.c_str());
return false;
}

// Check that each item in the template also exists in the server config
bool hasConfigChanged = false;
bool configChanged = false;
CXMLNode* previousNode = nullptr;

for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it)
{
CXMLNode* templateNode = *it;
SString templateNodeTagName = templateNode->GetTagName();
const std::string& templateNodeName = templateNode->GetTagName();

// Skip certain optional nodes
if (templateNodeName == "resource" || templateNodeName == "module")
continue;

// Find node with exact same attributes
CXMLAttributes& templateAttributes = templateNode->GetAttributes();
CXMLNode* foundNode = nullptr;
for (auto it2 = m_pRootNode->ChildrenBegin(); it2 != m_pRootNode->ChildrenEnd(); ++it2)
{
CXMLNode* tempNode = *it2;
if (tempNode->GetTagName() != templateNodeTagName)
{
if (tempNode->GetTagName() != templateNodeName)
continue;
}

CXMLAttributes& attributes = tempNode->GetAttributes();
bool attributesMatch = true;

bool attributesMatch = true;
for (auto it3 = templateAttributes.ListBegin(); it3 != templateAttributes.ListEnd(); ++it3)
{
CXMLAttribute* templateAttribute = *it3;
const SString& strKey = templateAttribute->GetName();
const SString& strValue = templateAttribute->GetValue();

CXMLAttribute* foundAttribute = attributes.Find(strKey);
if (!foundAttribute || foundAttribute->GetValue() != strValue)
const SString& attrName = templateAttribute->GetName();

// Don't check value attribute which is intended to be different
if (attrName == "value")
continue;

const SString& attrValue = templateAttribute->GetValue();

CXMLAttribute* foundAttribute = attributes.Find(attrName);
if (!foundAttribute || foundAttribute->GetValue() != attrValue)
{
attributesMatch = false;
break;
}
}

if (attributesMatch)
{
foundNode = tempNode;
break;
}
}
// Create missing node if not found

if (!foundNode)
{
CLogger::LogPrintf("Adding missing '%s' to mtaserver.conf\n", *templateNodeTagName);
SString value = templateNode->GetTagContent();
SString commentText = templateNode->GetCommentText();
foundNode = m_pRootNode->CreateSubNode(templateNodeTagName, previousNode);
foundNode->SetTagContent(value);
foundNode->SetCommentText(commentText, true);

// Copy attributes from template node
CXMLAttributes& templateAttributes = templateNode->GetAttributes();
for (auto it = templateAttributes.ListBegin(); it != templateAttributes.ListEnd(); ++it)
{
CXMLAttribute* templateAttribute = *it;
const SString& attributeName = templateAttribute->GetName();
const SString& attributeValue = templateAttribute->GetValue();
const std::string templateNodeValue = templateNode->GetTagContent();
const SString templateNodeComment = templateNode->GetCommentText();

CXMLAttribute* newAttribute = foundNode->GetAttributes().Create(attributeName);
if (newAttribute)
newAttribute->SetValue(attributeValue);
}
hasConfigChanged = true;
foundNode = m_pRootNode->CreateSubNode(templateNodeName.c_str(), previousNode);
foundNode->SetTagContent(templateNodeValue.c_str());
foundNode->SetCommentText(templateNodeComment.c_str(), true);

CLogger::LogPrintf("Added missing '%s' setting to mtaserver.conf\n", templateNodeName.c_str());
configChanged = true;
}
previousNode = foundNode;
}

// Clean up
g_pServerInterface->GetXML()->DeleteXML(templateFile);
FileDelete(templateFileName);
return hasConfigChanged;
return configChanged;
}

bool CMainConfig::IsValidPassword(const char* szPassword)
Expand Down
Loading
Loading