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.

51 lines
1.5KB

  1. using System;
  2. using System.Collections.Generic;
  3. using GameNetworkLayer.Shared;
  4. namespace CLre.API.Synergy
  5. {
  6. public static class Message
  7. {
  8. internal const NetworkDispatcherCode CLre_MESSAGE_NETCODE = (NetworkDispatcherCode) 219;
  9. private static readonly Dictionary<uint, Action<ReceiveMessageArgs>> handlers =
  10. new Dictionary<uint, Action<ReceiveMessageArgs>>();
  11. private static readonly ClientMessagingEngine msgEngine = new ClientMessagingEngine();
  12. public static void SendCLreMessage(ref SerializedCLreMessage message)
  13. {
  14. msgEngine.EnqueueMessage(ref message);
  15. }
  16. public static void RegisterListener(uint id, Action<ReceiveMessageArgs> handler)
  17. {
  18. if (handlers.TryGetValue(id, out Action<ReceiveMessageArgs> existing))
  19. {
  20. existing += handler;
  21. handlers[id] = existing;
  22. }
  23. else
  24. {
  25. handlers[id] = handler;
  26. }
  27. }
  28. internal static void HandleMessageReceive(ref SerializedCLreMessage msg)
  29. {
  30. ReceiveMessageArgs payload = new ReceiveMessageArgs
  31. {
  32. Message = msg,
  33. };
  34. if (handlers.TryGetValue(msg.Id, out Action<ReceiveMessageArgs> h))
  35. {
  36. h(payload);
  37. }
  38. }
  39. }
  40. public struct ReceiveMessageArgs
  41. {
  42. public SerializedCLreMessage Message;
  43. }
  44. }