diff --git a/Server/mods/deathmatch/logic/CMainConfig.cpp b/Server/mods/deathmatch/logic/CMainConfig.cpp index ec5bf9b9d9..3a6e6f961e 100644 --- a/Server/mods/deathmatch/logic/CMainConfig.cpp +++ b/Server/mods/deathmatch/logic/CMainConfig.cpp @@ -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; @@ -864,91 +864,90 @@ bool CMainConfig::AddMissingSettings() if (!g_pGame->IsUsingMtaServerConf()) return false; - const SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), "mtaserver.conf.template"); - + const std::string templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), SETTINGS_TEMPLATE_PATH); if (!FileExists(templateFileName)) return false; - CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName); - CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr; + std::unique_ptr 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; + } + + CXMLNode* templateRootNode = templateFile->GetRootNode(); if (!templateRootNode) { - CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName); + 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 hasConfigChanged = false; + bool configChanged = false; CXMLNode* previousNode = nullptr; + for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it) { CXMLNode* templateNode = *it; - SString templateNodeTagName = templateNode->GetTagName(); + const std::string& templateNodeName = templateNode->GetTagName(); + // Skip certain optional nodes + if (templateNodeName == "resource" || templateNodeName == "module") + continue; + // Find node with exact same attributes CXMLAttributes& templateAttributes = templateNode->GetAttributes(); CXMLNode* foundNode = nullptr; for (auto it2 = m_pRootNode->ChildrenBegin(); it2 != m_pRootNode->ChildrenEnd(); ++it2) { CXMLNode* tempNode = *it2; - if (tempNode->GetTagName() != templateNodeTagName) - { + if (tempNode->GetTagName() != templateNodeName) continue; - } + CXMLAttributes& attributes = tempNode->GetAttributes(); - bool attributesMatch = true; - + bool attributesMatch = true; + for (auto it3 = templateAttributes.ListBegin(); it3 != templateAttributes.ListEnd(); ++it3) { CXMLAttribute* templateAttribute = *it3; - const SString& strKey = templateAttribute->GetName(); - const SString& strValue = templateAttribute->GetValue(); - - CXMLAttribute* foundAttribute = attributes.Find(strKey); - if (!foundAttribute || foundAttribute->GetValue() != strValue) + const SString& attrName = templateAttribute->GetName(); + + // Don't check value attribute which is intended to be different + if (attrName == "value") + continue; + + const SString& attrValue = templateAttribute->GetValue(); + + CXMLAttribute* foundAttribute = attributes.Find(attrName); + if (!foundAttribute || foundAttribute->GetValue() != attrValue) { attributesMatch = false; break; } } - + if (attributesMatch) { foundNode = tempNode; break; } } - // Create missing node if not found + if (!foundNode) { - CLogger::LogPrintf("Adding missing '%s' to mtaserver.conf\n", *templateNodeTagName); - SString value = templateNode->GetTagContent(); - SString commentText = templateNode->GetCommentText(); - foundNode = m_pRootNode->CreateSubNode(templateNodeTagName, previousNode); - foundNode->SetTagContent(value); - foundNode->SetCommentText(commentText, true); - - // Copy attributes from template node - CXMLAttributes& templateAttributes = templateNode->GetAttributes(); - for (auto it = templateAttributes.ListBegin(); it != templateAttributes.ListEnd(); ++it) - { - CXMLAttribute* templateAttribute = *it; - const SString& attributeName = templateAttribute->GetName(); - const SString& attributeValue = templateAttribute->GetValue(); + const std::string templateNodeValue = templateNode->GetTagContent(); + const SString templateNodeComment = templateNode->GetCommentText(); - CXMLAttribute* newAttribute = foundNode->GetAttributes().Create(attributeName); - if (newAttribute) - newAttribute->SetValue(attributeValue); - } - hasConfigChanged = true; + 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.c_str()); + configChanged = true; } previousNode = foundNode; } - - // Clean up - g_pServerInterface->GetXML()->DeleteXML(templateFile); - FileDelete(templateFileName); - return hasConfigChanged; + return configChanged; } bool CMainConfig::IsValidPassword(const char* szPassword) diff --git a/Server/mods/deathmatch/mtaserver.conf.template b/Server/mods/deathmatch/mtaserver.conf.template deleted file mode 100644 index 1cb27e157e..0000000000 --- a/Server/mods/deathmatch/mtaserver.conf.template +++ /dev/null @@ -1,316 +0,0 @@ - - - - Default MTA Server - - - - - - - - - - - - - - - - - - - - - - - - - - - auto - - - 22003 - - - 32 - - - 22005 - - - - - - 5 - - - 20 - - - - - - none - - - - - - - - - - - - - - - - 1 - - - - - - 1 - - - 0 - - - - - - medium - - - - 100 - - 1500 - - 500 - - 400 - - 2000 - - 400 - - 100 - - 100 - - - 1 - - - 30 - - - 0 - - - 150 - - - 0 - - - server-id.keys - - - logs/server.log - - - logs/server_auth.log - - - logs/db.log - - - - - - acl.xml - - - logs/scripts.log - - - 0 - - - 0 - - - 1 - - - 60 - - - 0 - - - 1 - - - 4 - - - - - - backups - - - 3 - - - 10 - - - 1 - - - 1 - - - Admin - - - 1 - - - 127.0.0.1 - - - 1 - - - 0 - - - 1 - - - 1000 - 100 - - - 1 - diff --git a/Shared/installer/nightly.nsi b/Shared/installer/nightly.nsi index cba6d23680..eca077470c 100644 --- a/Shared/installer/nightly.nsi +++ b/Shared/installer/nightly.nsi @@ -858,6 +858,7 @@ SectionGroup /e "$(INST_SEC_SERVER)" SECGSERVER File "${SERVER_FILES_ROOT}\mods\deathmatch\libcrypto-3.dll" File "${SERVER_FILES_ROOT}\mods\deathmatch\libssl-3.dll" !endif + File "${SERVER_FILES_ROOT}\mods\deathmatch\mtaserver.conf.template" ;Only overwrite the following files if previous versions were bugged and explicitly need replacing !insertmacro FileIfMD5 "${SERVER_FILES_ROOT}\mods\deathmatch\editor_acl.xml" "711185d8f4ebb355542053ce408b82b3" diff --git a/utils/buildactions/compose_files.lua b/utils/buildactions/compose_files.lua index 0adaf2cfc1..a9fa967956 100644 --- a/utils/buildactions/compose_files.lua +++ b/utils/buildactions/compose_files.lua @@ -11,10 +11,10 @@ local WINDOWS = os.host() == "windows" newaction { trigger = "compose_files", description = "Composes files that are required for building the installer", - + execute = function() os.mkdir(OUTPUT_DIR) - + -- Copy data files if WINDOWS then os.copydir(DATA_DIR.."/MTA", OUTPUT_DIR.."/MTA") @@ -25,9 +25,9 @@ newaction { -- Copy configs os.copydir("Server/mods/deathmatch", OUTPUT_DIR.."/server/mods/deathmatch", "*.conf") - os.copydir("Server/mods/deathmatch", OUTPUT_DIR.."/server/mods/deathmatch", "mtaserver.conf.template") + makeconfigtemplate(OUTPUT_DIR.."/server/mods/deathmatch/mtaserver.conf", OUTPUT_DIR.."/server/mods/deathmatch/mtaserver.conf.template") os.copydir("Server/mods/deathmatch", OUTPUT_DIR.."/server/mods/deathmatch", "*.xml") - + -- Copy compiled binaries if WINDOWS then os.copydir(BIN_DIR, OUTPUT_DIR, "**.exe") diff --git a/utils/buildactions/install_data.lua b/utils/buildactions/install_data.lua index 930ecf3519..0b9dff9c60 100644 --- a/utils/buildactions/install_data.lua +++ b/utils/buildactions/install_data.lua @@ -49,9 +49,8 @@ newaction { return end - local success, message = os.copydir("Server/mods/deathmatch", BIN_DIR.."/server/mods/deathmatch", "mtaserver.conf.template", false, true) - if not success then - errormsg("ERROR: Couldn't copy server config files", "\n"..message) + if not makeconfigtemplate(BIN_DIR.."/server/mods/deathmatch/mtaserver.conf", BIN_DIR.."/server/mods/deathmatch/mtaserver.conf.template") then + errormsg("ERROR: Could not copy mtaserver.conf to mtaserver.conf.template") os.exit(1) return end @@ -76,7 +75,7 @@ newaction { success = success and http.download_print_errors(NET_PATH_X64_WIN, BIN_DIR.."/server/x64/net.dll") success = success and http.download_print_errors(NET_PATH_ARM64_WIN, BIN_DIR.."/server/arm64/net.dll") success = success and http.download_print_errors(NETC_PATH_WIN, BIN_DIR.."/MTA/netc.dll") - + -- A download failed if not success then os.exit(1) diff --git a/utils/buildactions/utils.lua b/utils/buildactions/utils.lua index 42899f4769..f4c8482099 100644 --- a/utils/buildactions/utils.lua +++ b/utils/buildactions/utils.lua @@ -153,3 +153,25 @@ function errormsg(title, message) end term.popColor() end + +-- Does a normal file copy and adds hardcoded text to the beginning of the resulting file +-- Used in compose_files.lua and install_data.lua +function makeconfigtemplate(file_path, result_path) + if not os.copyfile(file_path, result_path) then + return false + end + local result_file = io.open(result_path, "r") + if not result_file then + return false + end + local file_content = result_file:read("*all") + result_file:close() + result_file = io.open(result_path, "w") + if not result_file then + return false, "Failed to open result file for writing." + end + result_file:write("\n") + result_file:write(file_content) + result_file:close() + return true +end