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:
Kamron Batman 2020-11-20 21:50:13 -08:00 • committed by GitHub
parent 10d884fe30
commit b6cd408623
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
59 changed files with 1160 additions and 1025 deletions

View file

@ -2332,19 +2332,14 @@ namespace Server
MoveToWorld(location, m_Map);
}
public void LabelTo(Mobile to, int number)
public void LabelTo(Mobile to, int number, string args = "")
{
to.Send(new MessageLocalized(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, number, "", ""));
}
public void LabelTo(Mobile to, int number, string args)
{
to.Send(new MessageLocalized(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, number, "", args));
to.NetState.SendMessageLocalized(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, number, "", args);
}
public void LabelTo(Mobile to, string text)
{
to.Send(new UnicodeMessage(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, "ENU", "", text));
to.NetState.SendMessage(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, false, "ENU", "", text);
}
public void LabelTo(Mobile to, string format, params object[] args)
@ -2352,14 +2347,9 @@ namespace Server
LabelTo(to, string.Format(format, args));
}
public void LabelToAffix(Mobile to, int number, AffixType type, string affix)
public void LabelToAffix(Mobile to, int number, AffixType type, string affix, string args = "")
{
to.Send(new MessageLocalizedAffix(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, number, "", type, affix, ""));
}
public void LabelToAffix(Mobile to, int number, AffixType type, string affix, string args)
{
to.Send(new MessageLocalizedAffix(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, number, "", type, affix, args));
to.NetState.SendMessageLocalizedAffix(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, number, "", type, affix, args);
}
public virtual void LabelLootTypeTo(Mobile to)
@ -3433,71 +3423,61 @@ namespace Server
return;
}
Packet p = null;
var worldLoc = GetWorldLocation();
var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange());
var length = OutgoingMessagePackets.GetMaxMessageLength(text);
Span<byte> buffer = stackalloc byte[length];
length = OutgoingMessagePackets.CreateMessage(
ref buffer,
Serial, m_ItemID, type, hue, 3, ascii, "ENU", Name, text
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
foreach (var state in eable)
{
var m = state.Mobile;
if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m)))
{
if (p == null)
{
if (ascii)
{
p = new AsciiMessage(Serial, m_ItemID, type, hue, 3, Name, text);
}
else
{
p = new UnicodeMessage(Serial, m_ItemID, type, hue, 3, "ENU", Name, text);
}
p.Acquire();
}
state.Send(p);
state.Send(buffer);
}
}
Packet.Release(p);
eable.Free();
}
public void PublicOverheadMessage(MessageType type, int hue, int number)
{
PublicOverheadMessage(type, hue, number, "");
}
public void PublicOverheadMessage(MessageType type, int hue, int number, string args)
public void PublicOverheadMessage(MessageType type, int hue, int number, string args = "")
{
if (m_Map == null)
{
return;
}
Packet p = null;
var worldLoc = GetWorldLocation();
var eable = m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange());
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)];
var length = OutgoingMessagePackets.CreateMessageLocalized(
ref buffer,
Serial, m_ItemID, type, hue, 3, number, Name, args
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
foreach (var state in eable)
{
var m = state.Mobile;
if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m)))
{
p ??= Packet.Acquire(new MessageLocalized(Serial, m_ItemID, type, hue, 3, number, Name, args));
state.Send(p);
state.Send(buffer);
}
}
Packet.Release(p);
eable.Free();
}
@ -3994,24 +3974,14 @@ namespace Server
public Point3D GetWorldTop() => RootParent?.Location ??
new Point3D(m_Location.m_X, m_Location.m_Y, m_Location.m_Z + ItemData.CalcHeight);
public void SendLocalizedMessageTo(Mobile to, int number)
public void SendLocalizedMessageTo(Mobile to, int number, string args = "")
{
if (Deleted || !to.CanSee(this))
{
return;
}
to.Send(new MessageLocalized(Serial, ItemID, MessageType.Regular, 0x3B2, 3, number, "", ""));
}
public void SendLocalizedMessageTo(Mobile to, int number, string args)
{
if (Deleted || !to.CanSee(this))
{
return;
}
to.Send(new MessageLocalized(Serial, ItemID, MessageType.Regular, 0x3B2, 3, number, "", args));
to.NetState.SendMessageLocalized(Serial, ItemID, MessageType.Regular, 0x3B2, 3, number, "", args);
}
public void SendLocalizedMessageTo(Mobile to, int number, AffixType affixType, string affix, string args)
@ -4021,19 +3991,17 @@ namespace Server
return;
}
to.Send(
new MessageLocalizedAffix(
Serial,
ItemID,
MessageType.Regular,
0x3B2,
3,
number,
"",
affixType,
affix,
args
)
to.NetState.SendMessageLocalizedAffix(
Serial,
ItemID,
MessageType.Regular,
0x3B2,
3,
number,
"",
affixType,
affix,
args
);
}
@ -4254,17 +4222,15 @@ namespace Server
if (opl.Header > 0)
{
from.Send(
new MessageLocalized(
Serial,
m_ItemID,
MessageType.Label,
0x3B2,
3,
opl.Header,
Name,
opl.HeaderArgs
)
from.NetState.SendMessageLocalized(
Serial,
m_ItemID,
MessageType.Label,
0x3B2,
3,
opl.Header,
Name,
opl.HeaderArgs
);
}
}
@ -4292,39 +4258,35 @@ namespace Server
{
if (m_Amount <= 1)
{
ns.Send(new MessageLocalized(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, LabelNumber, "", ""));
ns.SendMessageLocalized(Serial, m_ItemID, MessageType.Label, 0x3B2, 3, LabelNumber);
}
else
{
ns.Send(
new MessageLocalizedAffix(
Serial,
m_ItemID,
MessageType.Label,
0x3B2,
3,
LabelNumber,
"",
AffixType.Append,
$" : {m_Amount}",
""
)
);
}
}
else
{
ns.Send(
new UnicodeMessage(
ns.SendMessageLocalizedAffix(
Serial,
m_ItemID,
MessageType.Label,
0x3B2,
3,
"ENU",
LabelNumber,
"",
Name + (m_Amount > 1 ? $" : {m_Amount}" : "")
)
AffixType.Append,
$" : {m_Amount}"
);
}
}
else
{
ns.SendMessage(
Serial,
m_ItemID,
MessageType.Label,
0x3B2,
3,
false,
"ENU",
"",
$"{Name}{(m_Amount > 1 ? $" : {m_Amount}" : "")}"
);
}
}

View file

@ -1434,7 +1434,7 @@ namespace Server
m_Warmode = value;
Delta(MobileDelta.Flags);
m_NetState?.SendSetWarMode(value);
m_NetState.SendSetWarMode(value);
if (!m_Warmode)
{
@ -3718,7 +3718,7 @@ namespace Server
hue = Notoriety.GetHue(Notoriety.Compute(from, this));
}
from.Send(new MessageLocalized(Serial, Body, MessageType.Label, hue, 3, opl.Header, Name, opl.HeaderArgs));
from.NetState.SendMessageLocalized(Serial, Body, MessageType.Label, hue, 3, opl.Header, Name, opl.HeaderArgs);
}
}
@ -6114,11 +6114,22 @@ namespace Server
ProcessDelta();
Packet regp = null;
Packet mutp = null;
Span<byte> regBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)];
Span<byte> mutBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(mutatedText)];
var length = OutgoingMessagePackets.CreateMessage(
ref regBuffer,
Serial, Body, type, hue, 3, false, m_Language, Name, text
);
regBuffer = regBuffer.Slice(0, length); // Adjust to the actual size
length = OutgoingMessagePackets.CreateMessage(
ref mutBuffer,
Serial, Body, type, hue, 3, false, m_Language, Name, mutatedText
);
mutBuffer = mutBuffer.Slice(0, length); // Adjust to the actual size
// TODO: Should this be sorted like onSpeech is below?
for (var i = 0; i < hears.Count; ++i)
{
var heard = hears[i];
@ -6126,36 +6137,15 @@ namespace Server
if (mutatedArgs == null || !CheckHearsMutatedSpeech(heard, mutateContext))
{
heard.OnSpeech(regArgs);
var ns = heard.NetState;
if (ns != null)
{
regp ??= Packet.Acquire(new UnicodeMessage(Serial, Body, type, hue, 3, m_Language, Name, text));
ns.Send(regp);
}
heard.NetState?.Send(regBuffer);
}
else
{
heard.OnSpeech(mutatedArgs);
var ns = heard.NetState;
if (ns != null)
{
mutp ??= Packet.Acquire(
new UnicodeMessage(Serial, Body, type, hue, 3, m_Language, Name, mutatedText)
);
ns.Send(mutp);
}
heard.NetState?.Send(mutBuffer);
}
}
Packet.Release(regp);
Packet.Release(mutp);
if (onSpeech.Count > 1)
{
onSpeech.Sort(LocationComparer.GetInstance(this));
@ -6459,7 +6449,7 @@ namespace Server
if (amount > 0)
{
ourState?.SendDamage(Serial, amount);
ourState.SendDamage(Serial, amount);
if (theirState != null && theirState != ourState)
{
@ -6534,7 +6524,7 @@ namespace Server
{
if (damaged.CanSeeVisibleDamage)
{
ourState?.SendDamage(Serial, amount);
ourState.SendDamage(Serial, amount);
}
if (theirState != null && theirState != ourState && damager.CanSeeVisibleDamage)
@ -6569,19 +6559,16 @@ namespace Server
if (message && amount > 0)
{
m_NetState?.Send(
new MessageLocalizedAffix(
Serial.MinusOne,
-1,
MessageType.Label,
0x3B2,
3,
1008158,
"",
AffixType.Append | AffixType.System,
amount.ToString(),
""
)
m_NetState.SendMessageLocalizedAffix(
Serial.MinusOne,
-1,
MessageType.Label,
0x3B2,
3,
1008158,
"",
AffixType.Append | AffixType.System,
amount.ToString()
);
}
}
@ -7291,13 +7278,13 @@ namespace Server
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SendSound(int soundID)
{
m_NetState?.SendSoundEffect(soundID, this);
m_NetState.SendSoundEffect(soundID, this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SendSound(int soundID, IPoint3D p)
{
m_NetState?.SendSoundEffect(soundID, p);
m_NetState.SendSoundEffect(soundID, p);
}
/**
@ -8769,11 +8756,8 @@ namespace Server
public void SayTo(Mobile to, bool ascii, string format, params object[] args) =>
SayTo(to, ascii, string.Format(format, args));
public void SayTo(Mobile to, int number) =>
to.Send(new MessageLocalized(Serial, Body, MessageType.Regular, SpeechHue, 3, number, Name, ""));
public void SayTo(Mobile to, int number, string args) =>
to.Send(new MessageLocalized(Serial, Body, MessageType.Regular, SpeechHue, 3, number, Name, args));
public void SayTo(Mobile to, int number, string args = "") =>
to.NetState.SendMessageLocalized(Serial, Body, MessageType.Regular, SpeechHue, 3, number, Name, args);
public void Say(bool ascii, string text) => PublicOverheadMessage(MessageType.Regular, SpeechHue, ascii, text);
@ -9578,15 +9562,7 @@ namespace Server
public Direction GetDirectionTo(Point2D p) => GetDirectionTo(p.m_X, p.m_Y);
public Direction GetDirectionTo(Point3D p) => GetDirectionTo(p.m_X, p.m_Y);
public Direction GetDirectionTo(IPoint2D p)
{
if (p == null)
{
return Direction.North;
}
return GetDirectionTo(p.X, p.Y);
}
public Direction GetDirectionTo(IPoint2D p) => p == null ? Direction.North : GetDirectionTo(p.X, p.Y);
public void PublicOverheadMessage(MessageType type, int hue, bool ascii, string text, bool noLineOfSight = true)
{
@ -9595,11 +9571,15 @@ namespace Server
return;
}
var p = ascii
? (Packet)new AsciiMessage(Serial, Body, type, hue, 3, Name, text)
: new UnicodeMessage(Serial, Body, type, hue, 3, m_Language, Name, text);
var length = OutgoingMessagePackets.GetMaxMessageLength(text);
p.Acquire();
Span<byte> buffer = stackalloc byte[length];
length = OutgoingMessagePackets.CreateMessage(
ref buffer,
Serial, Body, type, hue, 3, ascii, Language, Name, text
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
var eable = m_Map.GetClientsInRange(m_Location);
@ -9607,12 +9587,10 @@ namespace Server
{
if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this)))
{
state.Send(p);
state.Send(buffer);
}
}
Packet.Release(p);
eable.Free();
}
@ -9623,7 +9601,13 @@ namespace Server
return;
}
var p = Packet.Acquire(new MessageLocalized(Serial, Body, type, hue, 3, number, Name, args));
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)];
var length = OutgoingMessagePackets.CreateMessageLocalized(
ref buffer,
Serial, Body, type, hue, 3, number, Name, args
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
var eable = m_Map.GetClientsInRange(m_Location);
@ -9631,12 +9615,10 @@ namespace Server
{
if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this)))
{
state.Send(p);
state.Send(buffer);
}
}
Packet.Release(p);
eable.Free();
}
@ -9650,20 +9632,13 @@ namespace Server
return;
}
var p = Packet.Acquire(
new MessageLocalizedAffix(
Serial,
Body,
type,
hue,
3,
number,
Name,
affixType,
affix,
args
)
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedAffixLength(affix, args)];
var length = OutgoingMessagePackets.CreateMessageLocalizedAffix(
ref buffer,
Serial, Body, type, hue, 3, number, Name, affixType, affix, args
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
var eable = m_Map.GetClientsInRange(m_Location);
@ -9671,59 +9646,29 @@ namespace Server
{
if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this)))
{
state.Send(p);
state.Send(buffer);
}
}
Packet.Release(p);
eable.Free();
}
public void PrivateOverheadMessage(MessageType type, int hue, bool ascii, string text, NetState state)
{
if (state == null)
{
return;
}
if (ascii)
{
state.Send(new AsciiMessage(Serial, Body, type, hue, 3, Name, text));
}
else
{
state.Send(new UnicodeMessage(Serial, Body, type, hue, 3, m_Language, Name, text));
}
state.SendMessage(Serial, Body, type, hue, 3, ascii, m_Language, Name, text);
}
public void PrivateOverheadMessage(MessageType type, int hue, int number, NetState state) =>
PrivateOverheadMessage(type, hue, number, "", state);
public void PrivateOverheadMessage(MessageType type, int hue, int number, string args, NetState state) =>
state?.Send(new MessageLocalized(Serial, Body, type, hue, 3, number, Name, args));
state.SendMessageLocalized(Serial, Body, type, hue, 3, number, Name, args);
public void LocalOverheadMessage(MessageType type, int hue, bool ascii, string text)
{
var ns = m_NetState;
if (ns == null)
{
return;
}
if (ascii)
{
ns.Send(new AsciiMessage(Serial, Body, type, hue, 3, Name, text));
}
else
{
ns.Send(new UnicodeMessage(Serial, Body, type, hue, 3, m_Language, Name, text));
}
}
public void LocalOverheadMessage(MessageType type, int hue, bool ascii, string text) =>
m_NetState.SendMessage(Serial, Body, type, hue, 3, ascii, m_Language, Name, text);
public void LocalOverheadMessage(MessageType type, int hue, int number, string args = "") =>
m_NetState?.Send(new MessageLocalized(Serial, Body, type, hue, 3, number, Name, args));
m_NetState.SendMessageLocalized(Serial, Body, type, hue, 3, number, Name, args);
public void NonlocalOverheadMessage(MessageType type, int hue, int number, string args = "")
{
@ -9732,7 +9677,13 @@ namespace Server
return;
}
var p = Packet.Acquire(new MessageLocalized(Serial, Body, type, hue, 3, number, Name, args));
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)];
var length = OutgoingMessagePackets.CreateMessageLocalized(
ref buffer,
Serial, Body, type, hue, 3, number, Name, args
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
var eable = m_Map.GetClientsInRange(m_Location);
@ -9740,12 +9691,10 @@ namespace Server
{
if (state != m_NetState && state.Mobile.CanSee(this))
{
state.Send(p);
state.Send(buffer);
}
}
Packet.Release(p);
eable.Free();
}
@ -9756,11 +9705,15 @@ namespace Server
return;
}
var p = ascii
? (Packet)new AsciiMessage(Serial, Body, type, hue, 3, Name, text)
: new UnicodeMessage(Serial, Body, type, hue, 3, Language, Name, text);
var length = OutgoingMessagePackets.GetMaxMessageLength(text);
p.Acquire();
Span<byte> buffer = stackalloc byte[length];
length = OutgoingMessagePackets.CreateMessage(
ref buffer,
Serial, Body, type, hue, 3, ascii, Language, Name, text
);
buffer = buffer.Slice(0, length); // Adjust to the actual size
var eable = m_Map.GetClientsInRange(m_Location);
@ -9768,45 +9721,28 @@ namespace Server
{
if (state != m_NetState && state.Mobile.CanSee(this))
{
state.Send(p);
state.Send(buffer);
}
}
Packet.Release(p);
eable.Free();
}
public void SendLocalizedMessage(int number) => m_NetState?.Send(MessageLocalized.InstantiateGeneric(number));
public void SendLocalizedMessage(int number, string args, int hue = 0x3B2)
{
if (hue == 0x3B2 && string.IsNullOrEmpty(args))
{
m_NetState?.Send(MessageLocalized.InstantiateGeneric(number));
}
else
{
m_NetState?.Send(
new MessageLocalized(Serial.MinusOne, -1, MessageType.Regular, hue, 3, number, "System", args)
);
}
}
public void SendLocalizedMessage(int number, string args = "", int hue = 0x3B2) =>
m_NetState.SendMessageLocalized(Serial.MinusOne, -1, MessageType.Regular, hue, 3, number, "System", args);
public void SendLocalizedMessage(int number, bool append, string affix, string args = "", int hue = 0x3B2) =>
m_NetState?.Send(
new MessageLocalizedAffix(
Serial.MinusOne,
-1,
MessageType.Regular,
hue,
3,
number,
"System",
(append ? AffixType.Append : AffixType.Prepend) | AffixType.System,
affix,
args
)
m_NetState.SendMessageLocalizedAffix(
Serial.MinusOne,
-1,
MessageType.Regular,
hue,
3,
number,
"System",
(append ? AffixType.Append : AffixType.Prepend) | AffixType.System,
affix,
args
);
public void SendMessage(string text) => SendMessage(0x3B2, text);
@ -9815,7 +9751,7 @@ namespace Server
SendMessage(0x3B2, string.Format(format, args));
public void SendMessage(int hue, string text) =>
m_NetState?.Send(new UnicodeMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, "ENU", "System", text));
m_NetState.SendMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, false, "ENU", "System", text);
public void SendMessage(int hue, string format, params object[] args) =>
SendMessage(hue, string.Format(format, args));
@ -9826,7 +9762,7 @@ namespace Server
SendAsciiMessage(0x3B2, string.Format(format, args));
public void SendAsciiMessage(int hue, string text) =>
m_NetState?.Send(new AsciiMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, "System", text));
m_NetState.SendMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, true, null, "System", text);
public void SendAsciiMessage(int hue, string format, params object[] args) =>
SendAsciiMessage(hue, string.Format(format, args));

View file

@ -353,7 +353,7 @@ namespace Server.Network
public void LaunchBrowser(string url)
{
Send(new MessageLocalized(Serial.MinusOne, -1, MessageType.Label, 0x35, 3, 501231, "", ""));
this.SendMessageLocalized(Serial.MinusOne, -1, MessageType.Label, 0x35, 3, 501231);
Send(new LaunchBrowser(url));
}

View file

@ -15,11 +15,13 @@
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
namespace Server.Network
{
public static class PacketUtilities
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WritePacketLength(this CircularBufferWriter writer)
{
var length = writer.Position;
@ -27,5 +29,14 @@ namespace Server.Network
writer.Write((ushort)length);
writer.Seek(length, SeekOrigin.Begin);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WritePacketLength(this SpanWriter writer)
{
var length = writer.Position;
writer.Seek(1, SeekOrigin.Begin);
writer.Write((ushort)length);
writer.Seek(length, SeekOrigin.Begin);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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,

View file

@ -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))

View file

@ -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);
}
}
}

View 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);
}
}
}

View file

@ -152,28 +152,23 @@ namespace Server
public static void Broadcast(int hue, bool ascii, string text)
{
Packet p;
var length = OutgoingMessagePackets.GetMaxMessageLength(text);
if (ascii)
{
p = new AsciiMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, "System", text);
}
else
{
p = new UnicodeMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, "ENU", "System", text);
}
Span<byte> buffer = stackalloc byte[length];
length = OutgoingMessagePackets.CreateMessage(
ref buffer,
Serial.MinusOne, -1, MessageType.Regular, hue, 3, ascii, "ENU", "System", text
);
p.Acquire();
buffer = buffer.Slice(0, length); // Adjust to the actual size
foreach (var ns in TcpServer.Instances)
{
if (ns.Mobile != null)
{
ns.Send(p);
ns.Send(buffer);
}
}
p.Release();
}
public static void Broadcast(int hue, bool ascii, string format, params object[] args)