Skip to content

Commit

Permalink
Added backwards compat of previous servers file format
Browse files Browse the repository at this point in the history
  • Loading branch information
Measurity committed Jul 25, 2021
1 parent e9c0d7c commit 7ff77a0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using NitroxModel.Logger;
using NitroxModel.MultiplayerSession;
using NitroxModel.Packets;
using NitroxModel.Serialization;

namespace NitroxClient.Communication.MultiplayerSession
{
Expand Down Expand Up @@ -133,7 +134,7 @@ public void UpdateConnectionState(IMultiplayerSessionConnectionState sessionConn
public void ClearSessionState()
{
IpAddress = null;
ServerPort = 11000;
ServerPort = ServerList.DEFAULT_PORT;
SessionPolicy = null;
PlayerSettings = null;
AuthenticationContext = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void ShowAddServerWindow()
{
serverNameInput = "local";
serverHostInput = "127.0.0.1";
serverPortInput = "11000";
serverPortInput = ServerList.DEFAULT_PORT.ToString();
showingAddServer = true;
shouldFocus = true;
}
Expand Down
30 changes: 26 additions & 4 deletions NitroxModel/Serialization/ServerList.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using NitroxModel.Helper;
using NitroxModel.OS;
Expand All @@ -10,6 +11,7 @@ namespace NitroxModel.Serialization
public class ServerList
{
private const string SERVERS_FILE_NAME = "servers";
public const int DEFAULT_PORT = 11000;
private static ServerList instance;
private readonly List<Entry> entries = new();
public static ServerList Instance => instance ??= From(DefaultFile);
Expand All @@ -19,7 +21,7 @@ private static ServerList Default
get
{
ServerList list = new();
list.Add(new Entry("local server", "127.0.0.1", 11000));
list.Add(new Entry("local server", "127.0.0.1", DEFAULT_PORT));
return list;
}
}
Expand Down Expand Up @@ -114,12 +116,32 @@ public static Entry FromLine(string line)
return null;
}
string[] parts = line.Split('|');
if (parts.Length != 3)
int port;
string address;
switch (parts.Length)
{
throw new Exception($"Expected server entry to have 3 parts: {line}");
case 2:
// Split from address as format "hostname:port".
string[] addressSplit = parts[1].Split(':');
address = addressSplit[0];
if (!int.TryParse(addressSplit.ElementAtOrDefault(1), out port))
{
port = DEFAULT_PORT;
}
break;
case 3:
address = parts[1].Trim();
if (!int.TryParse(parts[2], out port))
{
port = DEFAULT_PORT;
}
break;
default:
throw new Exception($"Expected server entry to have 2 or 3 parts: {line}");
}

return new Entry(parts[0].Trim(), parts[1].Trim(), int.Parse(parts[2].Trim()));
string name = parts[0].Trim();
return new Entry(name, address, port);
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion NitroxServer/Serialization/ServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ServerConfig : NitroxConfig<ServerConfig>
{
private int maxConnectionsSetting = 100;

private int portSetting = 11000;
private int portSetting = ServerList.DEFAULT_PORT;

private int saveIntervalSetting = 120000;

Expand Down

0 comments on commit 7ff77a0

Please sign in to comment.