Updates Message Packets (#321)
### API Breaking Change Combined `AsciiMessage` and `UnicodeMessage` into a single function that takes two arguments, `bool ascii` and `string lang`. Lang can be null (or anything) if ascii is true. - [X] Changes message packets - [X] Cleans up some code - [X] Uses benchmarks to determine if the spanwriter + copyfrom
This commit is contained in:
parent
10d884fe30
commit
b6cd408623
59 changed files with 1160 additions and 1025 deletions
|
|
@ -1,207 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: MessagePackets.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
[Flags]
|
||||
public enum AffixType : byte
|
||||
{
|
||||
Append = 0x00,
|
||||
Prepend = 0x01,
|
||||
System = 0x02
|
||||
}
|
||||
|
||||
public sealed class MessageLocalized : Packet
|
||||
{
|
||||
private static readonly MessageLocalized[] m_Cache_IntLoc = new MessageLocalized[15000];
|
||||
private static readonly MessageLocalized[] m_Cache_CliLoc = new MessageLocalized[100000];
|
||||
private static readonly MessageLocalized[] m_Cache_CliLocCmp = new MessageLocalized[5000];
|
||||
|
||||
public MessageLocalized(
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name, string args
|
||||
) : base(0xC1)
|
||||
{
|
||||
name ??= "";
|
||||
args ??= "";
|
||||
|
||||
if (hue == 0)
|
||||
{
|
||||
hue = 0x3B2;
|
||||
}
|
||||
|
||||
EnsureCapacity(50 + args.Length * 2);
|
||||
|
||||
Stream.Write(serial);
|
||||
Stream.Write((short)graphic);
|
||||
Stream.Write((byte)type);
|
||||
Stream.Write((short)hue);
|
||||
Stream.Write((short)font);
|
||||
Stream.Write(number);
|
||||
Stream.WriteAsciiFixed(name, 30);
|
||||
Stream.WriteLittleUniNull(args);
|
||||
}
|
||||
|
||||
public static MessageLocalized InstantiateGeneric(int number)
|
||||
{
|
||||
MessageLocalized[] cache = null;
|
||||
var index = 0;
|
||||
|
||||
if (number >= 3000000)
|
||||
{
|
||||
cache = m_Cache_IntLoc;
|
||||
index = number - 3000000;
|
||||
}
|
||||
else if (number >= 1000000)
|
||||
{
|
||||
cache = m_Cache_CliLoc;
|
||||
index = number - 1000000;
|
||||
}
|
||||
else if (number >= 500000)
|
||||
{
|
||||
cache = m_Cache_CliLocCmp;
|
||||
index = number - 500000;
|
||||
}
|
||||
|
||||
MessageLocalized p;
|
||||
|
||||
if (cache != null && index < cache.Length)
|
||||
{
|
||||
p = cache[index];
|
||||
|
||||
if (p == null)
|
||||
{
|
||||
cache[index] = p = new MessageLocalized(
|
||||
Serial.MinusOne,
|
||||
-1,
|
||||
MessageType.Regular,
|
||||
0x3B2,
|
||||
3,
|
||||
number,
|
||||
"System",
|
||||
""
|
||||
);
|
||||
p.SetStatic();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
p = new MessageLocalized(Serial.MinusOne, -1, MessageType.Regular, 0x3B2, 3, number, "System", "");
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MessageLocalizedAffix : Packet
|
||||
{
|
||||
public MessageLocalizedAffix(
|
||||
Serial serial, int graphic, MessageType messageType, int hue, int font, int number,
|
||||
string name, AffixType affixType, string affix, string args
|
||||
) : base(0xCC)
|
||||
{
|
||||
name ??= "";
|
||||
affix ??= "";
|
||||
args ??= "";
|
||||
|
||||
if (hue == 0)
|
||||
{
|
||||
hue = 0x3B2;
|
||||
}
|
||||
|
||||
EnsureCapacity(52 + affix.Length + args.Length * 2);
|
||||
|
||||
Stream.Write(serial);
|
||||
Stream.Write((short)graphic);
|
||||
Stream.Write((byte)messageType);
|
||||
Stream.Write((short)hue);
|
||||
Stream.Write((short)font);
|
||||
Stream.Write(number);
|
||||
Stream.Write((byte)affixType);
|
||||
Stream.WriteAsciiFixed(name, 30);
|
||||
Stream.WriteAsciiNull(affix);
|
||||
Stream.WriteBigUniNull(args);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class AsciiMessage : Packet
|
||||
{
|
||||
public AsciiMessage(
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, string name, string text
|
||||
) : base(0x1C)
|
||||
{
|
||||
name ??= "";
|
||||
text ??= "";
|
||||
|
||||
if (hue == 0)
|
||||
{
|
||||
hue = 0x3B2;
|
||||
}
|
||||
|
||||
EnsureCapacity(45 + text.Length);
|
||||
|
||||
Stream.Write(serial);
|
||||
Stream.Write((short)graphic);
|
||||
Stream.Write((byte)type);
|
||||
Stream.Write((short)hue);
|
||||
Stream.Write((short)font);
|
||||
Stream.WriteAsciiFixed(name, 30);
|
||||
Stream.WriteAsciiNull(text);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class UnicodeMessage : Packet
|
||||
{
|
||||
public UnicodeMessage(
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, string lang, string name,
|
||||
string text
|
||||
) : base(0xAE)
|
||||
{
|
||||
if (string.IsNullOrEmpty(lang))
|
||||
{
|
||||
lang = "ENU";
|
||||
}
|
||||
|
||||
name ??= "";
|
||||
text ??= "";
|
||||
|
||||
if (hue == 0)
|
||||
{
|
||||
hue = 0x3B2;
|
||||
}
|
||||
|
||||
EnsureCapacity(50 + text.Length * 2);
|
||||
|
||||
Stream.Write(serial);
|
||||
Stream.Write((short)graphic);
|
||||
Stream.Write((byte)type);
|
||||
Stream.Write((short)hue);
|
||||
Stream.Write((short)font);
|
||||
Stream.WriteAsciiFixed(lang, 4);
|
||||
Stream.WriteAsciiFixed(name, 30);
|
||||
Stream.WriteBigUniNull(text);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FollowMessage : Packet
|
||||
{
|
||||
public FollowMessage(Serial serial1, Serial serial2) : base(0x15, 9)
|
||||
{
|
||||
Stream.Write(serial1);
|
||||
Stream.Write(serial2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -25,6 +25,19 @@ namespace Server.Network
|
|||
public const int HuedEffectLength = 36;
|
||||
public const int BoltEffectLength = 36;
|
||||
|
||||
public static void SendSoundEffect(this NetState ns, int soundID, IPoint3D target)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[SoundPacketLength];
|
||||
CreateSoundEffect(ref buffer, soundID, target);
|
||||
|
||||
ns.Send(buffer);
|
||||
}
|
||||
|
||||
public static void CreateSoundEffect(ref Span<byte> buffer, int soundID, IPoint3D target)
|
||||
{
|
||||
var writer = new SpanWriter(buffer);
|
||||
|
|
@ -37,25 +50,6 @@ namespace Server.Network
|
|||
writer.Write((short)target.Z);
|
||||
}
|
||||
|
||||
public static void SendSoundEffect(this NetState ns, int soundID, IPoint3D target)
|
||||
{
|
||||
if (ns == null || !ns.GetSendBuffer(out var buffer))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new CircularBufferWriter(buffer);
|
||||
writer.Write((byte)0x54); // Packet ID
|
||||
writer.Write((byte)1); // flags
|
||||
writer.Write((short)soundID);
|
||||
writer.Write((short)0); // volume
|
||||
writer.Write((short)target.X);
|
||||
writer.Write((short)target.Y);
|
||||
writer.Write((short)target.Z);
|
||||
|
||||
ns.Send(ref buffer, writer.Position);
|
||||
}
|
||||
|
||||
public static void CreateParticleEffect(
|
||||
ref Span<byte> buffer,
|
||||
EffectType type, Serial from, Serial to, int itemID, IPoint3D fromPoint, IPoint3D toPoint,
|
||||
|
|
|
|||
|
|
@ -36,11 +36,7 @@ namespace Server.Network
|
|||
{
|
||||
public static void SendDisplayEquipmentInfo(
|
||||
this NetState ns,
|
||||
Serial serial,
|
||||
int number,
|
||||
string crafterName,
|
||||
bool unidentified,
|
||||
List<EquipInfoAttribute> attrs
|
||||
Serial serial, int number, string crafterName, bool unidentified, List<EquipInfoAttribute> attrs
|
||||
)
|
||||
{
|
||||
if (ns == null || !ns.GetSendBuffer(out var buffer))
|
||||
|
|
|
|||
|
|
@ -72,11 +72,9 @@ namespace Server.Network
|
|||
}
|
||||
}
|
||||
|
||||
var length = writer.Position;
|
||||
writer.Seek(1, SeekOrigin.Begin);
|
||||
writer.Write((ushort)length);
|
||||
writer.WritePacketLength();
|
||||
|
||||
ns.Send(ref buffer, length);
|
||||
ns.Send(ref buffer, writer.Position);
|
||||
}
|
||||
|
||||
public static void SendDisplayQuestionMenu(this NetState ns, QuestionMenu menu)
|
||||
|
|
@ -129,11 +127,9 @@ namespace Server.Network
|
|||
}
|
||||
}
|
||||
|
||||
var length = writer.Position;
|
||||
writer.Seek(1, SeekOrigin.Begin);
|
||||
writer.Write((ushort)length);
|
||||
writer.WritePacketLength();
|
||||
|
||||
ns.Send(ref buffer, length);
|
||||
ns.Send(ref buffer, writer.Position);
|
||||
}
|
||||
|
||||
public static void SendDisplayContextMenu(this NetState ns, ContextMenu menu)
|
||||
|
|
@ -209,11 +205,9 @@ namespace Server.Network
|
|||
}
|
||||
}
|
||||
|
||||
var length = writer.Position;
|
||||
writer.Seek(1, SeekOrigin.Begin);
|
||||
writer.Write((ushort)length);
|
||||
writer.WritePacketLength();
|
||||
|
||||
ns.Send(ref buffer, length);
|
||||
ns.Send(ref buffer, writer.Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
235
Projects/Server/Network/Packets/OutgoingMessagePackets.cs
Normal file
235
Projects/Server/Network/Packets/OutgoingMessagePackets.cs
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: OutgoingMessagePackets.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
[Flags]
|
||||
public enum AffixType : byte
|
||||
{
|
||||
Append = 0x00,
|
||||
Prepend = 0x01,
|
||||
System = 0x02
|
||||
}
|
||||
|
||||
public static class OutgoingMessagePackets
|
||||
{
|
||||
public static void SendMessageLocalized(
|
||||
this NetState ns,
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name = "", string args = ""
|
||||
)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[GetMaxMessageLocalizedLength(args)];
|
||||
var length = CreateMessageLocalized(
|
||||
ref buffer,
|
||||
serial, graphic, type, hue, font, number, name, args
|
||||
);
|
||||
|
||||
ns.Send(buffer.Slice(0, length));
|
||||
}
|
||||
|
||||
public static int GetMaxMessageLocalizedLength(string args) => 50 + (args?.Length ?? 0) * 2;
|
||||
|
||||
public static int CreateMessageLocalized(
|
||||
ref Span<byte> buffer,
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name = "", string args = ""
|
||||
)
|
||||
{
|
||||
name = name?.Trim() ?? "";
|
||||
args = args?.Trim() ?? "";
|
||||
|
||||
if (hue == 0)
|
||||
{
|
||||
hue = 0x3B2;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0xC1);
|
||||
writer.Seek(2, SeekOrigin.Current);
|
||||
writer.Write(serial);
|
||||
writer.Write((short)graphic);
|
||||
writer.Write((byte)type);
|
||||
writer.Write((short)hue);
|
||||
writer.Write((short)font);
|
||||
writer.Write(number);
|
||||
writer.WriteAscii(name, 30);
|
||||
writer.WriteLittleUniNull(args);
|
||||
|
||||
writer.WritePacketLength();
|
||||
|
||||
return writer.Position;
|
||||
}
|
||||
|
||||
public static void SendMessageLocalizedAffix(
|
||||
this NetState ns,
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name,
|
||||
AffixType affixType, string affix = "", string args = ""
|
||||
)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[GetMaxMessageLocalizedAffixLength(affix, args)];
|
||||
var length = CreateMessageLocalizedAffix(
|
||||
ref buffer,
|
||||
serial, graphic, type, hue, font, number, name, affixType, affix, args
|
||||
);
|
||||
|
||||
ns.Send(buffer.Slice(0, length));
|
||||
}
|
||||
|
||||
public static int GetMaxMessageLocalizedAffixLength(string affix, string args) =>
|
||||
52 + (affix?.Length ?? 0) + (args?.Length ?? 0) * 2;
|
||||
|
||||
public static int CreateMessageLocalizedAffix(
|
||||
ref Span<byte> buffer,
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, int number, string name,
|
||||
AffixType affixType, string affix = "", string args = ""
|
||||
)
|
||||
{
|
||||
name = name?.Trim() ?? "";
|
||||
affix = affix?.Trim() ?? "";
|
||||
args = args?.Trim() ?? "";
|
||||
|
||||
if (hue == 0)
|
||||
{
|
||||
hue = 0x3B2;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0xCC);
|
||||
writer.Seek(2, SeekOrigin.Current);
|
||||
writer.Write(serial);
|
||||
writer.Write((short)graphic);
|
||||
writer.Write((byte)type);
|
||||
writer.Write((short)hue);
|
||||
writer.Write((short)font);
|
||||
writer.Write(number);
|
||||
writer.Write((byte)affixType);
|
||||
writer.WriteAscii(name, 30);
|
||||
writer.WriteAsciiNull(affix);
|
||||
writer.WriteBigUniNull(args);
|
||||
|
||||
writer.WritePacketLength();
|
||||
|
||||
return writer.Position;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void SendMessage(
|
||||
this NetState ns,
|
||||
Serial serial, int graphic, MessageType type, int hue, int font, bool ascii, string lang, string name, string text
|
||||
)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[GetMaxMessageLength(text)];
|
||||
var length = CreateMessage(
|
||||
ref buffer,
|
||||
serial,
|
||||
graphic,
|
||||
type,
|
||||
hue,
|
||||
font,
|
||||
ascii,
|
||||
lang,
|
||||
name,
|
||||
text
|
||||
);
|
||||
|
||||
ns.Send(buffer.Slice(0, length));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetMaxMessageLength(string text) => 50 + (text?.Length ?? 0) * 2;
|
||||
|
||||
public static int CreateMessage(
|
||||
ref Span<byte> buffer,
|
||||
Serial serial,
|
||||
int graphic,
|
||||
MessageType type,
|
||||
int hue,
|
||||
int font,
|
||||
bool ascii,
|
||||
string lang,
|
||||
string name,
|
||||
string text
|
||||
)
|
||||
{
|
||||
name = name?.Trim() ?? "";
|
||||
text = text?.Trim() ?? "";
|
||||
lang = lang?.Trim() ?? "ENU";
|
||||
|
||||
if (hue == 0)
|
||||
{
|
||||
hue = 0x3B2;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)(ascii ? 0x1C : 0xAE)); // Packet ID
|
||||
writer.Seek(2, SeekOrigin.Current);
|
||||
writer.Write(serial);
|
||||
writer.Write((short)graphic);
|
||||
writer.Write((byte)type);
|
||||
writer.Write((short)hue);
|
||||
writer.Write((short)font);
|
||||
if (ascii)
|
||||
{
|
||||
writer.WriteAscii(name, 30);
|
||||
writer.WriteAsciiNull(text);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteAscii(lang, 4);
|
||||
writer.WriteAscii(name, 30);
|
||||
writer.WriteBigUniNull(text);
|
||||
}
|
||||
|
||||
writer.WritePacketLength();
|
||||
|
||||
return writer.Position;
|
||||
}
|
||||
|
||||
public static void SendFollowMessage(this NetState ns, Serial s1, Serial s2)
|
||||
{
|
||||
if (ns == null || !ns.GetSendBuffer(out var buffer))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new CircularBufferWriter(buffer);
|
||||
writer.Write((byte)0x15); // Packet ID
|
||||
writer.Write(s1);
|
||||
writer.Write(s2);
|
||||
|
||||
ns.Send(ref buffer, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue