Skip to content

Commit 689bee1

Browse files
committed
Add ServerListTools and UseNat
1 parent 8cf108d commit 689bee1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+329
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Distance.dll
2828
x64/
2929
x86/
3030
[Bb]uild/
31+
BuildLinux/
32+
BuildWindows/
3133
bld/
3234
[Bb]in/
3335
[Oo]bj/

ServerBase/DistanceServer.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public int MaxPlayers
4040
Network.maxConnections = maxPlayers;
4141
}
4242
}
43+
44+
public bool UseNat = false;
45+
4346
public int Port = 45671;
4447

4548
bool reportToMasterServer = false;
@@ -261,6 +264,9 @@ public DistancePlayer GetDistancePlayer(int clientId)
261264
public LocalEvent<DistancePlayer> OnPlayerValidatedEvent = new LocalEvent<DistancePlayer>();
262265
public LocalEvent<DistancePlayer> OnPlayerValidatedPreReplicationEvent = new LocalEvent<DistancePlayer>();
263266

267+
public LocalEvent<NetworkConnectionError> OnFailedToConnectToMasterServerEvent = new LocalEvent<NetworkConnectionError>();
268+
public LocalEvent<MasterServerEvent> OnMasterServerEvent = new LocalEvent<MasterServerEvent>();
269+
264270
///
265271

266272
public void Init()
@@ -448,6 +454,16 @@ public void OnServerInitialized()
448454
OnServerInitializedEvent.Fire();
449455
}
450456

457+
public void OnFailedToConnectToMasterServer(NetworkConnectionError error)
458+
{
459+
OnFailedToConnectToMasterServerEvent.Fire(error);
460+
}
461+
462+
public void OnMasterServer(MasterServerEvent evt)
463+
{
464+
OnMasterServerEvent.Fire(evt);
465+
}
466+
451467
public void RequestSubmitLobbyInfo(NetworkPlayer player)
452468
{
453469
DistanceServerMain.GetEvent<Events.ServerToClient.Request>().Fire(

ServerBase/DistanceServerMain.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,10 @@ public override void Awake()
342342
Server.Init();
343343

344344
LoadPlugins();
345+
346+
Log.WriteLine($"Starting server version {Server.DistanceVersion} on port {Server.Port} (UseNat: {Server.UseNat})");
345347

346-
Log.WriteLine($"Starting server version {Server.DistanceVersion} on port {Server.Port}");
347-
Network.InitializeServer(Server.MaxPlayers, Server.Port, false);
348+
Network.InitializeServer(Server.MaxPlayers, Server.Port, Server.UseNat);
348349
}
349350

350351
public List<DistanceServerPlugin> Plugins = new List<DistanceServerPlugin>();
@@ -673,4 +674,14 @@ public override void SubmitServerNetworkTimeSync(NetworkMessageInfo info)
673674
num2
674675
});
675676
}
677+
678+
public override void OnFailedToConnectToMasterServer(NetworkConnectionError error)
679+
{
680+
Server.OnFailedToConnectToMasterServer(error);
681+
}
682+
683+
public override void OnMasterServerEvent(MasterServerEvent evt)
684+
{
685+
Server.OnMasterServer(evt);
686+
}
676687
}

ServerPlugins/BasicAutoServer/BasicAutoServer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public bool FilterWorkshopLevels(List<DistanceSearchResultItem> results)
8383
string PrivateServerPassword = null;
8484
int MaxPlayers = 24;
8585
int Port = 45671;
86+
bool UseNat = false;
8687
bool ReportToMasterServer = true;
8788
public double ReportToMasterServerInitialDelay = 0;
8889
double MasterServerReRegisterFrequency = 5 * 60.0;
@@ -113,6 +114,7 @@ public void ReadSettings()
113114
TryGetValue(dictionary, "ServerName", ref ServerName);
114115
TryGetValue(dictionary, "MaxPlayers", ref MaxPlayers);
115116
TryGetValue(dictionary, "Port", ref Port);
117+
TryGetValue(dictionary, "UseNat", ref UseNat);
116118
TryGetValue(dictionary, "PrivateServerPassword", ref PrivateServerPassword);
117119
TryGetValue(dictionary, "ReportToMasterServer", ref ReportToMasterServer);
118120
TryGetValue(dictionary, "ReportToMasterServerInitialDelay", ref ReportToMasterServerInitialDelay);
@@ -212,6 +214,7 @@ public override void Start()
212214
Server.ServerName = ServerName;
213215
Server.MaxPlayers = MaxPlayers;
214216
Server.Port = Port;
217+
Server.UseNat = UseNat;
215218
if (ReportToMasterServerInitialDelay > 0)
216219
{
217220
Server.ReportToMasterServer = false;
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
namespace ServerListTools
9+
{
10+
public class Entry : DistanceServerPlugin
11+
{
12+
public override string Author { get; } = "Corecii; Discord: Corecii#3019";
13+
public override string DisplayName { get; } = "Server List Tools";
14+
public override int Priority { get; } = 100;
15+
public override SemanticVersion ServerVersion => new SemanticVersion("0.1.3");
16+
17+
public double LogServersFrequency = -1.0;
18+
public double HealthCheckFrequency = -1.0;
19+
public double HealthCheckDelay = 60.0;
20+
public double HealthCheckTimeout = 120.0;
21+
22+
public double LastSuccessfulHealthCheckTime = -1.0;
23+
24+
public void ReadSettings()
25+
{
26+
var filePath = new System.IO.FileInfo(Manager.ServerDirectory.FullName + "/ServerListTools.json");
27+
if (!filePath.Exists)
28+
{
29+
Log.Info("No ServerListTools.json, using default settings");
30+
return;
31+
}
32+
try
33+
{
34+
var txt = System.IO.File.ReadAllText(filePath.FullName);
35+
var reader = new JsonFx.Json.JsonReader();
36+
var dictionary = (Dictionary<string, object>)reader.Read(txt);
37+
TryGetValue(dictionary, "LogServersFrequency", ref LogServersFrequency);
38+
TryGetValue(dictionary, "HealthCheckFrequency", ref HealthCheckFrequency);
39+
TryGetValue(dictionary, "HealthCheckDelay", ref HealthCheckDelay);
40+
TryGetValue(dictionary, "HealthCheckTimeout", ref HealthCheckTimeout);
41+
Log.Info("Loaded settings from ServerListTools.json");
42+
}
43+
catch (Exception e)
44+
{
45+
Log.Error($"Couldn't read ServerListTools.json. Is your json malformed?\n{e}");
46+
47+
}
48+
}
49+
50+
bool TryGetValue<T>(Dictionary<string, object> dict, string name, ref T value)
51+
{
52+
try
53+
{
54+
value = (T)dict[name];
55+
return true;
56+
}
57+
catch (Exception e)
58+
{
59+
return false;
60+
}
61+
}
62+
63+
public override void Start()
64+
{
65+
Log.Info("Server List Tools Plugin started!");
66+
Log.Info($"My guid: {DistanceServerMain.View.owner.guid}");
67+
68+
ReadSettings();
69+
70+
if (LogServersFrequency > 0 || HealthCheckFrequency > 0)
71+
{
72+
Server.OnUpdateEvent.Connect(() =>
73+
{
74+
MasterServer.PollHostList();
75+
});
76+
}
77+
78+
if (LogServersFrequency > 0)
79+
{
80+
Log.Info("Will be logging servers...");
81+
DistanceServerMainStarter.Instance.StartCoroutine(LogServers());
82+
}
83+
if (HealthCheckFrequency > 0)
84+
{
85+
Log.Info("Will be running health checks...");
86+
DistanceServerMainStarter.Instance.StartCoroutine(HealthCheck());
87+
}
88+
}
89+
90+
public System.Collections.IEnumerator HealthCheck()
91+
{
92+
yield return new WaitForSeconds((float)HealthCheckDelay);
93+
LastSuccessfulHealthCheckTime = DistanceServerMain.UnixTime;
94+
while (true)
95+
{
96+
if (DistanceServerMain.UnixTime - LastSuccessfulHealthCheckTime > HealthCheckTimeout)
97+
{
98+
Log.Error($"Quitting because health check failed: server is not on the master server list");
99+
Application.Quit();
100+
}
101+
DistanceServerMainStarter.Instance.StartCoroutine(ExitIfNotOnServerList());
102+
yield return new WaitForSeconds((float)HealthCheckFrequency);
103+
}
104+
}
105+
106+
public System.Collections.IEnumerator LogServers()
107+
{
108+
while (true)
109+
{
110+
DistanceServerMainStarter.Instance.StartCoroutine(LogServerList());
111+
yield return new WaitForSeconds((float)LogServersFrequency);
112+
}
113+
}
114+
115+
public System.Collections.IEnumerator ExitIfNotOnServerList()
116+
{
117+
MasterServer.ClearHostList();
118+
MasterServer.RequestHostList("Distance");
119+
yield return new WaitForServerList();
120+
var servers = MasterServer.PollHostList();
121+
var hasMe = servers.Any((server) =>
122+
{
123+
return server.guid == DistanceServerMain.View.owner.guid;
124+
});
125+
if (hasMe)
126+
{
127+
LastSuccessfulHealthCheckTime = DistanceServerMain.UnixTime;
128+
}
129+
yield break;
130+
}
131+
132+
public System.Collections.IEnumerator LogServerList()
133+
{
134+
MasterServer.ClearHostList();
135+
MasterServer.RequestHostList("Distance");
136+
yield return new WaitForServerList();
137+
var servers = MasterServer.PollHostList();
138+
var i = 0;
139+
Log.Debug("Server list:");
140+
foreach (var server in servers)
141+
{
142+
i++;
143+
Log.Debug($"{i}: {String.Join(",", server.ip)}:{server.port} {server.gameName} {server.gameType} {server.connectedPlayers}/{server.playerLimit} {server.useNat} {(server.passwordProtected ? "private" : "public")} {server.comment} {server.guid}");
144+
}
145+
yield break;
146+
}
147+
}
148+
149+
public class WaitForServerList : UnityEngine.CustomYieldInstruction
150+
{
151+
public override bool keepWaiting => !done;
152+
153+
bool done = false;
154+
LocalEvent<MasterServerEvent>.EventConnection conn;
155+
156+
public WaitForServerList()
157+
{
158+
conn = DistanceServerMain.Instance.Server.OnMasterServerEvent.Connect(evt =>
159+
{
160+
if (evt == MasterServerEvent.HostListReceived)
161+
{
162+
conn.Disconnect();
163+
conn = null;
164+
done = true;
165+
}
166+
});
167+
}
168+
}
169+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ServerListTools")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ServerListTools")]
13+
[assembly: AssemblyCopyright("Copyright © 2018")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("77836de9-5b0f-44bb-a70c-c9187a5b7a28")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

ServerPlugins/WorkshopSearch/DistanceSearchRetriever.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public System.Collections.IEnumerator Retrieve()
7272
}
7373
Log.DebugLine("Retrieve");
7474
var levelIds = new List<string>();
75+
76+
if (searchResult.Result == null)
77+
{
78+
Log.Warn("HtmlAgilityPack might not be loaded");
79+
}
7580
foreach (var item in searchResult.Result.Items)
7681
{
7782
levelIds.Add(item.PublishedFileId);

ServerPlugins/WorkshopSearch/WorkshopSearchRetriever.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ public System.Collections.IEnumerator RetrieveFiles()
356356
{
357357
Finished = true;
358358
Error = request.error;
359+
if (Error == null)
360+
{
361+
Error = "Unkown error";
362+
}
359363
yield break;
360364
}
361365

@@ -462,6 +466,10 @@ public System.Collections.IEnumerator RetrieveFiles()
462466
catch (Exception e)
463467
{
464468
Error = e.ToString();
469+
if (Error == null)
470+
{
471+
Error = "Unkown error";
472+
}
465473
}
466474

467475
Finished = true;

ServerUnity/Assets/DistanceServerMainBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public GameObject gameObject
2222
public abstract void OnServerInitialized();
2323
public abstract void OnPlayerConnected(NetworkPlayer player);
2424
public abstract void OnPlayerDisconnected(NetworkPlayer player);
25+
public abstract void OnFailedToConnectToMasterServer(NetworkConnectionError error);
26+
public abstract void OnMasterServerEvent(MasterServerEvent evt);
2527
public abstract void OnDestroy();
2628
public abstract void ReceiveBroadcastAllEvent(byte[] bytes, NetworkMessageInfo info);
2729
public abstract void ReceiveClientToServerEvent(byte[] bytes, NetworkMessageInfo info);

0 commit comments

Comments
 (0)