feat: Adds TextDefinition serialization support. (#1217)

This commit is contained in:
Kamron Batman 2022-10-30 01:42:48 -07:00 committed by GitHub
parent 39e6f62ec9
commit d791feb583
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 301 additions and 267 deletions

View file

@ -0,0 +1,65 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TextDefinition.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/>. *
*************************************************************************/
namespace Server;
[Parsable]
[PropertyObject]
public class TextDefinition
{
public TextDefinition(string text) : this(0, text)
{
}
public TextDefinition(int number = 0, string text = null)
{
Number = number;
String = text;
}
[CommandProperty(AccessLevel.GameMaster)]
public int Number { get; }
[CommandProperty(AccessLevel.GameMaster)]
public string String { get; }
public bool IsEmpty => Number <= 0 && String == null;
public override string ToString() => Number > 0 ? $"#{Number}" : String ?? "";
public string Format() =>
Number > 0 ? $"{Number} (0x{Number:X})" :
String != null ? $"\"{String}\"" : null;
public string GetValue() => Number > 0 ? Number.ToString() : String ?? "";
public static implicit operator TextDefinition(int v) => new(v);
public static implicit operator TextDefinition(string s) => new(s);
public static implicit operator int(TextDefinition m) => m?.Number ?? 0;
public static implicit operator string(TextDefinition m) => m?.String;
public static TextDefinition Parse(string value)
{
if (value == null)
{
return null;
}
return Utility.ToInt32(value, out var i) ? new TextDefinition(i) : new TextDefinition(value);
}
}

View file

@ -0,0 +1,159 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2022 - 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;
using Server.Network;
namespace Server;
public static class TextDefinitionExtensions
{
public static void AddHtmlText(
this TextDefinition def,
Gump g,
int x,
int y,
int width,
int height,
bool back,
bool scroll,
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)
{
if (stringColor >= 0) // 8 bits per RGB component (24 bit RGB)
{
g.AddHtml(
x,
y,
width,
height,
$"<BASEFONT COLOR=#{stringColor:X6}>{def.String}</BASEFONT>",
back,
scroll
);
}
else
{
g.AddHtml(x, y, width, height, def.String, 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 bool IsNullOrEmpty(this TextDefinition def) => def?.IsEmpty != false;
}