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

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