Skip to content

Commit

Permalink
fix: Removes support for v4 Client and old gump packet. (#1739)
Browse files Browse the repository at this point in the history
### Summary
* Removes old gump packet support
* Removes support for v4 clients
* Removes `Unpack` flag and assumes it is always true.
* Removes StringToBuffer since this is built into .NET now.

> [!Note]
> View the file changes with white space off: https://github.com/modernuo/ModernUO/pull/1739/files?diff=split&w=1
  • Loading branch information
kamronbatman committed Apr 25, 2024
1 parent 98d31bc commit 9cd84ba
Show file tree
Hide file tree
Showing 14 changed files with 972 additions and 1,148 deletions.
823 changes: 405 additions & 418 deletions Projects/Server.Tests/Helpers/GumpUtilities.cs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Projects/Server.Tests/Helpers/PacketWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ public void Write(byte[] buffer, int offset, int size)
UnderlyingStream.Write(buffer, offset, size);
}

/// <summary>
/// Writes a sequence of bytes to the underlying stream
/// </summary>
public void Write(ReadOnlySpan<byte> buffer)
{
UnderlyingStream.Write(buffer);
}

/// <summary>
/// Writes a fixed-length ASCII-encoded string value to the underlying stream. To fit (size), the string content is either
/// truncated or padded with null characters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,115 @@
using Server.Network;
using Xunit;

namespace Server.Tests.Network
namespace Server.Tests.Network;

public class GumpPacketTests : IClassFixture<ServerFixture>
{
public class GumpPacketTests : IClassFixture<ServerFixture>
[Theory]
[InlineData(100, 10)]
public void TestCloseGump(int typeId, int buttonId)
{
[Theory]
[InlineData(100, 10)]
public void TestCloseGump(int typeId, int buttonId)
{
var expected = new CloseGump(typeId, buttonId).Compile();
var expected = new CloseGump(typeId, buttonId).Compile();

var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCloseGump(typeId, buttonId);
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCloseGump(typeId, buttonId);

var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
public void TestDisplaySignGump()
{
Serial gumpSerial = (Serial)0x1000;
const int gumpId = 100;
const string unknownString = "This is an unknown string";
const string caption = "This is a caption";
[Fact]
public void TestDisplaySignGump()
{
Serial gumpSerial = (Serial)0x1000;
const int gumpId = 100;
const string unknownString = "This is an unknown string";
const string caption = "This is a caption";

var expected = new DisplaySignGump(gumpSerial, gumpId, unknownString, caption).Compile();
var expected = new DisplaySignGump(gumpSerial, gumpId, unknownString, caption).Compile();

var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplaySignGump(gumpSerial, gumpId, unknownString, caption);
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplaySignGump(gumpSerial, gumpId, unknownString, caption);

var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Theory]
[InlineData(ProtocolChanges.None)]
[InlineData(ProtocolChanges.Unpack)]
public void TestGumpPacketNameChange(ProtocolChanges changes)
{
var gump = new NameChangeDeedGump();
[Fact]
public void TestGumpPacketNameChange()
{
var gump = new NameChangeDeedGump();

var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
var ns = PacketTestUtilities.CreateTestNetState();

var expected = gump.Compile(ns).Compile();
var expected = gump.Compile(ns).Compile();

ns.SendDisplayGump(gump, out _, out _);
ns.SendDisplayGump(gump, out _, out _);

var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Theory]
[InlineData(ProtocolChanges.None)]
[InlineData(ProtocolChanges.Unpack)]
public void TestGumpPacketAdmin(ProtocolChanges changes)
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.RawName = "Test Mobile";
m.AccessLevel = AccessLevel.Administrator;
[Fact]
public void TestGumpPacketAdmin()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.RawName = "Test Mobile";
m.AccessLevel = AccessLevel.Administrator;

var gump = new AdminGump(m, AdminGumpPage.Clients);
var gump = new AdminGump(m, AdminGumpPage.Clients);

var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
var ns = PacketTestUtilities.CreateTestNetState();

var expected = gump.Compile(ns).Compile();
var expected = gump.Compile(ns).Compile();

ns.SendDisplayGump(gump, out _, out _);
ns.SendDisplayGump(gump, out _, out _);

var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
}

public class NameChangeDeedGump : Gump
public class NameChangeDeedGump : Gump
{
public NameChangeDeedGump() : base(50, 50)
{
public NameChangeDeedGump() : base(50, 50)
{
Closable = false;
Draggable = false;
Resizable = false;
Closable = false;
Draggable = false;
Resizable = false;

AddPage(0);
AddPage(0);

AddBlackAlpha(10, 120, 250, 85);
AddHtml(10, 125, 250, 20, Color(Center("Name Change Deed"), 0xFFFFFF));
AddBlackAlpha(10, 120, 250, 85);
AddHtml(10, 125, 250, 20, Color(Center("Name Change Deed"), 0xFFFFFF));

AddLabel(73, 15, 1152, "");
AddLabel(20, 150, 0x480, "New Name:");
AddTextField(100, 150, 150, 20, 0);
AddLabel(73, 15, 1152, "");
AddLabel(20, 150, 0x480, "New Name:");
AddTextField(100, 150, 150, 20, 0);

AddButtonLabeled(75, 180, 1, "Submit");
}
AddButtonLabeled(75, 180, 1, "Submit");
}

public void AddBlackAlpha(int x, int y, int width, int height)
{
AddImageTiled(x, y, width, height, 2624);
AddAlphaRegion(x, y, width, height);
}
public void AddBlackAlpha(int x, int y, int width, int height)
{
AddImageTiled(x, y, width, height, 2624);
AddAlphaRegion(x, y, width, height);
}

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 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 Center(string text) => $"<CENTER>{text}</CENTER>";

public static string Color(string text, int color) => $"<BASEFONT COLOR=#{color:X6}>{text}</BASEFONT>";
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));
}
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));
}
}
131 changes: 7 additions & 124 deletions Projects/Server.Tests/Tests/Network/Packets/Outgoing/GumpPackets.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using Server.Gumps;
Expand All @@ -20,7 +20,7 @@ public interface IGumpWriter
void AppendLayoutNS(int val);
void AppendLayout(string text);
void AppendLayoutNS(string text);
void AppendLayout(byte[] buffer);
void AppendLayout(ReadOnlySpan<byte> buffer);
void WriteStrings(List<string> strings);
void Flush();
}
Expand All @@ -39,12 +39,6 @@ public CloseGump(int typeID, int buttonID) : base(0xBF)

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;
Expand All @@ -71,7 +65,7 @@ public DisplayGumpPacked(Gump gump)

public void AppendLayout(bool val)
{
AppendLayout(val ? m_True : m_False);
AppendLayout(val ? " 1"u8 : " 0"u8);
}

public void AppendLayout(int val)
Expand Down Expand Up @@ -107,16 +101,16 @@ public void AppendLayoutNS(string text)

public void AppendLayout(string text)
{
AppendLayout(m_BeginTextSeparator);
AppendLayout(" @"u8);

m_Layout.WriteAsciiFixed(text, text.Length);

AppendLayout(m_EndTextSeparator);
AppendLayout("@"u8);
}

public void AppendLayout(byte[] buffer)
public void AppendLayout(ReadOnlySpan<byte> buffer)
{
m_Layout.Write(buffer, 0, buffer.Length);
m_Layout.Write(buffer);
}

public void WriteStrings(List<string> strings)
Expand Down Expand Up @@ -184,117 +178,6 @@ private void WritePacked(PacketWriter src)
}
}

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 AppendLayout(Serial serial) => AppendLayout(serial.Value);

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)
Expand Down

0 comments on commit 9cd84ba

Please sign in to comment.