Skip to content

Commit

Permalink
dont remove the template
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha committed Nov 15, 2024
1 parent ce676de commit f0195c7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
58 changes: 58 additions & 0 deletions Server/mods/deathmatch/logic/CMainConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "CHTTPD.h"
#include "CStaticFunctionDefinitions.h"

#define MTA_SERVER_CONF_TEMPLATE "mtaserver.conf.template"

extern CGame* g_pGame;

CBandwidthSettings* g_pBandwidthSettings = new CBandwidthSettings();
Expand Down Expand Up @@ -114,6 +116,11 @@ bool CMainConfig::Load()
return false;
}

if (AddMissingSettings())
{
Save();
}

// Name
int iResult = GetString(m_pRootNode, "servername", m_strServerName, 1, 96);
if (iResult == DOESNT_EXIST)
Expand Down Expand Up @@ -836,6 +843,57 @@ bool CMainConfig::Save()
return false;
}

//
// Compare against default config and add missing nodes.
// Returns true if nodes were added.
//
bool CMainConfig::AddMissingSettings()
{
// Only mtaserver.conf is currently supported
if (!g_pGame->IsUsingMtaServerConf())
return false;

SString templateFileName = PathJoin(g_pServerInterface->GetServerModPath(), MTA_SERVER_CONF_TEMPLATE);
if (!FileExists(templateFileName))
return false;

CXMLFile* templateFile = g_pServerInterface->GetXML()->CreateXML(templateFileName);
CXMLNode* templateRootNode = templateFile && templateFile->Parse() ? templateFile->GetRootNode() : nullptr;
if (!templateRootNode)
{
CLogger::ErrorPrintf("Can't parse '%s'\n", *templateFileName);
return false;
}

// Check that each item in the template also exists in the server config
bool configChanged = false;
CXMLNode* previousNode = nullptr;
for (auto it = templateRootNode->ChildrenBegin(); it != templateRootNode->ChildrenEnd(); ++it)
{
CXMLNode* templateNode = *it;
SString templateNodeName = templateNode->GetTagName();

// Skip certain optional nodes
if (templateNodeName == "resource" || templateNodeName == "module")
continue;

CXMLNode* foundNode = m_pRootNode->FindSubNode(templateNodeName);
if (!foundNode)
{
SString templateNodeValue = templateNode->GetTagContent();
SString templateNodeComment = templateNode->GetCommentText();
foundNode = m_pRootNode->CreateSubNode(templateNodeName, previousNode);
foundNode->SetTagContent(templateNodeValue);
foundNode->SetCommentText(templateNodeComment, true);
CLogger::LogPrintf("[%s] Added missing '%s' setting to mtaserver.conf\n", MTA_SERVER_CONF_TEMPLATE, *templateNodeName);
configChanged = true;
}
previousNode = foundNode;
}
g_pServerInterface->GetXML()->DeleteXML(templateFile);
return configChanged;
}

bool CMainConfig::IsValidPassword(const char* szPassword)
{
if (!szPassword)
Expand Down
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/CMainConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class CMainConfig : public CXMLConfig
private:
void RegisterCommand(const char* szName, FCommandHandler* pFunction, bool bRestricted, const char* szConsoleHelpText);
bool GetSettingTable(const SString& strName, const char** szAttribNames, uint uiNumAttribNames, CLuaArguments* outTable);
bool AddMissingSettings();

CConsole* m_pConsole;
CXMLNode* m_pRootNode;
Expand Down
7 changes: 4 additions & 3 deletions utils/buildactions/compose_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -25,8 +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")
os.copydir("Server/mods/deathmatch", OUTPUT_DIR.."/server/mods/deathmatch", "*.xml")

-- Copy compiled binaries
if WINDOWS then
os.copydir(BIN_DIR, OUTPUT_DIR, "**.exe")
Expand Down
8 changes: 7 additions & 1 deletion utils/buildactions/install_data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ newaction {
return
end

if not os.copyfile(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

local success, message = os.copydir("Server/mods/deathmatch", BIN_DIR.."/server/mods/deathmatch", "*.xml", false, true)
if not success then
errormsg("ERROR: Couldn't copy server xml files", "\n"..message)
Expand All @@ -69,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)
Expand Down

0 comments on commit f0195c7

Please sign in to comment.