ModernUO/Projects/UOContent/Network/Packets/IncomingVendorPackets.cs
Kamron Batman 7d9bc9ff0a
fix: Fixes thread guard and cleans up incoming packet reader (#1641)
### Summary
* Fixes syntax compile error when THREADGUARD is enabled.
* Removes `int packetLength` from incoming packet handles since they aren't needed.

### Developer Notes
Incoming packet handler `SpanReader` is now properly scoped to that packet by length.
2023-12-19 17:04:09 -08:00

110 lines
3.5 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IncomingVendorPackets.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;
namespace Server.Network;
public static class IncomingVendorPackets
{
public static unsafe void Configure()
{
IncomingPackets.Register(0x3B, 0, true, &VendorBuyReply);
IncomingPackets.Register(0x9F, 0, true, &VendorSellReply);
}
public static void VendorBuyReply(NetState state, SpanReader reader)
{
var vendor = World.FindMobile((Serial)reader.ReadUInt32());
if (vendor == null)
{
return;
}
var flag = reader.ReadByte();
if (!vendor.Deleted && Utility.InRange(vendor.Location, state.Mobile.Location, 10) && flag == 0x02)
{
var msgSize = reader.Remaining;
if (msgSize / 7 > 100)
{
return;
}
var buyList = new List<BuyItemResponse>(msgSize / 7);
while (msgSize > 0)
{
var layer = reader.ReadByte();
var serial = (Serial)reader.ReadUInt32();
int amount = reader.ReadInt16();
buyList.Add(new BuyItemResponse(serial, amount));
msgSize -= 7;
}
if (buyList.Count <= 0 || (vendor as IVendor)?.OnBuyItems(state.Mobile, buyList) != true)
{
return;
}
}
state.SendEndVendorBuy(vendor.Serial);
}
public static void VendorSellReply(NetState state, SpanReader reader)
{
var serial = (Serial)reader.ReadUInt32();
var vendor = World.FindMobile(serial);
if (vendor == null)
{
return;
}
if (vendor.Deleted || !Utility.InRange(vendor.Location, state.Mobile.Location, 10))
{
state.SendEndVendorSell(vendor.Serial);
return;
}
int count = reader.ReadUInt16();
if (count >= 100 || reader.Remaining != count * 6)
{
return;
}
var sellList = new List<SellItemResponse>(count);
for (var i = 0; i < count; i++)
{
var item = World.FindItem((Serial)reader.ReadUInt32());
int amount = reader.ReadInt16();
if (item != null && amount > 0)
{
sellList.Add(new SellItemResponse(item, amount));
}
}
if (sellList.Count > 0 && vendor is IVendor v && v.OnSellItems(state.Mobile, sellList))
{
state.SendEndVendorSell(vendor.Serial);
}
}
}