Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for optional parameters in add command #37

Merged
merged 1 commit into from Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 12 additions & 19 deletions Scripts/Commands/Add.cs
Expand Up @@ -194,15 +194,13 @@ public static void FixArgs(ref string[] args)
int totalParams = 0;

// Handle optional constructors
ParameterInfo[] paramList = ctor.GetParameters().Select(param =>
{
if (param.DefaultValue is DBNull)
ParameterInfo[] paramList = ctor.GetParameters();
for (int j = 0; j < paramList.Length; j++)
if (!paramList[j].HasDefaultValue)
totalParams += 1;

return param;
}).ToArray();

if (args.Length == totalParams)
if (args.Length >= totalParams && args.Length <= paramList.Length)
{
object[] paramValues = ParseValues(paramList, args);

Expand All @@ -223,25 +221,23 @@ public static object[] ParseValues(ParameterInfo[] paramList, string[] args)
{
object[] values = new object[paramList.Length];

for (int i = 0, a = 0; i < paramList.Length; ++i)
for (int i = 0, a = 0; i < paramList.Length; i++)
{
ParameterInfo param = paramList[i];
if (param.DefaultValue is DBNull)
{
object value = ParseValue(param.ParameterType, args[a++], param.DefaultValue);
if (value == null)
return null;
object value = ParseValue(param.ParameterType, a < args.Length ? args[a++] : null);

if (value != null)
values[i] = value;
}
else
else if (param.HasDefaultValue)
values[i] = Type.Missing;
else
return null;
}

return values;
}

public static object ParseValue(Type type, string value, object defaultValue)
public static object ParseValue(Type type, string value)
{
try
{
Expand Down Expand Up @@ -630,10 +626,7 @@ public static bool IsConstructible(ConstructorInfo ctor, AccessLevel accessLevel
{
object[] attrs = ctor.GetCustomAttributes(m_ConstructibleType, false);

if (attrs.Length == 0)
return false;

return accessLevel >= ((ConstructibleAttribute)attrs[0]).AccessLevel;
return attrs.Length != 0 && accessLevel >= ((ConstructibleAttribute)attrs[0]).AccessLevel;
}

public static bool IsEnum(Type type)
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Commands/Handlers.cs
Expand Up @@ -79,7 +79,7 @@ public static void Register(string command, AccessLevel access, CommandEventHand
}

[Usage("SpeedBoost [true|false]")]
[Description("Enables a speed boost for the invoker. Disable with paramaters.")]
[Description("Enables a speed boost for the invoker. Disable with parameters.")]
private static void SpeedBoost_OnCommand(CommandEventArgs e)
{
Mobile from = e.Mobile;
Expand Down
2 changes: 1 addition & 1 deletion Server/Mobile.cs
Expand Up @@ -4845,7 +4845,7 @@ public static Item LiftItemDupe(Item oldItem, int amount)
catch
{
Console.WriteLine(
"Warning: 0x{0:X}: Item must have a zero paramater constructor to be separated from a stack. '{1}'.",
"Warning: 0x{0:X}: Item must have a zero parameter constructor to be separated from a stack. '{1}'.",
oldItem.Serial.Value, oldItem.GetType().Name);
return null;
}
Expand Down