Converts equipment packets (#314)

### Change to how DisplayEquipInfo works
* Fixes a bug where raw name wasn't used
* Fixes another bug where an empty, blank, or null string was still sending an empty crafted by property.

Bumps release version
This commit is contained in:
Kamron Batman 2020-11-16 01:04:56 -08:00 • committed by GitHub
parent b0d8dd42c0
commit 1fb7ac5cda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 177 additions and 132 deletions

View file

@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using Server.Network;
using Xunit;
@ -7,18 +8,22 @@ namespace Server.Tests.Network
{
public class EquipmentPacketTests : IClassFixture<ServerFixture>
{
[Fact]
public void TestDisplayEquipmentInfo()
[Theory]
[InlineData(null, false)]
[InlineData("Some Crafter", false)]
[InlineData("", true)]
public void TestDisplayEquipmentInfo(string name, bool unidentified)
{
var m = new Mobile(0x1);
m.DefaultMobileInit();
m.RawName = name;
var item = new Item(World.NewItem);
var info = new EquipmentInfo(
500000,
m,
false,
unidentified,
new[]
{
new EquipInfoAttribute(500001, 1),
@ -27,48 +32,19 @@ namespace Server.Tests.Network
}
);
var data = new DisplayEquipmentInfo(item, info).Compile();
var expected = new DisplayEquipmentInfo(item, info).Compile();
var attrs = info.Attributes;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayEquipmentInfo(
item.Serial,
info.Number,
info.Crafter?.RawName,
info.Unidentified,
new List<EquipInfoAttribute>(info.Attributes)
);
var length = 17 + (info.Unidentified ? 4 : 0) + attrs.Length * 6;
if (info.Crafter != null)
{
length += 6 + (info.Crafter.Name?.Length ?? 0);
}
Span<byte> expectedData = stackalloc byte[length];
var pos = 0;
expectedData.Write(ref pos, (byte)0xBF); // Packet ID
expectedData.Write(ref pos, (ushort)length); // Length
expectedData.Write(ref pos, (ushort)0x10); // Subcommand
expectedData.Write(ref pos, item.Serial);
expectedData.Write(ref pos, info.Number);
if (info.Crafter != null)
{
var name = info.Crafter.Name ?? "";
expectedData.Write(ref pos, -3);
expectedData.Write(ref pos, (ushort)name.Length);
expectedData.WriteAscii(ref pos, name);
}
if (info.Unidentified)
{
expectedData.Write(ref pos, -4);
}
for (var i = 0; i < attrs.Length; i++)
{
var attr = attrs[i];
expectedData.Write(ref pos, attr.Number);
expectedData.Write(ref pos, (ushort)attr.Charges);
}
expectedData.Write(ref pos, -1);
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]
@ -79,25 +55,13 @@ namespace Server.Tests.Network
var item = new Item(World.NewItem) { Parent = m };
var data = new EquipUpdate(item).Compile();
var expected = new EquipUpdate(item).Compile();
Span<byte> expectedData = stackalloc byte[15];
var pos = 0;
expectedData.Write(ref pos, (byte)0x2E); // Packet ID
expectedData.Write(ref pos, item.Serial);
expectedData.Write(ref pos, (ushort)item.ItemID);
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendEquipUpdate(item);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0);
#else
pos++;
#endif
expectedData.Write(ref pos, (byte)item.Layer);
expectedData.Write(ref pos, item.Parent.Serial);
expectedData.Write(ref pos, (ushort)item.Hue);
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
}
}

View file

@ -0,0 +1,113 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: EquipmentPackets.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
{
public class EquipmentInfo
{
public EquipmentInfo(int number, Mobile crafter, bool unidentified, EquipInfoAttribute[] attributes)
{
Number = number;
Crafter = crafter;
Unidentified = unidentified;
Attributes = attributes;
}
public int Number { get; }
public Mobile Crafter { get; }
public bool Unidentified { get; }
public EquipInfoAttribute[] Attributes { get; }
}
public sealed class DisplayEquipmentInfo : Packet
{
public DisplayEquipmentInfo(Item item, EquipmentInfo info) : base(0xBF)
{
var attrs = info.Attributes;
EnsureCapacity(
17 + (info.Crafter?.RawName?.Length ?? 0) +
(info.Unidentified ? 4 : 0) + attrs.Length * 6
);
Stream.Write((short)0x10);
Stream.Write(item.Serial);
Stream.Write(info.Number);
var name = info.Crafter?.RawName?.Trim() ?? "";
if (name.Length > 0)
{
Stream.Write(-3);
var length = name.Length;
Stream.Write((ushort)length);
Stream.WriteAsciiFixed(name, length);
}
if (info.Unidentified)
{
Stream.Write(-4);
}
for (var i = 0; i < attrs.Length; ++i)
{
Stream.Write(attrs[i].Number);
Stream.Write((short)attrs[i].Charges);
}
Stream.Write(-1);
}
}
public sealed class EquipUpdate : Packet
{
public EquipUpdate(Item item) : base(0x2E, 15)
{
Serial parentSerial;
var parent = item.Parent as Mobile;
var hue = item.Hue;
if (parent != null)
{
parentSerial = parent.Serial;
if (parent.SolidHueOverride >= 0)
{
hue = parent.SolidHueOverride;
}
}
else
{
Console.WriteLine("Warning: EquipUpdate on item with !(parent is Mobile)");
parentSerial = Serial.Zero;
}
Stream.Write(item.Serial);
Stream.Write((short)item.ItemID);
Stream.Write((byte)0);
Stream.Write((byte)item.Layer);
Stream.Write(parentSerial);
Stream.Write((short)hue);
}
}
}