fix: Warn when sending empty gumps (#2563)

### Summary

Generates a console warning when users receive an empty gump. This will help prevent client side leaks.
This commit is contained in:
Guflly 2026-08-08 00:55:12 -07:00 • committed by GitHub
parent b2c59191bd
commit 64e6fe5da8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 114 additions and 0 deletions

View file

@ -6,6 +6,8 @@ public class DynamicTestGump : DynamicGump
{
private readonly string _petName;
public bool HasVisualElementsForTest => HasVisualElements;
public DynamicTestGump(string petName) : base(50, 50)
{
_petName = petName;

View file

@ -0,0 +1,40 @@
using Server.Gumps;
namespace Server.Tests.Gumps;
public sealed class EmptyLegacyTestGump : Gump
{
public bool HasVisualElementsForTest => HasVisualElements;
public EmptyLegacyTestGump() : base(0, 0)
{
}
}
public sealed class EmptyDynamicTestGump : DynamicGump
{
public bool HasVisualElementsForTest => HasVisualElements;
public EmptyDynamicTestGump() : base(0, 0)
{
}
protected override void BuildLayout(ref DynamicGumpBuilder builder)
{
builder.AddPage();
}
}
public sealed class EmptyStaticTestGump : StaticGump<EmptyStaticTestGump>
{
public bool HasVisualElementsForTest => HasVisualElements;
public EmptyStaticTestGump() : base(0, 0)
{
}
protected override void BuildLayout(ref StaticGumpBuilder builder)
{
builder.SetNoClose();
}
}

View file

@ -4,6 +4,8 @@ namespace Server.Tests.Gumps;
public sealed class LegacyTestGump : Gump
{
public bool HasVisualElementsForTest => HasVisualElements;
public LegacyTestGump(string petName) : base(50, 50)
{
Serial = (Serial)0x123;

View file

@ -4,6 +4,8 @@ namespace Server.Tests.Gumps;
public class StaticTestGump : StaticGump<StaticTestGump>
{
public bool HasVisualElementsForTest => HasVisualElements;
public StaticTestGump() : base(50, 50)
{
Serial = (Serial)0x123;

View file

@ -73,6 +73,32 @@ public class TestLayoutGumps
AssertThat.Equal(writer.Span, packet);
}
[Fact]
public void TestEmptyGumpsHaveNoVisualElements()
{
Assert.False(Compile(new EmptyLegacyTestGump()).HasVisualElementsForTest);
Assert.False(Compile(new EmptyDynamicTestGump()).HasVisualElementsForTest);
Assert.False(Compile(new EmptyStaticTestGump()).HasVisualElementsForTest);
Assert.False(Compile(new EmptyStaticTestGump()).HasVisualElementsForTest);
}
[Fact]
public void TestVisibleGumpsHaveVisualElements()
{
Assert.True(Compile(new LegacyTestGump("Test")).HasVisualElementsForTest);
Assert.True(Compile(new DynamicTestGump("Test")).HasVisualElementsForTest);
Assert.True(Compile(new StaticTestGump()).HasVisualElementsForTest);
Assert.True(Compile(new StaticTestGump()).HasVisualElementsForTest);
}
private static T Compile<T>(T gump) where T : BaseGump
{
var buffer = GC.AllocateUninitializedArray<byte>(512);
var writer = new SpanWriter(buffer);
gump.Compile(ref writer);
return gump;
}
private static void InternalTestStaticGump<T>(ReadOnlySpan<byte> expectedLayout, StaticGump<T> staticGump, string[] strings)
where T : StaticGump<T>
{