From d6c87a4da4ddbbac07e004ef4ff199c0053a94a7 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sat, 11 May 2024 11:13:57 -0700 Subject: [PATCH] fix: Fixes infinite loop in gump builders (#1772) ### Summary - Fixes infinite loop in gump builders - Removes allocations for centering/coloring html in builders --- Projects/Server/Gumps/DynamicGumpBuilder.cs | 20 ++++++++-- Projects/Server/Gumps/StaticGumpBuilder.cs | 21 +++++++++-- .../Serialization/GenericEntityPersistence.cs | 4 +- Projects/Server/Utilities/Html.cs | 37 +++++++++++++++---- 4 files changed, 64 insertions(+), 18 deletions(-) diff --git a/Projects/Server/Gumps/DynamicGumpBuilder.cs b/Projects/Server/Gumps/DynamicGumpBuilder.cs index 7c0d0fe36..afe345bb9 100644 --- a/Projects/Server/Gumps/DynamicGumpBuilder.cs +++ b/Projects/Server/Gumps/DynamicGumpBuilder.cs @@ -111,7 +111,11 @@ public ref struct DynamicGumpBuilder ReadOnlySpan text, bool background = false, bool scrollbar = false - ) => AddHtml(x, y, width, height, color, $"{text}", background, scrollbar); + ) + { + var handler = text.Color(color); + AddHtml(x, y, width, height, ref handler, background, scrollbar); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtml( @@ -132,7 +136,11 @@ public ref struct DynamicGumpBuilder [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtmlCentered( int x, int y, int width, int height, ReadOnlySpan text, bool background = false, bool scrollbar = false - ) => AddHtml(x, y, width, height, $"
{text}
", background, scrollbar); + ) + { + var handler = text.Center(); + AddHtml(x, y, width, height, ref handler, background, scrollbar); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtmlCentered( @@ -145,7 +153,7 @@ public ref struct DynamicGumpBuilder bool scrollbar = false ) { - AddHtml(x, y, width, height, $"
{handler.Text}
", background, scrollbar); + AddHtmlCentered(x, y, width, height, handler.Text, background, scrollbar); handler.Clear(); } @@ -159,7 +167,11 @@ public ref struct DynamicGumpBuilder ReadOnlySpan text, bool background = false, bool scrollbar = false - ) => AddHtml(x, y, width, height, color, $"
{text}
", background, scrollbar); + ) + { + var handler = text.Center(color); + AddHtml(x, y, width, height, ref handler, background, scrollbar); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtmlCentered( diff --git a/Projects/Server/Gumps/StaticGumpBuilder.cs b/Projects/Server/Gumps/StaticGumpBuilder.cs index 3f2f66e3d..c05e03f87 100644 --- a/Projects/Server/Gumps/StaticGumpBuilder.cs +++ b/Projects/Server/Gumps/StaticGumpBuilder.cs @@ -154,7 +154,11 @@ public ref struct StaticGumpBuilder ReadOnlySpan text, bool background = false, bool scrollbar = false - ) => AddHtml(x, y, width, height, color, $"{text}", background, scrollbar); + ) + { + var handler = text.Color(color); + AddHtml(x, y, width, height, ref handler, background, scrollbar); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtml( @@ -175,7 +179,11 @@ public ref struct StaticGumpBuilder [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtmlCentered( int x, int y, int width, int height, ReadOnlySpan text, bool background = false, bool scrollbar = false - ) => AddHtml(x, y, width, height, $"
{text}
", background, scrollbar); + ) + { + var handler = text.Center(); + AddHtml(x, y, width, height, ref handler, background, scrollbar); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtmlCentered( @@ -188,7 +196,8 @@ public ref struct StaticGumpBuilder bool scrollbar = false ) { - AddHtml(x, y, width, height, $"
{handler.Text}
", background, scrollbar); + var centerHandler = handler.Text.Center(); + AddHtml(x, y, width, height, ref centerHandler, background, scrollbar); handler.Clear(); } @@ -202,7 +211,11 @@ public ref struct StaticGumpBuilder ReadOnlySpan text, bool background = false, bool scrollbar = false - ) => AddHtml(x, y, width, height, color, $"
{text}
", background, scrollbar); + ) + { + var handler = text.Center(color); + AddHtml(x, y, width, height, ref handler, background, scrollbar); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddHtmlCentered( diff --git a/Projects/Server/Serialization/GenericEntityPersistence.cs b/Projects/Server/Serialization/GenericEntityPersistence.cs index 49e53979a..0a147aced 100644 --- a/Projects/Server/Serialization/GenericEntityPersistence.cs +++ b/Projects/Server/Serialization/GenericEntityPersistence.cs @@ -166,7 +166,7 @@ public class GenericEntityPersistence : Persistence, IGenericEntityPersistenc if (entityEntry == entity) { logger.Error( - $"Attempted to add '{{Entity}}' ({{Serial}}) to World.Items but it already exists in the collection.{Environment.NewLine}{{StackTrace}}", + $"Attempted to add '{{Entity}}' ({{Serial}}) but it already exists in the collection.{Environment.NewLine}{{StackTrace}}", entity.GetType().FullName, entity.Serial, new StackTrace() @@ -175,7 +175,7 @@ public class GenericEntityPersistence : Persistence, IGenericEntityPersistenc else { logger.Error( - $"Attempted to add '{{Entity}}' ({{Serial}}) to World.Items but found '{{ExistingEntity}}' ({{ExistingSerial}}).{Environment.NewLine}{{StackTrace}}", + $"Attempted to add '{{Entity}}' ({{Serial}}) but found '{{ExistingEntity}}' ({{ExistingSerial}}).{Environment.NewLine}{{StackTrace}}", entity.GetType().FullName, entity.Serial, entityEntry.GetType().FullName, diff --git a/Projects/Server/Utilities/Html.cs b/Projects/Server/Utilities/Html.cs index ef2f18b8b..00b8c91cd 100644 --- a/Projects/Server/Utilities/Html.cs +++ b/Projects/Server/Utilities/Html.cs @@ -13,8 +13,10 @@ * along with this program. If not, see . * *************************************************************************/ +using System; using System.Runtime.CompilerServices; using System.Text; +using Server.Buffers; namespace Server; @@ -23,6 +25,10 @@ public static class Html [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string Color(this string text, int color) => $"{text}"; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static RawInterpolatedStringHandler Color(this ReadOnlySpan text, int color) => + $"{text}"; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string Color(this string text, int color, int size) => $"{text}"; @@ -30,37 +36,52 @@ public static class Html public static string Color(this string text, string color) => $"{text}"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Color(this string text, string color, int size) => $"{text}"; + public static string Color(this string text, string color, int size) => + $"{text}"; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string Center(this string text) => $"
{text}
"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Center(this string text, int color) => $"
{text}
"; + public static RawInterpolatedStringHandler Center(this ReadOnlySpan text) => $"
{text}
"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Center(this string text, int color, int size) => $"
{text}
"; + public static string Center(this string text, int color) => + $"
{text}
"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Center(this string text, string color) => $"
{text}
"; + public static RawInterpolatedStringHandler Center(this ReadOnlySpan text, int color) => + $"
{text}
"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Center(this string text, string color, int size) => $"
{text}
"; + public static string Center(this string text, int color, int size) => + $"
{text}
"; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Center(this string text, string color) => + $"
{text}
"; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Center(this string text, string color, int size) => + $"
{text}
"; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string Right(this string text) => $"{text}"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Right(this string text, int color) => $"{text}"; + public static string Right(this string text, int color) => + $"{text}"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Right(this string text, int color, int size) => $"{text}"; + public static string Right(this string text, int color, int size) => + $"{text}"; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string Right(this string text, string color) => $"{text}"; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Right(this string text, string color, int size) => $"{text}"; + public static string Right(this string text, string color, int size) => + $"{text}"; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string EscapeHtml(this string input) =>