using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Unity.Networking.Transport; using KairoEngine.Core; using Sirenix.OdinInspector; namespace KairoEngine.Multiplayer { [HideMonoScript] public class NetMsgController : MonoBehaviour { private static List netMessageTypes = new List(); private void Start() { GetNetMessageTypes(); } private void GetNetMessageTypes() { netMessageTypes = ReflectiveEnumerator.GetEnumerableOfType() .Where(x => x.code != 0) .Select(x => { return x; }) .ToList(); } public NetMsg FindWithCode(uint code) { for (int i = 0; i < netMessageTypes.Count; i++) { if(netMessageTypes[i].code == code) return netMessageTypes[i]; } return null; } public void ReceiveData(ref DataStreamReader stream, ServerBehaviour server, int clientId) { uint code = stream.ReadByte(); NetMsg msg = FindWithCode(code).ReceiveMessage(server, ref stream, clientId); msg.ReadMessage(server, clientId); } public void ReceiveData(ref DataStreamReader stream, ClientBehaviour client) { uint code = stream.ReadByte(); NetMsg msg = FindWithCode(code).ReceiveMessage(client, ref stream); msg.ReadMessage(client); } public void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId) { FindWithCode(code).SendMessage(server, netMsg, ref writer, clientId); } public void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client) { FindWithCode(code).SendMessage(client, netMsg, ref writer); } // Remove rest of the code below /* public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, int clientId = -1) => Deserialize(code, ref stream, server, null, clientId); public static void Deserialize(uint code, ref DataStreamReader stream, ClientBehaviour client, int clientId = -1) => Deserialize(code, ref stream, null, client, clientId); public static void Deserialize(uint code, ref DataStreamReader stream, ServerBehaviour server, ClientBehaviour client, int clientId) { NetOpCode opCode = (NetOpCode)code; switch (opCode) { case NetOpCode.Disconnect: NetDisconnectMsg netDisconnectMsg = new NetDisconnectMsg(); netDisconnectMsg.Deserialize(ref stream); if(client != null) { string text = $"Server disconnected client"; if(client.debug) Debug.Log(text); NetMsgEvents.Trigger($"{client.eventStreamName}_ServerDisconnect", text, -1, code, netDisconnectMsg); } if(server != null) { string text = $"Client is disconnecting"; if(server.debug) Debug.Log(text); NetMsgEvents.Trigger($"{server.eventStreamName}_ClientDisconnect", text, clientId, code, netDisconnectMsg); } break; case NetOpCode.Handshake: NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg(); netHandshakeMsg.Deserialize(ref stream); if(client != null) { string text = $"Server accepted handshake (playerName = {netHandshakeMsg.playerName})"; if(client.debug) Debug.Log(text); NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, netHandshakeMsg); } if(server != null) { string text = $"Received handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})"; if(server.debug) Debug.Log(text); NetMsgEvents.Trigger($"{server.eventStreamName}_HandshakeReceived", text, clientId, code, netHandshakeMsg); } break; case NetOpCode.ClientConnected: NetClientConnectedMsg netClientConnectedMsg = new NetClientConnectedMsg(); netClientConnectedMsg.Deserialize(ref stream); if(client != null) { string text = $"New peer connected to server (playerName = {netClientConnectedMsg.playerName})"; if(client.debug) Debug.Log(text); NetMsgEvents.Trigger($"{client.eventStreamName}_PeerConnected", text, -1, code, netClientConnectedMsg); } break; case NetOpCode.ClientDisconnected: NetClientDisconnectedMsg netClientDisconnectedMsg = new NetClientDisconnectedMsg(); netClientDisconnectedMsg.Deserialize(ref stream); if(client != null) { string text = $"Peer disconnected to server (playerName = {netClientDisconnectedMsg.playerName})"; if(client.debug) Debug.Log(text); NetMsgEvents.Trigger($"{client.eventStreamName}_PeerDisconnected", text, -1, code, netClientDisconnectedMsg); } break; default: NetMsg netMsg = new NetMsg(); netMsg.Deserialize(ref stream); string error = $"Received data with invalid NetOpCode {code}"; if(client != null) { if(client.debug) Debug.Log(error); NetMsgEvents.Trigger($"{client.eventStreamName}_InvalidOpCode", error, -1, code, netMsg); } if(server != null) { if(server.debug) Debug.Log(error); NetMsgEvents.Trigger($"{server.eventStreamName}_InvalidOpCode", error, clientId, code, netMsg); } break; } } public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId = -1) => Serialize(code, netMsg, ref writer, server, null, clientId); public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client, int clientId = -1 ) => Serialize(code, netMsg, ref writer, null, client, clientId); public static void Serialize(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, ClientBehaviour client, int clientId) { NetOpCode opCode = (NetOpCode)code; switch (opCode) { case NetOpCode.Disconnect: NetDisconnectMsg netDisconnectMsg = (NetDisconnectMsg)netMsg; if(client != null) { string text = $"Sending disconnect message to server"; if(client.debug) Debug.Log(text); NetMsgEvents.Trigger($"{client.eventStreamName}_SendingDisconnectMessageToServer", text, clientId, code, netDisconnectMsg); } if(server != null) { string text = $"Sending disconnect message to client (ID {clientId})"; if(server.debug) Debug.Log(text); NetMsgEvents.Trigger($"{server.eventStreamName}_SendingDisconnectMessageToClient", text, clientId, code, netDisconnectMsg); } netDisconnectMsg.Serialize(ref writer); break; case NetOpCode.Handshake: NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg; if(client != null) { string text = $"Sending handshake (playerName = {netHandshakeMsg.playerName})"; if(client.debug) Debug.Log(text); NetMsgEvents.Trigger($"{client.eventStreamName}_SendingHandshake", text, clientId, code, netHandshakeMsg); } if(server != null) { string text = $"Accepting handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})"; if(server.debug) Debug.Log(text); NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netHandshakeMsg); } netHandshakeMsg.Serialize(ref writer); break; case NetOpCode.ClientConnected: NetClientConnectedMsg netClientConnectedMsg = (NetClientConnectedMsg)netMsg; if(server != null) { string text = $"Alerting clients of new connection (playerName = {netClientConnectedMsg.playerName})"; if(server.debug) Debug.Log(text); NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientConnectedMsg", text, clientId, code, netClientConnectedMsg); } netClientConnectedMsg.Serialize(ref writer); break; case NetOpCode.ClientDisconnected: NetClientDisconnectedMsg netClientDisconnectedMsg = (NetClientDisconnectedMsg)netMsg; if(server != null) { string text = $"Alerting clients of disconnection (playerName = {netClientDisconnectedMsg.playerName})"; if(server.debug) Debug.Log(text); NetMsgEvents.Trigger($"{server.eventStreamName}_SendingClientDisconnectedMsg", text, clientId, code, netClientDisconnectedMsg); } netClientDisconnectedMsg.Serialize(ref writer); break; default: break; } } */ } }