|
22 | 22 | #include "CHTTPD.h" |
23 | 23 | #include "CStaticFunctionDefinitions.h" |
24 | 24 |
|
25 | | -#define SETTINGS_TEMPLATE_PATH "mtaserver.conf.template" |
| 25 | +#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template" |
26 | 26 |
|
27 | 27 | extern CGame* g_pGame; |
28 | 28 |
|
@@ -864,90 +864,91 @@ bool CMainConfig::AddMissingSettings() |
864 | 864 | if (!g_pGame->IsUsingMtaServerConf()) |
865 | 865 | return false; |
866 | 866 |
|
867 | | - const std::string templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), SETTINGS_TEMPLATE_PATH); |
868 | | - if (!FileExists(templateFileName)) |
869 | | - return false; |
| 867 | + const SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), "mtaserver.conf.template"); |
870 | 868 |
|
871 | | - std::unique_ptr<CXMLFile> templateFile(g_pServerInterface->GetXML()->CreateXML(templateFileName.c_str())); |
872 | | - if (!templateFile || !templateFile->Parse()) |
873 | | - { |
874 | | - CLogger::ErrorPrintf("Failed to parse template file: '%s'\n", templateFileName.c_str()); |
| 869 | + if (!FileExists(templateFileName)) |
875 | 870 | return false; |
876 | | - } |
877 | 871 |
|
878 | | - CXMLNode* templateRootNode = templateFile->GetRootNode(); |
| 872 | + CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName); |
| 873 | + CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr; |
879 | 874 | if (!templateRootNode) |
880 | 875 | { |
881 | | - CLogger::ErrorPrintf("Template file '%s' has no root node\n", templateFileName.c_str()); |
| 876 | + CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName); |
882 | 877 | return false; |
883 | 878 | } |
884 | 879 |
|
885 | 880 | // Check that each item in the template also exists in the server config |
886 | | - bool configChanged = false; |
| 881 | + bool hasConfigChanged = false; |
887 | 882 | CXMLNode* previousNode = nullptr; |
888 | | - |
889 | 883 | for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it) |
890 | 884 | { |
891 | 885 | CXMLNode* templateNode = *it; |
892 | | - const std::string& templateNodeName = templateNode->GetTagName(); |
| 886 | + SString templateNodeTagName = templateNode->GetTagName(); |
893 | 887 |
|
894 | | - // Skip certain optional nodes |
895 | | - if (templateNodeName == "resource" || templateNodeName == "module") |
896 | | - continue; |
897 | | - |
898 | 888 | // Find node with exact same attributes |
899 | 889 | CXMLAttributes& templateAttributes = templateNode->GetAttributes(); |
900 | 890 | CXMLNode* foundNode = nullptr; |
901 | 891 | for (auto it2 = m_pRootNode->ChildrenBegin(); it2 != m_pRootNode->ChildrenEnd(); ++it2) |
902 | 892 | { |
903 | 893 | CXMLNode* tempNode = *it2; |
904 | | - if (tempNode->GetTagName() != templateNodeName) |
| 894 | + if (tempNode->GetTagName() != templateNodeTagName) |
| 895 | + { |
905 | 896 | continue; |
906 | | - |
| 897 | + } |
907 | 898 | CXMLAttributes& attributes = tempNode->GetAttributes(); |
908 | | - bool attributesMatch = true; |
909 | | - |
| 899 | + bool attributesMatch = true; |
| 900 | + |
910 | 901 | for (auto it3 = templateAttributes.ListBegin(); it3 != templateAttributes.ListEnd(); ++it3) |
911 | 902 | { |
912 | 903 | CXMLAttribute* templateAttribute = *it3; |
913 | | - const SString& attrName = templateAttribute->GetName(); |
914 | | - |
915 | | - // Don't check value attribute which is intended to be different |
916 | | - if (attrName == "value") |
917 | | - continue; |
918 | | - |
919 | | - const SString& attrValue = templateAttribute->GetValue(); |
920 | | - |
921 | | - CXMLAttribute* foundAttribute = attributes.Find(attrName); |
922 | | - if (!foundAttribute || foundAttribute->GetValue() != attrValue) |
| 904 | + const SString& strKey = templateAttribute->GetName(); |
| 905 | + const SString& strValue = templateAttribute->GetValue(); |
| 906 | + |
| 907 | + CXMLAttribute* foundAttribute = attributes.Find(strKey); |
| 908 | + if (!foundAttribute || foundAttribute->GetValue() != strValue) |
923 | 909 | { |
924 | 910 | attributesMatch = false; |
925 | 911 | break; |
926 | 912 | } |
927 | 913 | } |
928 | | - |
| 914 | + |
929 | 915 | if (attributesMatch) |
930 | 916 | { |
931 | 917 | foundNode = tempNode; |
932 | 918 | break; |
933 | 919 | } |
934 | 920 | } |
935 | | - |
| 921 | + // Create missing node if not found |
936 | 922 | if (!foundNode) |
937 | 923 | { |
938 | | - const std::string templateNodeValue = templateNode->GetTagContent(); |
939 | | - const SString templateNodeComment = templateNode->GetCommentText(); |
940 | | - |
941 | | - foundNode = m_pRootNode->CreateSubNode(templateNodeName.c_str(), previousNode); |
942 | | - foundNode->SetTagContent(templateNodeValue.c_str()); |
943 | | - foundNode->SetCommentText(templateNodeComment.c_str(), true); |
| 924 | + CLogger::LogPrintf("Adding missing '%s' to mtaserver.conf\n", *templateNodeTagName); |
| 925 | + SString value = templateNode->GetTagContent(); |
| 926 | + SString commentText = templateNode->GetCommentText(); |
| 927 | + foundNode = m_pRootNode->CreateSubNode(templateNodeTagName, previousNode); |
| 928 | + foundNode->SetTagContent(value); |
| 929 | + foundNode->SetCommentText(commentText, true); |
| 930 | + |
| 931 | + // Copy attributes from template node |
| 932 | + CXMLAttributes& templateAttributes = templateNode->GetAttributes(); |
| 933 | + for (auto it = templateAttributes.ListBegin(); it != templateAttributes.ListEnd(); ++it) |
| 934 | + { |
| 935 | + CXMLAttribute* templateAttribute = *it; |
| 936 | + const SString& attributeName = templateAttribute->GetName(); |
| 937 | + const SString& attributeValue = templateAttribute->GetValue(); |
944 | 938 |
|
945 | | - CLogger::LogPrintf("Added missing '%s' setting to mtaserver.conf\n", templateNodeName.c_str()); |
946 | | - configChanged = true; |
| 939 | + CXMLAttribute* newAttribute = foundNode->GetAttributes().Create(attributeName); |
| 940 | + if (newAttribute) |
| 941 | + newAttribute->SetValue(attributeValue); |
| 942 | + } |
| 943 | + hasConfigChanged = true; |
947 | 944 | } |
948 | 945 | previousNode = foundNode; |
949 | 946 | } |
950 | | - return configChanged; |
| 947 | + |
| 948 | + // Clean up |
| 949 | + g_pServerInterface->GetXML()->DeleteXML(templateFile); |
| 950 | + FileDelete(templateFileName); |
| 951 | + return hasConfigChanged; |
951 | 952 | } |
952 | 953 |
|
953 | 954 | bool CMainConfig::IsValidPassword(const char* szPassword) |
|
0 commit comments