fix: Stops creating blocked packets entirely (#944)

Optimizes larger servers where users are logging in and packets are being created for no reason.
This commit is contained in:
Kamron Batman 2022-02-27 00:13:49 -08:00 committed by GitHub
parent 5db8b1354c
commit 941452de4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 5510 additions and 5537 deletions

View file

@ -17,194 +17,193 @@ using System;
using System.Buffers;
using System.IO;
namespace Server.Network
namespace Server.Network;
public static class OutgoingContainerPackets
{
public static class OutgoingContainerPackets
public static void SendDisplaySpellbook(this NetState ns, Serial book) => ns.SendDisplayContainer(book, -1);
public static void SendSpellbookContent(this NetState ns, Serial book, int graphic, int offset, ulong content)
{
public static void SendDisplaySpellbook(this NetState ns, Serial book) => ns.SendDisplayContainer(book, -1);
public static void SendSpellbookContent(this NetState ns, Serial book, int graphic, int offset, ulong content)
if (ns.CannotSendPackets())
{
if (ns == null)
{
return;
}
if (ObjectPropertyList.Enabled && ns.NewSpellbook)
{
ns.SendNewSpellbookContent(book, graphic, offset, content);
}
else
{
ns.SendOldSpellbookContent(book, offset, content);
}
return;
}
public static void SendNewSpellbookContent(this NetState ns, Serial book, int graphic, int offset, ulong content)
if (ObjectPropertyList.Enabled && ns.NewSpellbook)
{
if (ns == null)
{
return;
}
var writer = new SpanWriter(stackalloc byte[23]);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)23); // Length
writer.Write((short)0x1B); // Subpacket
writer.Write((short)0x01); // Command
writer.Write(book);
writer.Write((short)graphic);
writer.Write((short)offset);
for (var i = 0; i < 8; ++i)
{
writer.Write((byte)(content >> (i * 8)));
}
ns.Send(writer.Span);
ns.SendNewSpellbookContent(book, graphic, offset, content);
}
public static void SendOldSpellbookContent(this NetState ns, Serial book, int offset, ulong content)
else
{
if (ns == null)
{
return;
}
var count = content.NumberOfSetBits();
var length = 5 + count * (ns.ContainerGridLines ? 20 : 19);
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0x3C); // Packet ID
writer.Write((ushort)length);
writer.Write((ushort)count);
ulong mask = 1;
for (var i = 0; i < 64; ++i, mask <<= 1)
{
if ((content & mask) != 0)
{
writer.Write(0x7FFFFFFF - i);
writer.Write((ushort)0); // child ItemID
writer.Write((byte)0); // ItemID offset
writer.Write((ushort)(i + offset)); // Amount
writer.Write(0); // X, Y
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location
}
writer.Write(book);
writer.Write((short)0); // Quest Hue
}
}
ns.Send(writer.Span);
}
public static void SendDisplayContainer(this NetState ns, Serial cont, int gumpId)
{
if (ns == null)
{
return;
}
var writer = new SpanWriter(stackalloc byte[ns.HighSeas ? 9 : 7]);
writer.Write((byte)0x24); // Packet ID
writer.Write(cont);
writer.Write((ushort)gumpId);
if (ns.HighSeas)
{
writer.Write((short)0x7D);
}
ns.Send(writer.Span);
}
public static void SendContainerContentUpdate(this NetState ns, Item item)
{
if (ns == null)
{
return;
}
Serial parentSerial;
if (item.Parent is Item parentItem)
{
parentSerial = parentItem.Serial;
}
else
{
Console.WriteLine("Warning: ContainerContentUpdate on item with !(parent is Item)");
parentSerial = Serial.Zero;
}
var writer = new SpanWriter(stackalloc byte[ns.ContainerGridLines ? 21 : 20]);
writer.Write((byte)0x25); // Packet ID
writer.Write(item.Serial);
writer.Write((ushort)item.ItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)Math.Min(item.Amount, ushort.MaxValue));
writer.Write((short)item.X);
writer.Write((short)item.Y);
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(parentSerial);
writer.Write((ushort)(item.QuestItem ? Item.QuestItemHue : item.Hue));
ns.Send(writer.Span);
}
public static void SendContainerContent(this NetState ns, Mobile beholder, Item beheld)
{
if (ns == null)
{
return;
}
var items = beheld.Items;
var count = items.Count;
var writer = new SpanWriter(stackalloc byte[5 + items.Count * (ns.ContainerGridLines ? 20 : 19)]);
writer.Write((byte)0x3C); // Packet ID
writer.Seek(4, SeekOrigin.Current); // Length & written count
var written = 0;
for (var i = 0; i < count; ++i)
{
var child = items[i];
if (!child.Deleted && beholder.CanSee(child))
{
var loc = child.Location;
writer.Write(child.Serial);
writer.Write((ushort)child.ItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)Math.Min(child.Amount, ushort.MaxValue));
writer.Write((short)loc.X);
writer.Write((short)loc.Y);
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)(child.QuestItem ? Item.QuestItemHue : child.Hue));
++written;
}
}
writer.Seek(1, SeekOrigin.Begin);
writer.Write((ushort)writer.BytesWritten);
writer.Write((ushort)written);
writer.Seek(0, SeekOrigin.End);
ns.Send(writer.Span);
ns.SendOldSpellbookContent(book, offset, content);
}
}
public static void SendNewSpellbookContent(this NetState ns, Serial book, int graphic, int offset, ulong content)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[23]);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)23); // Length
writer.Write((short)0x1B); // Subpacket
writer.Write((short)0x01); // Command
writer.Write(book);
writer.Write((short)graphic);
writer.Write((short)offset);
for (var i = 0; i < 8; ++i)
{
writer.Write((byte)(content >> (i * 8)));
}
ns.Send(writer.Span);
}
public static void SendOldSpellbookContent(this NetState ns, Serial book, int offset, ulong content)
{
if (ns.CannotSendPackets())
{
return;
}
var count = content.NumberOfSetBits();
var length = 5 + count * (ns.ContainerGridLines ? 20 : 19);
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0x3C); // Packet ID
writer.Write((ushort)length);
writer.Write((ushort)count);
ulong mask = 1;
for (var i = 0; i < 64; ++i, mask <<= 1)
{
if ((content & mask) != 0)
{
writer.Write(0x7FFFFFFF - i);
writer.Write((ushort)0); // child ItemID
writer.Write((byte)0); // ItemID offset
writer.Write((ushort)(i + offset)); // Amount
writer.Write(0); // X, Y
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location
}
writer.Write(book);
writer.Write((short)0); // Quest Hue
}
}
ns.Send(writer.Span);
}
public static void SendDisplayContainer(this NetState ns, Serial cont, int gumpId)
{
if (ns.CannotSendPackets())
{
return;
}
var writer = new SpanWriter(stackalloc byte[ns.HighSeas ? 9 : 7]);
writer.Write((byte)0x24); // Packet ID
writer.Write(cont);
writer.Write((ushort)gumpId);
if (ns.HighSeas)
{
writer.Write((short)0x7D);
}
ns.Send(writer.Span);
}
public static void SendContainerContentUpdate(this NetState ns, Item item)
{
if (ns.CannotSendPackets())
{
return;
}
Serial parentSerial;
if (item.Parent is Item parentItem)
{
parentSerial = parentItem.Serial;
}
else
{
Console.WriteLine("Warning: ContainerContentUpdate on item with !(parent is Item)");
parentSerial = Serial.Zero;
}
var writer = new SpanWriter(stackalloc byte[ns.ContainerGridLines ? 21 : 20]);
writer.Write((byte)0x25); // Packet ID
writer.Write(item.Serial);
writer.Write((ushort)item.ItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)Math.Min(item.Amount, ushort.MaxValue));
writer.Write((short)item.X);
writer.Write((short)item.Y);
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(parentSerial);
writer.Write((ushort)(item.QuestItem ? Item.QuestItemHue : item.Hue));
ns.Send(writer.Span);
}
public static void SendContainerContent(this NetState ns, Mobile beholder, Item beheld)
{
if (ns.CannotSendPackets())
{
return;
}
var items = beheld.Items;
var count = items.Count;
var writer = new SpanWriter(stackalloc byte[5 + items.Count * (ns.ContainerGridLines ? 20 : 19)]);
writer.Write((byte)0x3C); // Packet ID
writer.Seek(4, SeekOrigin.Current); // Length & written count
var written = 0;
for (var i = 0; i < count; ++i)
{
var child = items[i];
if (!child.Deleted && beholder.CanSee(child))
{
var loc = child.Location;
writer.Write(child.Serial);
writer.Write((ushort)child.ItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)Math.Min(child.Amount, ushort.MaxValue));
writer.Write((short)loc.X);
writer.Write((short)loc.Y);
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)(child.QuestItem ? Item.QuestItemHue : child.Hue));
++written;
}
}
writer.Seek(1, SeekOrigin.Begin);
writer.Write((ushort)writer.BytesWritten);
writer.Write((ushort)written);
writer.Seek(0, SeekOrigin.End);
ns.Send(writer.Span);
}
}