fix: Optimizes OPL using string interpolation (#1041)

## Breaking Changes (New API)
ObjectPropertyList supports the following API:
```cs
list.Add(500000);
list.Add(500001, stringArgument);
list.Add("Some text");
list.Add($"Some text with {argument}");
list.Add(500002, $"{arg1}\t{arg2}");
```

## Notes
1. All API uses that require a formatter like this:
    ```cs
    list.Add(500002, "{0}\t{1}", arg1, arg2);
    ```
    Should be changed to use string interpolation, for example:
    ```cs
    list.Add(500002, $"{arg1}\t{arg2}");
    ```
2. The following paradigm should no longer be used:
    ```cs
    list.Add(1061170, prop.ToString()); // strength requirement ~1_val~
    ```
    The new string interpolation API will avoid having to convert the argument to a string before writing it to the packet. Instead use the following:
    ```cs
    list.Add(1061170, $"{prop}"); // strength requirement ~1_val~
    ```

### Benchmarks
```cs
|                         Method |     Mean |   Error |  StdDev |  Gen 0 | Allocated |
|------------------------------- |---------:|--------:|--------:|-------:|----------:|
|                BenchmarkOldOPL | 241.0 ns | 0.56 ns | 0.47 ns | 0.0105 |      88 B |
| BenchmarkStringInterpolatedOPL | 199.9 ns | 2.44 ns | 2.39 ns |      - |         - |
```

### Changes
- [X] Removes crash in STArray.Return when array is null.
- [X] Fixes NPE in OPL when entity is null. Serial in packet will be 0 when entity is null.
- [X] Fixes NPE in AosAttributes when Parent is null.
- [X] Changes OPL to use string interpolation.
- [X] Introduces `IPropertyList` to allow extending PropertyList for other uses.
This commit is contained in:
Kamron Batman 2022-06-02 10:09:53 -07:00 • committed by GitHub
parent fe31470f05
commit ecbee17690
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 1426 additions and 1008 deletions

View file

@ -403,7 +403,7 @@ namespace Server
/// <summary>
/// Base class representing players, npcs, and creatures.
/// </summary>
public class Mobile : IHued, IComparable<Mobile>, ISpawnable, IPropertyListObject
public class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPropertyListEntity
{
// Allow four warmode changes in 0.5 seconds, any more will be delay for two seconds
private const int WarmodeCatchCount = 4;
@ -2476,7 +2476,7 @@ namespace Server
public virtual int HuedItemID => m_Female ? 0x2107 : 0x2106;
public ObjectPropertyList PropertyList => m_PropertyList ??= InitializePropertyList(new ObjectPropertyList(this));
public virtual void GetProperties(ObjectPropertyList list)
public virtual void GetProperties(IPropertyList list)
{
AddNameProperties(list);
}
@ -3445,57 +3445,51 @@ namespace Server
public virtual string ApplyNameSuffix(string suffix) => suffix;
public virtual void AddNameProperties(ObjectPropertyList list)
public virtual void AddNameProperties(IPropertyList list)
{
var name = Name ?? "";
var name = Name ?? " ";
string prefix;
if (ShowFameTitle && (m_Player || m_Body.IsHuman) && m_Fame >= 10000)
{
prefix = m_Female ? "Lady" : "Lord";
prefix = m_Female ? "Lady " : "Lord ";
}
else
{
prefix = "";
prefix = " ";
}
var suffix = "";
if (PropertyTitle && !string.IsNullOrEmpty(Title))
{
suffix = Title;
}
var title = PropertyTitle && !string.IsNullOrEmpty(Title) ? Title : "";
string suffix;
var guild = m_Guild;
if (guild != null && (m_Player || m_DisplayGuildTitle))
{
suffix = suffix.Length > 0
? $"{suffix} [{Utility.FixHtml(guild.Abbreviation)}]"
suffix = title.Length > 0
? $"{title} [{Utility.FixHtml(guild.Abbreviation)}]"
: $"[{Utility.FixHtml(guild.Abbreviation)}]";
}
else
{
suffix = " ";
}
suffix = ApplyNameSuffix(suffix);
list.Add(1050045, "{0} \t{1}\t {2}", prefix, name, suffix); // ~1_PREFIX~~2_NAME~~3_SUFFIX~
list.Add(1050045, $"{prefix}\t{name}\t{ApplyNameSuffix(suffix)}"); // ~1_PREFIX~~2_NAME~~3_SUFFIX~
if (guild != null && (m_DisplayGuildTitle || m_Player && guild.Type != GuildType.Regular))
{
var type = guild.Type >= 0 && (int)guild.Type < m_GuildTypes.Length ? m_GuildTypes[(int)guild.Type] : "";
var title = GuildTitle?.Trim() ?? "";
var guildTitle = GuildTitle?.Trim() ?? "";
if (title.Length > 0)
if (guildTitle.Length > 0)
{
if (NewGuildDisplay)
{
list.Add("{0}, {1}", Utility.FixHtml(title), Utility.FixHtml(guild.Name));
}
else
{
list.Add("{0}, {1} Guild{2}", Utility.FixHtml(title), Utility.FixHtml(guild.Name), type);
}
list.Add(
NewGuildDisplay
? $"{Utility.FixHtml(guildTitle)}, {Utility.FixHtml(guild.Name)}"
: $"{Utility.FixHtml(guildTitle)}, {Utility.FixHtml(guild.Name)} Guild{type}"
);
}
else
{
@ -3504,11 +3498,11 @@ namespace Server
}
}
public virtual void GetChildProperties(ObjectPropertyList list, Item item)
public virtual void GetChildProperties(IPropertyList list, Item item)
{
}
public virtual void GetChildNameProperties(ObjectPropertyList list, Item item)
public virtual void GetChildNameProperties(IPropertyList list, Item item)
{
}