using System; using System.Collections.Generic; using GameNetworkLayer.Shared; namespace CLre_server.API.Synergy { public static class Message { internal const NetworkDispatcherCode CLre_MESSAGE_NETCODE = (NetworkDispatcherCode) 219; private static readonly Dictionary> handlers = new Dictionary>(); private static readonly ServerMessagingEngine msgEngine = new ServerMessagingEngine(); private static readonly List clrePlayers = new List(); public static void SendToAllCLreClients(ref SerializedCLreMessage message) { foreach (var playerId in clrePlayers) { SendCLreMessage(playerId, ref message); } } public static void SendCLreMessage(int playerId, ref SerializedCLreMessage message) { msgEngine.EnqueueMessage(playerId, ref message); } public static void RegisterListener(uint id, Action handler) { if (handlers.TryGetValue(id, out Action existing)) { existing += handler; handlers[id] = existing; } else { handlers[id] = handler; } } internal static void HandleMessageReceive(int playerId, ref SerializedCLreMessage msg) { ReceiveMessageArgs payload = new ReceiveMessageArgs { Message = msg, PlayerId = playerId, }; if (handlers.TryGetValue(msg.Id, out Action h)) { h(payload); } } } public struct ReceiveMessageArgs { public SerializedCLreMessage Message; public int PlayerId; } }