Beispiel #1
0
        public void CharacterListPacket_Initialization()
        {
            const OutboundPacketType ExpectedPacketType  = OutboundPacketType.CharacterList;
            const ushort             ExpectedPremiumDays = 7;

            ExceptionAssert.Throws <ArgumentException>(() => new CharacterListPacket(null, ExpectedPremiumDays), "Value cannot be null. (Parameter 'characters')");

            var expectedCharList = new List <CharacterLoginInformation>()
            {
                new CharacterLoginInformation()
                {
                    Name = "Char 1", Ip = IPAddress.Loopback.GetAddressBytes(), Port = 123, World = "Fibula"
                },
                new CharacterLoginInformation()
                {
                    Name = "Char 2", Ip = IPAddress.Any.GetAddressBytes(), Port = 321, World = "Fibula 2"
                },
            };

            var packet = new CharacterListPacket(expectedCharList, ExpectedPremiumDays);

            Assert.AreEqual(ExpectedPacketType, packet.PacketType, $"Expected {nameof(packet.PacketType)} to match {ExpectedPacketType}.");

            Assert.IsNotNull(packet.Characters);
            CollectionAssert.AreEqual(expectedCharList, packet.Characters.ToArray(), $"Expected {nameof(packet.Characters)} to match {expectedCharList}.");

            Assert.AreEqual(ExpectedPremiumDays, packet.PremiumDaysLeft, $"Expected {nameof(packet.PremiumDaysLeft)} to match {ExpectedPremiumDays}.");
        }
Beispiel #2
0
        /// <summary>
        /// Selects the most appropriate packet writer for the specified type.
        /// </summary>
        /// <param name="forPacketType">The type of packet.</param>
        /// <returns>An instance of an <see cref="IPacketWriter"/> implementation.</returns>
        public IPacketWriter SelectPacketWriter(OutboundPacketType forPacketType)
        {
            if (this.packetWritersMap.TryGetValue(forPacketType, out IPacketWriter writer))
            {
                return(writer);
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Registers a packet writer to this protocol.
        /// </summary>
        /// <param name="forType">The type of packet to register for.</param>
        /// <param name="packetWriter">The packet writer to register.</param>
        public void RegisterPacketWriter(OutboundPacketType forType, IPacketWriter packetWriter)
        {
            packetWriter.ThrowIfNull(nameof(packetWriter));

            if (this.packetWritersMap.ContainsKey(forType))
            {
                throw new InvalidOperationException($"There is already a writer registered for the packet type: {forType}.");
            }

            this.logger.LogTrace($"Registered packet writer for type {forType}.");

            this.packetWritersMap[forType] = packetWriter;
        }
Beispiel #4
0
 /// <summary>
 /// Create a new outbound packet
 /// </summary>
 /// <param name="udp">True if this is a UDP packet</param>
 public OutboundPacket(bool udp)
 {
     if (udp)
     {
         Append(Protocol.UDP_PROTOCOL_VERSION);
         Type = OutboundPacketType.UDP;
     }
     else
     {
         packetData.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00 });
         Type = OutboundPacketType.TCP;
     }
 }
Beispiel #5
0
        public void AddCreaturePacket_Initialization()
        {
            const OutboundPacketType ExpectedPacketType = OutboundPacketType.AddThing;
            var creatureMock = new Mock <ICreature>();
            var playerMock   = new Mock <IPlayer>();

            ExceptionAssert.Throws <ArgumentException>(() => new AddCreaturePacket(null, creatureMock.Object), "Value cannot be null. (Parameter 'player')");
            ExceptionAssert.Throws <ArgumentException>(() => new AddCreaturePacket(playerMock.Object, null), "Value cannot be null. (Parameter 'creature')");

            var packet = new AddCreaturePacket(playerMock.Object, creatureMock.Object);

            Assert.AreEqual(ExpectedPacketType, packet.PacketType, $"Expected {nameof(packet.PacketType)} to match {ExpectedPacketType}.");
            Assert.AreSame(creatureMock.Object, packet.Creature, $"Expected {nameof(packet.Creature)} to be the same instance as {creatureMock.Object}.");
        }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MapPartialDescriptionPacket"/> class.
        /// </summary>
        /// <param name="mapDescriptionType">The type of map description.</param>
        /// <param name="player">The player that will be receiving the description.</param>
        /// <param name="descriptionTiles">The description tiles.</param>
        public MapPartialDescriptionPacket(OutboundPacketType mapDescriptionType, IPlayer player, IEnumerable <ITile> descriptionTiles)
        {
            if (mapDescriptionType != OutboundPacketType.MapSliceEast &&
                mapDescriptionType != OutboundPacketType.MapSliceNorth &&
                mapDescriptionType != OutboundPacketType.MapSliceSouth &&
                mapDescriptionType != OutboundPacketType.MapSliceWest &&
                mapDescriptionType != OutboundPacketType.FloorChangeUp &&
                mapDescriptionType != OutboundPacketType.FloorChangeDown)
            {
                throw new ArgumentException($"Unsupported partial description type {mapDescriptionType}.", nameof(mapDescriptionType));
            }

            this.PacketType       = mapDescriptionType;
            this.Player           = player;
            this.DescriptionTiles = descriptionTiles;
        }
Beispiel #7
0
        public void AnimatedTextPacket_Initialization()
        {
            const OutboundPacketType ExpectedPacketType = OutboundPacketType.AnimatedText;
            const TextColor          ExpectedTextColor  = TextColor.Green;
            const string             ExpectedText       = "this is a test!";

            Location expectedLocation = new Location()
            {
                X = 100, Y = 150, Z = 7
            };

            var packet = new AnimatedTextPacket(expectedLocation, ExpectedTextColor, ExpectedText);

            Assert.AreEqual(ExpectedPacketType, packet.PacketType, $"Expected {nameof(packet.PacketType)} to match {ExpectedPacketType}.");
            Assert.AreEqual(expectedLocation, packet.Location, $"Expected {nameof(packet.Location)} to match {expectedLocation}.");
            Assert.AreEqual(ExpectedTextColor, packet.Color, $"Expected {nameof(packet.Color)} to match {ExpectedTextColor}.");
            Assert.AreEqual(ExpectedText, packet.Text, $"Expected {nameof(packet.Text)} to match {ExpectedText}.");
        }
Beispiel #8
0
 /// <summary>
 /// Attempts to convert an <see cref="OutboundPacketType"/> value into a byte value.
 /// </summary>
 /// <param name="packetType">The packet type to convert.</param>
 /// <returns>The byte value converted to.</returns>
 public static byte ToByte(this OutboundPacketType packetType)
 {
     return(packetType switch
     {
         OutboundPacketType.GatewayDisconnect => 0x0A,
         OutboundPacketType.MessageOfTheDay => 0x14,
         OutboundPacketType.CharacterList => 0x64,
         OutboundPacketType.PlayerLogin => 0x0A,
         OutboundPacketType.GamemasterFlags => 0x0B,
         OutboundPacketType.GameDisconnect => 0x14,
         OutboundPacketType.WaitingList => 0x16,
         OutboundPacketType.HeartbeatResponse => 0x1D,
         OutboundPacketType.Heartbeat => 0x1E,
         OutboundPacketType.Death => 0x28,
         OutboundPacketType.AddUnknownCreature => 0x61,
         OutboundPacketType.AddKnownCreature => 0x62,
         OutboundPacketType.MapDescription => 0x64,
         OutboundPacketType.MapSliceNorth => 0x65,
         OutboundPacketType.MapSliceEast => 0x66,
         OutboundPacketType.MapSliceSouth => 0x67,
         OutboundPacketType.MapSliceWest => 0x68,
         OutboundPacketType.TileUpdate => 0x69,
         OutboundPacketType.AddThing => 0x6A,
         OutboundPacketType.UpdateThing => 0x6B,
         OutboundPacketType.RemoveThing => 0x6C,
         OutboundPacketType.CreatureMoved => 0x6D,
         OutboundPacketType.ContainerOpen => 0x6E,
         OutboundPacketType.ContainerClose => 0x6F,
         OutboundPacketType.ContainerAddItem => 0x70,
         OutboundPacketType.ContainerUpdateItem => 0x71,
         OutboundPacketType.ContainerRemoveItem => 0x72,
         OutboundPacketType.InventoryItem => 0x78,
         OutboundPacketType.InventoryEmpty => 0x79,
         OutboundPacketType.WorldLight => 0x82,
         OutboundPacketType.MagicEffect => 0x83,
         OutboundPacketType.AnimatedText => 0x84,
         OutboundPacketType.ProjectileEffect => 0x85,
         OutboundPacketType.Square => 0x86,
         OutboundPacketType.CreatureHealth => 0x8C,
         OutboundPacketType.CreatureLight => 0x8D,
         OutboundPacketType.CreatureOutfit => 0x8E,
         OutboundPacketType.CreatureSpeedChange => 0x8F,
         OutboundPacketType.CreatureSkull => 0x90,
         OutboundPacketType.CreatureShield => 0x91,
         OutboundPacketType.TextWindow => 0x96,
         OutboundPacketType.HouseWindow => 0x97,
         OutboundPacketType.PlayerStats => 0xA0,
         OutboundPacketType.PlayerSkills => 0xA1,
         OutboundPacketType.PlayerConditions => 0xA2,
         OutboundPacketType.CancelAttack => 0xA3,
         OutboundPacketType.PlayerModes => 0xA7,
         OutboundPacketType.CreatureSpeech => 0xAA,
         OutboundPacketType.TextMessage => 0xB4,
         OutboundPacketType.CancelWalk => 0xB5,
         OutboundPacketType.FloorChangeUp => 0xBE,
         OutboundPacketType.FloorChangeDown => 0xBF,
         OutboundPacketType.OutfitWindow => 0xC8,
         OutboundPacketType.VipDetails => 0xD2,
         OutboundPacketType.VipOnline => 0xD3,
         OutboundPacketType.VipOffline => 0xD4,
         _ => throw new NotSupportedException($"Outgoing packet type {packetType} is not supported in this client version.")
     });