Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.4KB

  1. using System.Collections;
  2. using System.Linq;
  3. using GameNetworkLayer.Shared;
  4. using HarmonyLib;
  5. using Svelto.ECS;
  6. namespace CLre_server.API.Synergy
  7. {
  8. class ServerHandshakeEngine : Engines.ServerEnginePreBuild
  9. {
  10. internal static ServerHandshakeEngine Instance = null;
  11. internal const NetworkDispatcherCode CLre_HANDSHAKE_NETCODE = (NetworkDispatcherCode) 218;
  12. private Utility.Reflection.INetMsgServerSender_SendMessage<SerializedCLreHandshake> _sendMessage;
  13. private Utility.Reflection.INetMsgServerListener_RegisterListener<SerializedCLreHandshake> _registerListener;
  14. public override void Ready()
  15. {
  16. Utility.Logging.MetaLog("Building send message delegate");
  17. _sendMessage =
  18. Utility.Reflection.MethodAsDelegate<Utility.Reflection.INetMsgServerSender_SendMessage<SerializedCLreHandshake>>(
  19. "GameNetworkLayer.Server.NetMessageServerSender:SendMessage",
  20. generics: new [] {typeof(SerializedCLreHandshake)},
  21. instance: MainGameServer_SetupContainer_Patch.netMessageSender);
  22. Utility.Logging.MetaLog("Building register listener delegate");
  23. _registerListener =
  24. Utility.Reflection.MethodAsDelegate<Utility.Reflection.INetMsgServerListener_RegisterListener<SerializedCLreHandshake>>(
  25. "GameNetworkLayer.Server.NetMessageServerListener:RegisterListener",
  26. generics: new [] {typeof(SerializedCLreHandshake)},
  27. instance: MainGameServer_SetupContainer_Patch.netMessageListener);
  28. _registerListener(CLre_HANDSHAKE_NETCODE, OnHandshakeReceived);
  29. }
  30. public void OnHandshakeReceived(int playerId, ref SerializedCLreHandshake p)
  31. {
  32. // validate handshake msg
  33. if (!(p.HasFlag(HandshakeFlag.Client)
  34. || p.Mods.Contains("CLre")))
  35. {
  36. Utility.Logging.LogWarning($"Received invalid CLre handshake from player {playerId}! {p}");
  37. return;
  38. }
  39. Utility.Logging.MetaLog($"Received CLre handshake from player {playerId}! {p}");
  40. Clients.RegisterCLreClient(playerId);
  41. SerializedCLreHandshake payload = SerializedCLreHandshake.Current();
  42. payload.SetFlag(HandshakeFlag.Confirm);
  43. Sender(payload, playerId).Run();
  44. }
  45. public IEnumerator Sender(SerializedCLreHandshake payload, int playerId)
  46. {
  47. yield return null;
  48. Utility.Logging.MetaLog("Sending Server CLre handshake");
  49. _sendMessage(CLre_HANDSHAKE_NETCODE, ref payload, playerId);
  50. yield return null;
  51. }
  52. internal static void Init()
  53. {
  54. Instance = new ServerHandshakeEngine();
  55. }
  56. }
  57. [HarmonyPatch(typeof(GameServer.GameFramework.MainGameServer), "SetupContainer")]
  58. class MainGameServer_SetupContainer_Patch
  59. {
  60. internal static object netMessageListener;
  61. internal static object netMessageSender;
  62. [HarmonyPostfix]
  63. public static void AfterMethodCall(object ____netMessageListener, object ____netMessageSender)
  64. {
  65. Utility.Logging.MetaLog($"Got NetMessage objects");
  66. netMessageListener = ____netMessageListener;
  67. netMessageSender = ____netMessageSender;
  68. }
  69. }
  70. }