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:
parent
5db8b1354c
commit
941452de4a
58 changed files with 5510 additions and 5537 deletions
|
|
@ -17,84 +17,83 @@ using System;
|
|||
using System.Buffers;
|
||||
using Server.Items;
|
||||
|
||||
namespace Server.Network
|
||||
namespace Server.Network;
|
||||
|
||||
public static class OutgoingItemPackets
|
||||
{
|
||||
public static class OutgoingItemPackets
|
||||
public static int CreateWorldItem(Span<byte> buffer, Item item)
|
||||
{
|
||||
public static int CreateWorldItem(Span<byte> buffer, Item item)
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
// This assumes the packet was sliced properly
|
||||
return buffer.Length;
|
||||
}
|
||||
|
||||
var itemID = item is BaseMulti ? item.ItemID | 0x4000 : item.ItemID & 0x3FFF;
|
||||
var amount = item.Amount;
|
||||
var hasAmount = amount != 0;
|
||||
var serial = hasAmount ? item.Serial.Value | 0x80000000 : item.Serial.Value & 0x7FFFFFFF;
|
||||
var loc = item.Location;
|
||||
var hue = item.Hue;
|
||||
var flags = item.GetPacketFlags();
|
||||
var direction = (int)item.Direction;
|
||||
var hasDirection = direction != 0;
|
||||
var hasHue = hue != 0;
|
||||
var hasFlags = flags != 0;
|
||||
var x = hasDirection ? loc.X | 0x8000 : loc.X & 0x7FFF;
|
||||
var y = (loc.Y & 0x3FFF) | (hasHue ? 0x8000 : 0) | (hasFlags ? 0x4000 : 0);
|
||||
var length = 14 + (hasAmount ? 2 : 0) +
|
||||
(hasDirection ? 1 : 0) +
|
||||
(hasHue ? 2 : 0) +
|
||||
(hasFlags ? 1 : 0);
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0x1A); // Packet ID
|
||||
writer.Write((ushort)length);
|
||||
writer.Write(serial);
|
||||
writer.Write((ushort)itemID);
|
||||
|
||||
if (hasAmount)
|
||||
{
|
||||
writer.Write((ushort)amount);
|
||||
}
|
||||
|
||||
writer.Write((ushort)x);
|
||||
writer.Write((ushort)y);
|
||||
|
||||
if (hasDirection)
|
||||
{
|
||||
writer.Write((byte)direction);
|
||||
}
|
||||
|
||||
writer.Write((sbyte)loc.Z);
|
||||
|
||||
if (hasHue)
|
||||
{
|
||||
writer.Write((ushort)hue);
|
||||
}
|
||||
|
||||
if (hasFlags)
|
||||
{
|
||||
writer.Write((byte)flags);
|
||||
}
|
||||
|
||||
return writer.BytesWritten;
|
||||
// This assumes the packet was sliced properly
|
||||
return buffer.Length;
|
||||
}
|
||||
|
||||
public static void SendWorldItem(this NetState ns, Item item)
|
||||
var itemID = item is BaseMulti ? item.ItemID | 0x4000 : item.ItemID & 0x3FFF;
|
||||
var amount = item.Amount;
|
||||
var hasAmount = amount != 0;
|
||||
var serial = hasAmount ? item.Serial.Value | 0x80000000 : item.Serial.Value & 0x7FFFFFFF;
|
||||
var loc = item.Location;
|
||||
var hue = item.Hue;
|
||||
var flags = item.GetPacketFlags();
|
||||
var direction = (int)item.Direction;
|
||||
var hasDirection = direction != 0;
|
||||
var hasHue = hue != 0;
|
||||
var hasFlags = flags != 0;
|
||||
var x = hasDirection ? loc.X | 0x8000 : loc.X & 0x7FFF;
|
||||
var y = (loc.Y & 0x3FFF) | (hasHue ? 0x8000 : 0) | (hasFlags ? 0x4000 : 0);
|
||||
var length = 14 + (hasAmount ? 2 : 0) +
|
||||
(hasDirection ? 1 : 0) +
|
||||
(hasHue ? 2 : 0) +
|
||||
(hasFlags ? 1 : 0);
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0x1A); // Packet ID
|
||||
writer.Write((ushort)length);
|
||||
writer.Write(serial);
|
||||
writer.Write((ushort)itemID);
|
||||
|
||||
if (hasAmount)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[OutgoingEntityPackets.MaxWorldEntityPacketLength].InitializePacket();
|
||||
|
||||
var length = ns.StygianAbyss ?
|
||||
OutgoingEntityPackets.CreateWorldEntity(buffer, item, ns.HighSeas) :
|
||||
CreateWorldItem(buffer, item);
|
||||
|
||||
ns.Send(buffer[..length]);
|
||||
writer.Write((ushort)amount);
|
||||
}
|
||||
|
||||
writer.Write((ushort)x);
|
||||
writer.Write((ushort)y);
|
||||
|
||||
if (hasDirection)
|
||||
{
|
||||
writer.Write((byte)direction);
|
||||
}
|
||||
|
||||
writer.Write((sbyte)loc.Z);
|
||||
|
||||
if (hasHue)
|
||||
{
|
||||
writer.Write((ushort)hue);
|
||||
}
|
||||
|
||||
if (hasFlags)
|
||||
{
|
||||
writer.Write((byte)flags);
|
||||
}
|
||||
|
||||
return writer.BytesWritten;
|
||||
}
|
||||
|
||||
public static void SendWorldItem(this NetState ns, Item item)
|
||||
{
|
||||
if (ns.CannotSendPackets())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[OutgoingEntityPackets.MaxWorldEntityPacketLength].InitializePacket();
|
||||
|
||||
var length = ns.StygianAbyss ?
|
||||
OutgoingEntityPackets.CreateWorldEntity(buffer, item, ns.HighSeas) :
|
||||
CreateWorldItem(buffer, item);
|
||||
|
||||
ns.Send(buffer[..length]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue