From 23c729f31b506763c0ce6de7257b4c3a20303be0 Mon Sep 17 00:00:00 2001 From: mdodkins Date: Fri, 11 Aug 2023 03:24:45 +0100 Subject: [PATCH] fix: Fixes PlayerVendor customizations not working due to constructor issues (#1451) --- .../Server/Utilities/ActivatorExtensions.cs | 7 ++++++ Projects/UOContent/Commands/Dupe.cs | 5 ++-- Projects/UOContent/Gumps/PlayerVendorGumps.cs | 25 ++++++++++++------- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Projects/Server/Utilities/ActivatorExtensions.cs b/Projects/Server/Utilities/ActivatorExtensions.cs index f7291212a..f2c0b69ee 100644 --- a/Projects/Server/Utilities/ActivatorExtensions.cs +++ b/Projects/Server/Utilities/ActivatorExtensions.cs @@ -26,6 +26,13 @@ public static class ActivatorExtensions Type[] args = null ) => type.GetConstructor(predicate, args, out _); + public static ConstructorInfo GetConstructor( + this Type type, + out int paramCount, + Predicate predicate = null, + Type[] args = null + ) => type.GetConstructor(predicate, args, out paramCount); + public static ConstructorInfo GetConstructor( this Type type, Predicate predicate, diff --git a/Projects/UOContent/Commands/Dupe.cs b/Projects/UOContent/Commands/Dupe.cs index f44bdf066..0911a5389 100644 --- a/Projects/UOContent/Commands/Dupe.cs +++ b/Projects/UOContent/Commands/Dupe.cs @@ -106,11 +106,10 @@ namespace Server.Commands pack = from.Backpack; } - var c = copy.GetType().GetConstructor(); + var c = copy.GetType().GetConstructor(out var paramCount); if (c != null) { - var paramList = c.GetParameters(); - var args = paramList.Length == 0 ? null : new object[paramList.Length]; + var args = paramCount == 0 ? null : new object[paramCount]; if (args != null) { Array.Fill(args, Type.Missing); diff --git a/Projects/UOContent/Gumps/PlayerVendorGumps.cs b/Projects/UOContent/Gumps/PlayerVendorGumps.cs index b525b59d6..14ea7e76b 100644 --- a/Projects/UOContent/Gumps/PlayerVendorGumps.cs +++ b/Projects/UOContent/Gumps/PlayerVendorGumps.cs @@ -1,8 +1,10 @@ using System; +using System.Reflection; using Server.HuePickers; using Server.Items; using Server.Mobiles; using Server.Network; +using Server.Utilities; namespace Server.Gumps { @@ -751,6 +753,9 @@ namespace Server.Gumps private class CustomItem { + private ConstructorInfo _ctor; + private object[] _params; + public CustomItem(int itemID, int loc, bool longText = false) : this(null, itemID, loc, 0, longText) { } @@ -785,22 +790,24 @@ namespace Server.Gumps return null; } - Item i = null; + if (_ctor == null) + { + _ctor = Type.GetConstructor(out var paramCount); + _params = paramCount == 0 ? null : new object[paramCount]; + if (_params != null) + { + Array.Fill(_params, Type.Missing); + } + } try { - var ctor = Type.GetConstructor(Array.Empty()); - if (ctor != null) - { - i = ctor.Invoke(null) as Item; - } + return _ctor?.Invoke(_params) as Item; } catch { - // ignored + return null; } - - return i; } }