ModernUO/Projects/Server/Text/TextDefinitionExtensions.cs
Kamron Batman 622250b8f4
fix: Fixes issue with cached static gump strings. Fixes bad gump colors (#1771)
### Summary
- Fixes an issue that causes CUO to crash due to bad string caching in static gumps
- Fixes wrong/bad 16bit html gump hues.
- Moves `C16232` (16-bit to 32-bit) and `C32216` (32-bit to 16-bit) to Utility class for broader use.

TODO:
- Some gumps have different 32bit (for string content) vs 16bit (for localized content) strings. Does this matter?
2024-05-10 22:44:16 -07:00

292 lines
7.6 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TextDefinitionExtensions.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using Server.Gumps;
namespace Server;
public static class TextDefinitionExtensions
{
public static void AddHtmlText(
this TextDefinition def,
ref DynamicGumpBuilder builder,
int x,
int y,
int width,
int height,
bool back = false,
bool scroll = false,
int numberColor = -1,
int stringColor = -1
)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
if (numberColor >= 0) // 5 bits per RGB component (15 bit RGB)
{
builder.AddHtmlLocalized(x, y, width, height, def.Number, numberColor, back, scroll);
}
else
{
builder.AddHtmlLocalized(x, y, width, height, def.Number, back, scroll);
}
}
else if (def.String != null)
{
builder.AddHtml(
x,
y,
width,
height,
stringColor >= 0 ? def.String.Color(stringColor) : def.String, // 8 bits per RGB component (24 bit RGB)
back,
scroll
);
}
}
public static void AddHtmlText(
this TextDefinition def,
ref StaticGumpBuilder builder,
int x,
int y,
int width,
int height,
bool back = false,
bool scroll = false,
int numberColor = -1,
int stringColor = -1
)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
if (numberColor >= 0) // 5 bits per RGB component (15 bit RGB)
{
builder.AddHtmlLocalized(x, y, width, height, def.Number, numberColor, back, scroll);
}
else
{
builder.AddHtmlLocalized(x, y, width, height, def.Number, back, scroll);
}
}
else if (def.String != null)
{
builder.AddHtml(
x,
y,
width,
height,
stringColor >= 0 ? def.String.Color(stringColor) : def.String, // 8 bits per RGB component (24 bit RGB)
back,
scroll
);
}
}
public static void AddHtmlText(
this TextDefinition def,
Gump g,
int x,
int y,
int width,
int height,
bool back = false,
bool scroll = false,
int numberColor = -1,
int stringColor = -1
)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
if (numberColor >= 0) // 5 bits per RGB component (15 bit RGB)
{
g.AddHtmlLocalized(x, y, width, height, def.Number, numberColor, back, scroll);
}
else
{
g.AddHtmlLocalized(x, y, width, height, def.Number, back, scroll);
}
}
else if (def.String != null)
{
g.AddHtml(
x,
y,
width,
height,
stringColor >= 0 ? def.String.Color(stringColor) : def.String, // 8 bits per RGB component (24 bit RGB)
back,
scroll
);
}
}
public static void AddHtmlText(
this TextDefinition def,
Gump g,
int x,
int y,
int width,
int height,
string args,
bool back = false,
bool scroll = false,
int numberColor = -1,
int stringColor = -1
)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
// 5 bits per RGB component (15 bit RGB)
g.AddHtmlLocalized(x, y, width, height, def.Number, args, numberColor >= 0 ? numberColor : 0x7FFF, back, scroll);
}
else if (def.String != null)
{
g.AddHtml(
x,
y,
width,
height,
stringColor >= 0 ? string.Format(def.String, args).Color(stringColor) : string.Format(def.String, args), // 8 bits per RGB component (24 bit RGB)
back,
scroll
);
}
}
public static void AddTo(this TextDefinition def, IPropertyList list)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
list.Add(def.Number);
}
else if (def.String != null)
{
list.Add(def.String);
}
}
public static void AddTo(this TextDefinition def, IPropertyList list, int number)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
list.Add(number, $"{def.Number:#}");
}
else if (def.String != null)
{
list.Add(number, $"{def.String}");
}
}
public static void SendMessageTo(this TextDefinition def, Mobile m)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
m.SendLocalizedMessage(def.Number);
}
else if (def.String != null)
{
m.SendMessage(def.String);
}
}
public static void SendMessageTo(this TextDefinition def, Mobile m, int hue)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
m.SendLocalizedMessage(def.Number, "", hue);
}
else if (def.String != null)
{
m.SendMessage(hue, def.String);
}
}
public static void PublicOverheadMessage(this TextDefinition def, Mobile m, MessageType messageType, int hue)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
m.PublicOverheadMessage(messageType, hue, def.Number);
}
else if (def.String != null)
{
m.PublicOverheadMessage(messageType, hue, false, def.String);
}
}
public static void PublicOverheadMessage(this TextDefinition def, Item item, MessageType messageType, int hue)
{
if (def == null)
{
return;
}
if (def.Number > 0)
{
item.PublicOverheadMessage(messageType, hue, def.Number);
}
else if (def.String != null)
{
item.PublicOverheadMessage(messageType, hue, false, def.String);
}
}
public static bool IsNullOrEmpty(this TextDefinition def) => def?.IsEmpty != false;
}