From d19ea40e383299c2f97afb9b8f1bbe0988272c61 Mon Sep 17 00:00:00 2001 From: Raistlin Wolfe Date: Mon, 12 Sep 2022 18:22:03 -0600 Subject: [PATCH 1/4] Update Node.cs --- Scripts/Node.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 704e99d..b705627 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -52,7 +52,13 @@ namespace XNode { /// Allow connections where output value type is assignable from input value type (eg. Object --> ScriptableObject) InheritedInverse, /// Allow connections where output value type is assignable from input value or input value type is assignable from output value type - InheritedAny + InheritedAny, + /// Allow connections where input value type is castable from output value type. + Castable, + /// Allow connections where output value type is castable from input value type. + CastableInverse, + /// Allow connections where input value type is castable from output value type or output value type is castable from input value type. + CastableAny } #region Obsolete From 7473083b8440d0e2d87215d45d631b6f51e10ca6 Mon Sep 17 00:00:00 2001 From: Raistlin Wolfe Date: Mon, 12 Sep 2022 18:25:14 -0600 Subject: [PATCH 2/4] Update NodePort.cs --- Scripts/NodePort.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 6bcc638..14c3b7d 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -279,6 +279,9 @@ namespace XNode { if (output.typeConstraint == XNode.Node.TypeConstraint.Strict && input.ValueType != output.ValueType) return false; if (output.typeConstraint == XNode.Node.TypeConstraint.InheritedInverse && !output.ValueType.IsAssignableFrom(input.ValueType)) return false; if (output.typeConstraint == XNode.Node.TypeConstraint.InheritedAny && !input.ValueType.IsAssignableFrom(output.ValueType) && !output.ValueType.IsAssignableFrom(input.ValueType)) return false; + if (output.typeConstraint == XNode.Node.TypeConstraint.Castable && !input.ValueType.IsCastableFrom(output.ValueType)) return false; + if (output.typeConstraint == XNode.Node.TypeConstraint.CastableInverse && !output.ValueType.IsCastableFrom(input.ValueType)) return false; + if (output.typeConstraint == XNode.Node.TypeConstraint.CastableAny && !input.ValueType.IsCastableFrom(output.ValueType) && !output.ValueType.IsCastableFrom(input.ValueType)) return false; // Success return true; } From 5e3c5c0012025ca09ccc9363cb20318519f20957 Mon Sep 17 00:00:00 2001 From: Raistlin Wolfe Date: Mon, 12 Sep 2022 18:36:45 -0600 Subject: [PATCH 3/4] Create TypeExtensions.cs Provided Implementation for IsCastableFrom --- Scripts/TypeExtensions.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Scripts/TypeExtensions.cs diff --git a/Scripts/TypeExtensions.cs b/Scripts/TypeExtensions.cs new file mode 100644 index 0000000..5f79664 --- /dev/null +++ b/Scripts/TypeExtensions.cs @@ -0,0 +1,15 @@ +using System.Linq; +using System.Reflection; + +public static class TypeExtensions +{ + public static bool IsCastableFrom(this Type to, Type from) + { + if ( to.IsAssignableFrom ( from ) ) + return true; + return from.GetMethods ( BindingFlags.Public | BindingFlags.Static ).Any ( m => + { + return m.ReturnType == to && ( m.Name == "op_Implicit" || m.Name == "op_Explicit" ); + } ); + } +} From ba988f7f28aaf4a1d97e6ebea76ebed658fdbc9a Mon Sep 17 00:00:00 2001 From: Raistlin Wolfe Date: Mon, 12 Sep 2022 18:43:28 -0600 Subject: [PATCH 4/4] Update TypeExtensions.cs Added xml comment on IsCastableFrom --- Scripts/TypeExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Scripts/TypeExtensions.cs b/Scripts/TypeExtensions.cs index 5f79664..ae49c31 100644 --- a/Scripts/TypeExtensions.cs +++ b/Scripts/TypeExtensions.cs @@ -3,6 +3,7 @@ using System.Reflection; public static class TypeExtensions { + /// Determines whether an instance of a specified type can be assigned to a variable of the current type. public static bool IsCastableFrom(this Type to, Type from) { if ( to.IsAssignableFrom ( from ) )