diff --git a/Projects/UOContent.Tests/Tests/Utilities/TryParseTests.cs b/Projects/UOContent.Tests/Tests/Utilities/TryParseTests.cs index 314290264..b6c693c84 100644 --- a/Projects/UOContent.Tests/Tests/Utilities/TryParseTests.cs +++ b/Projects/UOContent.Tests/Tests/Utilities/TryParseTests.cs @@ -45,3 +45,38 @@ public class TryParseTests } } } + +// Interface-typed properties (BaseCreature.TargetLocation : IPoint2D) have no static Parse, so +// without these branches the parser fell through to Convert.ChangeType and reported the value as +// "not properly formatted". +public class InterfacePointParseTests +{ + [Theory] + [InlineData(typeof(IPoint3D), "(1, 2, 3)", 1, 2, 3)] + [InlineData(typeof(IPoint2D), "(1, 2, 3)", 1, 2, 3)] // a 3-tuple is a valid IPoint2D + public void TuplesParseIntoPoint3D(Type type, string value, int x, int y, int z) + { + Assert.Null(Server.Types.TryParse(type, value, out var constructed)); + Assert.Equal(new Point3D(x, y, z), constructed); + } + + [Fact] + public void PairParsesIntoPoint2D() + { + Assert.Null(Server.Types.TryParse(typeof(IPoint2D), "(4, 5)", out var constructed)); + Assert.Equal(new Point2D(4, 5), constructed); + } + + [Fact] + public void PairIsNotAPoint3D() + { + Assert.NotNull(Server.Types.TryParse(typeof(IPoint3D), "(4, 5)", out _)); + } + + [Fact] + public void NullSentinelClearsAnInterfaceProperty() + { + Assert.Null(Server.Types.TryParse(typeof(IPoint2D), "(-null-)", out var constructed)); + Assert.Null(constructed); + } +} diff --git a/Projects/UOContent/Gumps/Props/PropsGump.cs b/Projects/UOContent/Gumps/Props/PropsGump.cs index 00fd2557d..e24b2f571 100644 --- a/Projects/UOContent/Gumps/Props/PropsGump.cs +++ b/Projects/UOContent/Gumps/Props/PropsGump.cs @@ -224,11 +224,13 @@ namespace Server.Gumps { from.Target = new SetObjectTarget(prop, from, m_Object, type, this); } - else if (IsType(type, OfPoint3D)) + // A null interface-typed value (TargetLocation : IPoint2D) routes on the + // declared type; a held entity was already caught above. + else if (IsType(type, OfPoint3D) || type == OfIPoint3D) { from.SendGump(new SetPoint3DGump(prop, from, m_Object, this)); } - else if (IsType(type, OfPoint2D)) + else if (IsType(type, OfPoint2D) || type == OfIPoint2D) { from.SendGump(new SetPoint2DGump(prop, from, m_Object, this)); } diff --git a/Projects/UOContent/Gumps/Props/SetPoint2DGump.cs b/Projects/UOContent/Gumps/Props/SetPoint2DGump.cs index 35fd70103..5007bef22 100644 --- a/Projects/UOContent/Gumps/Props/SetPoint2DGump.cs +++ b/Projects/UOContent/Gumps/Props/SetPoint2DGump.cs @@ -26,7 +26,7 @@ namespace Server.Gumps m_Mobile = mobile; m_Object = o; - var p = (Point2D)(prop?.GetValue(o, null) ?? new Point2D()); + var p = prop?.GetValue(o, null) is IPoint2D current ? new Point2D(current) : new Point2D(); AddPage(0); diff --git a/Projects/UOContent/Gumps/Props/SetPoint3DGump.cs b/Projects/UOContent/Gumps/Props/SetPoint3DGump.cs index b0707bc3d..911f01a86 100644 --- a/Projects/UOContent/Gumps/Props/SetPoint3DGump.cs +++ b/Projects/UOContent/Gumps/Props/SetPoint3DGump.cs @@ -27,7 +27,7 @@ namespace Server.Gumps m_Mobile = mobile; m_Object = o; - var p = (Point3D)(prop?.GetValue(o, null) ?? new Point3D()); + var p = prop?.GetValue(o, null) is IPoint3D current ? new Point3D(current) : new Point3D(); AddPage(0); diff --git a/Projects/UOContent/Utilities/Types.cs b/Projects/UOContent/Utilities/Types.cs index 184773e84..b77511e96 100644 --- a/Projects/UOContent/Utilities/Types.cs +++ b/Projects/UOContent/Utilities/Types.cs @@ -33,6 +33,8 @@ namespace Server public static readonly Type OfTimeSpan = typeof(TimeSpan); public static readonly Type OfPoint3D = typeof(Point3D); public static readonly Type OfPoint2D = typeof(Point2D); + public static readonly Type OfIPoint3D = typeof(IPoint3D); + public static readonly Type OfIPoint2D = typeof(IPoint2D); public static readonly Type OfEnum = typeof(Enum); public static readonly Type OfType = typeof(Type); @@ -278,6 +280,23 @@ namespace Server return null; } + if (type == OfIPoint3D || type == OfIPoint2D) + { + if (Point3D.TryParse(value, null, out var p3)) + { + constructed = p3; + return null; + } + + if (type == OfIPoint2D && Point2D.TryParse(value, null, out var p2)) + { + constructed = p2; + return null; + } + + return "That is not properly formatted."; + } + if (IsType(type, OfBool)) { if (bool.TryParse(value, out var parsed))