Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion src/DynamoCore/Core/CustomNodeDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Dynamo.Engine;
using Dynamo.Engine.CodeGeneration;
using Dynamo.Graph.Nodes;
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;

Expand Down Expand Up @@ -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() { }

/// <summary>
/// Returns custom node unique ID
/// </summary>
Expand Down Expand Up @@ -337,13 +343,80 @@ public CustomNodeInfo(Guid functionId, string name, string category, string desc
/// Indicates if custom node is part of the library search.
/// If true, then custom node is part of library search.
/// </summary>
public bool IsVisibleInDynamoLibrary { get; private set; }
public bool IsVisibleInDynamoLibrary { get; set; }

/// <summary>
/// Only valid if IsPackageMember is true.
/// Can be used to identify which package
/// requested this CustomNode to load.
/// </summary>
public PackageInfo PackageInfo { get; internal set; }


private static readonly string[] topLevelJsonKeys = { "Uuid", "Category", "Description", "Name" };
private static readonly Dictionary<string, object> propertyLookup = [];
private static object isVisibleInDynamoLibraryProp = null;
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 topLevelJsonKeys)
{
propertyLookup[pn] = propertyTable.Add(pn);
}
isVisibleInDynamoLibraryProp = propertyTable.Add(nameof(IsVisibleInDynamoLibrary));
}

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 == topLevelJsonKeys.Length + 1)
{
break; // we have all of the
}

if (jr.TokenType == JsonToken.PropertyName)
{
if (jr.Depth == 1)
{
foreach (var prop in propertyLookup)
{
if (jr.Value == prop.Value)
{
data[prop.Key] = jr.ReadAsString() ?? "";
break;
}
}
}
else if (jr.Value == isVisibleInDynamoLibraryProp)
{
data[nameof(IsVisibleInDynamoLibrary)] = jr.ReadAsBoolean();
}
}
}
}

ex = null;
data["FunctionId"] = data.GetValue("Uuid");
data["Path"] = path;
info = data.ToObject<CustomNodeInfo>();
return true;
}
catch (Exception e)
{
ex = e;
info = null;
return false;
}
}
}
}
34 changes: 14 additions & 20 deletions src/DynamoCore/Core/CustomNodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -678,39 +678,33 @@ internal bool IsInitialized(Guid guid)
/// <returns>The custom node info object - null if we failed</returns>
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)
{
Expand Down
Loading