From 0c5435a8f1fc3d5ae970a07477fa65da8f9f25bf Mon Sep 17 00:00:00 2001 From: DimitarVen Date: Mon, 2 Jun 2025 17:05:30 +0300 Subject: [PATCH 1/2] stream files --- src/DynamoCore/Core/CustomNodeDefinition.cs | 67 +++++++++++++++++++++ src/DynamoCore/Core/CustomNodeManager.cs | 34 +++++------ 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/src/DynamoCore/Core/CustomNodeDefinition.cs b/src/DynamoCore/Core/CustomNodeDefinition.cs index b9a574d15c1..b270b4a1fd7 100644 --- a/src/DynamoCore/Core/CustomNodeDefinition.cs +++ b/src/DynamoCore/Core/CustomNodeDefinition.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Dynamo.Engine; using Dynamo.Engine.CodeGeneration; @@ -7,6 +8,8 @@ using Dynamo.Graph.Nodes.CustomNodes; using Dynamo.Graph.Workspaces; using Dynamo.Library; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using ProtoCore; using ProtoCore.AST.AssociativeAST; @@ -302,6 +305,9 @@ public CustomNodeInfo(Guid functionId, string name, string category, string desc if (String.IsNullOrWhiteSpace(Category)) Category = Dynamo.Properties.Resources.DefaultCustomNodeCategory; } + + [JsonConstructor] public CustomNodeInfo() { } + /// /// Returns custom node unique ID /// @@ -345,5 +351,66 @@ public CustomNodeInfo(Guid functionId, string name, string category, string desc /// requested this CustomNode to load. /// public PackageInfo PackageInfo { get; internal set; } + + + private static readonly string[] jsonKeys = { "Uuid", "Category", "Description", "Name", "IsVisibleInDynamoLibrary" }; + private static readonly Dictionary propertyLookup = []; + private static DefaultJsonNameTable propertyTable = null; + + internal static bool GetFromJsonDocument(string path, out CustomNodeInfo info, out Exception ex) + { + if (propertyTable == null) + { + propertyTable = new DefaultJsonNameTable(); + foreach (var pn in jsonKeys) + { + propertyLookup[pn] = propertyTable.Add(pn); + } + } + + try + { + var data = new JObject(); + // JsonTextRead will automatically dispose of the stream reader + using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) + using (var jr = new JsonTextReader(new StreamReader(fs)) { PropertyNameTable = propertyTable }) + { + while (jr.Read()) + { + if (data.Count == jsonKeys.Length) + { + break; + } + + if (jr.TokenType == JsonToken.PropertyName) + { + foreach (var prop in propertyLookup) + { + if (jr.Value == prop.Value) + { + data[prop.Key] = jr.ReadAsString() ?? ""; + break; + } + } + } + } + } + + ex = null; + if (data.TryGetValue("Uuid", out var v)) + { + data["FunctionId"] = v; + } + + info = data.ToObject(); + return true; + } + catch (Exception e) + { + ex = e; + info = null; + return false; + } + } } } diff --git a/src/DynamoCore/Core/CustomNodeManager.cs b/src/DynamoCore/Core/CustomNodeManager.cs index 8dabe1ec45b..9172c2561cb 100644 --- a/src/DynamoCore/Core/CustomNodeManager.cs +++ b/src/DynamoCore/Core/CustomNodeManager.cs @@ -678,39 +678,33 @@ internal bool IsInitialized(Guid guid) /// The custom node info object - null if we failed internal bool TryGetInfoFromPath(string path, bool isTestMode, out CustomNodeInfo info) { - WorkspaceInfo header = null; - XmlDocument xmlDoc; - string jsonDoc; Exception ex; try { - if (DynamoUtilities.PathHelper.isValidJson(path, out jsonDoc, out ex)) + //if (DynamoUtilities.PathHelper.isValidJson(path, out jsonDoc, out ex)) + if (CustomNodeInfo.GetFromJsonDocument(path, out info, out ex)) { - if (!WorkspaceInfo.FromJsonDocument(jsonDoc, path, isTestMode, false, AsLogger(), out header)) - { - Log(String.Format(Properties.Resources.FailedToLoadHeader, path)); - info = null; - return false; - } + return true; } - else if (DynamoUtilities.PathHelper.isValidXML(path, out xmlDoc, out ex)) + else if (DynamoUtilities.PathHelper.isValidXML(path, out var xmlDoc, out ex)) { - if (!WorkspaceInfo.FromXmlDocument(xmlDoc, path, isTestMode, false, AsLogger(), out header)) + if (!WorkspaceInfo.FromXmlDocument(xmlDoc, path, isTestMode, false, AsLogger(), out var header)) { Log(String.Format(Properties.Resources.FailedToLoadHeader, path)); info = null; return false; } + + info = new CustomNodeInfo( + Guid.Parse(header.ID), + header.Name, + header.Category, + header.Description, + path, + header.IsVisibleInDynamoLibrary); + return true; } else throw ex; - info = new CustomNodeInfo( - Guid.Parse(header.ID), - header.Name, - header.Category, - header.Description, - path, - header.IsVisibleInDynamoLibrary); - return true; } catch (Exception e) { From c14b06fc1ce08e936e2ba530e99c9050d2400a23 Mon Sep 17 00:00:00 2001 From: DimitarVen Date: Mon, 2 Jun 2025 18:42:51 +0300 Subject: [PATCH 2/2] bugfix --- src/DynamoCore/Core/CustomNodeDefinition.cs | 34 ++++++++++++--------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/DynamoCore/Core/CustomNodeDefinition.cs b/src/DynamoCore/Core/CustomNodeDefinition.cs index b270b4a1fd7..3dcf2cbccac 100644 --- a/src/DynamoCore/Core/CustomNodeDefinition.cs +++ b/src/DynamoCore/Core/CustomNodeDefinition.cs @@ -343,7 +343,7 @@ [JsonConstructor] public CustomNodeInfo() { } /// Indicates if custom node is part of the library search. /// If true, then custom node is part of library search. /// - public bool IsVisibleInDynamoLibrary { get; private set; } + public bool IsVisibleInDynamoLibrary { get; set; } /// /// Only valid if IsPackageMember is true. @@ -353,8 +353,9 @@ [JsonConstructor] public CustomNodeInfo() { } public PackageInfo PackageInfo { get; internal set; } - private static readonly string[] jsonKeys = { "Uuid", "Category", "Description", "Name", "IsVisibleInDynamoLibrary" }; + private static readonly string[] topLevelJsonKeys = { "Uuid", "Category", "Description", "Name" }; private static readonly Dictionary propertyLookup = []; + private static object isVisibleInDynamoLibraryProp = null; private static DefaultJsonNameTable propertyTable = null; internal static bool GetFromJsonDocument(string path, out CustomNodeInfo info, out Exception ex) @@ -362,10 +363,11 @@ internal static bool GetFromJsonDocument(string path, out CustomNodeInfo info, o if (propertyTable == null) { propertyTable = new DefaultJsonNameTable(); - foreach (var pn in jsonKeys) + foreach (var pn in topLevelJsonKeys) { propertyLookup[pn] = propertyTable.Add(pn); } + isVisibleInDynamoLibraryProp = propertyTable.Add(nameof(IsVisibleInDynamoLibrary)); } try @@ -377,31 +379,35 @@ internal static bool GetFromJsonDocument(string path, out CustomNodeInfo info, o { while (jr.Read()) { - if (data.Count == jsonKeys.Length) + if (data.Count == topLevelJsonKeys.Length + 1) { - break; + break; // we have all of the } if (jr.TokenType == JsonToken.PropertyName) { - foreach (var prop in propertyLookup) + if (jr.Depth == 1) { - if (jr.Value == prop.Value) + foreach (var prop in propertyLookup) { - data[prop.Key] = jr.ReadAsString() ?? ""; - break; + if (jr.Value == prop.Value) + { + data[prop.Key] = jr.ReadAsString() ?? ""; + break; + } } } + else if (jr.Value == isVisibleInDynamoLibraryProp) + { + data[nameof(IsVisibleInDynamoLibrary)] = jr.ReadAsBoolean(); + } } } } ex = null; - if (data.TryGetValue("Uuid", out var v)) - { - data["FunctionId"] = v; - } - + data["FunctionId"] = data.GetValue("Uuid"); + data["Path"] = path; info = data.ToObject(); return true; }