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 15 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
65 changes: 37 additions & 28 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 @@ -855,44 +855,53 @@ bool CMainConfig::AddMissingSettings()
if (!g_pGame->IsUsingMtaServerConf())
return false;

SString strTemplateFilename = PathJoin(g_pServerInterface->GetServerModPath(), "mtaserver.conf.template");
const std::string templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), SETTINGS_TEMPLATE_PATH);
Fernando-A-Rocha marked this conversation as resolved.
Show resolved Hide resolved
if (!FileExists(templateFileName))
return false;

if (!FileExists(strTemplateFilename))
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;
}

CXMLFile* pFileTemplate = g_pServerInterface->GetXML()->CreateXML(strTemplateFilename);
CXMLNode* pRootNodeTemplate = pFileTemplate && pFileTemplate->Parse() ? pFileTemplate->GetRootNode() : nullptr;
if (!pRootNodeTemplate)
CXMLNode* templateRootNode = templateFile->GetRootNode();
if (!templateRootNode)
{
CLogger::ErrorPrintf("Can't parse '%s'\n", *strTemplateFilename);
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 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;
const std::string& templateNodeName = templateNode->GetTagName();

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

CXMLNode* foundNode = m_pRootNode->FindSubNode(templateNodeName.c_str());
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;
const std::string templateNodeValue = templateNode->GetTagContent();
const std::string templateNodeComment = templateNode->GetCommentText();
Fernando-A-Rocha marked this conversation as resolved.
Show resolved Hide resolved

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);
Fernando-A-Rocha marked this conversation as resolved.
Show resolved Hide resolved
configChanged = true;
}
pPrevNode = pNode;
previousNode = foundNode;
}

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

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