fix(core): Converts vendor buy packets (#339)

This commit is contained in:
Kamron Batman 2020-12-11 00:04:39 -08:00 committed by GitHub
parent d71856ddbe
commit 5e991f6fb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 255 additions and 513 deletions

View file

@ -2,6 +2,7 @@ using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Server.Items;
using Server.Network;
using Xunit;
@ -10,8 +11,10 @@ namespace Server.Tests.Network
{
public class VendorBuyPacketTests : IClassFixture<ServerFixture>
{
[Fact]
public void TestVendorBuyContent()
[Theory]
[InlineData(ProtocolChanges.None)]
[InlineData(ProtocolChanges.ContainerGridLines)]
public void TestVendorBuyContent(ProtocolChanges protocolChanges)
{
var cont = new Container(World.NewItem);
@ -22,115 +25,34 @@ namespace Server.Tests.Network
new("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyContent(buyStates).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = protocolChanges;
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 19];
var expected = new VendorBuyContent(buyStates, ns.ContainerGridLines).Compile();
var pos = 0;
ns.SendVendorBuyContent(buyStates);
expectedData.Write(ref pos, (byte)0x3C); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, (ushort)buyStates.Count); // Count
for (var i = buyStates.Count - 1; i >= 0; i--)
{
var buyState = buyStates[i];
expectedData.Write(ref pos, buyState.MySerial);
expectedData.Write(ref pos, (ushort)buyState.ItemID);
pos++; // ItemID Offset
expectedData.Write(ref pos, (ushort)buyState.Amount);
expectedData.Write(ref pos, (ushort)(i + 1)); // X
expectedData.Write(ref pos, (ushort)1); // Y
expectedData.Write(ref pos, buyState.ContainerSerial);
expectedData.Write(ref pos, (ushort)buyState.Hue);
}
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]
public void TestVendorBuyContent6017()
{
var cont = new Container(World.NewItem);
var buyStates = new List<BuyItemState>
{
new("First Item", cont.Serial, World.NewItem, 10, 1, 0x01, 0),
new("Second Item", cont.Serial, World.NewItem, 20, 2, 0x0A, 0),
new("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyContent6017(buyStates).Compile();
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 20];
var pos = 0;
expectedData.Write(ref pos, (byte)0x3C); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, (ushort)buyStates.Count); // Count
for (var i = buyStates.Count - 1; i >= 0; i--)
{
var buyState = buyStates[i];
expectedData.Write(ref pos, buyState.MySerial);
expectedData.Write(ref pos, (ushort)buyState.ItemID);
pos++; // ItemID Offset
expectedData.Write(ref pos, (ushort)buyState.Amount);
expectedData.Write(ref pos, (ushort)(i + 1)); // X
expectedData.Write(ref pos, (ushort)1); // Y
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0); // Grid Location?
#else
pos++;
#endif
expectedData.Write(ref pos, buyState.ContainerSerial);
expectedData.Write(ref pos, (ushort)buyState.Hue);
}
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestDisplayBuyList()
[Theory]
[InlineData(ProtocolChanges.None)]
[InlineData(ProtocolChanges.HighSeas)]
public void TestDisplayBuyList(ProtocolChanges protocolChanges)
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new DisplayBuyList(vendor).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = protocolChanges;
Span<byte> expectedData = stackalloc byte[7];
var pos = 0;
var expected = new DisplayBuyList(vendor.Serial, ns.HighSeas).Compile();
expectedData.Write(ref pos, (byte)0x24); // Packet ID
expectedData.Write(ref pos, vendor.Serial);
expectedData.Write(ref pos, (ushort)0x30); // Buy gump
ns.SendDisplayBuyList(vendor.Serial);
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestDisplayBuyListHS()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new DisplayBuyListHS(vendor).Compile();
Span<byte> expectedData = stackalloc byte[9];
var pos = 0;
expectedData.Write(ref pos, (byte)0x24); // Packet ID
expectedData.Write(ref pos, vendor.Serial);
expectedData.Write(ref pos, (ushort)0x30); // Buy gump
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (ushort)0);
#endif
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]
@ -148,29 +70,13 @@ namespace Server.Tests.Network
new("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyList(vendor, buyStates).Compile();
var expected = new VendorBuyList(vendor, buyStates).Compile();
var length = 8 + buyStates.Sum(state => 6 + state.Description.Length);
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendVendorBuyList(vendor, buyStates);
Span<byte> expectedData = stackalloc byte[length];
var pos = 0;
expectedData.Write(ref pos, (byte)0x74); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, Serial.MinusOne); // Vendor Buy Pack Serial or -1
expectedData.Write(ref pos, (byte)buyStates.Count);
for (var i = 0; i < buyStates.Count; i++)
{
var state = buyStates[i];
expectedData.Write(ref pos, state.Price);
var description = state.Description ?? "";
expectedData.Write(ref pos, (byte)Math.Min(255, description.Length + 1));
expectedData.WriteAsciiNull(ref pos, description, 255);
}
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]
@ -179,20 +85,13 @@ namespace Server.Tests.Network
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new EndVendorBuy(vendor).Compile();
var expected = new EndVendorBuy(vendor.Serial).Compile();
Span<byte> expectedData = stackalloc byte[8];
var pos = 0;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendEndVendorBuy(vendor.Serial);
expectedData.Write(ref pos, (byte)0x3B); // Packet ID
expectedData.Write(ref pos, (ushort)0x8); // Length
expectedData.Write(ref pos, vendor.Serial);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)-);
#endif
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
}
}

View file

@ -0,0 +1,80 @@
using System.Collections.Generic;
using Server.Items;
namespace Server.Network
{
public sealed class VendorBuyContent : Packet
{
public VendorBuyContent(List<BuyItemState> list, bool containerGridLines) : base(0x3C)
{
EnsureCapacity(list.Count * 19 + 5);
Stream.Write((short)list.Count);
for (var i = list.Count - 1; i >= 0; --i)
{
var bis = list[i];
Stream.Write(bis.MySerial);
Stream.Write((ushort)bis.ItemID);
Stream.Write((byte)0); // itemID offset
Stream.Write((ushort)bis.Amount);
Stream.Write((short)(i + 1)); // x
Stream.Write((short)1); // y
if (containerGridLines)
{
Stream.Write((byte)0); // Grid Location?
}
Stream.Write(bis.ContainerSerial);
Stream.Write((ushort)bis.Hue);
}
}
}
public sealed class DisplayBuyList : Packet
{
public DisplayBuyList(Serial vendor, bool highSeas) : base(0x24, highSeas ? 9 : 7)
{
Stream.Write(vendor);
Stream.Write((short)0x30); // buy window id?
if (highSeas)
{
Stream.Write((short)0x00);
}
}
}
public sealed class VendorBuyList : Packet
{
public VendorBuyList(Mobile vendor, List<BuyItemState> list) : base(0x74)
{
EnsureCapacity(256);
Stream.Write(!(vendor.FindItemOnLayer(Layer.ShopBuy) is Container buyPack) ? Serial.MinusOne : buyPack.Serial);
Stream.Write((byte)list.Count);
for (var i = 0; i < list.Count; ++i)
{
var bis = list[i];
Stream.Write(bis.Price);
var desc = bis.Description ?? "";
Stream.Write((byte)desc.Length);
Stream.WriteAsciiFixed(desc, desc.Length);
}
}
}
public sealed class EndVendorBuy : Packet
{
public EndVendorBuy(Serial vendor) : base(0x3B, 8)
{
Stream.Write((ushort)8); // length
Stream.Write(vendor);
Stream.Write((byte)0);
}
}
}

View file

@ -63,7 +63,7 @@ namespace Server.Tests.Network
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new EndVendorBuy(vendor).Compile();
var data = new EndVendorBuy(vendor.Serial).Compile();
Span<byte> expectedData = stackalloc byte[8];
var pos = 0;