Implement CLre-only support (disabled by default)
This commit is contained in:
parent
6db837b11a
commit
34a4c2844f
8 changed files with 146 additions and 8 deletions
30
CLre_server/API/Config/CLreConfig.cs
Normal file
30
CLre_server/API/Config/CLreConfig.cs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace CLre_server.API.Config
|
||||||
|
{
|
||||||
|
[Serializable]
|
||||||
|
public struct CLreConfig
|
||||||
|
{
|
||||||
|
public bool clre_clients_only;
|
||||||
|
public bool web_server;
|
||||||
|
|
||||||
|
public static CLreConfig Default()
|
||||||
|
{
|
||||||
|
return new CLreConfig
|
||||||
|
{
|
||||||
|
clre_clients_only = false,
|
||||||
|
web_server = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CLreConfig FromString(string s)
|
||||||
|
{
|
||||||
|
return UnityEngine.JsonUtility.FromJson<CLreConfig>(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return UnityEngine.JsonUtility.ToJson(this, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -187,6 +187,7 @@ namespace CLre_server.API.MainServer
|
||||||
|
|
||||||
public static void OnConnect(int playerId)
|
public static void OnConnect(int playerId)
|
||||||
{
|
{
|
||||||
|
Synergy.Clients.RegisterClient(playerId);
|
||||||
if (playerConnected != null) playerConnected(mgs, new PlayerConnectArgs
|
if (playerConnected != null) playerConnected(mgs, new PlayerConnectArgs
|
||||||
{
|
{
|
||||||
PlayerId = playerId,
|
PlayerId = playerId,
|
||||||
|
@ -195,6 +196,7 @@ namespace CLre_server.API.MainServer
|
||||||
|
|
||||||
public static void OnDisconnect(int playerId)
|
public static void OnDisconnect(int playerId)
|
||||||
{
|
{
|
||||||
|
Synergy.Clients.RemoveClient(playerId);
|
||||||
if (playerDisconnected != null) playerDisconnected(mgs, new PlayerConnectArgs
|
if (playerDisconnected != null) playerDisconnected(mgs, new PlayerConnectArgs
|
||||||
{
|
{
|
||||||
PlayerId = playerId,
|
PlayerId = playerId,
|
||||||
|
|
36
CLre_server/API/Synergy/CLreEnforcer.cs
Normal file
36
CLre_server/API/Synergy/CLreEnforcer.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
using System.Collections;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace CLre_server.API.Synergy
|
||||||
|
{
|
||||||
|
internal static class CLreEnforcer
|
||||||
|
{
|
||||||
|
private const float _waitTime = 10.0f;
|
||||||
|
|
||||||
|
public static IEnumerator WaitABitForHandshakeThenKick(int playerId)
|
||||||
|
{
|
||||||
|
float elapsedTime = 0.0f;
|
||||||
|
while (elapsedTime < _waitTime)
|
||||||
|
{
|
||||||
|
yield return null;
|
||||||
|
elapsedTime += Time.deltaTime;
|
||||||
|
}
|
||||||
|
yield return null;
|
||||||
|
if (Clients.IsConnected(playerId) && !Clients.IsCLreClient(playerId))
|
||||||
|
{
|
||||||
|
MainServer.UserVerification.Instance.DisconnectPlayer(playerId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void Init()
|
||||||
|
{
|
||||||
|
if (CLre_server.CLre.Config.clre_clients_only)
|
||||||
|
{
|
||||||
|
MainServer.Server.Instance.PlayerConnect += (_, playerData) =>
|
||||||
|
{
|
||||||
|
WaitABitForHandshakeThenKick(playerData.PlayerId).Run();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
CLre_server/API/Synergy/Clients.cs
Normal file
44
CLre_server/API/Synergy/Clients.cs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CLre_server.API.Synergy
|
||||||
|
{
|
||||||
|
public static class Clients
|
||||||
|
{
|
||||||
|
private static readonly List<int> clrePlayers = new List<int>();
|
||||||
|
private static readonly List<int> players = new List<int>();
|
||||||
|
|
||||||
|
internal static void RegisterCLreClient(int playerId)
|
||||||
|
{
|
||||||
|
clrePlayers.Add(playerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void RegisterClient(int playerId)
|
||||||
|
{
|
||||||
|
players.Add(playerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void RemoveClient(int playerId)
|
||||||
|
{
|
||||||
|
if (IsCLreClient(playerId))
|
||||||
|
{
|
||||||
|
clrePlayers.Remove(playerId);
|
||||||
|
}
|
||||||
|
players.Remove(playerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsCLreClient(int playerId)
|
||||||
|
{
|
||||||
|
return clrePlayers.Contains(playerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsConnected(int playerId)
|
||||||
|
{
|
||||||
|
return players.Contains(playerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerator<int> CLreClients()
|
||||||
|
{
|
||||||
|
return clrePlayers.GetEnumerator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -53,11 +53,6 @@ namespace CLre_server.API.Synergy
|
||||||
h(payload);
|
h(payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void RegisterCLreClient(int playerId)
|
|
||||||
{
|
|
||||||
clrePlayers.Add(playerId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ReceiveMessageArgs
|
public struct ReceiveMessageArgs
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Linq;
|
||||||
using GameNetworkLayer.Shared;
|
using GameNetworkLayer.Shared;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using Svelto.ECS;
|
using Svelto.ECS;
|
||||||
|
@ -35,9 +36,15 @@ namespace CLre_server.API.Synergy
|
||||||
|
|
||||||
public void OnHandshakeReceived(int playerId, ref SerializedCLreHandshake p)
|
public void OnHandshakeReceived(int playerId, ref SerializedCLreHandshake p)
|
||||||
{
|
{
|
||||||
// TODO validate handshake msg
|
// validate handshake msg
|
||||||
|
if (!(p.HasFlag(HandshakeFlag.Client)
|
||||||
|
|| p.Mods.Contains("CLre")))
|
||||||
|
{
|
||||||
|
Utility.Logging.LogWarning($"Received invalid CLre handshake from player {playerId}! {p}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
Utility.Logging.MetaLog($"Received CLre handshake from player {playerId}! {p}");
|
Utility.Logging.MetaLog($"Received CLre handshake from player {playerId}! {p}");
|
||||||
Message.RegisterCLreClient(playerId);
|
Clients.RegisterCLreClient(playerId);
|
||||||
SerializedCLreHandshake payload = SerializedCLreHandshake.Current();
|
SerializedCLreHandshake payload = SerializedCLreHandshake.Current();
|
||||||
payload.SetFlag(HandshakeFlag.Confirm);
|
payload.SetFlag(HandshakeFlag.Confirm);
|
||||||
Sender(payload, playerId).Run();
|
Sender(payload, playerId).Run();
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using CLre_server.API.Config;
|
||||||
using CLre_server.API.Tools;
|
using CLre_server.API.Tools;
|
||||||
using CLre_server.WebStatus;
|
using CLre_server.WebStatus;
|
||||||
using GameNetworkLayer.Shared;
|
using GameNetworkLayer.Shared;
|
||||||
|
@ -19,6 +21,8 @@ namespace CLre_server
|
||||||
public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
||||||
|
|
||||||
internal static Harmony harmonyInstance = null;
|
internal static Harmony harmonyInstance = null;
|
||||||
|
|
||||||
|
public static CLreConfig Config = CLreConfig.Default();
|
||||||
|
|
||||||
// called when Cardlife shuts down
|
// called when Cardlife shuts down
|
||||||
public override void OnApplicationQuit()
|
public override void OnApplicationQuit()
|
||||||
|
@ -83,7 +87,27 @@ namespace CLre_server
|
||||||
API.MainServer.Server.Instance.InitStart += (_, __) => API.Utility.Logging.MetaLog("(!) Server initialising");
|
API.MainServer.Server.Instance.InitStart += (_, __) => API.Utility.Logging.MetaLog("(!) Server initialising");
|
||||||
API.MainServer.Server.Instance.InitComplete += (_, __) => API.Utility.Logging.MetaLog("(!) Server successfully initialised");
|
API.MainServer.Server.Instance.InitComplete += (_, __) => API.Utility.Logging.MetaLog("(!) Server successfully initialised");
|
||||||
#endif
|
#endif
|
||||||
|
// try to load config file
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string s = File.ReadAllText("CLre_server.json");
|
||||||
|
Config = CLreConfig.FromString(s);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
API.Utility.Logging.LogWarning($"Failed to load CLre_server.json: {e}\n{e.StackTrace}");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.WriteAllText("CLre_server.json", Config.ToString());
|
||||||
|
}
|
||||||
|
catch (Exception e2)
|
||||||
|
{
|
||||||
|
API.Utility.Logging.LogWarning($"Failed to write CLre_server.json: {e2}\n{e2.StackTrace}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// init config-dependent functionality
|
||||||
WebServer.Init();
|
WebServer.Init();
|
||||||
|
API.Synergy.CLreEnforcer.Init();
|
||||||
// Log info
|
// Log info
|
||||||
API.Utility.Logging.MetaLog($"{Name} init complete.");
|
API.Utility.Logging.MetaLog($"{Name} init complete.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ namespace CLre_server.WebStatus
|
||||||
|
|
||||||
internal static void Init()
|
internal static void Init()
|
||||||
{
|
{
|
||||||
if (Environment.GetCommandLineArgs().Contains("-web"))
|
if (CLre.Config.web_server || Environment.GetCommandLineArgs().Contains("-web"))
|
||||||
{
|
{
|
||||||
API.Utility.Logging.Log("Starting status web server");
|
API.Utility.Logging.Log("Starting status web server");
|
||||||
StatusEndpoints.Init();
|
StatusEndpoints.Init();
|
||||||
|
|
Loading…
Reference in a new issue