parent
11227391d3
commit
d9f417ee19
17 changed files with 1084 additions and 381 deletions
|
|
@ -1,35 +0,0 @@
|
|||
using System;
|
||||
using Server.Network;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public class DamageOldPacketTests : IClassFixture<ServerFixture>
|
||||
{
|
||||
private Mobile mobile;
|
||||
|
||||
public DamageOldPacketTests(ServerFixture fixture) => mobile = fixture.mobile;
|
||||
|
||||
[Theory]
|
||||
[InlineData(10, 10)]
|
||||
[InlineData(-5, 0)]
|
||||
[InlineData(1024, 255)]
|
||||
public void TestDamagePacketOld(int inputAmount, byte expectedAmount)
|
||||
{
|
||||
DamagePacketOld packet = new DamagePacketOld(mobile, inputAmount);
|
||||
|
||||
Span<byte> data = packet.Compile(false, out int length).AsSpan(0, length);
|
||||
|
||||
byte[] expectedData = {
|
||||
0xBF, // Packet
|
||||
0x00, 0x0B, // Length
|
||||
0x00, 0x22, // Sub-packet
|
||||
0x01, // Command
|
||||
0x00, 0x00, 0x00, 0x01, // Serial
|
||||
expectedAmount // Amount
|
||||
};
|
||||
|
||||
Assert.Equal(data.ToArray(), expectedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
using System;
|
||||
using Server.Network;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public class ArrowPacketTests
|
||||
{
|
||||
[Fact]
|
||||
public void TestCancelArrow()
|
||||
{
|
||||
Span<byte> data = new CancelArrow().Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[] {
|
||||
0xBA, // Packet
|
||||
0x00, // Command
|
||||
0xFF, 0xFF, // X
|
||||
0xFF, 0xFF // Y
|
||||
};
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(100, 10)]
|
||||
[InlineData(100000, 100000)]
|
||||
public void TestSetArrow(int x, int y)
|
||||
{
|
||||
Span<byte> data = new SetArrow(x, y).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[] {
|
||||
0xBA, // Packet
|
||||
0x01, // Command
|
||||
0x00, 0x00, // X
|
||||
0x00, 0x00 // Y
|
||||
};
|
||||
|
||||
((ushort)x).CopyTo(expectedData.Slice(2, 2));
|
||||
((ushort)y).CopyTo(expectedData.Slice(4, 2));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(100, 10)]
|
||||
[InlineData(100000, 100000)]
|
||||
public void TestCancelArrowHS(int x, int y)
|
||||
{
|
||||
Span<byte> data = new CancelArrowHS(x, y, 0x1).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[] {
|
||||
0xBA, // Packet
|
||||
0x00, // Command
|
||||
0x00, 0x00, // X
|
||||
0x00, 0x00, // Y
|
||||
0x00, 0x00, 0x00, 0x01 // Serial
|
||||
};
|
||||
|
||||
((ushort)x).CopyTo(expectedData.Slice(2, 2));
|
||||
((ushort)y).CopyTo(expectedData.Slice(4, 2));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(100, 10)]
|
||||
[InlineData(100000, 100000)]
|
||||
public void TestSetArrowHS(int x, int y)
|
||||
{
|
||||
Span<byte> data = new SetArrowHS(x, y, 0x1).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[] {
|
||||
0xBA, // Packet
|
||||
0x01, // Command
|
||||
0x00, 0x00, // X
|
||||
0x00, 0x00, // Y
|
||||
0x00, 0x00, 0x00, 0x01 // Serial
|
||||
};
|
||||
|
||||
((ushort)x).CopyTo(expectedData.Slice(2, 2));
|
||||
((ushort)y).CopyTo(expectedData.Slice(4, 2));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
using System;
|
||||
using Server.Network;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public class DamagePacketTests : IClassFixture<ServerFixture>
|
||||
{
|
||||
private readonly Mobile mobile;
|
||||
|
||||
public DamagePacketTests(ServerFixture fixture) => mobile = fixture.fromMobile;
|
||||
|
||||
[Theory]
|
||||
[InlineData(10, 10)]
|
||||
[InlineData(-5, 0)]
|
||||
[InlineData(1024, 0xFF)]
|
||||
public void TestDamagePacketOld(int inputAmount, byte expectedAmount)
|
||||
{
|
||||
Span<byte> data = new DamagePacketOld(mobile, inputAmount).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[] {
|
||||
0xBF, // Packet
|
||||
0x00, 0x0B, // Length
|
||||
0x00, 0x22, // Sub-packet
|
||||
0x01, // Command
|
||||
0x00, 0x00, 0x00, 0x00, // Mobile Serial
|
||||
expectedAmount // Amount
|
||||
};
|
||||
|
||||
mobile.Serial.CopyTo(expectedData.Slice(6, 4));
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(10, 10)]
|
||||
[InlineData(-5, 0)]
|
||||
[InlineData(1024, 1024)]
|
||||
[InlineData(100000, 0xFFFF)]
|
||||
public void TestDamagePacket(int inputAmount, ushort expectedAmount)
|
||||
{
|
||||
Span<byte> data = new DamagePacket(mobile, inputAmount).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[] {
|
||||
0x0B, // Packet
|
||||
0x00, 0x00, 0x00, 0x00, // Mobile Serial
|
||||
0x00, 0x00 // Amount
|
||||
};
|
||||
|
||||
mobile.Serial.CopyTo(expectedData.Slice(1, 4));
|
||||
expectedAmount.CopyTo(expectedData.Slice(5, 2));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using Server.Network;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public class MapPatchesTests : IClassFixture<ServerFixture>
|
||||
{
|
||||
[Fact]
|
||||
public void TestMapPatches()
|
||||
{
|
||||
Span<byte> data = new MapPatches().Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[] {
|
||||
0xBF, // Packet
|
||||
0x00, 0x29, // Length
|
||||
0x00, 0x18, // Sub-packet
|
||||
0x00, 0x00, 0x00, 0x04, // 4 maps
|
||||
0x00, 0x00, 0x00, 0x00, // Felucca
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, // Trammel
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, // Ilshenar
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, // Malas
|
||||
0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
using System;
|
||||
using Server.Items;
|
||||
using Server.Network;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public class SecureTradePacketTests : IClassFixture<ServerFixture>
|
||||
{
|
||||
private readonly Mobile to;
|
||||
private readonly Container firstCont;
|
||||
private readonly Container secondCont;
|
||||
private readonly Item itemInFirstCont;
|
||||
|
||||
public SecureTradePacketTests(ServerFixture fixture)
|
||||
{
|
||||
to = fixture.toMobile;
|
||||
firstCont = fixture.fromCont;
|
||||
secondCont = fixture.toCont;
|
||||
itemInFirstCont = fixture.itemInFromCont;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("short-name")]
|
||||
[InlineData("this is a really long name that is more than 30 characters, probably")]
|
||||
public void TestDisplaySecureTrade(string name)
|
||||
{
|
||||
Span<byte> data = new DisplaySecureTrade(to, firstCont, secondCont, name).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x6F, // Packet ID
|
||||
0x00, 0x2F, // Length
|
||||
(byte)TradeFlag.Display, // Command
|
||||
0x00, 0x00, 0x00, 0x00, // Mobile Serial
|
||||
0x00, 0x00, 0x00, 0x00, // First Container Serial
|
||||
0x00, 0x00, 0x00, 0x00, // Second Container Serial
|
||||
0x01, // true if has name
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Name
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
int pos = 4;
|
||||
to.Serial.CopyTo(ref pos, expectedData);
|
||||
firstCont.Serial.CopyTo(ref pos, expectedData);
|
||||
secondCont.Serial.CopyTo(ref pos, expectedData);
|
||||
pos++;
|
||||
name.CopyASCIITo(ref pos, 30, expectedData);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestCloseSecureTrade()
|
||||
{
|
||||
Span<byte> data = new CloseSecureTrade(firstCont).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x6F, // Packet ID
|
||||
0x00, 0x8, // Length
|
||||
(byte)TradeFlag.Close, // Command
|
||||
0x00, 0x00, 0x00, 0x00 // Container Serial
|
||||
};
|
||||
|
||||
firstCont.Serial.CopyTo(expectedData.Slice(4, 4));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, false)] // Update first
|
||||
[InlineData(false, true)] // Update second
|
||||
public void TestUpdateSecureTrade(bool first, bool second)
|
||||
{
|
||||
Container cont = first ? firstCont : secondCont;
|
||||
Span<byte> data = new UpdateSecureTrade(cont, first, second).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x6F, // Packet ID
|
||||
0x00, 0x10, // Length
|
||||
(byte)TradeFlag.Update, // Command
|
||||
0x00, 0x00, 0x00, 0x00, // Container Serial
|
||||
0x00, 0x00, 0x00, first ? (byte)0x01 : (byte)0x00, // (int)1 if first
|
||||
0x00, 0x00, 0x00, second ? (byte)0x01 : (byte)0x00 // (int)1 if second
|
||||
};
|
||||
|
||||
cont.Serial.CopyTo(expectedData.Slice(4, 4));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(100000, 30, TradeFlag.UpdateGold)]
|
||||
[InlineData(250000, 50000, TradeFlag.UpdateLedger)]
|
||||
public void TestUpdateGoldSecureTrade(int gold, int plat, TradeFlag flag)
|
||||
{
|
||||
Span<byte> data = new UpdateSecureTrade(firstCont, flag, gold, plat).Compile();
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x6F, // Packet ID
|
||||
0x00, 0x10, // Length
|
||||
(byte)flag, // Command
|
||||
0x00, 0x00, 0x00, 0x00, // Container Serial
|
||||
0x00, 0x00, 0x00, 0x00, // Gold
|
||||
0x00, 0x00, 0x00, 0x00 // Platinum
|
||||
};
|
||||
|
||||
int pos = 4;
|
||||
firstCont.Serial.CopyTo(ref pos, expectedData);
|
||||
gold.CopyTo(ref pos, expectedData);
|
||||
plat.CopyTo(ref pos, expectedData);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestSecureTradeEquip()
|
||||
{
|
||||
Span<byte> data = new SecureTradeEquip(itemInFirstCont, to).Compile();
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x25, // Packet ID
|
||||
0x00, 0x00, 0x00, 0x00, // Item Serial
|
||||
0x00, 0x00, // Item ItemID
|
||||
0x00,
|
||||
0x00, 0x00, // Item Amount
|
||||
0x00, 0x00, // X
|
||||
0x00, 0x00, // Y
|
||||
0x00, 0x00, 0x00, 0x00, // Mobile Serial
|
||||
0x00, 0x00 // Item Hue
|
||||
};
|
||||
|
||||
int pos = 1;
|
||||
itemInFirstCont.Serial.CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.ItemID).CopyTo(ref pos, expectedData);
|
||||
pos++;
|
||||
((short)itemInFirstCont.Amount).CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.X).CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.Y).CopyTo(ref pos, expectedData);
|
||||
to.Serial.CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.Hue).CopyTo(ref pos, expectedData);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestSecureTradeEquip6017()
|
||||
{
|
||||
Span<byte> data = new SecureTradeEquip6017(itemInFirstCont, to).Compile();
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x25, // Packet ID
|
||||
0x00, 0x00, 0x00, 0x00, // Item Serial
|
||||
0x00, 0x00, // Item ItemID
|
||||
0x00, // Unknown
|
||||
0x00, 0x00, // Item Amount
|
||||
0x00, 0x00, // X
|
||||
0x00, 0x00, // Y
|
||||
0x00, // Grid Location
|
||||
0x00, 0x00, 0x00, 0x00, // Mobile Serial
|
||||
0x00, 0x00 // Item Hue
|
||||
};
|
||||
|
||||
int pos = 1;
|
||||
itemInFirstCont.Serial.CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.ItemID).CopyTo(ref pos, expectedData);
|
||||
pos++;
|
||||
((short)itemInFirstCont.Amount).CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.X).CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.Y).CopyTo(ref pos, expectedData);
|
||||
pos++;
|
||||
to.Serial.CopyTo(ref pos, expectedData);
|
||||
((short)itemInFirstCont.Hue).CopyTo(ref pos, expectedData);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Server.Items;
|
||||
using Server.Network;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public class VendorBuyPacketTests : IClassFixture<ServerFixture>
|
||||
{
|
||||
private readonly Mobile vendor;
|
||||
private readonly Container cont;
|
||||
|
||||
private readonly List<BuyItemState> buyStates;
|
||||
|
||||
public VendorBuyPacketTests(ServerFixture fixture)
|
||||
{
|
||||
vendor = fixture.fromMobile;
|
||||
cont = fixture.fromCont;
|
||||
|
||||
buyStates = new List<BuyItemState>
|
||||
{
|
||||
new BuyItemState("First Item", cont.Serial, Serial.NewItem, 10, 1, 0x01, 0),
|
||||
new BuyItemState("Second Item", cont.Serial, Serial.NewItem, 20, 2, 0x0A, 0),
|
||||
new BuyItemState("Third Item", cont.Serial, Serial.NewItem, 30, 10, 0x0F, 0)
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestVendorBuyContent()
|
||||
{
|
||||
Span<byte> data = new VendorBuyContent(buyStates).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 19];
|
||||
|
||||
int pos = 0;
|
||||
|
||||
expectedData[pos++] = 0x3C; // Packet ID
|
||||
((ushort)expectedData.Length).CopyTo(ref pos, expectedData); // Length
|
||||
((ushort)buyStates.Count).CopyTo(ref pos, expectedData); // Count
|
||||
|
||||
for (int i = buyStates.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BuyItemState buyState = buyStates[i];
|
||||
|
||||
buyState.MySerial.CopyTo(ref pos, expectedData);
|
||||
((ushort)buyState.ItemID).CopyTo(ref pos, expectedData);
|
||||
pos++; // ItemID Offset
|
||||
((ushort)buyState.Amount).CopyTo(ref pos, expectedData);
|
||||
((ushort)(i + 1)).CopyTo(ref pos, expectedData); // X
|
||||
expectedData[pos++] = 0;
|
||||
expectedData[pos++] = 1; // Y
|
||||
buyState.ContainerSerial.CopyTo(ref pos, expectedData);
|
||||
((ushort)buyState.Hue).CopyTo(ref pos, expectedData);
|
||||
}
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestVendorBuyContent6017()
|
||||
{
|
||||
Span<byte> data = new VendorBuyContent6017(buyStates).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 20];
|
||||
|
||||
int pos = 0;
|
||||
|
||||
expectedData[pos++] = 0x3C; // Packet ID
|
||||
((ushort)expectedData.Length).CopyTo(ref pos, expectedData); // Length
|
||||
((ushort)buyStates.Count).CopyTo(ref pos, expectedData); // Count
|
||||
|
||||
for (int i = buyStates.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BuyItemState buyState = buyStates[i];
|
||||
|
||||
buyState.MySerial.CopyTo(ref pos, expectedData);
|
||||
((ushort)buyState.ItemID).CopyTo(ref pos, expectedData);
|
||||
pos++; // ItemID Offset
|
||||
((ushort)buyState.Amount).CopyTo(ref pos, expectedData);
|
||||
((ushort)(i + 1)).CopyTo(ref pos, expectedData); // X
|
||||
expectedData[pos++] = 0;
|
||||
expectedData[pos++] = 1; // Y
|
||||
expectedData[pos++] = 0; // Grid Location
|
||||
buyState.ContainerSerial.CopyTo(ref pos, expectedData);
|
||||
((ushort)buyState.Hue).CopyTo(ref pos, expectedData);
|
||||
}
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestDisplayBuyList()
|
||||
{
|
||||
Span<byte> data = new DisplayBuyList(vendor).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x24, // Packet ID
|
||||
0x00, 0x00, 0x00, 0x00, // Vendor Serial
|
||||
0x00, 0x30 // Buy Window Gump Id
|
||||
};
|
||||
|
||||
vendor.Serial.CopyTo(expectedData.Slice(1, 4));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestDisplayBuyListHS()
|
||||
{
|
||||
Span<byte> data = new DisplayBuyListHS(vendor).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[]
|
||||
{
|
||||
0x24, // Packet ID
|
||||
0x00, 0x00, 0x00, 0x00, // Vendor Serial
|
||||
0x00, 0x30, // Buy Window Gump Id
|
||||
0x00, 0x00
|
||||
};
|
||||
|
||||
vendor.Serial.CopyTo(expectedData.Slice(1, 4));
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestVendorBuyList()
|
||||
{
|
||||
Span<byte> data = new VendorBuyList(vendor, buyStates).Compile();
|
||||
|
||||
int length = 8 + 5 * 3 + buyStates.Sum(state => state.Description.Length + 1);
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[length];
|
||||
|
||||
int pos = 0;
|
||||
|
||||
expectedData[pos++] = 0x74; // Packet ID
|
||||
((ushort)expectedData.Length).CopyTo(ref pos, expectedData); // Length
|
||||
Serial.MinusOne.CopyTo(ref pos, expectedData); // Vendor Buy Pack Serial or -1
|
||||
expectedData[pos++] = (byte)buyStates.Count;
|
||||
|
||||
for (int i = 0; i < buyStates.Count; i++)
|
||||
{
|
||||
BuyItemState state = buyStates[i];
|
||||
state.Price.CopyTo(ref pos, expectedData);
|
||||
string desc = state.Description ?? "";
|
||||
expectedData[pos++] = (byte)(desc.Length + 1);
|
||||
desc.CopyASCIITo(ref pos, expectedData);
|
||||
pos++;
|
||||
}
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
84
Projects/Server.Tests/Network/Packets/PacketTestUtilities.cs
Normal file
84
Projects/Server.Tests/Network/Packets/PacketTestUtilities.cs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Server.Network;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public static class PacketTestUtilities
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Span<byte> Compile(this Packet p) =>
|
||||
p.Compile(false, out int length).AsSpan(0, length);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this ushort number, Span<byte> bytes) => BinaryPrimitives.WriteUInt16BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this short number, Span<byte> bytes) => BinaryPrimitives.WriteInt16BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this Serial s, Span<byte> bytes) => BinaryPrimitives.WriteUInt32BigEndian(bytes, s);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this uint number, Span<byte> bytes) => BinaryPrimitives.WriteUInt32BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this int number, Span<byte> bytes) => BinaryPrimitives.WriteInt32BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, Span<byte> bytes) => Encoding.ASCII.GetBytes(str.AsSpan(), bytes);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, int max, Span<byte> bytes) => Encoding.ASCII.GetBytes(str.AsSpan(0, Math.Min(str.Length, max)), bytes);
|
||||
|
||||
// With Position Reference
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this ushort number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt16BigEndian(bytes.Slice(pos, 2), number);
|
||||
pos += 2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this short number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteInt16BigEndian(bytes.Slice(pos, 2), number);
|
||||
pos += 2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this Serial s, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt32BigEndian(bytes.Slice(pos, 4), s);
|
||||
pos += 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this uint number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt32BigEndian(bytes.Slice(pos, 4), number);
|
||||
pos += 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this int number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteInt32BigEndian(bytes.Slice(pos, 4), number);
|
||||
pos += 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
pos += Encoding.ASCII.GetBytes(str.AsSpan(), bytes.Slice(pos));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, ref int pos, int max, Span<byte> bytes)
|
||||
{
|
||||
pos += Encoding.ASCII.GetBytes(str.AsSpan(0, Math.Min(str.Length, max)), bytes.Slice(pos));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue