/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is TextMessagePacket textMessagePacket)) { this.Logger.LogWarning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(textMessagePacket.PacketType.ToByte()); byte valueToSend = textMessagePacket.Type switch { MessageType.CenterWhite => 0x13, MessageType.CenterGreen => 0x16, MessageType.CenterRed => 0x12, MessageType.Status => 0x15, MessageType.StatusNoConsole => 0x17, MessageType.ConsoleOnlyBlue => 0x18, MessageType.ConsoleOnlyRed => 0x19, MessageType.ConsoleOnlyYellow => 0x01, MessageType.ConsoleOnlyLightBlue => 0x04, MessageType.ConsoleOnlyOrange => 0x11, _ => 0x14, }; message.AddByte(valueToSend); message.AddString(textMessagePacket.Message); }
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is CreatureSpeechPacket creatureSpeechPacket)) { this.Logger.LogWarning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(creatureSpeechPacket.PacketType.ToByte()); message.AddUInt32(0); message.AddString(creatureSpeechPacket.SenderName); message.AddByte(creatureSpeechPacket.SpeechType switch { // ChannelRed = 0x05, //Talk red on chat - #c // PrivateRed = 0x04, //Red private - @name@ text // ChannelOrange = 0x05, //Talk orange on text // ChannelRedAnonymous = 0x05, //Talk red anonymously on chat - #d // MonsterYell = 0x0E, //Yell orange SpeechType.Normal => 0x01, SpeechType.Whisper => 0x02, SpeechType.Yell => 0x03, SpeechType.Private => 0x04, SpeechType.ChannelYellow => 0x05, SpeechType.RuleViolationReport => 0x06, SpeechType.RuleViolationAnswer => 0x07, SpeechType.RuleViolationContinue => 0x08, SpeechType.Broadcast => 0x09, SpeechType.MonsterNormal => 0x0E, _ => 0x01, });
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is CreatureSpeechPacket creatureSpeechPacket)) { this.Logger.LogWarning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(creatureSpeechPacket.PacketType.ToByte()); message.AddUInt32(0); message.AddString(creatureSpeechPacket.SenderName); message.AddByte((byte)creatureSpeechPacket.SpeechType); switch (creatureSpeechPacket.SpeechType) { case SpeechType.Say: case SpeechType.Whisper: case SpeechType.Yell: case SpeechType.MonsterSay: // case SpeechType.MonsterYell: message.AddLocation(creatureSpeechPacket.Location); break; // case SpeechType.ChannelRed: // case SpeechType.ChannelRedAnonymous: // case SpeechType.ChannelOrange: case SpeechType.ChannelYellow: // case SpeechType.ChannelWhite: message.AddUInt16((ushort)creatureSpeechPacket.Channel); break; case SpeechType.RuleViolationReport: message.AddUInt32(creatureSpeechPacket.Time); break; default: break; } message.AddString(creatureSpeechPacket.Text); }
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is GameServerDisconnectPacket gameServerDisconnectPacket)) { this.Logger.Warning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(gameServerDisconnectPacket.PacketType.ToByte()); message.AddString(gameServerDisconnectPacket.Reason); }
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is MessageOfTheDayPacket messageOfTheDayPacket)) { this.Logger.Warning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(messageOfTheDayPacket.PacketType); message.AddString("1\n" + messageOfTheDayPacket.MessageOfTheDay); }
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is TextMessagePacket textMessagePacket)) { this.Logger.LogWarning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(textMessagePacket.PacketType.ToByte()); message.AddByte((byte)textMessagePacket.Type); message.AddString(textMessagePacket.Message); }
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is CharacterListPacket characterListPacket)) { this.Logger.Warning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(characterListPacket.PacketType); message.AddByte((byte)characterListPacket.Characters.Count()); foreach (var character in characterListPacket.Characters) { message.AddString(character.Name); message.AddString(character.World); message.AddBytes(character.Ip); message.AddUInt16(character.Port); } message.AddUInt16(characterListPacket.PremiumDaysLeft); }
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is AnimatedTextPacket animatedTextPacket)) { this.Logger.LogWarning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(animatedTextPacket.PacketType.ToByte()); message.AddLocation(animatedTextPacket.Location); message.AddByte((byte)animatedTextPacket.Color); message.AddString(animatedTextPacket.Text); }
/// <summary> /// Writes a packet to the given <see cref="INetworkMessage"/>. /// </summary> /// <param name="packet">The packet to write.</param> /// <param name="message">The message to write into.</param> public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { if (!(packet is ContainerOpenPacket containerOpenPacket)) { this.Logger.LogWarning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); return; } message.AddByte(containerOpenPacket.PacketType.ToByte()); message.AddByte(containerOpenPacket.ContainerId); message.AddUInt16(containerOpenPacket.TypeId); message.AddString(containerOpenPacket.Name); message.AddByte(containerOpenPacket.Volume); message.AddByte(Convert.ToByte(containerOpenPacket.HasParent ? 0x01 : 0x00)); message.AddByte(Convert.ToByte(containerOpenPacket.Contents.Count)); foreach (var item in containerOpenPacket.Contents) { message.AddItem(item); } }
public void NetworkMessage_AddStringTest() { const string StringToAdd = "Moonwalking."; const ushort ExpectedStringLen = 12; INetworkMessage testMessage1 = this.SetupOutboundMessage(); Assert.IsNotNull(testMessage1); Assert.AreEqual(DefaultTestBufferOutgoingMessageCursor, testMessage1.Cursor); Assert.AreEqual(DefaultTestBufferOutgoingMessageLength, testMessage1.Length); // Add to the message testMessage1.AddString(StringToAdd); var asInboundMessage = ConvertToInboundMessage(testMessage1); // Leverage other NetworkMessage methods, which is fine as they have their own tests. var strLength = asInboundMessage.GetUInt16(); var strRead = Encoding.Default.GetString(asInboundMessage.Buffer.ToArray(), asInboundMessage.Cursor, strLength); Assert.AreEqual(ExpectedStringLen, strLength); Assert.AreEqual(StringToAdd, strRead); }
/// <summary> /// Add a <see cref="ICreature"/>'s description to the message. /// </summary> /// <param name="message">The message to add the creature description to.</param> /// <param name="creature">The creature to describe and add.</param> /// <param name="asKnown">A value indicating whether this creature is known.</param> /// <param name="creatureToRemoveId">The id of another creature to replace if the client buffer is known to be full.</param> public static void AddCreature(this INetworkMessage message, ICreature creature, bool asKnown, uint creatureToRemoveId) { if (asKnown) { message.AddByte((byte)OutgoingGamePacketType.AddKnownCreature); // known message.AddByte(0x00); message.AddUInt32(creature.Id); } else { message.AddByte((byte)OutgoingGamePacketType.AddUnknownCreature); // unknown message.AddByte(0x00); message.AddUInt32(creatureToRemoveId); message.AddUInt32(creature.Id); message.AddString(creature.Name); } message.AddByte(Convert.ToByte(Math.Min(100, creature.Hitpoints * 100 / creature.MaxHitpoints))); // health bar, needs a percentage. message.AddByte(Convert.ToByte(creature.Direction.GetClientSafeDirection())); if (creature.IsInvisible) { message.AddUInt16(0x00); message.AddUInt16(0x00); } else { message.AddOutfit(creature.Outfit); } message.AddByte(creature.EmittedLightLevel); message.AddByte(creature.EmittedLightColor); message.AddUInt16(creature.Speed); message.AddByte(creature.Skull); message.AddByte(creature.Shield); }