Esempio n. 1
0
        public static bool Load()
        {
            Handlers = new Dictionary <byte, Dictionary <byte, MethodInfo> >();
            foreach (var info in Reflector.FindMethodsByAttribute <PacketHandlerAttribute>())
            {
                PacketHandlerAttribute attribute = info.First;
                MethodInfo             method    = info.Second;
                if (!Handlers.ContainsKey(attribute.Header))
                {
                    Handlers.Add(attribute.Header, new Dictionary <byte, MethodInfo>());
                }
                if (Handlers[attribute.Header].ContainsKey(attribute.Type))
                {
                    Log.WriteLine(LogLevel.Warn, "Duplicate handler found: {0}:{1}", attribute.Header, attribute.Type);
                    Handlers[attribute.Header].Remove(attribute.Type);
                }
                Handlers[attribute.Header].Add(attribute.Type, method);
            }

            int count = 0;

            foreach (var dict in Handlers.Values)
            {
                count += dict.Count;
            }
            Log.WriteLine(LogLevel.Info, "{0} Handlers loaded.", count);
            return(true);
        }
Esempio n. 2
0
    static PacketSerializer()
    {
        RegisteredPackets = new Dictionary <ushort, PacketInfo>();

        foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(type => type.GetInterface("InPacket") != null))
        {
            object[] attributes = type.GetCustomAttributes(typeof(PacketHandlerAttribute), true); // get the attributes of the packet.
            if (attributes.Length == 0)
            {
                return;
            }
            PacketHandlerAttribute ma = (PacketHandlerAttribute)attributes[0];
            RegisteredPackets.Add(ma.MethodId, new PacketInfo {
                Size = ma.Size, Type = type
            });
        }

        if (DUMP_REGISTERED_PACKETS)
        {
            using (StreamWriter w = File.AppendText(Path.Combine(Directory.GetCurrentDirectory(), "Logs", "registered_packets.txt"))) {
                foreach (var pkt in RegisteredPackets)
                {
                    w.WriteLine($"{string.Format("0x{0:x3}", pkt.Key)},{pkt.Value.Size} \t//{pkt.Value.Type}");
                }
            }
        }
    }
Esempio n. 3
0
        public static bool Initialize()
        {
            _handlers = new Dictionary <ushort, MethodInfo>();

            foreach (var info in MapleHandler.FindMethodsByAttribute <PacketHandlerAttribute>())
            {
                PacketHandlerAttribute attribute = info.Left;
                MethodInfo             method    = info.Right;

                if (!_handlers.ContainsKey(attribute.Header))
                {
                    _handlers.Add(attribute.Header, method);
                }
            }

            return(true);
        }
Esempio n. 4
0
    static PacketSerializer()
    {
        RegisteredPackets = new Dictionary <ushort, PacketInfo>();

        foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(type => type.GetInterface("InPacket") != null))
        {
            object[] attributes = type.GetCustomAttributes(typeof(PacketHandlerAttribute), true); // get the attributes of the packet.
            if (attributes.Length == 0)
            {
                return;
            }
            PacketHandlerAttribute ma = (PacketHandlerAttribute)attributes[0];
            RegisteredPackets.Add(ma.MethodId, new PacketInfo {
                Size = ma.Size, Type = type
            });
        }
    }
Esempio n. 5
0
        private void LoadPacketHandlers(Type type)
        {
            var methods = type.GetMethods();

            foreach (MethodInfo method in methods)
            {
                {
                    PacketHandlerAttribute packetHandlerAttribute = Attribute.GetCustomAttribute(method, typeof(PacketHandlerAttribute), false) as PacketHandlerAttribute;
                    if (packetHandlerAttribute != null)
                    {
                        ParameterInfo[] parameters = method.GetParameters();
                        if (parameters.Length < 1)
                        {
                            continue;
                        }
                        if (!typeof(Package).IsAssignableFrom(parameters[0].ParameterType))
                        {
                            continue;
                        }
                        if (packetHandlerAttribute.PacketType == null)
                        {
                            packetHandlerAttribute.PacketType = parameters[0].ParameterType;
                        }

                        if (Attribute.GetCustomAttribute(method, typeof(SendAttribute), false) != null)
                        {
                            _packetSendHandlerDictionary.Add(method, packetHandlerAttribute);
                        }
                        else
                        {
                            _packetHandlerDictionary.Add(method, packetHandlerAttribute);
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        internal Package PluginPacketHandler(Package message, bool isReceiveHandler, Player player)
        {
            if (message == null)
            {
                return(null);
            }

            Package currentPackage = message;
            Package returnPacket   = currentPackage;

            try
            {
                Dictionary <MethodInfo, PacketHandlerAttribute> packetHandlers;
                if (isReceiveHandler)
                {
                    packetHandlers = _packetHandlerDictionary;
                }
                else
                {
                    packetHandlers = _packetSendHandlerDictionary;
                }

                if (packetHandlers == null)
                {
                    return(message);
                }

                foreach (var handler in packetHandlers)
                {
                    if (handler.Value == null)
                    {
                        continue;
                    }
                    if (handler.Key == null)
                    {
                        continue;
                    }

                    PacketHandlerAttribute atrib = handler.Value;
                    if (atrib.PacketType == null)
                    {
                        continue;
                    }

                    if (!atrib.PacketType.IsInstanceOfType(currentPackage) && atrib.PacketType != currentPackage.GetType())
                    {
                        //Log.Warn($"No assignable {atrib.PacketType.Name} from {currentPackage.GetType().Name}");
                        continue;
                    }

                    //Log.Warn($"IS assignable {atrib.PacketType.Name} from {currentPackage.GetType().Name}");

                    MethodInfo method = handler.Key;
                    if (method == null)
                    {
                        continue;
                    }
                    if (method.IsStatic)
                    {
                        //TODO: Move below and set pluginInstance = null instead
                        method.Invoke(null, new object[] { currentPackage, player });
                    }
                    else
                    {
                        object pluginInstance = _plugins.FirstOrDefault(plugin => plugin.GetType() == method.DeclaringType);
                        if (pluginInstance == null)
                        {
                            continue;
                        }

                        if (method.ReturnType == typeof(void))
                        {
                            ParameterInfo[] parameters = method.GetParameters();
                            if (parameters.Length == 1)
                            {
                                method.Invoke(pluginInstance, new object[] { currentPackage });
                            }
                            else if (parameters.Length == 2 && parameters[1].ParameterType == typeof(Player))
                            {
                                method.Invoke(pluginInstance, new object[] { currentPackage, player });
                            }
                        }
                        else
                        {
                            ParameterInfo[] parameters = method.GetParameters();
                            if (parameters.Length == 1)
                            {
                                returnPacket = method.Invoke(pluginInstance, new object[] { currentPackage }) as Package;
                            }
                            else if (parameters.Length == 2 && parameters[1].ParameterType == typeof(Player))
                            {
                                returnPacket = method.Invoke(pluginInstance, new object[] { currentPackage, player }) as Package;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //For now we will just ignore this, not to big of a deal.
                //Will have to think a bit more about this later on.
                Log.Warn("Plugin Error: ", ex);
                Log.Warn("Plugin Error: ", ex.InnerException);
            }

            return(returnPacket);
        }