Skip to content
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

Refactor mtaserver.conf.template #3857

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
57 changes: 29 additions & 28 deletions Server/mods/deathmatch/logic/CMainConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,44 +853,45 @@ bool CMainConfig::AddMissingSettings()
if (!g_pGame->IsUsingMtaServerConf())
return false;

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

if (!FileExists(strTemplateFilename))
SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), MTA_SERVER_CONF_TEMPLATE);
Fernando-A-Rocha marked this conversation as resolved.
Show resolved Hide resolved
if (!FileExists(templateFileName))
return false;

CXMLFile* pFileTemplate = g_pServerInterface->GetXML()->CreateXML(strTemplateFilename);
CXMLNode* pRootNodeTemplate = pFileTemplate && pFileTemplate->Parse() ? pFileTemplate->GetRootNode() : nullptr;
if (!pRootNodeTemplate)
CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName);
CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr;
if (!templateRootNode)
{
CLogger::ErrorPrintf("Can't parse '%s'\n", *strTemplateFilename);
CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName);
return false;
}

// Check that each item in the template also exists in the server config
bool bChanged = false;
CXMLNode* pPrevNode = nullptr;
for (auto it = pRootNodeTemplate->ChildrenBegin(); it != pRootNodeTemplate->ChildrenEnd(); ++it)
{
CXMLNode* pNodeTemplate = *it;
SString strNodeName = pNodeTemplate->GetTagName();
CXMLNode* pNode = m_pRootNode->FindSubNode(strNodeName);
if (!pNode)
bool configChanged = false;
CXMLNode* previousNode = nullptr;
for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it)
{
CXMLNode* templateNode = *it;
SString templateNodeName = templateNode->GetTagName();

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

CXMLNode* foundNode = m_pRootNode->FindSubNode(templateNodeName);
if (!foundNode)
{
CLogger::LogPrintf("Adding missing '%s' to mtaserver.conf\n", *strNodeName);
SString strNodeValue = pNodeTemplate->GetTagContent();
SString strNodeComment = pNodeTemplate->GetCommentText();
pNode = m_pRootNode->CreateSubNode(strNodeName, pPrevNode);
pNode->SetTagContent(strNodeValue);
pNode->SetCommentText(strNodeComment, true);
bChanged = true;
SString templateNodeValue = templateNode->GetTagContent();
SString templateNodeComment = templateNode->GetCommentText();
Fernando-A-Rocha marked this conversation as resolved.
Show resolved Hide resolved
foundNode = m_pRootNode->CreateSubNode(templateNodeName, previousNode);
foundNode->SetTagContent(templateNodeValue);
foundNode->SetCommentText(templateNodeComment, true);
CLogger::LogPrintf("[%s] Added missing '%s' setting to mtaserver.conf\n", MTA_SERVER_CONF_TEMPLATE, *templateNodeName);
configChanged = true;
}
pPrevNode = pNode;
previousNode = foundNode;
}

// Clean up
g_pServerInterface->GetXML()->DeleteXML(pFileTemplate);
FileDelete(strTemplateFilename);
return bChanged;
g_pServerInterface->GetXML()->DeleteXML(templateFile);
return configChanged;
}

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