-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
329 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ Distance.dll | |
x64/ | ||
x86/ | ||
[Bb]uild/ | ||
BuildLinux/ | ||
BuildWindows/ | ||
bld/ | ||
[Bb]in/ | ||
[Oo]bj/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
|
||
namespace ServerListTools | ||
{ | ||
public class Entry : DistanceServerPlugin | ||
{ | ||
public override string Author { get; } = "Corecii; Discord: Corecii#3019"; | ||
public override string DisplayName { get; } = "Server List Tools"; | ||
public override int Priority { get; } = 100; | ||
public override SemanticVersion ServerVersion => new SemanticVersion("0.1.3"); | ||
|
||
public double LogServersFrequency = -1.0; | ||
public double HealthCheckFrequency = -1.0; | ||
public double HealthCheckDelay = 60.0; | ||
public double HealthCheckTimeout = 120.0; | ||
|
||
public double LastSuccessfulHealthCheckTime = -1.0; | ||
|
||
public void ReadSettings() | ||
{ | ||
var filePath = new System.IO.FileInfo(Manager.ServerDirectory.FullName + "/ServerListTools.json"); | ||
if (!filePath.Exists) | ||
{ | ||
Log.Info("No ServerListTools.json, using default settings"); | ||
return; | ||
} | ||
try | ||
{ | ||
var txt = System.IO.File.ReadAllText(filePath.FullName); | ||
var reader = new JsonFx.Json.JsonReader(); | ||
var dictionary = (Dictionary<string, object>)reader.Read(txt); | ||
TryGetValue(dictionary, "LogServersFrequency", ref LogServersFrequency); | ||
TryGetValue(dictionary, "HealthCheckFrequency", ref HealthCheckFrequency); | ||
TryGetValue(dictionary, "HealthCheckDelay", ref HealthCheckDelay); | ||
TryGetValue(dictionary, "HealthCheckTimeout", ref HealthCheckTimeout); | ||
Log.Info("Loaded settings from ServerListTools.json"); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error($"Couldn't read ServerListTools.json. Is your json malformed?\n{e}"); | ||
|
||
} | ||
} | ||
|
||
bool TryGetValue<T>(Dictionary<string, object> dict, string name, ref T value) | ||
{ | ||
try | ||
{ | ||
value = (T)dict[name]; | ||
return true; | ||
} | ||
catch (Exception e) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public override void Start() | ||
{ | ||
Log.Info("Server List Tools Plugin started!"); | ||
Log.Info($"My guid: {DistanceServerMain.View.owner.guid}"); | ||
|
||
ReadSettings(); | ||
|
||
if (LogServersFrequency > 0 || HealthCheckFrequency > 0) | ||
{ | ||
Server.OnUpdateEvent.Connect(() => | ||
{ | ||
MasterServer.PollHostList(); | ||
}); | ||
} | ||
|
||
if (LogServersFrequency > 0) | ||
{ | ||
Log.Info("Will be logging servers..."); | ||
DistanceServerMainStarter.Instance.StartCoroutine(LogServers()); | ||
} | ||
if (HealthCheckFrequency > 0) | ||
{ | ||
Log.Info("Will be running health checks..."); | ||
DistanceServerMainStarter.Instance.StartCoroutine(HealthCheck()); | ||
} | ||
} | ||
|
||
public System.Collections.IEnumerator HealthCheck() | ||
{ | ||
yield return new WaitForSeconds((float)HealthCheckDelay); | ||
LastSuccessfulHealthCheckTime = DistanceServerMain.UnixTime; | ||
while (true) | ||
{ | ||
if (DistanceServerMain.UnixTime - LastSuccessfulHealthCheckTime > HealthCheckTimeout) | ||
{ | ||
Log.Error($"Quitting because health check failed: server is not on the master server list"); | ||
Application.Quit(); | ||
} | ||
DistanceServerMainStarter.Instance.StartCoroutine(ExitIfNotOnServerList()); | ||
yield return new WaitForSeconds((float)HealthCheckFrequency); | ||
} | ||
} | ||
|
||
public System.Collections.IEnumerator LogServers() | ||
{ | ||
while (true) | ||
{ | ||
DistanceServerMainStarter.Instance.StartCoroutine(LogServerList()); | ||
yield return new WaitForSeconds((float)LogServersFrequency); | ||
} | ||
} | ||
|
||
public System.Collections.IEnumerator ExitIfNotOnServerList() | ||
{ | ||
MasterServer.ClearHostList(); | ||
MasterServer.RequestHostList("Distance"); | ||
yield return new WaitForServerList(); | ||
var servers = MasterServer.PollHostList(); | ||
var hasMe = servers.Any((server) => | ||
{ | ||
return server.guid == DistanceServerMain.View.owner.guid; | ||
}); | ||
if (hasMe) | ||
{ | ||
LastSuccessfulHealthCheckTime = DistanceServerMain.UnixTime; | ||
} | ||
yield break; | ||
} | ||
|
||
public System.Collections.IEnumerator LogServerList() | ||
{ | ||
MasterServer.ClearHostList(); | ||
MasterServer.RequestHostList("Distance"); | ||
yield return new WaitForServerList(); | ||
var servers = MasterServer.PollHostList(); | ||
var i = 0; | ||
Log.Debug("Server list:"); | ||
foreach (var server in servers) | ||
{ | ||
i++; | ||
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}"); | ||
} | ||
yield break; | ||
} | ||
} | ||
|
||
public class WaitForServerList : UnityEngine.CustomYieldInstruction | ||
{ | ||
public override bool keepWaiting => !done; | ||
|
||
bool done = false; | ||
LocalEvent<MasterServerEvent>.EventConnection conn; | ||
|
||
public WaitForServerList() | ||
{ | ||
conn = DistanceServerMain.Instance.Server.OnMasterServerEvent.Connect(evt => | ||
{ | ||
if (evt == MasterServerEvent.HostListReceived) | ||
{ | ||
conn.Disconnect(); | ||
conn = null; | ||
done = true; | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("ServerListTools")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("ServerListTools")] | ||
[assembly: AssemblyCopyright("Copyright © 2018")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("77836de9-5b0f-44bb-a70c-c9187a5b7a28")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.