modernuo/Projects/Server/Network/Packets/OutgoingEntityPackets.cs
Kamron Batman ecbee17690
fix: Optimizes OPL using string interpolation (#1041)
## Breaking Changes (New API)
ObjectPropertyList supports the following API:
```cs
list.Add(500000);
list.Add(500001, stringArgument);
list.Add("Some text");
list.Add($"Some text with {argument}");
list.Add(500002, $"{arg1}\t{arg2}");
```

## Notes
1. All API uses that require a formatter like this:
    ```cs
    list.Add(500002, "{0}\t{1}", arg1, arg2);
    ```
    Should be changed to use string interpolation, for example:
    ```cs
    list.Add(500002, $"{arg1}\t{arg2}");
    ```
2. The following paradigm should no longer be used:
    ```cs
    list.Add(1061170, prop.ToString()); // strength requirement ~1_val~
    ```
    The new string interpolation API will avoid having to convert the argument to a string before writing it to the packet. Instead use the following:
    ```cs
    list.Add(1061170, $"{prop}"); // strength requirement ~1_val~
    ```

### Benchmarks
```cs
|                         Method |     Mean |   Error |  StdDev |  Gen 0 | Allocated |
|------------------------------- |---------:|--------:|--------:|-------:|----------:|
|                BenchmarkOldOPL | 241.0 ns | 0.56 ns | 0.47 ns | 0.0105 |      88 B |
| BenchmarkStringInterpolatedOPL | 199.9 ns | 2.44 ns | 2.39 ns |      - |         - |
```

### Changes
- [X] Removes crash in STArray.Return when array is null.
- [X] Fixes NPE in OPL when entity is null. Serial in packet will be 0 when entity is null.
- [X] Fixes NPE in AosAttributes when Parent is null.
- [X] Changes OPL to use string interpolation.
- [X] Introduces `IPropertyList` to allow extending PropertyList for other uses.
2022-06-02 10:09:53 -07:00

150 lines
4.5 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: OutgoingEntityPackets.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 Server.Items;
namespace Server.Network;
public static class OutgoingEntityPackets
{
public const int OPLPacketLength = 9;
public const int RemoveEntityLength = 5;
public const int MaxWorldEntityPacketLength = 26;
public static void CreateOPLInfo(Span<byte> buffer, Item item) =>
CreateOPLInfo(buffer, item.Serial, item.PropertyList.Hash);
public static void CreateOPLInfo(Span<byte> buffer, Serial serial, int hash)
{
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer);
writer.Write((byte)0xDC); // Packet ID
writer.Write(serial);
writer.Write(hash);
}
public static void SendOPLInfo(this NetState ns, IObjectPropertyListEntity obj) =>
ns.SendOPLInfo(obj.Serial, obj.PropertyList.Hash);
public static void SendOPLInfo(this NetState ns, Serial serial, int hash)
{
if (ns.CannotSendPackets())
{
return;
}
Span<byte> buffer = stackalloc byte[OPLPacketLength].InitializePacket();
CreateOPLInfo(buffer, serial, hash);
ns.Send(buffer);
}
public static void CreateRemoveEntity(Span<byte> buffer, Serial serial)
{
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer);
writer.Write((byte)0x1D); // Packet ID
writer.Write(serial);
}
public static void SendRemoveEntity(this NetState ns, Serial serial)
{
if (ns.CannotSendPackets())
{
return;
}
Span<byte> buffer = stackalloc byte[RemoveEntityLength].InitializePacket();
CreateRemoveEntity(buffer, serial);
ns.Send(buffer);
}
public static int CreateWorldEntity(Span<byte> buffer, IEntity entity, bool isHS)
{
if (buffer[0] != 0)
{
return buffer.Length;
}
var writer = new SpanWriter(buffer);
writer.Write((byte)0xF3); // Packet ID
writer.Write((short)0x1); // command
int type = 0;
int gfx = 0;
int amount = 1;
int hue = 0;
byte light = 0;
int flags = 0;
if (entity is BaseMulti multi)
{
type = 2;
gfx = multi.ItemID & (isHS ? 0xFFFF : 0x7FFF);
hue = multi.Hue;
amount = multi.Amount;
}
else if (entity is Item item)
{
// type = 3 if is damageable
gfx = item.ItemID & (isHS ? 0xFFFF : 0x7FFF);
hue = item.Hue;
amount = item.Amount;
light = (byte)item.Light;
flags = item.GetPacketFlags();
}
else if (entity is Mobile mobile)
{
type = 1;
gfx = mobile.Body;
hue = mobile.Hue;
flags = mobile.GetPacketFlags(true);
}
writer.Write((byte)type);
writer.Write(entity.Serial);
writer.Write((ushort)gfx);
writer.Write((byte)0);
writer.Write((short)amount); // Min
writer.Write((short)amount); // Max
writer.Write((short)(entity.X & 0x7FFF));
writer.Write((short)(entity.Y & 0x3FFF));
writer.Write((sbyte)entity.Z);
writer.Write(light);
writer.Write((short)hue);
writer.Write((byte)flags);
if (isHS)
{
writer.Write((short)0);
}
return writer.Position;
}
}