Esempio n. 1
0
        /// <summary>
        /// Checks all types in executing assmebly and registers packets.
        /// </summary>
        public static void RegisterPackets()
        {
            // Load tables
            LoadTables("Data\\PacketTables.xml");

            // Load packets
            Type[] types = Assembly.GetExecutingAssembly().GetTypes();

            foreach (Type type in types)
            {
                UltimaPacketAttribute[] attrs = (UltimaPacketAttribute[])type.GetCustomAttributes(typeof(UltimaPacketAttribute), false);

                if (attrs.Length == 1)
                {
                    UltimaPacketAttribute ultimaPacket = attrs[0];

                    if (ultimaPacket.Ids == null || ultimaPacket.Ids.Length == 0)
                    {
                        throw new SpyException("Packet {0} must have at least one ID", type);
                    }

                    _PacketTable.RegisterPacket(type, ultimaPacket, 0);
                }
                else if (attrs.Length > 1)
                {
                    throw new SpyException("Class {0} has too many UltimaPacket attributes", type);
                }
            }

            _DefaultDefinition = new UltimaPacketDefinition(typeof(UltimaPacket), null);
        }
Esempio n. 2
0
        /// <summary>
        /// Constructs a new instance of UltimaPacketDefinition.
        /// </summary>
        /// <param name="type">Packet type.</param>
        /// <param name="attribute">Ultima packet attribute.</param>
        public UltimaPacketDefinition( Type type, UltimaPacketAttribute attribute )
            : base(type)
        {
            _Attribute = attribute;
            _IsDefault = attribute == null;

            // Construct constructor delegate
            ConstructorInfo constructor = type.GetConstructor( new Type[] { } );

            if ( constructor == null )
                throw new SpyException( "Type '{0}' does not have a constructor with no parameters", type );

            DynamicMethod dynamicMethod = new DynamicMethod( "CreateInstance", type, null );
            ILGenerator generator = dynamicMethod.GetILGenerator();
            generator.Emit( OpCodes.Newobj, constructor );
            generator.Emit( OpCodes.Ret );

            _Constructor = (UltimaPacketConstructor) dynamicMethod.CreateDelegate( typeof( UltimaPacketConstructor ) );
        }
Esempio n. 3
0
        /// <summary>
        /// Constructs a new instance of UltimaPacketDefinition.
        /// </summary>
        /// <param name="type">Packet type.</param>
        /// <param name="attribute">Ultima packet attribute.</param>
        public UltimaPacketDefinition(Type type, UltimaPacketAttribute attribute) : base(type)
        {
            _Attribute = attribute;
            _IsDefault = attribute == null;

            // Construct constructor delegate
            ConstructorInfo constructor = type.GetConstructor(new Type[] { });

            if (constructor == null)
            {
                throw new SpyException("Type '{0}' does not have a constructor with no parameters", type);
            }

            DynamicMethod dynamicMethod = new DynamicMethod("CreateInstance", type, null);
            ILGenerator   generator     = dynamicMethod.GetILGenerator();

            generator.Emit(OpCodes.Newobj, constructor);
            generator.Emit(OpCodes.Ret);

            _Constructor = (UltimaPacketConstructor)dynamicMethod.CreateDelegate(typeof(UltimaPacketConstructor));
        }
Esempio n. 4
0
        /// <summary>
        /// Tries to register packet by UltimaPacketAttribute.
        /// </summary>
        /// <param name="packetType">Type to register.</param>
        /// <param name="packetIds">Packet IDs.</param>
        /// <param name="index">Current packet ID index.</param>
        public void RegisterPacket( Type packetType, UltimaPacketAttribute packet, int index )
        {
            byte id = packet.Ids[ index ];
            object item = _Table[ id ];

            if ( packet.Ids.Length - 1 == index )
            {
                UltimaPacketTableEntry entry = item as UltimaPacketTableEntry;

                if ( item is UltimaPacketTable )
                    throw new SpyException( "Packet '{0}' is missing one or more IDs", packetType );

                if ( entry == null )
                    entry = new UltimaPacketTableEntry();

                if ( packet.Direction == UltimaPacketDirection.FromClient )
                {
                    if ( entry.FromClient != null )
                        throw new SpyException( "Packet from client with ID '{0}' already exists", id );

                    entry.FromClient = new UltimaPacketDefinition( packetType, packet );
                }
                else if ( packet.Direction == UltimaPacketDirection.FromServer )
                {
                    if ( entry.FromServer != null )
                        throw new SpyException( "Packet from server with ID '{0}' already exists", id );

                    entry.FromServer = new UltimaPacketDefinition( packetType, packet );
                }
                else if ( packet.Direction == UltimaPacketDirection.FromBoth )
                {
                    if ( entry.FromClient != null )
                        throw new SpyException( "Packet from client with ID '{0}' already exists", id );
                    else if ( entry.FromServer != null )
                        throw new SpyException( "Packet from server with ID '{0}' already exists", id );

                    entry.FromClient = new UltimaPacketDefinition( packetType, packet );
                    entry.FromServer = entry.FromClient;
                }

                _Table[ id ] = entry;
            }
            else if ( item is UltimaPacketTable )
            {
                UltimaPacketTable table = (UltimaPacketTable) item;
                table.RegisterPacket( packetType, packet, index + 1 );
            }
            else
                throw new SpyException( "Table for Packet '{0}' not defined", packetType );
        }
Esempio n. 5
0
        /// <summary>
        /// Tries to register packet by UltimaPacketAttribute.
        /// </summary>
        /// <param name="packetType">Type to register.</param>
        /// <param name="packetIds">Packet IDs.</param>
        /// <param name="index">Current packet ID index.</param>
        public void RegisterPacket(Type packetType, UltimaPacketAttribute packet, int index)
        {
            byte   id   = packet.Ids[index];
            object item = _Table[id];

            if (packet.Ids.Length - 1 == index)
            {
                UltimaPacketTableEntry entry = item as UltimaPacketTableEntry;

                if (item is UltimaPacketTable)
                {
                    throw new SpyException("Packet '{0}' is missing one or more IDs", packetType);
                }

                if (entry == null)
                {
                    entry = new UltimaPacketTableEntry();
                }

                if (packet.Direction == UltimaPacketDirection.FromClient)
                {
                    if (entry.FromClient != null)
                    {
                        throw new SpyException("Packet from client with ID '{0}' already exists", id);
                    }

                    entry.FromClient = new UltimaPacketDefinition(packetType, packet);
                }
                else if (packet.Direction == UltimaPacketDirection.FromServer)
                {
                    if (entry.FromServer != null)
                    {
                        throw new SpyException("Packet from server with ID '{0}' already exists", id);
                    }

                    entry.FromServer = new UltimaPacketDefinition(packetType, packet);
                }
                else if (packet.Direction == UltimaPacketDirection.FromBoth)
                {
                    if (entry.FromClient != null)
                    {
                        throw new SpyException("Packet from client with ID '{0}' already exists", id);
                    }
                    else if (entry.FromServer != null)
                    {
                        throw new SpyException("Packet from server with ID '{0}' already exists", id);
                    }

                    entry.FromClient = new UltimaPacketDefinition(packetType, packet);
                    entry.FromServer = entry.FromClient;
                }

                _Table[id] = entry;
            }
            else if (item is UltimaPacketTable)
            {
                UltimaPacketTable table = (UltimaPacketTable)item;
                table.RegisterPacket(packetType, packet, index + 1);
            }
            else
            {
                throw new SpyException("Table for Packet '{0}' not defined", packetType);
            }
        }