Skip to content

Commit afd75f8

Browse files
authored
Added backwards compat of previous servers file format (SubnauticaNitrox#1544)
1 parent e085ca2 commit afd75f8

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

NitroxClient/Communication/MultiplayerSession/MultiplayerSessionManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using NitroxModel.Logger;
1010
using NitroxModel.MultiplayerSession;
1111
using NitroxModel.Packets;
12+
using NitroxModel.Serialization;
1213

1314
namespace NitroxClient.Communication.MultiplayerSession
1415
{
@@ -133,7 +134,7 @@ public void UpdateConnectionState(IMultiplayerSessionConnectionState sessionConn
133134
public void ClearSessionState()
134135
{
135136
IpAddress = null;
136-
ServerPort = 11000;
137+
ServerPort = ServerList.DEFAULT_PORT;
137138
SessionPolicy = null;
138139
PlayerSettings = null;
139140
AuthenticationContext = null;

NitroxClient/MonoBehaviours/Gui/MainMenu/MainMenuMultiplayerPanel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void ShowAddServerWindow()
106106
{
107107
serverNameInput = "local";
108108
serverHostInput = "127.0.0.1";
109-
serverPortInput = "11000";
109+
serverPortInput = ServerList.DEFAULT_PORT.ToString();
110110
showingAddServer = true;
111111
shouldFocus = true;
112112
}

NitroxModel/Serialization/ServerList.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Net;
56
using NitroxModel.Helper;
67
using NitroxModel.OS;
@@ -10,6 +11,7 @@ namespace NitroxModel.Serialization
1011
public class ServerList
1112
{
1213
private const string SERVERS_FILE_NAME = "servers";
14+
public const int DEFAULT_PORT = 11000;
1315
private static ServerList instance;
1416
private readonly List<Entry> entries = new();
1517
public static ServerList Instance => instance ??= From(DefaultFile);
@@ -19,7 +21,7 @@ private static ServerList Default
1921
get
2022
{
2123
ServerList list = new();
22-
list.Add(new Entry("local server", "127.0.0.1", 11000));
24+
list.Add(new Entry("local server", "127.0.0.1", DEFAULT_PORT));
2325
return list;
2426
}
2527
}
@@ -114,12 +116,32 @@ public static Entry FromLine(string line)
114116
return null;
115117
}
116118
string[] parts = line.Split('|');
117-
if (parts.Length != 3)
119+
int port;
120+
string address;
121+
switch (parts.Length)
118122
{
119-
throw new Exception($"Expected server entry to have 3 parts: {line}");
123+
case 2:
124+
// Split from address as format "hostname:port".
125+
string[] addressSplit = parts[1].Split(':');
126+
address = addressSplit[0];
127+
if (!int.TryParse(addressSplit.ElementAtOrDefault(1), out port))
128+
{
129+
port = DEFAULT_PORT;
130+
}
131+
break;
132+
case 3:
133+
address = parts[1].Trim();
134+
if (!int.TryParse(parts[2], out port))
135+
{
136+
port = DEFAULT_PORT;
137+
}
138+
break;
139+
default:
140+
throw new Exception($"Expected server entry to have 2 or 3 parts: {line}");
120141
}
121142

122-
return new Entry(parts[0].Trim(), parts[1].Trim(), int.Parse(parts[2].Trim()));
143+
string name = parts[0].Trim();
144+
return new Entry(name, address, port);
123145
}
124146

125147
public override string ToString()

NitroxServer/Serialization/ServerConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ServerConfig : NitroxConfig<ServerConfig>
1010
{
1111
private int maxConnectionsSetting = 100;
1212

13-
private int portSetting = 11000;
13+
private int portSetting = ServerList.DEFAULT_PORT;
1414

1515
private int saveIntervalSetting = 120000;
1616

0 commit comments

Comments
 (0)