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

@ -28,20 +28,15 @@ namespace Server.Network
public static void VendorBuyReply(NetState state, CircularBufferReader reader)
{
var vendor = World.FindMobile(reader.ReadUInt32());
var flag = reader.ReadByte();
if (vendor == null)
{
return;
}
if (vendor.Deleted || !Utility.RangeCheck(vendor.Location, state.Mobile.Location, 10))
{
state.Send(new EndVendorBuy(vendor));
return;
}
var flag = reader.ReadByte();
if (flag == 0x02)
if (!vendor.Deleted && Utility.RangeCheck(vendor.Location, state.Mobile.Location, 10) && flag == 0x02)
{
var msgSize = reader.Remaining;
@ -61,15 +56,13 @@ namespace Server.Network
msgSize -= 7;
}
if (buyList.Count > 0 && vendor is IVendor v && v.OnBuyItems(state.Mobile, buyList))
if (buyList.Count <= 0 || (vendor as IVendor)?.OnBuyItems(state.Mobile, buyList) != true)
{
state.Send(new EndVendorBuy(vendor));
return;
}
}
else
{
state.Send(new EndVendorBuy(vendor));
}
state.SendEndVendorBuy(vendor.Serial);
}
public static void VendorSellReply(NetState state, CircularBufferReader reader)

View file

@ -0,0 +1,125 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: OutgoingVendorBuyPackets.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.Buffers;
using System.Collections.Generic;
using System.IO;
using Server.Items;
namespace Server.Network
{
public static class OutgoingVendorBuyPackets
{
public static void SendVendorBuyContent(this NetState ns, List<BuyItemState> list)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x3C); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write((short)list.Count);
for (var i = list.Count - 1; i >= 0; --i)
{
var bis = list[i];
writer.Write(bis.MySerial);
writer.Write((ushort)bis.ItemID);
writer.Write((byte)0); // itemID offset
writer.Write((ushort)bis.Amount);
writer.Write((short)(i + 1)); // x
writer.Write((short)1); // y
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(bis.ContainerSerial);
writer.Write((ushort)bis.Hue);
}
writer.WritePacketLength();
ns.Send(ref buffer, writer.Position);
}
public static void SendDisplayBuyList(this NetState ns, Serial vendor)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x24); // Packet ID
writer.Write(vendor);
writer.Write((short)0x30); // Vendor Buy Window
if (ns.HighSeas)
{
writer.Write((short)0x0);
}
ns.Send(ref buffer, writer.Position);
}
public static void SendVendorBuyList(this NetState ns, Mobile vendor, List<BuyItemState> list)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x74); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write((vendor.FindItemOnLayer(Layer.ShopBuy) as Container)?.Serial ?? Serial.MinusOne);
writer.Write((byte)list.Count);
for (var i = 0; i < list.Count; ++i)
{
var bis = list[i];
writer.Write(bis.Price);
var desc = bis.Description ?? "";
writer.Write((byte)desc.Length);
writer.WriteAscii(desc); // Doesn't look like it is used anymore
}
writer.WritePacketLength();
ns.Send(ref buffer, writer.Position);
}
public static void SendEndVendorBuy(this NetState ns, Serial vendor)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x3B); // Packet ID
writer.Write((ushort)8);
writer.Write(vendor);
writer.Write((byte)0); // Buy count
ns.Send(ref buffer, writer.Position);
}
}
}

View file

@ -1,123 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: VendorBuyPackets.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.Collections.Generic;
using Server.Items;
namespace Server.Network
{
public sealed class VendorBuyContent : Packet
{
public VendorBuyContent(List<BuyItemState> list) : 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
Stream.Write(bis.ContainerSerial);
Stream.Write((ushort)bis.Hue);
}
}
}
public sealed class VendorBuyContent6017 : Packet
{
public VendorBuyContent6017(List<BuyItemState> list) : base(0x3C)
{
EnsureCapacity(list.Count * 20 + 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
Stream.Write((byte)0); // Grid Location?
Stream.Write(bis.ContainerSerial);
Stream.Write((ushort)bis.Hue);
}
}
}
public sealed class DisplayBuyList : Packet
{
public DisplayBuyList(Mobile vendor) : base(0x24, 7)
{
Stream.Write(vendor.Serial);
Stream.Write((short)0x30); // buy window id?
}
}
public sealed class DisplayBuyListHS : Packet
{
public DisplayBuyListHS(Mobile vendor) : base(0x24, 9)
{
Stream.Write(vendor.Serial);
Stream.Write((short)0x30); // buy window id?
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 ?? "";
// TODO: Test if this is actually WriteAsciiFixed and the extra null doesn't matter.
Stream.Write((byte)(desc.Length + 1));
Stream.WriteAsciiNull(desc);
}
}
}
public sealed class EndVendorBuy : Packet
{
public EndVendorBuy(Mobile vendor) : base(0x3B, 8)
{
Stream.Write((ushort)8); // length
Stream.Write(vendor.Serial);
Stream.Write((byte)0);
}
}
}