|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Collections.ObjectModel; |
4 | 4 | using System.ComponentModel; |
| 5 | +using System.Globalization; |
5 | 6 | using System.IO; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Reflection; |
@@ -173,6 +174,88 @@ protected override bool UpdateValueCore(UpdateValueParams updateValueParams) |
173 | 174 | return base.UpdateValueCore(updateValueParams); |
174 | 175 | } |
175 | 176 |
|
| 177 | + #region SerializeCore/DeserializeCore |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Preserves custom input and output port names and tooltips during XML serialization. |
| 181 | + /// The base NodeModel serialization writes PortInfo elements for input ports; |
| 182 | + /// this override adds "portName" and "portToolTip" attributes to each of those |
| 183 | + /// elements and appends OutPortInfo elements for output ports. |
| 184 | + /// These fields are emitted and consumed for all SaveContext values (Copy, Undo, Save, etc.). |
| 185 | + /// </summary> |
| 186 | + protected override void SerializeCore(XmlElement element, SaveContext context) |
| 187 | + { |
| 188 | + base.SerializeCore(element, context); |
| 189 | + |
| 190 | + // Add portName and portToolTip attributes to each PortInfo element written by NodeModel.SerializeCore. |
| 191 | + foreach (XmlNode child in element.ChildNodes) |
| 192 | + { |
| 193 | + if (child.Name == "PortInfo" && child is XmlElement portInfo) |
| 194 | + { |
| 195 | + var indexAttr = portInfo.GetAttribute("index"); |
| 196 | + if (int.TryParse(indexAttr, NumberStyles.Integer, CultureInfo.InvariantCulture, out int index) |
| 197 | + && index >= 0 && index < InPorts.Count) |
| 198 | + { |
| 199 | + portInfo.SetAttribute("portName", InPorts[index].Name); |
| 200 | + portInfo.SetAttribute("portToolTip", InPorts[index].ToolTip); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + // Write output port names and tooltips as separate elements (NodeModel does not serialize output PortInfo). |
| 206 | + for (int i = 0; i < OutPorts.Count; i++) |
| 207 | + { |
| 208 | + XmlElement outPortInfo = element.OwnerDocument.CreateElement("OutPortInfo"); |
| 209 | + outPortInfo.SetAttribute("index", i.ToString(CultureInfo.InvariantCulture)); |
| 210 | + outPortInfo.SetAttribute("portName", OutPorts[i].Name); |
| 211 | + outPortInfo.SetAttribute("portToolTip", OutPorts[i].ToolTip); |
| 212 | + element.AppendChild(outPortInfo); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + /// <summary> |
| 217 | + /// Restores custom input and output port names after base deserialization has |
| 218 | + /// recreated the ports with their default names. |
| 219 | + /// </summary> |
| 220 | + protected override void DeserializeCore(XmlElement nodeElement, SaveContext context) |
| 221 | + { |
| 222 | + base.DeserializeCore(nodeElement, context); |
| 223 | + |
| 224 | + // After base.DeserializeCore, all ports exist with default names and tooltips. |
| 225 | + // Re-apply any custom values that were serialized. |
| 226 | + foreach (XmlNode child in nodeElement.ChildNodes) |
| 227 | + { |
| 228 | + if (child.Name == "PortInfo" && child is XmlElement portInfo) |
| 229 | + { |
| 230 | + var indexAttr = portInfo.GetAttribute("index"); |
| 231 | + if (int.TryParse(indexAttr, NumberStyles.Integer, CultureInfo.InvariantCulture, out int index) |
| 232 | + && index >= 0 && index < InPorts.Count) |
| 233 | + { |
| 234 | + if (portInfo.HasAttribute("portName")) |
| 235 | + InPorts[index].Name = portInfo.GetAttribute("portName"); |
| 236 | + |
| 237 | + if (portInfo.HasAttribute("portToolTip")) |
| 238 | + InPorts[index].ToolTip = portInfo.GetAttribute("portToolTip"); |
| 239 | + } |
| 240 | + } |
| 241 | + else if (child.Name == "OutPortInfo" && child is XmlElement outPortInfo) |
| 242 | + { |
| 243 | + var indexAttr = outPortInfo.GetAttribute("index"); |
| 244 | + if (int.TryParse(indexAttr, NumberStyles.Integer, CultureInfo.InvariantCulture, out int outIndex) |
| 245 | + && outIndex >= 0 && outIndex < OutPorts.Count) |
| 246 | + { |
| 247 | + if (outPortInfo.HasAttribute("portName")) |
| 248 | + OutPorts[outIndex].Name = outPortInfo.GetAttribute("portName"); |
| 249 | + |
| 250 | + if (outPortInfo.HasAttribute("portToolTip")) |
| 251 | + OutPorts[outIndex].ToolTip = outPortInfo.GetAttribute("portToolTip"); |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + #endregion |
| 258 | + |
176 | 259 | } |
177 | 260 |
|
178 | 261 | [NodeName("Python Script")] |
|
0 commit comments