ModernUO/Projects/Server/Text/TextDefinition.cs
Kamron Batman 910e06767b
fix: Optimizes TextDefinition to eliminate allocations. Removes TextDefinition ctor. (#1221)
### BREAKING CHANGE ###
The constructor for `TextDefinition` has been removed. Instead use `TextDefinition.Of()` or cast the integer/string to TextDefinition.
2022-10-30 16:53:23 -07:00

121 lines
3.8 KiB
C#

/*************************************************************************
* 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/>. *
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
namespace Server;
[Parsable]
[PropertyObject]
public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>
{
public static readonly TextDefinition Empty = new();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TextDefinition Of(int number) => Of(number, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TextDefinition Of(string text) => Of(0, text);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TextDefinition Of(int number, string text) => new(number, text);
private TextDefinition()
{
}
private TextDefinition(int number, string text)
{
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) => Of(v);
public static implicit operator TextDefinition(string s) => Of(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) ? Of(i) : Of(value);
}
public void Deconstruct(out int number, out string s)
{
if (Number > 0)
{
number = Number;
s = null;
}
else
{
number = 0;
s = String;
}
}
public override bool Equals(object obj) => Equals(obj as TextDefinition);
public bool Equals(TextDefinition other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
if (Number > 0 || other.Number > 0)
{
return Number == other.Number;
}
return String == other.String;
}
public override int GetHashCode() => Number > 0 ? HashCode.Combine(Number) : HashCode.Combine(String);
public static bool operator ==(TextDefinition left, TextDefinition right) => Equals(left, right);
public static bool operator !=(TextDefinition left, TextDefinition right) => !Equals(left, right);
}