ModernUO/Projects/Server.Tests/Network/Packets/PacketTestUtilities.cs
2020-06-26 12:04:29 -07:00

103 lines
4.1 KiB
C#

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));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyASCIIFixedTo(this string str, ref int pos, int max, Span<byte> bytes)
{
int bytesWritten = Encoding.ASCII.GetBytes(str.AsSpan(0, Math.Min(str.Length, max)), bytes.Slice(pos));
// This is not needed in the current stackalloc implementation, but not guaranteed in the future.
if (bytesWritten < max)
bytes.Slice(pos + bytesWritten, max - bytesWritten).Clear();
pos += max;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clear(this Span<byte> span, ref int pos, int amount)
{
span.Slice(pos, amount).Clear();
pos += amount;
}
}
}