fix(core): Optimizes strings / .NET 5 compatibility changes (#354)

- [X] Removes some string allocations (e.g. split)
- [X] Optimizes some collections
- [X] Converts insensitive to extension methods of built-ins.
- [X] Adds ordinal (case sensitive) string helpers
- [X] Fixes conditionals for in-game commands so they use Ordinal comparisons.
- [X] Replaces ToLower.Contains with InsensitiveContains
- [X] Adds ValueStringBuilder
- [X] Implements ValueStringBuilder in a few places where it makes sense
- [X] Removes the redundant Wrap function and replaces it with an optimized version
- [X] Fixes list conversions in Utility

Closes #351

Bumps release version
This commit is contained in:
Kamron Batman 2020-12-20 23:21:55 -08:00 • committed by GitHub
parent 92aae8d482
commit 77ce2e1980
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 2000 additions and 1226 deletions

View file

@ -167,7 +167,7 @@ namespace Server.Gumps
AddLabel(150, 150, LabelHue, banned.ToString());
AddLabel(20, 170, LabelHue, "Firewalled:");
AddLabel(150, 170, LabelHue, Firewall.List.Count.ToString());
AddLabel(150, 170, LabelHue, Firewall.Set.Count.ToString());
AddLabel(20, 190, LabelHue, "Clients:");
AddLabel(150, 190, LabelHue, TcpServer.Instances.Count.ToString());
@ -196,8 +196,8 @@ namespace Server.Gumps
AddLabel(20, 350, LabelHue, "Operating System: ");
var os = Environment.OSVersion.ToString();
os = os.Replace("Microsoft", "MSFT", StringComparison.Ordinal);
os = os.Replace("Service Pack", "SP", StringComparison.Ordinal);
os = os.ReplaceOrdinal("Microsoft", "MSFT");
os = os.ReplaceOrdinal("Service Pack", "SP");
AddLabel(150, 350, LabelHue, os);
@ -612,11 +612,12 @@ namespace Server.Gumps
if (m_List == null)
{
sharedAccounts = GetAllSharedAccounts();
m_List = sharedAccounts.CastListContravariant<KeyValuePair<IPAddress, List<Account>>, object>();
// TODO: Find a better way, don't use KVPs?
m_List = sharedAccounts.ConvertAll(kvp => (object)kvp);
}
else
{
sharedAccounts = m_List.CastListCovariant<object, KeyValuePair<IPAddress, List<Account>>>();
sharedAccounts = m_List.SafeConvertList<object, KeyValuePair<IPAddress, List<Account>>>();
}
AddLabelCropped(12, 120, 60, 20, LabelHue, "Count");
@ -1004,11 +1005,11 @@ namespace Server.Gumps
if (m_List == null)
{
ipAddresses = a.LoginIPs.ToList();
m_List = ipAddresses.CastListContravariant<IPAddress, object>();
m_List = ipAddresses.ToList<object>();
}
else
{
ipAddresses = m_List.CastListCovariant<object, IPAddress>();
ipAddresses = m_List.SafeConvertList<object, IPAddress>();
}
AddHtml(10, 195, 400, 20, Color(Center("Client Addresses"), LabelColor32));
@ -1076,11 +1077,11 @@ namespace Server.Gumps
if (m_List == null)
{
ipRestrictions = a.IPRestrictions.ToList();
m_List = ipRestrictions.CastListContravariant<string, object>();
m_List = ipRestrictions.ToList<object>();
}
else
{
ipRestrictions = m_List.CastListCovariant<object, string>();
ipRestrictions = m_List.SafeConvertList<object, string>();
}
AddHtml(10, 195, 400, 20, Color(Center("Address Restrictions"), LabelColor32));
@ -1260,16 +1261,16 @@ namespace Server.Gumps
{
AddFirewallHeader();
List<Firewall.IFirewallEntry> firewallEntries;
HashSet<Firewall.IFirewallEntry> firewallEntries;
if (m_List == null)
{
firewallEntries = Firewall.List;
m_List = firewallEntries.CastListContravariant<Firewall.IFirewallEntry, object>();
firewallEntries = Firewall.Set;
m_List = firewallEntries.ToList<object>();
}
else
{
firewallEntries = m_List.CastListCovariant<object, Firewall.IFirewallEntry>();
firewallEntries = m_List.SafeConvertSet<object, Firewall.IFirewallEntry>();
}
AddLabelCropped(12, 120, 358, 20, LabelHue, "IP Address");
@ -1296,17 +1297,22 @@ namespace Server.Gumps
{
AddLabel(12, 140, LabelHue, "The firewall list is empty.");
}
for (int i = 0, index = listPage * 12;
i < 12 && index >= 0 && index < firewallEntries.Count;
++i, ++index)
else
{
var firewallEntry = firewallEntries[index];
var i = 0;
var index = listPage * 12;
foreach (var firewallEntry in firewallEntries)
{
if (i >= 12)
{
break;
}
var offset = 140 + i * 20;
var offset = 140 + i++ * 20;
AddLabelCropped(12, offset, 358, 20, LabelHue, firewallEntry.ToString());
AddButton(380, offset - 1, 0xFA5, 0xFA7, GetButtonID(6, index + 4));
AddLabelCropped(12, offset, 358, 20, LabelHue, firewallEntry.ToString());
AddButton(380, offset - 1, 0xFA5, 0xFA7, GetButtonID(6, index++ + 4));
}
}
break;
@ -1351,11 +1357,11 @@ namespace Server.Gumps
}
blockedAccts.Sort(AccountComparer.Instance);
m_List = blockedAccts.CastListContravariant<Account, object>();
m_List = blockedAccts.ToList<object>();
}
else
{
blockedAccts = m_List.CastListCovariant<object, Account>();
blockedAccts = m_List.SafeConvertList<object, Account>();
}
if (listPage > 0)
@ -1968,7 +1974,7 @@ namespace Server.Gumps
if (m_PageType == AdminGumpPage.Accounts)
{
var list = m_List.CastListCovariant<object, Account>();
var list = m_List.SafeConvertList<object, Account>();
if (list != null && m_State is List<Account> rads)
{
@ -2453,12 +2459,12 @@ namespace Server.Gumps
var m = ns.Mobile;
var a = ns.Account;
isMatch = m?.Name.ToLower().Contains(match, StringComparison.Ordinal) == true
|| a?.Username.ToLower().Contains(match, StringComparison.Ordinal) == true;
isMatch = m?.Name.InsensitiveContains(match) == true
|| a?.Username.InsensitiveContains(match) == true;
}
else
{
isMatch = ns.ToString().Contains(match, StringComparison.Ordinal);
isMatch = ns.ToString().ContainsOrdinal(match);
}
if (isMatch)
@ -2508,7 +2514,7 @@ namespace Server.Gumps
from,
AdminGumpPage.Clients,
0,
results.CastListContravariant<NetState, object>(),
results.ToList<object>(),
"One match found."
)
);
@ -2521,7 +2527,7 @@ namespace Server.Gumps
from,
AdminGumpPage.Clients,
0,
results.CastListContravariant<NetState, object>(),
results.ToList<object>(),
notice ?? (results.Count == 0 ? "Nothing matched your search terms." : null)
)
);
@ -2715,8 +2721,7 @@ namespace Server.Gumps
else
{
results = Accounts.GetAccounts()
.Where(acct =>
acct.Username.ToLower().Contains(match, StringComparison.Ordinal))
.Where(acct => acct.Username.InsensitiveContains(match))
.ToList();
results.Sort(AccountComparer.Instance);
}
@ -2741,7 +2746,7 @@ namespace Server.Gumps
from,
AdminGumpPage.Accounts,
0,
results.CastListContravariant<IAccount, object>(),
results.ToList<object>(),
results.Count == 0 ? "Nothing matched your search terms." : null,
new List<object>()
)
@ -2869,7 +2874,7 @@ namespace Server.Gumps
from,
AdminGumpPage.Accounts,
0,
list.CastListContravariant<Account, object>(),
list.ToList<object>(),
null,
new List<object>()
)
@ -3492,7 +3497,7 @@ namespace Server.Gumps
from,
AdminGumpPage.Accounts,
0,
kvp.Value.CastListContravariant<Account, object>(),
kvp.Value.ToList<object>(),
null,
new List<object>()
)
@ -3525,13 +3530,13 @@ namespace Server.Gumps
}
else
{
for (var i = 0; i < Firewall.List.Count; ++i)
foreach (var check in Firewall.Set)
{
var check = Firewall.List[i].ToString();
var checkStr = check.ToString();
if (check?.Contains(match, StringComparison.Ordinal) == true)
if (checkStr.ContainsOrdinal(match))
{
results.Add(Firewall.List[i]);
results.Add(check);
}
}
}
@ -3958,7 +3963,7 @@ namespace Server.Gumps
from,
AdminGumpPage.Accounts,
0,
list.CastListContravariant<Account, object>(),
list.ToList<object>(),
null,
new List<object>()
)
@ -4222,7 +4227,7 @@ namespace Server.Gumps
return -1;
}
return aMob.AccessLevel < bMob.AccessLevel ? 1 : Insensitive.Compare(aMob.Name, bMob.Name);
return aMob.AccessLevel < bMob.AccessLevel ? 1 : aMob.Name.InsensitiveCompare(bMob.Name);
}
}
@ -4265,7 +4270,7 @@ namespace Server.Gumps
return -1;
}
return aLevel < bLevel ? 1 : Insensitive.Compare(x.Username, y.Username);
return aLevel < bLevel ? 1 : x.Username.InsensitiveCompare(y.Username);
}
}
}

View file

@ -1,3 +1,4 @@
using System.Linq;
using Server.Guilds;
using Server.Prompts;
@ -36,7 +37,7 @@ namespace Server.Gumps
if (text.Length >= 3)
{
var guilds = BaseGuild.Search(text).CastListCovariant<BaseGuild, Guild>();
var guilds = BaseGuild.Search(text).SafeConvertList<BaseGuild, Guild>();
GuildGump.EnsureClosed(m_Mobile);

View file

@ -22,7 +22,7 @@ namespace Server.Gumps
Design();
m_List = new List<Guild>(list);
m_List = list;
for (var i = 0; i < m_List.Count; ++i)
{

View file

@ -120,7 +120,7 @@ namespace Server.Guilds
for (var i = 0; i < disallowed.Length; i++)
{
if (s.IndexOf(disallowed[i], StringComparison.Ordinal) != -1)
if (s.IndexOfOrdinal(disallowed[i]) != -1)
{
return false;
}

View file

@ -26,7 +26,7 @@ namespace Server.Guilds
"",
0,
GuildDisplayType.All,
World.Guilds.Values.CastListCovariant<BaseGuild, Guild>(),
World.Guilds.Values.SafeConvertList<BaseGuild, Guild>(),
1063136 + (int)GuildDisplayType.All
)
{
@ -44,7 +44,7 @@ namespace Server.Guilds
filter,
startNumber,
display,
World.Guilds.Values.CastListCovariant<BaseGuild, Guild>(),
World.Guilds.Values.SafeConvertList<BaseGuild, Guild>(),
1063136 + (int)display
)
{
@ -224,7 +224,7 @@ namespace Server.Guilds
}
}
return !(Insensitive.Contains(g.Name, filter) || Insensitive.Contains(g.Abbreviation, filter));
return !(g.Name.InsensitiveContains(filter) || g.Abbreviation.InsensitiveContains(filter));
}
public override Gump GetResentGump(
@ -285,7 +285,7 @@ namespace Server.Guilds
return 1;
}
return Insensitive.Compare(x.Name, y.Name);
return x.Name.InsensitiveCompare(y.Name);
}
}
@ -365,7 +365,7 @@ namespace Server.Guilds
return 1;
}
return Insensitive.Compare(x.Abbreviation, y.Abbreviation);
return x.Abbreviation.InsensitiveCompare(y.Abbreviation);
}
}
}

View file

@ -277,7 +277,7 @@ namespace Server.Guilds
}
else
{
if (Insensitive.Equals(title, "none"))
if (title.InsensitiveEquals("none"))
{
m_Member.GuildTitle = null;
}

View file

@ -85,7 +85,7 @@ namespace Server.Guilds
return true;
}
return !Insensitive.Contains(pm.Name, filter);
return !pm.Name.InsensitiveContains(filter);
}
public override Gump GetResentGump(
@ -208,7 +208,7 @@ namespace Server.Guilds
return 1;
}
return Insensitive.Compare(x.Name, y.Name);
return x.Name.InsensitiveCompare(y.Name);
}
}
@ -276,7 +276,7 @@ namespace Server.Guilds
return 1;
}
return Insensitive.Compare(x.GuildTitle, y.GuildTitle);
return x.GuildTitle.InsensitiveCompare(y.GuildTitle);
}
}

View file

@ -242,7 +242,7 @@ namespace Server.Gumps
if (m_House.Sign != null)
{
var lines = Wrap(m_House.Sign.GetName());
var lines = m_House.Sign.GetName().Wrap(10, 6);
for (int i = 0, y = (101 - lines.Count * 14) / 2; i < lines.Count; ++i, y += 14)
{
@ -402,75 +402,6 @@ namespace Server.Gumps
}
}
private List<string> Wrap(string value)
{
if (value == null || (value = value.Trim()).Length <= 0)
{
return null;
}
var values = value.Split(' ');
var list = new List<string>();
var current = "";
for (var i = 0; i < values.Length; ++i)
{
var val = values[i];
var v = current.Length == 0 ? val : $"{current} {val}";
if (v.Length < 10)
{
current = v;
}
else if (v.Length == 10)
{
list.Add(v);
if (list.Count == 6)
{
return list;
}
current = "";
}
else if (val.Length <= 10)
{
list.Add(current);
if (list.Count == 6)
{
return list;
}
current = val;
}
else
{
while (v.Length >= 10)
{
list.Add(v.Substring(0, 10));
if (list.Count == 6)
{
return list;
}
v = v.Substring(10);
}
current = v;
}
}
if (current.Length > 0)
{
list.Add(current);
}
return list;
}
private string GetOwnerName()
{
var m = m_House.Owner;

View file

@ -115,7 +115,7 @@ namespace Server.Gumps
if (m_House.Sign != null)
{
var lines = Wrap(m_House.Sign.GetName());
var lines = m_House.Sign.GetName().Wrap(10, 6);
for (int i = 0, y = (114 - lines.Count * 14) / 2; i < lines.Count; ++i, y += 14)
{
@ -1739,74 +1739,5 @@ namespace Server.Gumps
}
}
}
private List<string> Wrap(string value)
{
if (value == null || (value = value.Trim()).Length <= 0)
{
return null;
}
var values = value.Split(' ');
var list = new List<string>();
var current = "";
for (var i = 0; i < values.Length; ++i)
{
var val = values[i];
var v = current.Length == 0 ? val : $"{current} {val}";
if (v.Length < 10)
{
current = v;
}
else if (v.Length == 10)
{
list.Add(v);
if (list.Count == 6)
{
return list;
}
current = "";
}
else if (val.Length <= 10)
{
list.Add(current);
if (list.Count == 6)
{
return list;
}
current = val;
}
else
{
while (v.Length >= 10)
{
list.Add(v.Substring(0, 10));
if (list.Count == 6)
{
return list;
}
v = v.Substring(10);
}
current = v;
}
}
if (current.Length > 0)
{
list.Add(current);
}
return list;
}
}
}

View file

@ -757,7 +757,7 @@ namespace Server.Gumps
if (t == typeof(byte) || t == typeof(sbyte) || t == typeof(short) || t == typeof(ushort) || t == typeof(int) ||
t == typeof(uint) || t == typeof(long) || t == typeof(ulong))
{
if (s.StartsWith("0x", StringComparison.Ordinal))
if (s.StartsWithOrdinal("0x"))
{
if (t == typeof(ulong) || t == typeof(uint) || t == typeof(ushort) || t == typeof(byte))
{

View file

@ -310,7 +310,7 @@ namespace Server.Gumps
for (var i = 0; i < m_GroupNames.Length; ++i)
{
if (DisplayName.StartsWith(m_GroupNames[i], StringComparison.Ordinal))
if (DisplayName.StartsWithOrdinal(m_GroupNames[i]))
{
DisplayName = DisplayName.Substring(m_GroupNames[i].Length);
break;

View file

@ -103,7 +103,7 @@ namespace Server.Gumps
if (m != null && (m == owner || !m.Hidden || owner.AccessLevel >= m.AccessLevel ||
m is PlayerMobile mobile && mobile.VisibilityList.Contains(owner)))
{
if (filter != null && m.Name?.ToLower().Contains(filter, StringComparison.Ordinal) != true)
if (filter != null && !m.Name.InsensitiveContains(filter))
{
continue;
}
@ -330,7 +330,7 @@ namespace Server.Gumps
return -1;
}
return x.AccessLevel < y.AccessLevel ? 1 : Insensitive.Compare(x.Name, y.Name);
return x.AccessLevel < y.AccessLevel ? 1 : x.Name.InsensitiveCompare(y.Name);
}
}
}