From 0d7851367b5069bec63555c1197ac09c2459add2 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 3 Jul 2017 18:28:23 +0300 Subject: [PATCH] Dedicated server loads the name, password and other initial settings from the serversettings file. + Fixed not being able to change the server name if the current name is null or empty --- .../Source/Networking/GameClient.cs | 9 +++++-- .../BarotraumaServer/Source/GameMain.cs | 17 ++++++++++++- .../Source/Networking/GameServer.cs | 5 ++-- .../Source/Networking/GameServerSettings.cs | 25 +++++++++++++++++++ .../Source/Networking/NetworkMember.cs | 13 +++++++--- 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs index 05934eec1..703e945ad 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs @@ -113,11 +113,16 @@ namespace Barotrauma.Networking { serverIP = address[0]; - if (!int.TryParse(address[1], out Port)) + int port = 0; + if (!int.TryParse(address[1], out port)) { DebugConsole.ThrowError("Invalid port: "+address[1]+"!"); Port = NetConfig.DefaultPort; - } + } + else + { + Port = port; + } } myCharacter = Character.Controlled; diff --git a/Barotrauma/BarotraumaServer/Source/GameMain.cs b/Barotrauma/BarotraumaServer/Source/GameMain.cs index 066f69790..9d8f84cc0 100644 --- a/Barotrauma/BarotraumaServer/Source/GameMain.cs +++ b/Barotrauma/BarotraumaServer/Source/GameMain.cs @@ -7,6 +7,7 @@ using Barotrauma.Networking; using System.Collections.Generic; using Microsoft.Xna.Framework; using System.Threading; +using System.Xml.Linq; namespace Barotrauma { @@ -100,7 +101,21 @@ namespace Barotrauma public void StartServer() { - Server = new GameServer("Dedicated Server Test", 14242, false, "asd", false, 10); + XDocument doc = ToolBox.TryLoadXml(GameServer.SettingsFile); + if (doc == null) + { + DebugConsole.ThrowError("File \""+GameServer.SettingsFile+"\" not found. Starting the server with default settings."); + Server = new GameServer("Server", 14242, false, "", false, 10); + return; + } + + Server = new GameServer( + ToolBox.GetAttributeString(doc.Root, "name", "Server"), + ToolBox.GetAttributeInt(doc.Root, "port", 14242), + ToolBox.GetAttributeBool(doc.Root, "public", false), + ToolBox.GetAttributeString(doc.Root, "password", ""), + ToolBox.GetAttributeBool(doc.Root, "attemptupnp", false), + ToolBox.GetAttributeInt(doc.Root, "maxplayers", 10)); } public void CloseServer() diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs index a2b80be33..f13a2d0c6 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs @@ -24,8 +24,6 @@ namespace Barotrauma.Networking private NetServer server; private NetPeerConfiguration config; - - private int MaxPlayers; private DateTime refreshMasterTimer; @@ -69,7 +67,7 @@ namespace Barotrauma.Networking { get { return updateInterval; } } - + public GameServer(string name, int port, bool isPublic = false, string password = "", bool attemptUPnP = false, int maxPlayers = 10) { name = name.Replace(":", ""); @@ -78,6 +76,7 @@ namespace Barotrauma.Networking AdminAuthPass = ""; this.name = name; + this.Public = isPublic; this.password = ""; if (password.Length>0) { diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs index a7e204e53..4e78e4b60 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs @@ -171,6 +171,27 @@ namespace Barotrauma.Networking } } + [HasDefaultValue(8, true)] + private int MaxPlayers + { + get; + set; + } + + [HasDefaultValue(false, true)] + private bool Public + { + get; + set; + } + + [HasDefaultValue(false, true)] + private bool AttemptUPNP + { + get { return config.EnableUPnP; } + set { config.EnableUPnP = value; } + } + public YesNoMaybe TraitorsEnabled { get; @@ -231,6 +252,10 @@ namespace Barotrauma.Networking doc.Root.SetAttributeValue("TraitorsEnabled", TraitorsEnabled.ToString()); +#if SERVER + doc.Root.SetAttributeValue("password", password); +#endif + if (GameMain.NetLobbyScreen != null #if CLIENT && GameMain.NetLobbyScreen.ServerMessage != null diff --git a/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs b/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs index 64cb11e6e..656cf226e 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs @@ -83,9 +83,6 @@ namespace Barotrauma.Networking protected DateTime updateTimer; public int EndVoteCount, EndVoteMax; - //private GUITextBlock endVoteText; - - public int Port; protected bool gameStarted; @@ -94,13 +91,21 @@ namespace Barotrauma.Networking protected RespawnManager respawnManager; public Voting Voting; + + [HasDefaultValue(14242, true)] + public int Port + { + get; + set; + } + [HasDefaultValue("", true)] public string Name { get { return name; } set { - if (string.IsNullOrEmpty(name)) return; + if (string.IsNullOrEmpty(value)) return; name = value; } }