|
22 | 22 | #include "CHTTPD.h" |
23 | 23 | #include "CStaticFunctionDefinitions.h" |
24 | 24 |
|
| 25 | +#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template" |
| 26 | + |
25 | 27 | extern CGame* g_pGame; |
26 | 28 |
|
27 | 29 | CBandwidthSettings* g_pBandwidthSettings = new CBandwidthSettings(); |
@@ -114,6 +116,11 @@ bool CMainConfig::Load() |
114 | 116 | return false; |
115 | 117 | } |
116 | 118 |
|
| 119 | + if (AddMissingSettings()) |
| 120 | + { |
| 121 | + Save(); |
| 122 | + } |
| 123 | + |
117 | 124 | // Name |
118 | 125 | int iResult = GetString(m_pRootNode, "servername", m_strServerName, 1, 96); |
119 | 126 | if (iResult == DOESNT_EXIST) |
@@ -836,6 +843,57 @@ bool CMainConfig::Save() |
836 | 843 | return false; |
837 | 844 | } |
838 | 845 |
|
| 846 | +// |
| 847 | +// Compare against default config and add missing nodes. |
| 848 | +// Returns true if nodes were added. |
| 849 | +// |
| 850 | +bool CMainConfig::AddMissingSettings() |
| 851 | +{ |
| 852 | + // Only mtaserver.conf is currently supported |
| 853 | + if (!g_pGame->IsUsingMtaServerConf()) |
| 854 | + return false; |
| 855 | + |
| 856 | + SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), MTA_SERVER_CONF_TEMPLATE); |
| 857 | + if (!FileExists(templateFileName)) |
| 858 | + return false; |
| 859 | + |
| 860 | + CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName); |
| 861 | + CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr; |
| 862 | + if (!templateRootNode) |
| 863 | + { |
| 864 | + CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName); |
| 865 | + return false; |
| 866 | + } |
| 867 | + |
| 868 | + // Check that each item in the template also exists in the server config |
| 869 | + bool configChanged = false; |
| 870 | + CXMLNode* previousNode = nullptr; |
| 871 | + for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it) |
| 872 | + { |
| 873 | + CXMLNode* templateNode = *it; |
| 874 | + SString templateNodeName = templateNode->GetTagName(); |
| 875 | + |
| 876 | + // Skip certain optional nodes |
| 877 | + if (templateNodeName == "resource" || templateNodeName == "module") |
| 878 | + continue; |
| 879 | + |
| 880 | + CXMLNode* foundNode = m_pRootNode->FindSubNode(templateNodeName); |
| 881 | + if (!foundNode) |
| 882 | + { |
| 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); |
| 889 | + configChanged = true; |
| 890 | + } |
| 891 | + previousNode = foundNode; |
| 892 | + } |
| 893 | + g_pServerInterface->GetXML()->DeleteXML(templateFile); |
| 894 | + return configChanged; |
| 895 | +} |
| 896 | + |
839 | 897 | bool CMainConfig::IsValidPassword(const char* szPassword) |
840 | 898 | { |
841 | 899 | if (!szPassword) |
|
0 commit comments