Converts secure trade packets (#329)

This commit is contained in:
Kamron Batman 2020-11-29 22:20:10 -08:00 • committed by GitHub
parent 77f665502b
commit 8c4c30da7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 184 additions and 184 deletions

View file

@ -8,8 +8,9 @@ namespace Server.Tests.Network
{ {
public class SecureTradePacketTests : IClassFixture<ServerFixture> public class SecureTradePacketTests : IClassFixture<ServerFixture>
{ {
[Theory, InlineData("short-name"), [Theory]
InlineData("this is a really long name that is more than 30 characters, probably")] [InlineData("short-name")]
[InlineData("this is a really long name that is more than 30 characters, probably")]
public void TestDisplaySecureTrade(string name) public void TestDisplaySecureTrade(string name)
{ {
var m = new Mobile(0x1); var m = new Mobile(0x1);
@ -18,26 +19,13 @@ namespace Server.Tests.Network
var firstCont = new Container(World.NewItem); var firstCont = new Container(World.NewItem);
var secondCont = new Container(World.NewItem); var secondCont = new Container(World.NewItem);
var data = new DisplaySecureTrade(m, firstCont, secondCont, name).Compile(); var expected = new DisplaySecureTrade(m, firstCont, secondCont, name).Compile();
var hasName = name.Length > 0; using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplaySecureTrade(m, firstCont, secondCont, name);
Span<byte> expectedData = stackalloc byte[17 + (hasName ? 30 : 0)]; var result = ns.SendPipe.Reader.TryRead();
var pos = 0; AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
expectedData.Write(ref pos, (byte)0x6F); // Packet ID
expectedData.Write(ref pos, (ushort)0x2F); // Length
expectedData.Write(ref pos, (byte)TradeFlag.Display); // Command
expectedData.Write(ref pos, m.Serial);
expectedData.Write(ref pos, firstCont.Serial);
expectedData.Write(ref pos, secondCont.Serial);
expectedData.Write(ref pos, hasName);
if (hasName)
{
expectedData.WriteAsciiFixed(ref pos, name, 30);
}
AssertThat.Equal(data, expectedData);
} }
[Fact] [Fact]
@ -45,17 +33,13 @@ namespace Server.Tests.Network
{ {
var cont = new Container(World.NewItem); var cont = new Container(World.NewItem);
var data = new CloseSecureTrade(cont).Compile(); var expected = new CloseSecureTrade(cont).Compile();
Span<byte> expectedData = stackalloc byte[8]; using var ns = PacketTestUtilities.CreateTestNetState();
var pos = 0; ns.SendCloseSecureTrade(cont);
expectedData.Write(ref pos, (byte)0x6F); // Packet ID var result = ns.SendPipe.Reader.TryRead();
expectedData.Write(ref pos, (ushort)0x8); // Length AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
expectedData.Write(ref pos, (byte)TradeFlag.Close); // Command
expectedData.Write(ref pos, cont.Serial);
AssertThat.Equal(data, expectedData);
} }
[Theory, InlineData(true, false), InlineData(false, true)] [Theory, InlineData(true, false), InlineData(false, true)]
@ -67,38 +51,28 @@ namespace Server.Tests.Network
var secondCont = new Container(World.NewItem); var secondCont = new Container(World.NewItem);
var cont = first ? firstCont : secondCont; var cont = first ? firstCont : secondCont;
var data = new UpdateSecureTrade(cont, first, second).Compile(); var expected = new UpdateSecureTrade(cont, first, second).Compile();
Span<byte> expectedData = stackalloc byte[16]; using var ns = PacketTestUtilities.CreateTestNetState();
var pos = 0; ns.SendUpdateSecureTrade(cont, first, second);
expectedData.Write(ref pos, (byte)0x6F); // Packet ID var result = ns.SendPipe.Reader.TryRead();
expectedData.Write(ref pos, (ushort)0x10); // Length AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
expectedData.Write(ref pos, (byte)TradeFlag.Update); // Command
expectedData.Write(ref pos, cont.Serial);
expectedData.Write(ref pos, first ? 1 : 0); // true if first
expectedData.Write(ref pos, second ? 1 : 0); // true if second
AssertThat.Equal(data, expectedData);
} }
[Theory, InlineData(100000, 30, TradeFlag.UpdateGold), InlineData(250000, 50000, TradeFlag.UpdateLedger)] [Theory]
[InlineData(100000, 30, TradeFlag.UpdateGold)]
[InlineData(250000, 50000, TradeFlag.UpdateLedger)]
public void TestUpdateGoldSecureTrade(int gold, int plat, TradeFlag flag) public void TestUpdateGoldSecureTrade(int gold, int plat, TradeFlag flag)
{ {
var cont = new Container(World.NewItem); var cont = new Container(World.NewItem);
var data = new UpdateSecureTrade(cont, flag, gold, plat).Compile(); var expected = new UpdateSecureTrade(cont, flag, gold, plat).Compile();
Span<byte> expectedData = stackalloc byte[16]; using var ns = PacketTestUtilities.CreateTestNetState();
var pos = 0; ns.SendUpdateSecureTrade(cont, flag, gold, plat);
expectedData.Write(ref pos, (byte)0x6F); // Packet ID var result = ns.SendPipe.Reader.TryRead();
expectedData.Write(ref pos, (ushort)0x10); // Length AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
expectedData.Write(ref pos, (byte)flag); // Command
expectedData.Write(ref pos, cont.Serial);
expectedData.Write(ref pos, gold);
expectedData.Write(ref pos, plat);
AssertThat.Equal(data, expectedData);
} }
[Fact] [Fact]
@ -110,25 +84,13 @@ namespace Server.Tests.Network
var cont = new Container(World.NewItem); var cont = new Container(World.NewItem);
var itemInCont = new Item(World.NewItem) { Parent = cont }; var itemInCont = new Item(World.NewItem) { Parent = cont };
var data = new SecureTradeEquip(itemInCont, m).Compile(); var expected = new SecureTradeEquip(itemInCont, m).Compile();
Span<byte> expectedData = stackalloc byte[20];
var pos = 0;
expectedData.Write(ref pos, (byte)0x25); // Packet ID using var ns = PacketTestUtilities.CreateTestNetState();
expectedData.Write(ref pos, itemInCont.Serial); ns.SendSecureTradeEquip(itemInCont, m);
expectedData.Write(ref pos, (short)itemInCont.ItemID);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0);
#else
pos++;
#endif
expectedData.Write(ref pos, (short)itemInCont.Amount);
expectedData.Write(ref pos, (short)itemInCont.X);
expectedData.Write(ref pos, (short)itemInCont.Y);
expectedData.Write(ref pos, m.Serial);
expectedData.Write(ref pos, (short)itemInCont.Hue);
AssertThat.Equal(data, expectedData); var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
} }
[Fact] [Fact]
@ -140,31 +102,15 @@ namespace Server.Tests.Network
var cont = new Container(World.NewItem); var cont = new Container(World.NewItem);
var itemInCont = new Item(World.NewItem) { Parent = cont }; var itemInCont = new Item(World.NewItem) { Parent = cont };
var data = new SecureTradeEquip6017(itemInCont, m).Compile(); var expected = new SecureTradeEquip6017(itemInCont, m).Compile();
Span<byte> expectedData = stackalloc byte[21]; using var ns = PacketTestUtilities.CreateTestNetState();
var pos = 0; ns.ProtocolChanges |= ProtocolChanges.ContainerGridLines;
expectedData.Write(ref pos, (byte)0x25); // Packet ID ns.SendSecureTradeEquip(itemInCont, m);
expectedData.Write(ref pos, itemInCont.Serial);
expectedData.Write(ref pos, (short)itemInCont.ItemID);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0);
#else
pos++;
#endif
expectedData.Write(ref pos, (short)itemInCont.Amount);
expectedData.Write(ref pos, (short)itemInCont.X);
expectedData.Write(ref pos, (short)itemInCont.Y);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0);
#else
pos++;
#endif
expectedData.Write(ref pos, m.Serial);
expectedData.Write(ref pos, (short)itemInCont.Hue);
AssertThat.Equal(data, expectedData); var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
} }
} }
} }

View file

@ -1,18 +1,3 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SecureTradePackets.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 Server.Items; using Server.Items;
namespace Server.Network namespace Server.Network
@ -48,15 +33,6 @@ namespace Server.Network
} }
} }
public enum TradeFlag : byte
{
Display = 0x0,
Close = 0x1,
Update = 0x2,
UpdateGold = 0x3,
UpdateLedger = 0x4
}
public sealed class UpdateSecureTrade : Packet public sealed class UpdateSecureTrade : Packet
{ {
public UpdateSecureTrade(Container cont, bool first, bool second) public UpdateSecureTrade(Container cont, bool first, bool second)

View file

@ -0,0 +1,117 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: OutgoingSecureTradePackets.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 Server.Items;
namespace Server.Network
{
public enum TradeFlag : byte
{
Display,
Close,
Update,
UpdateGold,
UpdateLedger
}
public static class OutgoingSecureTradePackets
{
public static void SendDisplaySecureTrade(
this NetState ns, Mobile them, Container first, Container second, string name
)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x6F); // Packet ID
writer.Write((ushort)47); // Length
writer.Write((byte)TradeFlag.Display);
writer.Write(them.Serial);
writer.Write(first.Serial);
writer.Write(second.Serial);
writer.Write(true);
writer.WriteAscii(name ?? "", 30);
ns.Send(ref buffer, writer.Position);
}
public static void SendCloseSecureTrade(this NetState ns, Container cont)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x6F); // Packet ID
writer.Write((ushort)8); // Length
writer.Write((byte)TradeFlag.Close);
writer.Write(cont.Serial);
ns.Send(ref buffer, writer.Position);
}
public static void SendUpdateSecureTrade(this NetState ns, Container cont, bool first, bool second) =>
ns.SendUpdateSecureTrade(cont, TradeFlag.Update, first ? 1 : 0, second ? 1 : 0);
public static void SendUpdateSecureTrade(this NetState ns, Container cont, TradeFlag flag, int first, int second)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x6F); // Packet ID
writer.Write((ushort)16); // Length
writer.Write((byte)flag);
writer.Write(cont.Serial);
writer.Write(first);
writer.Write(second);
ns.Send(ref buffer, writer.Position);
}
public static void SendSecureTradeEquip(this NetState ns, Item item, Mobile m)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x25); // Packet ID
writer.Write(item.Serial);
writer.Write((short)item.ItemID);
writer.Write((byte)0);
writer.Write((short)item.Amount);
writer.Write((short)item.X);
writer.Write((short)item.Y);
if (ns.ContainerGridLines)
{
writer.Write((byte)0);
}
writer.Write(m.Serial);
writer.Write((short)item.Hue);
ns.Send(ref buffer, writer.Position);
}
}
}

View file

@ -14,85 +14,46 @@ namespace Server
From = new SecureTradeInfo(this, from, new SecureTradeContainer(this)); From = new SecureTradeInfo(this, from, new SecureTradeContainer(this));
To = new SecureTradeInfo(this, to, new SecureTradeContainer(this)); To = new SecureTradeInfo(this, to, new SecureTradeContainer(this));
var from6017 = from.NetState?.ContainerGridLines == true;
var to6017 = to.NetState?.ContainerGridLines == true;
var from704565 = from.NetState?.NewSecureTrading == true; var from704565 = from.NetState?.NewSecureTrading == true;
var to704565 = to.NetState?.NewSecureTrading == true; var to704565 = to.NetState?.NewSecureTrading == true;
from.Send(new MobileStatus(from, to)); from.Send(new MobileStatus(from, to));
from.Send(new UpdateSecureTrade(From.Container, false, false)); from.NetState.SendUpdateSecureTrade(From.Container, false, false);
from.NetState.SendSecureTradeEquip(To.Container, to);
if (from6017) from.NetState.SendUpdateSecureTrade(From.Container, false, false);
{ from.NetState.SendSecureTradeEquip(From.Container, from);
from.Send(new SecureTradeEquip6017(To.Container, to));
}
else
{
from.Send(new SecureTradeEquip(To.Container, to));
}
from.Send(new UpdateSecureTrade(From.Container, false, false)); from.NetState.SendDisplaySecureTrade(to, From.Container, To.Container, to.Name);
from.NetState.SendUpdateSecureTrade(From.Container, false, false);
if (from6017)
{
from.Send(new SecureTradeEquip6017(From.Container, from));
}
else
{
from.Send(new SecureTradeEquip(From.Container, from));
}
from.Send(new DisplaySecureTrade(to, From.Container, To.Container, to.Name));
from.Send(new UpdateSecureTrade(From.Container, false, false));
if (from.Account != null && from704565) if (from.Account != null && from704565)
{ {
from.Send( from.NetState.SendUpdateSecureTrade(
new UpdateSecureTrade( From.Container,
From.Container, TradeFlag.UpdateLedger,
TradeFlag.UpdateLedger, from.Account.TotalGold,
from.Account.TotalGold, from.Account.TotalPlat
from.Account.TotalPlat
)
); );
} }
to.Send(new MobileStatus(to, from)); to.Send(new MobileStatus(to, from));
to.Send(new UpdateSecureTrade(To.Container, false, false)); to.NetState.SendUpdateSecureTrade(To.Container, false, false);
to.NetState.SendSecureTradeEquip(From.Container, from);
if (to6017) to.NetState.SendUpdateSecureTrade(To.Container, false, false);
{ to.NetState.SendSecureTradeEquip(To.Container, to);
to.Send(new SecureTradeEquip6017(From.Container, from));
}
else
{
to.Send(new SecureTradeEquip(From.Container, from));
}
to.Send(new UpdateSecureTrade(To.Container, false, false)); to.NetState.SendDisplaySecureTrade(from, To.Container, From.Container, from.Name);
to.NetState.SendUpdateSecureTrade(To.Container, false, false);
if (to6017)
{
to.Send(new SecureTradeEquip6017(To.Container, to));
}
else
{
to.Send(new SecureTradeEquip(To.Container, to));
}
to.Send(new DisplaySecureTrade(from, To.Container, From.Container, from.Name));
to.Send(new UpdateSecureTrade(To.Container, false, false));
if (to.Account != null && to704565) if (to.Account != null && to704565)
{ {
to.Send( to.NetState.SendUpdateSecureTrade(
new UpdateSecureTrade( To.Container,
To.Container, TradeFlag.UpdateLedger,
TradeFlag.UpdateLedger, to.Account.TotalGold,
to.Account.TotalGold, to.Account.TotalPlat
to.Account.TotalPlat
)
); );
} }
} }
@ -164,8 +125,8 @@ namespace Server
return; return;
} }
From.Mobile.Send(new CloseSecureTrade(From.Container)); From.Mobile.NetState.SendCloseSecureTrade(From.Container);
To.Mobile.Send(new CloseSecureTrade(To.Container)); To.Mobile.NetState.SendCloseSecureTrade(To.Container);
Valid = false; Valid = false;
@ -198,12 +159,12 @@ namespace Server
var plat = left.Mobile.Account.TotalPlat; var plat = left.Mobile.Account.TotalPlat;
var gold = left.Mobile.Account.TotalGold; var gold = left.Mobile.Account.TotalGold;
left.Mobile.Send(new UpdateSecureTrade(left.Container, TradeFlag.UpdateLedger, gold, plat)); left.Mobile.NetState.SendUpdateSecureTrade(left.Container, TradeFlag.UpdateLedger, gold, plat);
} }
if (right.Mobile.NetState?.NewSecureTrading == true) if (right.Mobile.NetState?.NewSecureTrading == true)
{ {
right.Mobile.Send(new UpdateSecureTrade(right.Container, TradeFlag.UpdateGold, left.Gold, left.Plat)); right.Mobile.NetState.SendUpdateSecureTrade(right.Container, TradeFlag.UpdateGold, left.Gold, left.Plat);
} }
} }
@ -290,8 +251,8 @@ namespace Server
From.Accepted = false; From.Accepted = false;
To.Accepted = false; To.Accepted = false;
From.Mobile.Send(new UpdateSecureTrade(From.Container, From.Accepted, To.Accepted)); From.Mobile.NetState.SendUpdateSecureTrade(From.Container, From.Accepted, To.Accepted);
To.Mobile.Send(new UpdateSecureTrade(To.Container, To.Accepted, From.Accepted)); To.Mobile.NetState.SendUpdateSecureTrade(To.Container, To.Accepted, From.Accepted);
return; return;
} }
@ -349,8 +310,8 @@ namespace Server
} }
else if (!From.IsDisposed && !To.IsDisposed) else if (!From.IsDisposed && !To.IsDisposed)
{ {
From.Mobile.Send(new UpdateSecureTrade(From.Container, From.Accepted, To.Accepted)); From.Mobile.NetState.SendUpdateSecureTrade(From.Container, From.Accepted, To.Accepted);
To.Mobile.Send(new UpdateSecureTrade(To.Container, To.Accepted, From.Accepted)); To.Mobile.NetState.SendUpdateSecureTrade(To.Container, To.Accepted, From.Accepted);
} }
} }