fix: Fixes adding items with ambiguous type lookup (#2307)
Fix [add command failing with ambiguous type names + refactor for performance ### Problem [add blight would fail with "No type with that name was found" because multiple types contain "blight" (e.g., Server.Items.Blight, Server.Ethics.Evil.Blight, Server.Items.BlightGrippedLongbow, Server.Items.QuiverOfBlight). The old code only succeeded when exactly one type matched the search regardless of constructability and inheriting Mobi les/Items. ### Solution Exact match takes priority: If a type's name exactly equals the search string (case-insensitive), use it directly. Otherwise, show the AddGump with all partial matches. - [add blight → Creates Blight (exact name match) - [add bligh → Shows gump with Blight, BlightGrippedLongbow, etc. ### Refactoring - CommandEventArgs context: Added GetContext<T>/SetContext<T> to pass resolved type through the command chain without method signature changes - Removed TrySetupTarget duplication: Validation now happens only in ValidateArgs, eliminating redundant code paths - Split type matching: - ExactMatch(string) → Returns Type for exact name match (used by [add) - MatchEmptyCtor(string) → Returns ConstructorInfo[] for gump display (empty-callable constructors only) ### Memory & Performance Improvements | Optimization | Benefit | |-------------------------------|----------------------------------------------------------------------------------------------| | _mobileItemTypes cache | Filters Mobile/Item types once per assembly, reused on all subsequent searches | | ReadOnlySpan<string> for args | Avoids string[] heap allocations when slicing arguments | | ValueStringBuilder | Stack-allocated string building, avoids StringBuilder heap allocation | | Single type resolution | Type resolved once in ValidateArgs, passed via context to Execute (was resolved 2-3x before) |
This commit is contained in:
parent
c4c8b45b79
commit
f4a87a8629
12 changed files with 837 additions and 684 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Server.Accounting;
|
||||
using Server.Engines.Help;
|
||||
using Server.Factions;
|
||||
|
|
@ -511,36 +512,39 @@ namespace Server.Commands.Generic
|
|||
|
||||
public override bool ValidateArgs(BaseCommandImplementor impl, CommandEventArgs e)
|
||||
{
|
||||
if (e.Length >= 1)
|
||||
{
|
||||
var t = AssemblyHandler.FindTypeByName(e.GetString(0));
|
||||
|
||||
if (t == null)
|
||||
{
|
||||
e.Mobile.SendMessage("No type with that name was found.");
|
||||
|
||||
var match = e.GetString(0).Trim();
|
||||
|
||||
if (match.Length < 3)
|
||||
{
|
||||
e.Mobile.SendMessage("Invalid search string.");
|
||||
e.Mobile.SendGump(new AddGump(match, 0, Type.EmptyTypes, false));
|
||||
}
|
||||
else
|
||||
{
|
||||
e.Mobile.SendGump(new AddGump(match, 0, AddGump.Match(match), true));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (e.Length < 1)
|
||||
{
|
||||
e.Mobile.SendGump(new CategorizedAddGump(e.Mobile));
|
||||
return false;
|
||||
}
|
||||
|
||||
var match = e.GetString(0).Trim();
|
||||
if (e.GetContext<Type>("exactMatch", out var exactMatch))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
exactMatch = AddGump.ExactMatch(match);
|
||||
if (exactMatch != null)
|
||||
{
|
||||
e.SetContext("exactMatch", exactMatch);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (match.Length < 3)
|
||||
{
|
||||
e.Mobile.SendMessage("Invalid search string.");
|
||||
e.Mobile.SendGump(new AddGump(match, 0, [], false));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!e.GetContext<ConstructorInfo[]>("matches", out var matches))
|
||||
{
|
||||
matches = e.SetContext("matches", AddGump.MatchEmptyCtor(match));
|
||||
}
|
||||
|
||||
e.Mobile.SendMessage("No type with that name was found.");
|
||||
e.Mobile.SendGump(new AddGump(match, 0, matches, true));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -558,7 +562,13 @@ namespace Server.Commands.Generic
|
|||
_ => new Point3D(ip)
|
||||
};
|
||||
|
||||
Add.Invoke(e.Mobile, p, p, e.Arguments);
|
||||
if (e.GetContext<Type>("exactMatch", out var exactMatch))
|
||||
{
|
||||
Add.Invoke(e.Mobile, p, p, exactMatch, e.Arguments.AsSpan(1));
|
||||
return;
|
||||
}
|
||||
|
||||
e.Mobile.SendMessage("No type with that name was found.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -213,8 +213,6 @@ namespace Server.Commands.Generic
|
|||
|
||||
public void RunCommand(Mobile from, object obj, BaseCommand command, string[] args)
|
||||
{
|
||||
// try
|
||||
// {
|
||||
var e = new CommandEventArgs(from, command.Commands[0], GenerateArgString(args), args);
|
||||
|
||||
if (!command.ValidateArgs(this, e))
|
||||
|
|
@ -256,11 +254,6 @@ namespace Server.Commands.Generic
|
|||
}
|
||||
|
||||
command.Flush(from, flushToLog);
|
||||
// }
|
||||
// catch ( Exception ex )
|
||||
// {
|
||||
// from.SendMessage( ex.Message );
|
||||
// }
|
||||
}
|
||||
|
||||
public virtual void Process(Mobile from, BaseCommand command, string[] args)
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ namespace Server.Commands.Generic
|
|||
{
|
||||
e.Mobile.SendMessage("You do not have access to that command.");
|
||||
}
|
||||
else if (command.ValidateArgs(this, e))
|
||||
else
|
||||
{
|
||||
Process(e.Mobile, command, e.Arguments);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue