Implement CLre-only support (disabled by default)

This commit is contained in:
NGnius (Graham) 2021-05-26 21:00:26 -04:00
parent 6db837b11a
commit 34a4c2844f
8 changed files with 146 additions and 8 deletions

View 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);
}
}
}

View file

@ -187,6 +187,7 @@ namespace CLre_server.API.MainServer
public static void OnConnect(int playerId)
{
Synergy.Clients.RegisterClient(playerId);
if (playerConnected != null) playerConnected(mgs, new PlayerConnectArgs
{
PlayerId = playerId,
@ -195,6 +196,7 @@ namespace CLre_server.API.MainServer
public static void OnDisconnect(int playerId)
{
Synergy.Clients.RemoveClient(playerId);
if (playerDisconnected != null) playerDisconnected(mgs, new PlayerConnectArgs
{
PlayerId = playerId,

View 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();
};
}
}
}
}

View 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();
}
}
}

View file

@ -53,11 +53,6 @@ namespace CLre_server.API.Synergy
h(payload);
}
}
internal static void RegisterCLreClient(int playerId)
{
clrePlayers.Add(playerId);
}
}
public struct ReceiveMessageArgs

View file

@ -1,4 +1,5 @@
using System.Collections;
using System.Linq;
using GameNetworkLayer.Shared;
using HarmonyLib;
using Svelto.ECS;
@ -35,9 +36,15 @@ namespace CLre_server.API.Synergy
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}");
Message.RegisterCLreClient(playerId);
Clients.RegisterCLreClient(playerId);
SerializedCLreHandshake payload = SerializedCLreHandshake.Current();
payload.SetFlag(HandshakeFlag.Confirm);
Sender(payload, playerId).Run();

View file

@ -1,8 +1,10 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CLre_server.API.Config;
using CLre_server.API.Tools;
using CLre_server.WebStatus;
using GameNetworkLayer.Shared;
@ -19,6 +21,8 @@ namespace CLre_server
public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
internal static Harmony harmonyInstance = null;
public static CLreConfig Config = CLreConfig.Default();
// called when Cardlife shuts down
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.InitComplete += (_, __) => API.Utility.Logging.MetaLog("(!) Server successfully initialised");
#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();
API.Synergy.CLreEnforcer.Init();
// Log info
API.Utility.Logging.MetaLog($"{Name} init complete.");
}

View file

@ -50,7 +50,7 @@ namespace CLre_server.WebStatus
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");
StatusEndpoints.Init();