# New Gump API We are pleased to release a new API that is faster, allocates nearly zero memory, and still feels very similar to the original API. The API is broken into 3 types of gumps, dynamic, static with placeholders, and static without placeholders. ### Dynamic Gumps These gumps will inherit `DynamicGump` and are meant for gumps that have a dynamic layout. This includes specifying dynamic arguments to HtmlLocalized entries. ## Static Gumps Static gumps are those where the function to the build the layout is called only once and cached forever. They can optionally have placeholders. These placeholders allow the developer to specify the string values later, dynamically in a `BuildStrings` method on the gump. If a gump does not have any placeholders, the string entries will also be cached forever. ## Benchmarks To make sure we were going in the right direction and not wasting time, we took copious benchmarks. Here are the final benchmarks for a really simple gump. Note: * The majority of creating a gump is compressing the layout and the strings. Compressing each section takes ~6,000ns (12us total). ```cs | Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | |------------------------------- |-------------:|-------------:|-------------:|-------------:|------:|--------:|-------:|----------:|------------:| | OldGump | 13,308.29 ns | 1,059.695 ns | 1,883.608 ns | 14,330.72 ns | 1.000 | 0.00 | 0.1526 | 2400 B | 1.00 | | DynamicLayoutGump | 13,357.86 ns | 129.144 ns | 226.185 ns | 13,323.60 ns | 1.029 | 0.17 | - | 48 B | 0.02 | | StaticLayoutDynamicStringsGump | 6,653.10 ns | 81.815 ns | 143.292 ns | 6,617.45 ns | 0.514 | 0.09 | - | 40 B | 0.02 | | StaticLayoutGump | 86.33 ns | 0.760 ns | 1.350 ns | 86.07 ns | 0.007 | 0.00 | 0.0020 | 32 B | 0.01 | ``` # Non-Breaking Changes * All gump components in the core have been moved to `Gumps/Legacy`. * All legacy gumps will still inherit `Gump`, which now inherits `BaseGump` # Special Thanks Thank you to @stefanomerotta for considerable contributions/benchmarking/testing to make this effort a reality! We collectively went through over 10 iterations, but it is finally ready.
143 lines
4.8 KiB
C#
143 lines
4.8 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.IO;
|
|
using Server.Gumps;
|
|
using Server.Network;
|
|
using Server.Tests.Network;
|
|
using Xunit;
|
|
|
|
namespace Server.Tests.Gumps;
|
|
|
|
[Collection("Sequential Tests")]
|
|
public class TestLayoutGumps
|
|
{
|
|
[Fact]
|
|
public void TestDynamicGumpPacket()
|
|
{
|
|
var legacyGump = new LegacyTestGump("Test");
|
|
var legacyPacketData = legacyGump.Compile().Compile();
|
|
|
|
var staticGump = new DynamicTestGump("Test");
|
|
var buffer = GC.AllocateUninitializedArray<byte>(512);
|
|
var writer = new SpanWriter(buffer);
|
|
staticGump.CreatePacket(ref writer);
|
|
|
|
AssertThat.Equal(writer.Span, legacyPacketData);
|
|
}
|
|
|
|
[Fact]
|
|
public void TestStaticLayoutGumpPacket()
|
|
{
|
|
var expectedLayout =
|
|
"{ page 0 }{ resizepic 10 10 9260 265 140 }{ tilepic 205 40 4 }{ tilepic 227 40 5 }{ tilepic 180 78 3246 }{ tilepic 195 90 3245 }{ tilepic 218 95 3248 }{ htmlgump 30 30 150 75 1 0 0 }{ htmlgump 30 70 150 25 00002 1 0 }{ button 40 105 2074 2075 1 0 1 }{ button 110 105 2073 2072 1 0 2 }\0"u8;
|
|
|
|
string[] strings =
|
|
[
|
|
"<div align=center>Wilt thou sanctify the resurrection of:</div>",
|
|
"<CENTER>Test</CENTER>"
|
|
];
|
|
|
|
InternalTestStaticGump(expectedLayout, new StaticLayoutTestGump("Test"), strings);
|
|
}
|
|
|
|
[Fact]
|
|
public void TestStaticGumpPacket()
|
|
{
|
|
var expectedLayout =
|
|
"{ page 0 }{ resizepic 10 10 9260 265 140 }{ tilepic 205 40 4 }{ tilepic 227 40 5 }{ tilepic 180 78 3246 }{ tilepic 195 90 3245 }{ tilepic 218 95 3248 }{ htmlgump 30 30 150 75 1 0 0 }{ htmlgump 30 70 150 25 2 1 0 }{ button 40 105 2074 2075 1 0 1 }{ button 110 105 2073 2072 1 0 2 }\0"u8;
|
|
|
|
string[] strings =
|
|
[
|
|
"<div align=center>Wilt thou sanctify the resurrection of:</div>",
|
|
"<CENTER>Test</CENTER>"
|
|
];
|
|
|
|
InternalTestStaticGump(expectedLayout, new StaticTestGump(), strings);
|
|
}
|
|
|
|
[Fact]
|
|
public void TestStaticGumpIsCached()
|
|
{
|
|
var gump = new CachedGump();
|
|
var buffer = GC.AllocateUninitializedArray<byte>(512);
|
|
var writer = new SpanWriter(buffer);
|
|
gump.CreatePacket(ref writer);
|
|
|
|
var packet = writer.Span.ToArray();
|
|
|
|
// Reset the writer
|
|
writer.Seek(0, SeekOrigin.Begin);
|
|
|
|
// Second call should not call BuildLayout
|
|
gump.CreatePacket(ref writer);
|
|
|
|
AssertThat.Equal(writer.Span, packet);
|
|
}
|
|
|
|
private static void InternalTestStaticGump<T>(ReadOnlySpan<byte> expectedLayout, StaticGump<T> staticGump, string[] strings)
|
|
where T : StaticGump<T>
|
|
{
|
|
// Expected layout
|
|
var expectedBuffer = GC.AllocateUninitializedArray<byte>(512);
|
|
var expectedBufferWriter = new SpanWriter(expectedBuffer);
|
|
OutgoingGumpPackets.WritePacked(expectedLayout, ref expectedBufferWriter);
|
|
var layoutLength = expectedBufferWriter.BytesWritten;
|
|
|
|
var buffer = GC.AllocateUninitializedArray<byte>(512);
|
|
var writer = new SpanWriter(buffer);
|
|
staticGump.CreatePacket(ref writer);
|
|
|
|
// Assert layout is exactly what we are expecting
|
|
AssertThat.Equal(writer.Span.Slice(19, layoutLength), expectedBufferWriter.Span);
|
|
|
|
// Assert strings count
|
|
AssertThat.Equal(writer.Span.Slice(19 + layoutLength, 4), stackalloc byte[] { 0, 0, 0, 3 });
|
|
|
|
var expectedStringsBuffer = GC.AllocateUninitializedArray<byte>(512);
|
|
var expectedStringsWriter = new SpanWriter(expectedStringsBuffer);
|
|
|
|
// Empty string
|
|
expectedStringsWriter.Write((ushort)0);
|
|
|
|
// loop through the strings, write them to the strings writer
|
|
foreach (var str in strings)
|
|
{
|
|
expectedStringsWriter.Write((ushort)str.Length);
|
|
expectedStringsWriter.WriteBigUni(str);
|
|
}
|
|
|
|
// Reset buffer
|
|
expectedBufferWriter.Seek(0, SeekOrigin.Begin);
|
|
|
|
OutgoingGumpPackets.WritePacked(expectedStringsWriter.Span, ref expectedBufferWriter);
|
|
|
|
// Assert strings are exactly what we are expecting
|
|
AssertThat.Equal(writer.Span[(19 + layoutLength + 4)..], expectedBufferWriter.Span);
|
|
}
|
|
|
|
private class CachedGump : StaticGump<CachedGump>
|
|
{
|
|
private bool _isCachedLayout;
|
|
|
|
public CachedGump() : base(50, 50)
|
|
{
|
|
Serial = (Serial)0x124;
|
|
TypeID = 0x5346;
|
|
}
|
|
|
|
protected override void BuildLayout(ref StaticGumpBuilder builder)
|
|
{
|
|
Assert.False(_isCachedLayout);
|
|
_isCachedLayout = true;
|
|
|
|
builder.AddPage();
|
|
|
|
builder.AddHtml(30, 30, 150, 75, "Some text");
|
|
}
|
|
|
|
protected override void BuildStrings(ref GumpStringsBuilder builder)
|
|
{
|
|
Assert.Fail("BuildStrings should not be called when the layout is cached.");
|
|
}
|
|
}
|
|
}
|