fix(core): Converts Gump Packets (v1) (#379)
- [X] Converts gump packets
This commit is contained in:
parent
126b80f74b
commit
a278623f38
31 changed files with 790 additions and 763 deletions
|
|
@ -8,26 +8,19 @@ using Xunit;
|
|||
|
||||
namespace Server.Tests.Network
|
||||
{
|
||||
public class GumpPacketTests
|
||||
public class GumpPacketTests : IClassFixture<ServerFixture>
|
||||
{
|
||||
[Fact]
|
||||
public void TestCloseGump()
|
||||
[Theory]
|
||||
[InlineData(100, 10)]
|
||||
public void TestCloseGump(int typeId, int buttonId)
|
||||
{
|
||||
var typeId = 100;
|
||||
var buttonId = 10;
|
||||
var expected = new CloseGump(typeId, buttonId).Compile();
|
||||
|
||||
var data = new CloseGump(typeId, buttonId).Compile();
|
||||
using var ns = PacketTestUtilities.CreateTestNetState();
|
||||
ns.SendCloseGump(typeId, buttonId);
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[13];
|
||||
var pos = 0;
|
||||
|
||||
expectedData.Write(ref pos, (byte)0xBF); // Packet ID
|
||||
expectedData.Write(ref pos, (ushort)0xD); // Length
|
||||
expectedData.Write(ref pos, (ushort)0x4); // Close Gump
|
||||
expectedData.Write(ref pos, typeId);
|
||||
expectedData.Write(ref pos, buttonId);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
var result = ns.SendPipe.Reader.TryRead();
|
||||
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -38,212 +31,97 @@ namespace Server.Tests.Network
|
|||
var unknownString = "This is an unknown string";
|
||||
var caption = "This is a caption";
|
||||
|
||||
var data = new DisplaySignGump(gumpSerial, gumpId, unknownString, caption).Compile();
|
||||
var expected = new DisplaySignGump(gumpSerial, gumpId, unknownString, caption).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[15 + unknownString.Length + caption.Length];
|
||||
var pos = 0;
|
||||
using var ns = PacketTestUtilities.CreateTestNetState();
|
||||
ns.SendDisplaySignGump(gumpSerial, gumpId, unknownString, caption);
|
||||
|
||||
expectedData.Write(ref pos, (byte)0x8B);
|
||||
expectedData.Write(ref pos, (ushort)expectedData.Length);
|
||||
expectedData.Write(ref pos, gumpSerial);
|
||||
expectedData.Write(ref pos, (ushort)gumpId);
|
||||
expectedData.Write(ref pos, (ushort)(unknownString.Length + 1));
|
||||
expectedData.WriteAsciiNull(ref pos, unknownString);
|
||||
expectedData.Write(ref pos, (ushort)(caption.Length + 1));
|
||||
expectedData.WriteAsciiNull(ref pos, caption);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
var result = ns.SendPipe.Reader.TryRead();
|
||||
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestFastGumpPacket()
|
||||
[Theory]
|
||||
[InlineData(ProtocolChanges.None)]
|
||||
[InlineData(ProtocolChanges.Unpack)]
|
||||
public void TestGumpPacketNameChange(ProtocolChanges changes)
|
||||
{
|
||||
var ns = new NetState(null);
|
||||
var gump = new NameChangeDeedGump();
|
||||
|
||||
var gump = new ResurrectGump(2);
|
||||
using var ns = PacketTestUtilities.CreateTestNetState();
|
||||
ns.ProtocolChanges = changes;
|
||||
|
||||
var data = gump.Compile(ns).Compile();
|
||||
var expected = gump.Compile(ns).Compile();
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[0x1000];
|
||||
ns.SendDisplayGump(gump, out _, out _);
|
||||
|
||||
var pos = 0;
|
||||
|
||||
expectedData[pos++] = 0xB0; // Packet ID
|
||||
pos += 2; // Length
|
||||
|
||||
expectedData.Write(ref pos, gump.Serial);
|
||||
expectedData.Write(ref pos, gump.TypeID);
|
||||
expectedData.Write(ref pos, gump.X);
|
||||
expectedData.Write(ref pos, gump.Y);
|
||||
pos += 2; // Layout Length
|
||||
|
||||
var layoutLength = 0;
|
||||
|
||||
if (!gump.Draggable)
|
||||
{
|
||||
expectedData.Write(ref pos, GumpUtilities.NoMoveBuffer);
|
||||
layoutLength += GumpUtilities.NoMove.Length;
|
||||
}
|
||||
|
||||
if (!gump.Closable)
|
||||
{
|
||||
expectedData.Write(ref pos, GumpUtilities.NoCloseBuffer);
|
||||
layoutLength += GumpUtilities.NoClose.Length;
|
||||
}
|
||||
|
||||
if (!gump.Disposable)
|
||||
{
|
||||
expectedData.Write(ref pos, GumpUtilities.NoDisposeBuffer);
|
||||
layoutLength += GumpUtilities.NoDispose.Length;
|
||||
}
|
||||
|
||||
if (!gump.Resizable)
|
||||
{
|
||||
expectedData.Write(ref pos, GumpUtilities.NoResizeBuffer);
|
||||
layoutLength += GumpUtilities.NoResize.Length;
|
||||
}
|
||||
|
||||
foreach (var entry in gump.Entries)
|
||||
{
|
||||
var str = entry.Compile(ns);
|
||||
expectedData.WriteAscii(ref pos, str);
|
||||
layoutLength += str.Length; // ASCII so 1:1
|
||||
}
|
||||
|
||||
expectedData.Slice(19, 2).Write((ushort)layoutLength);
|
||||
expectedData.Write(ref pos, (ushort)gump.Strings.Count);
|
||||
|
||||
for (var i = 0; i < gump.Strings.Count; ++i)
|
||||
{
|
||||
expectedData.WriteBigUni(ref pos, gump.Strings[i] ?? "");
|
||||
}
|
||||
|
||||
expectedData.Slice(1, 2).Write((ushort)pos); // Length
|
||||
|
||||
expectedData = expectedData.Slice(0, pos);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
var result = ns.SendPipe.Reader.TryRead();
|
||||
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestPackedGumpPacket()
|
||||
[Theory]
|
||||
[InlineData(ProtocolChanges.None)]
|
||||
[InlineData(ProtocolChanges.Unpack)]
|
||||
public void TestGumpPacketAdmin(ProtocolChanges changes)
|
||||
{
|
||||
var ns = new NetState(null)
|
||||
{
|
||||
ProtocolChanges = ProtocolChanges.Unpack
|
||||
};
|
||||
var m = new Mobile(0x1);
|
||||
m.DefaultMobileInit();
|
||||
m.RawName = "Test Mobile";
|
||||
m.AccessLevel = AccessLevel.Administrator;
|
||||
|
||||
var gump = new ResurrectGump(2);
|
||||
var gump = new AdminGump(m, AdminGumpPage.Clients);
|
||||
|
||||
var data = gump.Compile(ns).Compile();
|
||||
using var ns = PacketTestUtilities.CreateTestNetState();
|
||||
ns.ProtocolChanges = changes;
|
||||
|
||||
Span<byte> expectedData = stackalloc byte[0x1000];
|
||||
var expected = gump.Compile(ns).Compile();
|
||||
|
||||
var pos = 0;
|
||||
ns.SendDisplayGump(gump, out _, out _);
|
||||
|
||||
expectedData[pos++] = 0xDD; // Packet ID
|
||||
pos += 2; // Length
|
||||
|
||||
expectedData.Write(ref pos, gump.Serial);
|
||||
expectedData.Write(ref pos, gump.TypeID);
|
||||
expectedData.Write(ref pos, gump.X);
|
||||
expectedData.Write(ref pos, gump.Y);
|
||||
|
||||
var layoutList = new List<string>();
|
||||
var bufferLength = 1; // Null terminated
|
||||
|
||||
if (!gump.Draggable)
|
||||
{
|
||||
layoutList.Add(GumpUtilities.NoMove);
|
||||
bufferLength += GumpUtilities.NoMove.Length;
|
||||
}
|
||||
|
||||
if (!gump.Closable)
|
||||
{
|
||||
layoutList.Add(GumpUtilities.NoClose);
|
||||
bufferLength += GumpUtilities.NoClose.Length;
|
||||
}
|
||||
|
||||
if (!gump.Disposable)
|
||||
{
|
||||
layoutList.Add(GumpUtilities.NoDispose);
|
||||
bufferLength += GumpUtilities.NoDispose.Length;
|
||||
}
|
||||
|
||||
if (!gump.Resizable)
|
||||
{
|
||||
layoutList.Add(GumpUtilities.NoResize);
|
||||
bufferLength += GumpUtilities.NoResize.Length;
|
||||
}
|
||||
|
||||
foreach (var entry in gump.Entries)
|
||||
{
|
||||
var str = entry.Compile(ns);
|
||||
bufferLength += str.Length;
|
||||
layoutList.Add(str);
|
||||
}
|
||||
|
||||
var rawBuffer = ArrayPool<byte>.Shared.Rent(bufferLength);
|
||||
Span<byte> buffer = rawBuffer;
|
||||
var bufferPos = 0;
|
||||
|
||||
foreach (var layout in layoutList)
|
||||
{
|
||||
buffer.WriteAscii(ref bufferPos, layout);
|
||||
}
|
||||
|
||||
#if NO_LOCAL_INIT
|
||||
buffer.Write(ref bufferPos, (byte)0); // Layout terminator
|
||||
#else
|
||||
bufferPos++;
|
||||
#endif
|
||||
|
||||
expectedData.WritePacked(ref pos, buffer.Slice(0, bufferPos));
|
||||
ArrayPool<byte>.Shared.Return(rawBuffer);
|
||||
|
||||
expectedData.Write(ref pos, gump.Strings.Count);
|
||||
bufferLength = gump.Strings.Sum(str => 2 + str.Length * 2);
|
||||
rawBuffer = ArrayPool<byte>.Shared.Rent(bufferLength);
|
||||
buffer = rawBuffer;
|
||||
bufferPos = 0;
|
||||
|
||||
foreach (var str in gump.Strings)
|
||||
{
|
||||
buffer.WriteBigUni(ref bufferPos, str);
|
||||
}
|
||||
|
||||
expectedData.WritePacked(ref pos, buffer.Slice(0, bufferPos));
|
||||
ArrayPool<byte>.Shared.Return(rawBuffer);
|
||||
|
||||
// Length
|
||||
expectedData.Slice(1, 2).Write((ushort)pos);
|
||||
expectedData = expectedData.Slice(0, pos);
|
||||
|
||||
AssertThat.Equal(data, expectedData);
|
||||
var result = ns.SendPipe.Reader.TryRead();
|
||||
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
|
||||
}
|
||||
}
|
||||
|
||||
public class ResurrectGump : Gump
|
||||
public class NameChangeDeedGump : Gump
|
||||
{
|
||||
public ResurrectGump(int msg) : base(100, 0)
|
||||
public NameChangeDeedGump() : base(50, 50)
|
||||
{
|
||||
Closable = false;
|
||||
Draggable = false;
|
||||
Resizable = false;
|
||||
|
||||
AddPage(0);
|
||||
|
||||
AddBackground(0, 0, 400, 350, 2600);
|
||||
AddBlackAlpha(10, 120, 250, 85);
|
||||
AddHtml(10, 125, 250, 20, Color(Center("Name Change Deed"), 0xFFFFFF));
|
||||
|
||||
AddHtmlLocalized(0, 20, 400, 35, 1011022); // <center>Resurrection</center>
|
||||
AddLabel(73, 15, 1152, "");
|
||||
AddLabel(20, 150, 0x480, "New Name:");
|
||||
AddTextField(100, 150, 150, 20, 0);
|
||||
|
||||
/* It is possible for you to be resurrected here by this healer. Do you wish to try?<br>
|
||||
* CONTINUE - You chose to try to come back to life now.<br>
|
||||
* CANCEL - You prefer to remain a ghost for now.
|
||||
*/
|
||||
AddHtmlLocalized(50, 55, 300, 140, 1011023 + msg, true, true);
|
||||
AddButtonLabeled(75, 180, 1, "Submit");
|
||||
}
|
||||
|
||||
AddButton(200, 227, 4005, 4007, 0);
|
||||
AddHtmlLocalized(235, 230, 110, 35, 1011012); // CANCEL
|
||||
public void AddBlackAlpha(int x, int y, int width, int height)
|
||||
{
|
||||
AddImageTiled(x, y, width, height, 2624);
|
||||
AddAlphaRegion(x, y, width, height);
|
||||
}
|
||||
|
||||
AddButton(65, 227, 4005, 4007, 1);
|
||||
AddHtmlLocalized(100, 230, 110, 35, 1011011); // CONTINUE
|
||||
public void AddTextField(int x, int y, int width, int height, int index)
|
||||
{
|
||||
AddBackground(x - 2, y - 2, width + 4, height + 4, 0x2486);
|
||||
AddTextEntry(x + 2, y + 2, width - 4, height - 4, 0, index, "");
|
||||
}
|
||||
|
||||
public static string Center(string text) => $"<CENTER>{text}</CENTER>";
|
||||
|
||||
public static string Color(string text, int color) => $"<BASEFONT COLOR=#{color:X6}>{text}</BASEFONT>";
|
||||
|
||||
public void AddButtonLabeled(int x, int y, int buttonID, string text)
|
||||
{
|
||||
AddButton(x, y - 1, 4005, 4007, buttonID);
|
||||
AddHtml(x + 35, y, 240, 20, Color(text, 0xFFFFFF));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,310 @@
|
|||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using Server.Gumps;
|
||||
using Server.Network;
|
||||
|
||||
namespace Server.Tests
|
||||
{
|
||||
public interface IGumpWriter
|
||||
{
|
||||
int TextEntries { get; set; }
|
||||
int Switches { get; set; }
|
||||
|
||||
void AppendLayout(bool val);
|
||||
void AppendLayout(int val);
|
||||
void AppendLayout(uint val);
|
||||
void AppendLayoutNS(int val);
|
||||
void AppendLayout(string text);
|
||||
void AppendLayoutNS(string text);
|
||||
void AppendLayout(byte[] buffer);
|
||||
void WriteStrings(List<string> strings);
|
||||
void Flush();
|
||||
}
|
||||
|
||||
public sealed class CloseGump : Packet
|
||||
{
|
||||
public CloseGump(int typeID, int buttonID) : base(0xBF)
|
||||
{
|
||||
EnsureCapacity(13);
|
||||
|
||||
Stream.Write((short)0x04);
|
||||
Stream.Write(typeID);
|
||||
Stream.Write(buttonID);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DisplayGumpPacked : Packet, IGumpWriter
|
||||
{
|
||||
private static readonly byte[] m_True = Gump.StringToBuffer(" 1");
|
||||
private static readonly byte[] m_False = Gump.StringToBuffer(" 0");
|
||||
|
||||
private static readonly byte[] m_BeginTextSeparator = Gump.StringToBuffer(" @");
|
||||
private static readonly byte[] m_EndTextSeparator = Gump.StringToBuffer("@");
|
||||
|
||||
private static readonly byte[] m_Buffer = new byte[48];
|
||||
|
||||
private readonly Gump m_Gump;
|
||||
|
||||
private readonly PacketWriter m_Layout;
|
||||
private readonly PacketWriter m_Strings;
|
||||
|
||||
private int m_StringCount;
|
||||
|
||||
static DisplayGumpPacked() => m_Buffer[0] = (byte)' ';
|
||||
|
||||
public DisplayGumpPacked(Gump gump)
|
||||
: base(0xDD)
|
||||
{
|
||||
m_Gump = gump;
|
||||
|
||||
m_Layout = PacketWriter.CreateInstance(8192);
|
||||
m_Strings = PacketWriter.CreateInstance(8192);
|
||||
}
|
||||
|
||||
public int TextEntries { get; set; }
|
||||
|
||||
public int Switches { get; set; }
|
||||
|
||||
public void AppendLayout(bool val)
|
||||
{
|
||||
AppendLayout(val ? m_True : m_False);
|
||||
}
|
||||
|
||||
public void AppendLayout(int val)
|
||||
{
|
||||
var toString = val.ToString();
|
||||
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
|
||||
|
||||
m_Layout.Write(m_Buffer, 0, bytes);
|
||||
}
|
||||
|
||||
public void AppendLayout(uint val)
|
||||
{
|
||||
var toString = val.ToString();
|
||||
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
|
||||
|
||||
m_Layout.Write(m_Buffer, 0, bytes);
|
||||
}
|
||||
|
||||
public void AppendLayoutNS(int val)
|
||||
{
|
||||
var toString = val.ToString();
|
||||
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1);
|
||||
|
||||
m_Layout.Write(m_Buffer, 1, bytes);
|
||||
}
|
||||
|
||||
public void AppendLayoutNS(string text)
|
||||
{
|
||||
m_Layout.WriteAsciiFixed(text, text.Length);
|
||||
}
|
||||
|
||||
public void AppendLayout(string text)
|
||||
{
|
||||
AppendLayout(m_BeginTextSeparator);
|
||||
|
||||
m_Layout.WriteAsciiFixed(text, text.Length);
|
||||
|
||||
AppendLayout(m_EndTextSeparator);
|
||||
}
|
||||
|
||||
public void AppendLayout(byte[] buffer)
|
||||
{
|
||||
m_Layout.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public void WriteStrings(List<string> strings)
|
||||
{
|
||||
m_StringCount = strings.Count;
|
||||
|
||||
for (var i = 0; i < strings.Count; ++i)
|
||||
{
|
||||
var v = strings[i] ?? "";
|
||||
|
||||
m_Strings.Write((ushort)v.Length);
|
||||
m_Strings.WriteBigUniFixed(v, v.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
EnsureCapacity(28 + (int)m_Layout.Length + (int)m_Strings.Length);
|
||||
|
||||
Stream.Write(m_Gump.Serial);
|
||||
Stream.Write(m_Gump.TypeID);
|
||||
Stream.Write(m_Gump.X);
|
||||
Stream.Write(m_Gump.Y);
|
||||
|
||||
// Note: layout MUST be null terminated (don't listen to krrios)
|
||||
m_Layout.Write((byte)0);
|
||||
|
||||
WritePacked(m_Layout);
|
||||
|
||||
Stream.Write(m_StringCount);
|
||||
|
||||
WritePacked(m_Strings);
|
||||
|
||||
PacketWriter.ReleaseInstance(m_Layout);
|
||||
PacketWriter.ReleaseInstance(m_Strings);
|
||||
}
|
||||
|
||||
private void WritePacked(PacketWriter src)
|
||||
{
|
||||
var buffer = src.UnderlyingStream.GetBuffer();
|
||||
var length = (int)src.Length;
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
Stream.Write(0);
|
||||
return;
|
||||
}
|
||||
|
||||
var wantLength = 1 + length * 1024 / 1000;
|
||||
|
||||
wantLength += 4095;
|
||||
wantLength &= ~4095;
|
||||
|
||||
var packBuffer = ArrayPool<byte>.Shared.Rent(wantLength);
|
||||
|
||||
var packLength = wantLength;
|
||||
|
||||
Zlib.Pack(packBuffer, ref packLength, buffer, length, ZlibQuality.Default);
|
||||
|
||||
Stream.Write(4 + packLength);
|
||||
Stream.Write(length);
|
||||
Stream.Write(packBuffer, 0, packLength);
|
||||
|
||||
ArrayPool<byte>.Shared.Return(packBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DisplayGumpFast : Packet, IGumpWriter
|
||||
{
|
||||
private static readonly byte[] m_True = Gump.StringToBuffer(" 1");
|
||||
private static readonly byte[] m_False = Gump.StringToBuffer(" 0");
|
||||
|
||||
private static readonly byte[] m_BeginTextSeparator = Gump.StringToBuffer(" @");
|
||||
private static readonly byte[] m_EndTextSeparator = Gump.StringToBuffer("@");
|
||||
|
||||
private readonly byte[] m_Buffer = new byte[48];
|
||||
private int m_LayoutLength;
|
||||
|
||||
public DisplayGumpFast(Gump g) : base(0xB0)
|
||||
{
|
||||
m_Buffer[0] = (byte)' ';
|
||||
|
||||
EnsureCapacity(4096);
|
||||
|
||||
Stream.Write(g.Serial);
|
||||
Stream.Write(g.TypeID);
|
||||
Stream.Write(g.X);
|
||||
Stream.Write(g.Y);
|
||||
Stream.Write((ushort)0xFFFF);
|
||||
}
|
||||
|
||||
public int TextEntries { get; set; }
|
||||
|
||||
public int Switches { get; set; }
|
||||
|
||||
public void AppendLayout(bool val)
|
||||
{
|
||||
AppendLayout(val ? m_True : m_False);
|
||||
}
|
||||
|
||||
public void AppendLayout(int val)
|
||||
{
|
||||
var toString = val.ToString();
|
||||
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
|
||||
|
||||
Stream.Write(m_Buffer, 0, bytes);
|
||||
m_LayoutLength += bytes;
|
||||
}
|
||||
|
||||
public void AppendLayout(uint val)
|
||||
{
|
||||
var toString = val.ToString();
|
||||
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
|
||||
|
||||
Stream.Write(m_Buffer, 0, bytes);
|
||||
m_LayoutLength += bytes;
|
||||
}
|
||||
|
||||
public void AppendLayoutNS(int val)
|
||||
{
|
||||
var toString = val.ToString();
|
||||
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1);
|
||||
|
||||
Stream.Write(m_Buffer, 1, bytes);
|
||||
m_LayoutLength += bytes;
|
||||
}
|
||||
|
||||
public void AppendLayoutNS(string text)
|
||||
{
|
||||
var length = text.Length;
|
||||
Stream.WriteAsciiFixed(text, length);
|
||||
m_LayoutLength += length;
|
||||
}
|
||||
|
||||
public void AppendLayout(string text)
|
||||
{
|
||||
AppendLayout(m_BeginTextSeparator);
|
||||
|
||||
var length = text.Length;
|
||||
Stream.WriteAsciiFixed(text, length);
|
||||
m_LayoutLength += length;
|
||||
|
||||
AppendLayout(m_EndTextSeparator);
|
||||
}
|
||||
|
||||
public void AppendLayout(byte[] buffer)
|
||||
{
|
||||
var length = buffer.Length;
|
||||
Stream.Write(buffer, 0, length);
|
||||
m_LayoutLength += length;
|
||||
}
|
||||
|
||||
public void WriteStrings(List<string> text)
|
||||
{
|
||||
Stream.Seek(19, SeekOrigin.Begin);
|
||||
Stream.Write((ushort)m_LayoutLength);
|
||||
Stream.Seek(0, SeekOrigin.End);
|
||||
|
||||
Stream.Write((ushort)text.Count);
|
||||
|
||||
for (var i = 0; i < text.Count; ++i)
|
||||
{
|
||||
var v = text[i] ?? "";
|
||||
|
||||
int length = (ushort)v.Length;
|
||||
|
||||
Stream.Write((ushort)length);
|
||||
Stream.WriteBigUniFixed(v, length);
|
||||
}
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DisplaySignGump : Packet
|
||||
{
|
||||
public DisplaySignGump(Serial serial, int gumpID, string unknown, string caption) : base(0x8B)
|
||||
{
|
||||
unknown ??= "";
|
||||
caption ??= "";
|
||||
|
||||
EnsureCapacity(15 + unknown.Length + caption.Length);
|
||||
|
||||
Stream.Write(serial);
|
||||
Stream.Write((short)gumpID);
|
||||
Stream.Write((short)(unknown.Length + 1));
|
||||
Stream.WriteAsciiNull(unknown);
|
||||
Stream.Write((short)(caption.Length + 1));
|
||||
Stream.WriteAsciiNull(caption);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +1,469 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using Server.Gumps;
|
||||
using Server.Network;
|
||||
|
||||
namespace Server.Tests.Network
|
||||
{
|
||||
public static class GumpUtilities
|
||||
{
|
||||
public const string NoMove = "{ nomove }";
|
||||
public const string NoClose = "{ noclose }";
|
||||
public const string NoDispose = "{ nodispose }";
|
||||
public const string NoResize = "{ noresize }";
|
||||
public static readonly byte[] NoMoveBuffer = Encoding.ASCII.GetBytes("{ nomove }");
|
||||
public static readonly byte[] NoCloseBuffer = Encoding.ASCII.GetBytes("{ noclose }");
|
||||
public static readonly byte[] NoDisposeBuffer = Encoding.ASCII.GetBytes("{ nodispose }");
|
||||
public static readonly byte[] NoResizeBuffer = Encoding.ASCII.GetBytes("{ noresize }");
|
||||
private static readonly byte[] m_BeginLayout = Gump.StringToBuffer("{ ");
|
||||
private static readonly byte[] m_EndLayout = Gump.StringToBuffer(" }");
|
||||
|
||||
public static void WritePacked(this Span<byte> dest, ref int pos, ReadOnlySpan<byte> source)
|
||||
public static Packet Compile(this Gump g, NetState ns = null)
|
||||
{
|
||||
var length = source.Length;
|
||||
IGumpWriter disp;
|
||||
|
||||
if (length == 0)
|
||||
if (ns?.Unpack == true)
|
||||
{
|
||||
#if NO_LOCAL_INIT
|
||||
dest.Write(ref pos, 0);
|
||||
#else
|
||||
pos += 4;
|
||||
#endif
|
||||
return;
|
||||
disp = new DisplayGumpPacked(g);
|
||||
}
|
||||
else
|
||||
{
|
||||
disp = new DisplayGumpFast(g);
|
||||
}
|
||||
|
||||
var packLength = (ulong)dest.Length - 8;
|
||||
|
||||
ZlibError ce = Zlib.Pack(dest.Slice(pos + 8), ref packLength, source, ZlibQuality.Default);
|
||||
if (ce != ZlibError.Okay)
|
||||
if (!g.Draggable)
|
||||
{
|
||||
Console.WriteLine("ZLib error: {0} (#{1})", ce, (int)ce);
|
||||
disp.AppendLayout(Gump.NoMove);
|
||||
}
|
||||
|
||||
dest.Write(ref pos, (int)(4 + packLength));
|
||||
dest.Write(ref pos, length);
|
||||
pos += (int)packLength;
|
||||
if (!g.Closable)
|
||||
{
|
||||
disp.AppendLayout(Gump.NoClose);
|
||||
}
|
||||
|
||||
if (!g.Disposable)
|
||||
{
|
||||
disp.AppendLayout(Gump.NoDispose);
|
||||
}
|
||||
|
||||
if (!g.Resizable)
|
||||
{
|
||||
disp.AppendLayout(Gump.NoResize);
|
||||
}
|
||||
|
||||
var count = g.Entries.Count;
|
||||
|
||||
for (var i = 0; i < count; ++i)
|
||||
{
|
||||
var e = g.Entries[i];
|
||||
|
||||
disp.AppendLayout(m_BeginLayout);
|
||||
e.AppendToByType(disp);
|
||||
disp.AppendLayout(m_EndLayout);
|
||||
}
|
||||
|
||||
disp.WriteStrings(g.Strings);
|
||||
|
||||
disp.Flush();
|
||||
|
||||
return (Packet)disp;
|
||||
}
|
||||
|
||||
public static void AppendToByType(this GumpEntry e, IGumpWriter disp)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case GumpAlphaRegion g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpBackground g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpButton g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpCheck g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpGroup g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpECHandleInput g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpHtml g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpHtmlLocalized g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpImage g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpImageTileButton g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpImageTiled g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpItem g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpItemProperty g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpLabel g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpLabelCropped g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpMasterGump g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpPage g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpRadio g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpSpriteImage g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpTextEntry g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpTextEntryLimited g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
case GumpTooltip g:
|
||||
{
|
||||
g.AppendTo(disp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpAlphaRegion g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpAlphaRegion.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpBackground g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpBackground.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.GumpID);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpButton g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpButton.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.NormalID);
|
||||
disp.AppendLayout(g.PressedID);
|
||||
disp.AppendLayout((int)g.Type);
|
||||
disp.AppendLayout(g.Param);
|
||||
disp.AppendLayout(g.ButtonID);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpCheck g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpButton.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.InactiveID);
|
||||
disp.AppendLayout(g.ActiveID);
|
||||
disp.AppendLayout(g.InitialState);
|
||||
disp.AppendLayout(g.SwitchID);
|
||||
|
||||
disp.Switches++;
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpGroup g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpGroup.LayoutName);
|
||||
disp.AppendLayout(g.Group);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpECHandleInput g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpECHandleInput.LayoutName);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpHtml g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpHtml.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.Parent.Intern(g.Text));
|
||||
disp.AppendLayout(g.Background);
|
||||
disp.AppendLayout(g.Scrollbar);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpHtmlLocalized g, IGumpWriter disp)
|
||||
{
|
||||
switch (g.Type)
|
||||
{
|
||||
case GumpHtmlLocalizedType.Plain:
|
||||
{
|
||||
disp.AppendLayout(GumpHtmlLocalized.LayoutNamePlain);
|
||||
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.Number);
|
||||
disp.AppendLayout(g.Background);
|
||||
disp.AppendLayout(g.Scrollbar);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case GumpHtmlLocalizedType.Color:
|
||||
{
|
||||
disp.AppendLayout(GumpHtmlLocalized.LayoutNameColor);
|
||||
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.Number);
|
||||
disp.AppendLayout(g.Background);
|
||||
disp.AppendLayout(g.Scrollbar);
|
||||
disp.AppendLayout(g.Color);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case GumpHtmlLocalizedType.Args:
|
||||
{
|
||||
disp.AppendLayout(GumpHtmlLocalized.LayoutNameArgs);
|
||||
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.Background);
|
||||
disp.AppendLayout(g.Scrollbar);
|
||||
disp.AppendLayout(g.Color);
|
||||
disp.AppendLayout(g.Number);
|
||||
disp.AppendLayout(g.Args);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpImage g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpImage.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.GumpID);
|
||||
|
||||
if (g.Hue != 0)
|
||||
{
|
||||
disp.AppendLayout(GumpImage.HueEquals);
|
||||
disp.AppendLayoutNS(g.Hue);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(g.Class))
|
||||
{
|
||||
disp.AppendLayout(GumpImage.ClassEquals);
|
||||
disp.AppendLayoutNS(g.Class);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpImageTileButton g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpImageTileButton.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.NormalID);
|
||||
disp.AppendLayout(g.PressedID);
|
||||
disp.AppendLayout((int)g.Type);
|
||||
disp.AppendLayout(g.Param);
|
||||
disp.AppendLayout(g.ButtonID);
|
||||
|
||||
disp.AppendLayout(g.ItemID);
|
||||
disp.AppendLayout(g.Hue);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
|
||||
if (g.LocalizedTooltip > 0)
|
||||
{
|
||||
disp.AppendLayout(GumpImageTileButton.LayoutTooltip);
|
||||
disp.AppendLayout(g.LocalizedTooltip);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpImageTiled g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpImageTiled.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.GumpID);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpItem g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(g.Hue == 0 ? GumpItem.LayoutName : GumpItem.LayoutNameHue);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.ItemID);
|
||||
|
||||
if (g.Hue != 0)
|
||||
{
|
||||
disp.AppendLayout(g.Hue);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpItemProperty g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpItemProperty.LayoutName);
|
||||
disp.AppendLayout(g.Serial);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpLabel g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpLabel.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Hue);
|
||||
disp.AppendLayout(g.Parent.Intern(g.Text));
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpLabelCropped g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpLabelCropped.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.Hue);
|
||||
disp.AppendLayout(g.Parent.Intern(g.Text));
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpMasterGump g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpMasterGump.LayoutName);
|
||||
disp.AppendLayout(g.GumpID);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpPage g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpPage.LayoutName);
|
||||
disp.AppendLayout(g.Page);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpRadio g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpRadio.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.InactiveID);
|
||||
disp.AppendLayout(g.ActiveID);
|
||||
disp.AppendLayout(g.InitialState);
|
||||
disp.AppendLayout(g.SwitchID);
|
||||
|
||||
disp.Switches++;
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpSpriteImage g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpSpriteImage.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.GumpID);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.SX);
|
||||
disp.AppendLayout(g.SY);
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpTextEntry g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpTextEntry.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.Hue);
|
||||
disp.AppendLayout(g.EntryID);
|
||||
disp.AppendLayout(g.Parent.Intern(g.InitialText));
|
||||
|
||||
disp.TextEntries++;
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpTextEntryLimited g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpTextEntryLimited.LayoutName);
|
||||
disp.AppendLayout(g.X);
|
||||
disp.AppendLayout(g.Y);
|
||||
disp.AppendLayout(g.Width);
|
||||
disp.AppendLayout(g.Height);
|
||||
disp.AppendLayout(g.Hue);
|
||||
disp.AppendLayout(g.EntryID);
|
||||
disp.AppendLayout(g.Parent.Intern(g.InitialText));
|
||||
disp.AppendLayout(g.Size);
|
||||
|
||||
disp.TextEntries++;
|
||||
}
|
||||
|
||||
public static void AppendTo(this GumpTooltip g, IGumpWriter disp)
|
||||
{
|
||||
disp.AppendLayout(GumpTooltip.LayoutName);
|
||||
disp.AppendLayout(g.Number);
|
||||
|
||||
if (!string.IsNullOrEmpty(g.Args))
|
||||
{
|
||||
disp.AppendLayout(g.Args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue