Exemplo n.º 1
0
        private void CollectReflectionPacketHandlers()
        {
            foreach (MethodInfo methodInfo in AssemblyUtil.GetAllMethodsWithAttribute(typeof(ServerReceiveAttribute)))
            {
                if (!methodInfo.IsStatic)
                {
                    Debug.Log($"Server receive method {methodInfo.Name} is not static!");
                }
                else
                {
                    if (methodInfo.GetParameters().Length != 2 || methodInfo.GetParameters()[0].ParameterType != typeof(int) || methodInfo.GetParameters()[1].ParameterType != typeof(Packet))
                    {
                        Debug.Log($"Server receive method {methodInfo.Name} must have int as the first parameter and Packet as the second!");
                    }
                    else
                    {
                        PacketHandler          handler = (PacketHandler)methodInfo.CreateDelegate(typeof(PacketHandler));
                        ServerReceiveAttribute attrib  = methodInfo.GetCustomAttribute <ServerReceiveAttribute>();
                        if (!packetHandlers.ContainsKey(attrib.PacketID))
                        {
                            packetHandlers.Add(attrib.PacketID, new VerifiedPacketHandler(attrib.ExpectedVerification, handler));
                        }
                    }
                }
            }

            foreach (PacketID packetID in packetHandlers.Keys)
            {
                Debug.Log("Collected packet handler " + packetID);
            }
        }
Exemplo n.º 2
0
        private void CollectReflectionPacketHandlers()
        {
            foreach (MethodInfo methodInfo in AssemblyUtil.GetAllMethodsWithAttribute(typeof(ClientReceiveAttribute)))
            {
                if (!methodInfo.IsStatic)
                {
                    Debug.Log($"Client receive method {methodInfo.Name} is not static!");
                }
                else
                {
                    if (methodInfo.GetParameters().Length != 1 || methodInfo.GetParameters()[0].ParameterType != typeof(Packet))
                    {
                        Debug.Log($"Client receive method {methodInfo.Name} must have Packet as the first and only parameter!");
                    }
                    else
                    {
                        PacketHandler          handler = (PacketHandler)methodInfo.CreateDelegate(typeof(PacketHandler));
                        ClientReceiveAttribute attrib  = methodInfo.GetCustomAttribute <ClientReceiveAttribute>();
                        Debug.Log("Attrib found: " + attrib != null);
                        if (!packetHandlers.ContainsKey(attrib.PacketID))
                        {
                            packetHandlers.Add(attrib.PacketID, new VerifiedPacketHandler(attrib.ExpectedVerification, handler));
                        }
                    }
                }
            }

            foreach (PacketID packetID in packetHandlers.Keys)
            {
                Debug.Log("Collected packet handler " + packetID);
            }
        }