CLre/CLre_server/API/Synergy/ServerHandshakeEngine.cs

82 lines
No EOL
3.4 KiB
C#

using System.Collections;
using System.Linq;
using GameNetworkLayer.Shared;
using HarmonyLib;
using Svelto.ECS;
namespace CLre_server.API.Synergy
{
class ServerHandshakeEngine : Engines.ServerEnginePreBuild
{
internal static ServerHandshakeEngine Instance = null;
internal const NetworkDispatcherCode CLre_HANDSHAKE_NETCODE = (NetworkDispatcherCode) 218;
private Utility.Reflection.INetMsgServerSender_SendMessage<SerializedCLreHandshake> _sendMessage;
private Utility.Reflection.INetMsgServerListener_RegisterListener<SerializedCLreHandshake> _registerListener;
public override void Ready()
{
Utility.Logging.MetaLog("Building send message delegate");
_sendMessage =
Utility.Reflection.MethodAsDelegate<Utility.Reflection.INetMsgServerSender_SendMessage<SerializedCLreHandshake>>(
"GameNetworkLayer.Server.NetMessageServerSender:SendMessage",
generics: new [] {typeof(SerializedCLreHandshake)},
instance: MainGameServer_SetupContainer_Patch.netMessageSender);
Utility.Logging.MetaLog("Building register listener delegate");
_registerListener =
Utility.Reflection.MethodAsDelegate<Utility.Reflection.INetMsgServerListener_RegisterListener<SerializedCLreHandshake>>(
"GameNetworkLayer.Server.NetMessageServerListener:RegisterListener",
generics: new [] {typeof(SerializedCLreHandshake)},
instance: MainGameServer_SetupContainer_Patch.netMessageListener);
_registerListener(CLre_HANDSHAKE_NETCODE, OnHandshakeReceived);
}
public void OnHandshakeReceived(int playerId, ref SerializedCLreHandshake p)
{
// 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}");
Clients.RegisterCLreClient(playerId);
SerializedCLreHandshake payload = SerializedCLreHandshake.Current();
payload.SetFlag(HandshakeFlag.Confirm);
Sender(payload, playerId).Run();
}
public IEnumerator Sender(SerializedCLreHandshake payload, int playerId)
{
yield return null;
Utility.Logging.MetaLog("Sending Server CLre handshake");
_sendMessage(CLre_HANDSHAKE_NETCODE, ref payload, playerId);
yield return null;
}
internal static void Init()
{
Instance = new ServerHandshakeEngine();
}
}
[HarmonyPatch(typeof(GameServer.GameFramework.MainGameServer), "SetupContainer")]
class MainGameServer_SetupContainer_Patch
{
internal static object netMessageListener;
internal static object netMessageSender;
[HarmonyPostfix]
public static void AfterMethodCall(object ____netMessageListener, object ____netMessageSender)
{
Utility.Logging.MetaLog($"Got NetMessage objects");
netMessageListener = ____netMessageListener;
netMessageSender = ____netMessageSender;
}
}
}