|
22 | 22 | #include "CHTTPD.h" |
23 | 23 | #include "CStaticFunctionDefinitions.h" |
24 | 24 |
|
25 | | -#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template" |
| 25 | +#define SETTINGS_TEMPLATE_PATH "mtaserver.conf.template" |
26 | 26 |
|
27 | 27 | extern CGame* g_pGame; |
28 | 28 |
|
@@ -853,44 +853,52 @@ bool CMainConfig::AddMissingSettings() |
853 | 853 | if (!g_pGame->IsUsingMtaServerConf()) |
854 | 854 | return false; |
855 | 855 |
|
856 | | - SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), MTA_SERVER_CONF_TEMPLATE); |
| 856 | + const std::string templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), SETTINGS_TEMPLATE_PATH); |
857 | 857 | if (!FileExists(templateFileName)) |
858 | 858 | return false; |
859 | 859 |
|
860 | | - CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName); |
861 | | - CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr; |
| 860 | + std::unique_ptr<CXMLFile> templateFile(g_pServerInterface->GetXML()->CreateXML(templateFileName.c_str())); |
| 861 | + if (!templateFile || !templateFile->Parse()) |
| 862 | + { |
| 863 | + CLogger::ErrorPrintf("Failed to parse template file: '%s'\n", templateFileName.c_str()); |
| 864 | + return false; |
| 865 | + } |
| 866 | + |
| 867 | + CXMLNode* templateRootNode = templateFile->GetRootNode(); |
862 | 868 | if (!templateRootNode) |
863 | 869 | { |
864 | | - CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName); |
| 870 | + CLogger::ErrorPrintf("Template file '%s' has no root node\n", templateFileName.c_str()); |
865 | 871 | return false; |
866 | 872 | } |
867 | 873 |
|
868 | 874 | // Check that each item in the template also exists in the server config |
869 | | - bool configChanged = false; |
| 875 | + bool configChanged = false; |
870 | 876 | CXMLNode* previousNode = nullptr; |
| 877 | + |
871 | 878 | for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it) |
872 | 879 | { |
873 | 880 | CXMLNode* templateNode = *it; |
874 | | - SString templateNodeName = templateNode->GetTagName(); |
| 881 | + const std::string& templateNodeName = templateNode->GetTagName(); |
875 | 882 |
|
876 | 883 | // Skip certain optional nodes |
877 | 884 | if (templateNodeName == "resource" || templateNodeName == "module") |
878 | 885 | continue; |
879 | 886 |
|
880 | | - CXMLNode* foundNode = m_pRootNode->FindSubNode(templateNodeName); |
| 887 | + CXMLNode* foundNode = m_pRootNode->FindSubNode(templateNodeName.c_str()); |
881 | 888 | if (!foundNode) |
882 | 889 | { |
883 | | - SString templateNodeValue = templateNode->GetTagContent(); |
884 | | - SString templateNodeComment = templateNode->GetCommentText(); |
885 | | - foundNode = m_pRootNode->CreateSubNode(templateNodeName, previousNode); |
886 | | - foundNode->SetTagContent(templateNodeValue); |
887 | | - foundNode->SetCommentText(templateNodeComment, true); |
888 | | - CLogger::LogPrintf("[%s] Added missing '%s' setting to mtaserver.conf\n", MTA_SERVER_CONF_TEMPLATE, *templateNodeName); |
| 890 | + const std::string templateNodeValue = templateNode->GetTagContent(); |
| 891 | + const std::string templateNodeComment = templateNode->GetCommentText(); |
| 892 | + |
| 893 | + foundNode = m_pRootNode->CreateSubNode(templateNodeName.c_str(), previousNode); |
| 894 | + foundNode->SetTagContent(templateNodeValue.c_str()); |
| 895 | + foundNode->SetCommentText(templateNodeComment.c_str(), true); |
| 896 | + |
| 897 | + CLogger::LogPrintf("Added missing '%s' setting to mtaserver.conf\n", &templateNodeName); |
889 | 898 | configChanged = true; |
890 | 899 | } |
891 | 900 | previousNode = foundNode; |
892 | 901 | } |
893 | | - g_pServerInterface->GetXML()->DeleteXML(templateFile); |
894 | 902 | return configChanged; |
895 | 903 | } |
896 | 904 |
|
|
0 commit comments