Skip to content

Commit f0195c7

Browse files
dont remove the template
1 parent ce676de commit f0195c7

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

Server/mods/deathmatch/logic/CMainConfig.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "CHTTPD.h"
2323
#include "CStaticFunctionDefinitions.h"
2424

25+
#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template"
26+
2527
extern CGame* g_pGame;
2628

2729
CBandwidthSettings* g_pBandwidthSettings = new CBandwidthSettings();
@@ -114,6 +116,11 @@ bool CMainConfig::Load()
114116
return false;
115117
}
116118

119+
if (AddMissingSettings())
120+
{
121+
Save();
122+
}
123+
117124
// Name
118125
int iResult = GetString(m_pRootNode, "servername", m_strServerName, 1, 96);
119126
if (iResult == DOESNT_EXIST)
@@ -836,6 +843,57 @@ bool CMainConfig::Save()
836843
return false;
837844
}
838845

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+
839897
bool CMainConfig::IsValidPassword(const char* szPassword)
840898
{
841899
if (!szPassword)

Server/mods/deathmatch/logic/CMainConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class CMainConfig : public CXMLConfig
152152
private:
153153
void RegisterCommand(const char* szName, FCommandHandler* pFunction, bool bRestricted, const char* szConsoleHelpText);
154154
bool GetSettingTable(const SString& strName, const char** szAttribNames, uint uiNumAttribNames, CLuaArguments* outTable);
155+
bool AddMissingSettings();
155156

156157
CConsole* m_pConsole;
157158
CXMLNode* m_pRootNode;

utils/buildactions/compose_files.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ local WINDOWS = os.host() == "windows"
1111
newaction {
1212
trigger = "compose_files",
1313
description = "Composes files that are required for building the installer",
14-
14+
1515
execute = function()
1616
os.mkdir(OUTPUT_DIR)
17-
17+
1818
-- Copy data files
1919
if WINDOWS then
2020
os.copydir(DATA_DIR.."/MTA", OUTPUT_DIR.."/MTA")
@@ -25,8 +25,9 @@ newaction {
2525

2626
-- Copy configs
2727
os.copydir("Server/mods/deathmatch", OUTPUT_DIR.."/server/mods/deathmatch", "*.conf")
28+
os.copydir("Server/mods/deathmatch", OUTPUT_DIR.."/server/mods/deathmatch", "mtaserver.conf.template")
2829
os.copydir("Server/mods/deathmatch", OUTPUT_DIR.."/server/mods/deathmatch", "*.xml")
29-
30+
3031
-- Copy compiled binaries
3132
if WINDOWS then
3233
os.copydir(BIN_DIR, OUTPUT_DIR, "**.exe")

utils/buildactions/install_data.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ newaction {
4949
return
5050
end
5151

52+
if not os.copyfile(BIN_DIR.."/server/mods/deathmatch/mtaserver.conf", BIN_DIR.."/server/mods/deathmatch/mtaserver.conf.template") then
53+
errormsg("ERROR: Could not copy mtaserver.conf to mtaserver.conf.template")
54+
os.exit(1)
55+
return
56+
end
57+
5258
local success, message = os.copydir("Server/mods/deathmatch", BIN_DIR.."/server/mods/deathmatch", "*.xml", false, true)
5359
if not success then
5460
errormsg("ERROR: Couldn't copy server xml files", "\n"..message)
@@ -69,7 +75,7 @@ newaction {
6975
success = success and http.download_print_errors(NET_PATH_X64_WIN, BIN_DIR.."/server/x64/net.dll")
7076
success = success and http.download_print_errors(NET_PATH_ARM64_WIN, BIN_DIR.."/server/arm64/net.dll")
7177
success = success and http.download_print_errors(NETC_PATH_WIN, BIN_DIR.."/MTA/netc.dll")
72-
78+
7379
-- A download failed
7480
if not success then
7581
os.exit(1)

0 commit comments

Comments
 (0)