From f9200ae6a8ef31dc9c3244e8ae0b90d63c16bb64 Mon Sep 17 00:00:00 2001 From: Lumos Date: Thu, 13 Feb 2020 21:48:59 +0000 Subject: [PATCH] Change the serialised type of dynamic port lists defined as lists/arrays to match the respective element type. (#232) This is logical, given that xNode already draws such dynamic ports as belonging to the element type. --- Scripts/NodeDataCache.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index 24458ec..71931b1 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -76,13 +76,28 @@ namespace XNode { NodePort backingPort = staticPorts[backingPortName]; // Update port constraints. Creating a new port instead will break the editor, mandating the need for setters. - listPort.ValueType = backingPort.ValueType; + listPort.ValueType = GetBackingValueType(backingPort.ValueType); listPort.direction = backingPort.direction; listPort.connectionType = backingPort.connectionType; listPort.typeConstraint = backingPort.typeConstraint; } } + /// + /// Extracts the underlying types from arrays and lists, the only collections for dynamic port lists + /// currently supported. If the given type is not applicable (i.e. if the dynamic list port was not + /// defined as an array or a list), returns the given type itself. + /// + private static System.Type GetBackingValueType(System.Type portValType) { + if (portValType.HasElementType) { + return portValType.GetElementType(); + } + if (portValType.IsGenericType && portValType.GetGenericTypeDefinition() == typeof(List<>)) { + return portValType.GetGenericArguments()[0]; + } + return portValType; + } + /// Returns true if the given port is in a dynamic port list. private static bool IsDynamicListPort(NodePort port) { // Ports flagged as "dynamicPortList = true" end up having a "backing port" and a name with an index, but we have