NodeModel GetNodeFromCommand(CreateNodeCommand command) { if (command.Node != null) { return(command.Node); } if (command.NodeXml != null) { // command was deserialized, we must create the node directly return(NodeFactory.CreateNodeFromXml(command.NodeXml, SaveContext.File, currentWorkspace.ElementResolver)); } // legacy command, hold on to your butts var name = command.Name; var nodeId = command.ModelGuid; // find nodes with of the same type with the same GUID var query = CurrentWorkspace.Nodes.Where(n => n.GUID.Equals(nodeId) && n.Name.Equals(name)); // safely ignore a node of the same type with the same GUID if (query.Any()) { return(query.First()); } // To be used in the event it's a custom node we're making. Guid customNodeId; if (command is CreateProxyNodeCommand) { var proxyCommand = command as CreateProxyNodeCommand; return(NodeFactory.CreateProxyNodeInstance(nodeId, name, proxyCommand.NickName, proxyCommand.Inputs, proxyCommand.Outputs)); } // Then, we have to figure out what kind of node to make, based on the name. NodeModel node = CreateNodeFromNameOrType(nodeId, name); if (node != null) { return(node); } // And if that didn't work, then it must be a custom node. if (Guid.TryParse(name, out customNodeId)) { node = CustomNodeManager.CreateCustomNodeInstance(customNodeId, null, false); node.GUID = nodeId; return(node); } // We're out of ideas, log an error. Logger.LogError("Could not create instance of node with name: " + name); return(null); }
void CreateNodeImpl(CreateNodeCommand command) { var node = command.Node; if (node == null) { if (command.NodeXml != null) { // command was deserialized, we must create the node directly node = NodeFactory.CreateNodeFromXml(command.NodeXml, SaveContext.File); } else { // legacy command, hold on to your butts var name = command.Name; var nodeId = command.NodeId; Guid customNodeId; // To be used in the event it's a custom node we're making. // Then, we have to figure out what kind of node to make, based on the name. // First, we check for a DSFunction by looking for a FunctionDescriptor var functionItem = LibraryServices.GetFunctionDescriptor(name); if (functionItem != null) { node = (functionItem.IsVarArg) ? new DSVarArgFunction(functionItem) as NodeModel : new DSFunction(functionItem); node.GUID = nodeId; } // If that didn't work, let's try using the NodeFactory else if (NodeFactory.CreateNodeFromTypeName(name, out node)) { node.GUID = nodeId; } // And if that didn't work, then it must be a custom node. else if (Guid.TryParse(name, out customNodeId)) { node = CustomNodeManager.CreateCustomNodeInstance(customNodeId); node.GUID = nodeId; } // We're out of ideas, log an error. else { Logger.LogError("Could not create instance of node with name: " + name); return; } } } node.X = command.X; node.Y = command.Y; AddNodeToCurrentWorkspace(node, centered: command.DefaultPosition); CurrentWorkspace.RecordCreatedModel(node); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { NodeModel node = null; var obj = JObject.Load(reader); var type = Type.GetType(obj["$type"].Value <string>()); // If we can't find this type - try to look in our load from assemblies, // but only during testing - this is required during testing because some dlls are loaded // using Assembly.LoadFrom using the assemblyHelper - which loads dlls into loadFrom context - // dlls loaded with LoadFrom context cannot be found using Type.GetType() - this should // not be an issue during normal dynamo use but if it is we can enable this code. if (type == null && this.isTestMode == true) { List <Assembly> resultList; var typeName = obj["$type"].Value <string>().Split(',').FirstOrDefault(); // This assemblyName does not usually contain version information... var assemblyName = obj["$type"].Value <string>().Split(',').Skip(1).FirstOrDefault().Trim(); if (assemblyName != null) { if (this.loadedAssemblies.TryGetValue(assemblyName, out resultList)) { var matchingTypes = resultList.Select(x => x.GetType(typeName)).ToList(); type = matchingTypes.FirstOrDefault(); } } } // Check for and attempt to resolve an unknown type before proceeding if (type == null) { // Attempt to resolve the type using `AlsoKnownAs` var unresolvedName = obj["$type"].Value <string>().Split(',').FirstOrDefault(); Type newType; nodeFactory.ResolveType(unresolvedName, out newType); // If resolved update the type if (newType != null) { type = newType; } } // If the id is not a guid, makes a guid based on the id of the node var guid = GuidUtility.tryParseOrCreateGuid(obj["Id"].Value <string>()); var replication = obj["Replication"].Value <string>(); var inPorts = obj["Inputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var outPorts = obj["Outputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var resolver = (IdReferenceResolver)serializer.ReferenceResolver; string assemblyLocation = objectType.Assembly.Location; bool remapPorts = true; // If type is still null at this point return a dummy node if (type == null) { node = CreateDummyNode(obj, assemblyLocation, inPorts, outPorts); } // Attempt to create a valid node using the type else if (type == typeof(Function)) { var functionId = Guid.Parse(obj["FunctionSignature"].Value <string>()); CustomNodeDefinition def = null; CustomNodeInfo info = null; bool isUnresolved = !manager.TryGetCustomNodeData(functionId, null, false, out def, out info); Function function = manager.CreateCustomNodeInstance(functionId, null, false, def, info); node = function; if (isUnresolved) { function.UpdatePortsForUnresolved(inPorts, outPorts); } } else if (type == typeof(CodeBlockNodeModel)) { var code = obj["Code"].Value <string>(); CodeBlockNodeModel codeBlockNode = new CodeBlockNodeModel(code, guid, 0.0, 0.0, libraryServices, ElementResolver); node = codeBlockNode; // If the code block node is in an error state read the extra port data // and initialize the input and output ports if (node.IsInErrorState) { List <string> inPortNames = new List <string>(); var inputs = obj["Inputs"]; foreach (var input in inputs) { inPortNames.Add(input["Name"].ToString()); } // NOTE: This could be done in a simpler way, but is being implemented // in this manner to allow for possible future port line number // information being available in the file List <int> outPortLineIndexes = new List <int>(); var outputs = obj["Outputs"]; int outputLineIndex = 0; foreach (var output in outputs) { outPortLineIndexes.Add(outputLineIndex++); } codeBlockNode.SetErrorStatePortData(inPortNames, outPortLineIndexes); } } else if (typeof(DSFunctionBase).IsAssignableFrom(type)) { var mangledName = obj["FunctionSignature"].Value <string>(); var priorNames = libraryServices.GetPriorNames(); var functionDescriptor = libraryServices.GetFunctionDescriptor(mangledName); string newName; // Update the function descriptor if a newer migrated version of the node exists if (priorNames.TryGetValue(mangledName, out newName)) { functionDescriptor = libraryServices.GetFunctionDescriptor(newName); } // Use the functionDescriptor to try and restore the proper node if possible if (functionDescriptor == null) { node = CreateDummyNode(obj, assemblyLocation, inPorts, outPorts); } else { if (type == typeof(DSVarArgFunction)) { node = new DSVarArgFunction(functionDescriptor); // The node syncs with the function definition. // Then we need to make the inport count correct var varg = (DSVarArgFunction)node; varg.VarInputController.SetNumInputs(inPorts.Count()); } else if (type == typeof(DSFunction)) { node = new DSFunction(functionDescriptor); } } } else if (type == typeof(DSVarArgFunction)) { var functionId = Guid.Parse(obj["FunctionSignature"].Value <string>()); node = manager.CreateCustomNodeInstance(functionId); } else if (type.ToString() == "CoreNodeModels.Formula") { node = (NodeModel)obj.ToObject(type); } else { node = (NodeModel)obj.ToObject(type); // if node is an customNode input symbol - assign the element resolver. if (node is Nodes.CustomNodes.Symbol) { (node as Nodes.CustomNodes.Symbol).ElementResolver = ElementResolver; } // We don't need to remap ports for any nodes with json constructors which pass ports remapPorts = false; } if (remapPorts) { RemapPorts(node, inPorts, outPorts, resolver, manager.AsLogger()); } // Cannot set Lacing directly as property is protected node.UpdateValue(new UpdateValueParams("ArgumentLacing", replication)); node.GUID = guid; // Add references to the node and the ports to the reference resolver, // so that they are available for entities which are deserialized later. serializer.ReferenceResolver.AddReference(serializer.Context, node.GUID.ToString(), node); foreach (var p in node.InPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } foreach (var p in node.OutPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } return(node); }
NodeModel GetNodeFromCommand(CreateNodeCommand command) { if (command.Node != null) { return(command.Node); } if (command.NodeXml != null) { // command was deserialized, we must create the node directly return(NodeFactory.CreateNodeFromXml(command.NodeXml, SaveContext.File)); } // legacy command, hold on to your butts var name = command.Name; var nodeId = command.NodeId; // find nodes with of the same type with the same GUID var query = CurrentWorkspace.Nodes.Where(n => n.GUID.Equals(nodeId) && n.Name.Equals(name)); // safely ignore a node of the same type with the same GUID if (query.Any()) { return(query.First()); } // To be used in the event it's a custom node we're making. Guid customNodeId; if (command is CreateProxyNodeCommand) { var proxyCommand = command as CreateProxyNodeCommand; return(NodeFactory.CreateProxyNodeInstance(nodeId, name, proxyCommand.NickName, proxyCommand.Inputs, proxyCommand.Outputs)); } // Then, we have to figure out what kind of node to make, based on the name. NodeModel node; // First, we check for a DSFunction by looking for a FunctionDescriptor var functionItem = LibraryServices.GetFunctionDescriptor(name); if (functionItem != null) { node = (functionItem.IsVarArg) ? new DSVarArgFunction(functionItem) as NodeModel : new DSFunction(functionItem); node.GUID = nodeId; return(node); } // If that didn't work, let's try using the NodeFactory if (NodeFactory.CreateNodeFromTypeName(name, out node)) { node.GUID = nodeId; return(node); } // And if that didn't work, then it must be a custom node. if (Guid.TryParse(name, out customNodeId)) { node = CustomNodeManager.CreateCustomNodeInstance(customNodeId); node.GUID = nodeId; return(node); } // We're out of ideas, log an error. Logger.LogError("Could not create instance of node with name: " + name); return(null); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { NodeModel node = null; var obj = JObject.Load(reader); var type = Type.GetType(obj["$type"].Value <string>()); //if we can't find this type - try to look in our load from assemblies, //but only during testing - this is required during testing because some dlls are loaded //using Assembly.LoadFrom using the assemblyHelper - which loads dlls into loadFrom context - //dlls loaded with LoadFrom context cannot be found using Type.GetType() - this should //not be an issue during normal dynamo use but if it is we can enable this code. if (type == null && this.isTestMode == true) { List <Assembly> resultList; var typeName = obj["$type"].Value <string>().Split(',').FirstOrDefault(); //this assemblyName does not usually contain version information... var assemblyName = obj["$type"].Value <string>().Split(',').Skip(1).FirstOrDefault().Trim(); if (assemblyName != null) { if (this.loadedAssemblies.TryGetValue(assemblyName, out resultList)) { var matchingTypes = resultList.Select(x => x.GetType(typeName)).ToList(); type = matchingTypes.FirstOrDefault(); } } } // If the id is not a guid, makes a guid based on the id of the node var guid = GuidUtility.tryParseOrCreateGuid(obj["Id"].Value <string>()); var replication = obj["Replication"].Value <string>(); var inPorts = obj["Inputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var outPorts = obj["Outputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var resolver = (IdReferenceResolver)serializer.ReferenceResolver; string assemblyLocation = objectType.Assembly.Location; bool remapPorts = true; if (type == null) { node = CreateDummyNode(obj, assemblyLocation, inPorts, outPorts); } else if (type == typeof(Function)) { var functionId = Guid.Parse(obj["FunctionSignature"].Value <string>()); CustomNodeDefinition def = null; CustomNodeInfo info = null; bool isUnresolved = !manager.TryGetCustomNodeData(functionId, null, false, out def, out info); Function function = manager.CreateCustomNodeInstance(functionId, null, false, def, info); node = function; if (isUnresolved) { function.UpdatePortsForUnresolved(inPorts, outPorts); } } else if (type == typeof(CodeBlockNodeModel)) { var code = obj["Code"].Value <string>(); node = new CodeBlockNodeModel(code, guid, 0.0, 0.0, libraryServices, ElementResolver); } else if (typeof(DSFunctionBase).IsAssignableFrom(type)) { var mangledName = obj["FunctionSignature"].Value <string>(); var functionDescriptor = libraryServices.GetFunctionDescriptor(mangledName); if (functionDescriptor == null) { node = CreateDummyNode(obj, assemblyLocation, inPorts, outPorts); } else { if (type == typeof(DSVarArgFunction)) { node = new DSVarArgFunction(functionDescriptor); // The node syncs with the function definition. // Then we need to make the inport count correct var varg = (DSVarArgFunction)node; varg.VarInputController.SetNumInputs(inPorts.Count()); } else if (type == typeof(DSFunction)) { node = new DSFunction(functionDescriptor); } } } else if (type == typeof(DSVarArgFunction)) { var functionId = Guid.Parse(obj["FunctionSignature"].Value <string>()); node = manager.CreateCustomNodeInstance(functionId); } else if (type.ToString() == "CoreNodeModels.Formula") { node = (NodeModel)obj.ToObject(type); } else { node = (NodeModel)obj.ToObject(type); // We don't need to remap ports for any nodes with json constructors which pass ports remapPorts = false; } if (remapPorts) { RemapPorts(node, inPorts, outPorts, resolver); } // Cannot set Lacing directly as property is protected node.UpdateValue(new UpdateValueParams("ArgumentLacing", replication)); node.GUID = guid; // Add references to the node and the ports to the reference resolver, // so that they are available for entities which are deserialized later. serializer.ReferenceResolver.AddReference(serializer.Context, node.GUID.ToString(), node); foreach (var p in node.InPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } foreach (var p in node.OutPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } return(node); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { NodeModel node = null; var obj = JObject.Load(reader); var type = Type.GetType(obj["$type"].Value <string>()); var guid = Guid.Parse(obj["Uuid"].Value <string>()); var displayName = obj["DisplayName"].Value <string>(); //var x = obj["X"].Value<double>(); //var y = obj["Y"].Value<double>(); var inPorts = obj["InputPorts"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var outPorts = obj["OutputPorts"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var resolver = (IdReferenceResolver)serializer.ReferenceResolver; if (type == typeof(Function)) { var functionId = Guid.Parse(obj["FunctionUuid"].Value <string>()); node = manager.CreateCustomNodeInstance(functionId); RemapPorts(node, inPorts, outPorts, resolver); } else if (type == typeof(CodeBlockNodeModel)) { var code = obj["Code"].Value <string>(); node = new CodeBlockNodeModel(code, guid, 0.0, 0.0, libraryServices, ElementResolver); RemapPorts(node, inPorts, outPorts, resolver); } else if (typeof(DSFunctionBase).IsAssignableFrom(type)) { var mangledName = obj["FunctionName"].Value <string>(); var description = libraryServices.GetFunctionDescriptor(mangledName); if (type == typeof(DSVarArgFunction)) { node = new DSVarArgFunction(description); // The node syncs with the function definition. // Then we need to make the inport count correct var varg = (DSVarArgFunction)node; varg.VarInputController.SetNumInputs(inPorts.Count()); } else if (type == typeof(DSFunction)) { node = new DSFunction(description); } RemapPorts(node, inPorts, outPorts, resolver); } else if (type == typeof(DSVarArgFunction)) { var functionId = Guid.Parse(obj["FunctionUuid"].Value <string>()); node = manager.CreateCustomNodeInstance(functionId); RemapPorts(node, inPorts, outPorts, resolver); } else if (type == typeof(Formula)) { node = (Formula)obj.ToObject(type); RemapPorts(node, inPorts, outPorts, resolver); } else { node = (NodeModel)obj.ToObject(type); } node.GUID = guid; node.NickName = displayName; //node.X = x; //node.Y = y; // Add references to the node and the ports to the reference resolver, // so that they are available for entities which are deserialized later. serializer.ReferenceResolver.AddReference(serializer.Context, node.GUID.ToString(), node); foreach (var p in node.InPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } foreach (var p in node.OutPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } return(node); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { NodeModel node = null; var obj = JObject.Load(reader); var type = Type.GetType(obj["$type"].Value <string>()); // If the id is not a guid, makes a guid based on the id of the node var guid = GuidUtility.tryParseOrCreateGuid(obj["Id"].Value <string>()); var replication = obj["Replication"].Value <string>(); var inPorts = obj["Inputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var outPorts = obj["Outputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var resolver = (IdReferenceResolver)serializer.ReferenceResolver; string assemblyLocation = objectType.Assembly.Location; bool remapPorts = true; if (type == null) { node = CreateDummyNode(obj, assemblyLocation, inPorts, outPorts); } else if (type == typeof(Function)) { var functionId = Guid.Parse(obj["FunctionSignature"].Value <string>()); CustomNodeDefinition def = null; CustomNodeInfo info = null; bool isUnresolved = !manager.TryGetCustomNodeData(functionId, null, false, out def, out info); Function function = manager.CreateCustomNodeInstance(functionId, null, false, def, info); node = function; if (isUnresolved) { function.UpdatePortsForUnresolved(inPorts, outPorts); } } else if (type == typeof(CodeBlockNodeModel)) { var code = obj["Code"].Value <string>(); node = new CodeBlockNodeModel(code, guid, 0.0, 0.0, libraryServices, ElementResolver); } else if (typeof(DSFunctionBase).IsAssignableFrom(type)) { var mangledName = obj["FunctionSignature"].Value <string>(); var functionDescriptor = libraryServices.GetFunctionDescriptor(mangledName); if (functionDescriptor == null) { node = CreateDummyNode(obj, assemblyLocation, inPorts, outPorts); } else { if (type == typeof(DSVarArgFunction)) { node = new DSVarArgFunction(functionDescriptor); // The node syncs with the function definition. // Then we need to make the inport count correct var varg = (DSVarArgFunction)node; varg.VarInputController.SetNumInputs(inPorts.Count()); } else if (type == typeof(DSFunction)) { node = new DSFunction(functionDescriptor); } } } else if (type == typeof(DSVarArgFunction)) { var functionId = Guid.Parse(obj["FunctionSignature"].Value <string>()); node = manager.CreateCustomNodeInstance(functionId); } else if (type.ToString() == "CoreNodeModels.Formula") { node = (NodeModel)obj.ToObject(type); } else { node = (NodeModel)obj.ToObject(type); // We don't need to remap ports for any nodes with json constructors which pass ports remapPorts = false; } if (remapPorts) { RemapPorts(node, inPorts, outPorts, resolver); } // Cannot set Lacing directly as property is protected node.UpdateValue(new UpdateValueParams("ArgumentLacing", replication)); node.GUID = guid; // Add references to the node and the ports to the reference resolver, // so that they are available for entities which are deserialized later. serializer.ReferenceResolver.AddReference(serializer.Context, node.GUID.ToString(), node); foreach (var p in node.InPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } foreach (var p in node.OutPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } return(node); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { NodeModel node = null; var obj = JObject.Load(reader); var type = Type.GetType(obj["$type"].Value <string>()); //if the id is not a guid, makes a guid based on the id of the node var guid = GuidUtility.tryParseOrCreateGuid(obj["Id"].Value <string>()); var replication = obj["Replication"].Value <string>(); var inPorts = obj["Inputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var outPorts = obj["Outputs"].ToArray().Select(t => t.ToObject <PortModel>()).ToArray(); var resolver = (IdReferenceResolver)serializer.ReferenceResolver; if (type == typeof(Function)) { var functionId = Guid.Parse(obj["FunctionUuid"].Value <string>()); node = manager.CreateCustomNodeInstance(functionId); RemapPorts(node, inPorts, outPorts, resolver); } else if (type == typeof(CodeBlockNodeModel)) { var code = obj["Code"].Value <string>(); node = new CodeBlockNodeModel(code, guid, 0.0, 0.0, libraryServices, ElementResolver); RemapPorts(node, inPorts, outPorts, resolver); } else if (typeof(DSFunctionBase).IsAssignableFrom(type)) { var mangledName = obj["FunctionSignature"].Value <string>(); var description = libraryServices.GetFunctionDescriptor(mangledName); if (type == typeof(DSVarArgFunction)) { node = new DSVarArgFunction(description); // The node syncs with the function definition. // Then we need to make the inport count correct var varg = (DSVarArgFunction)node; varg.VarInputController.SetNumInputs(inPorts.Count()); } else if (type == typeof(DSFunction)) { node = new DSFunction(description); } RemapPorts(node, inPorts, outPorts, resolver); } else if (type == typeof(DSVarArgFunction)) { var functionId = Guid.Parse(obj["FunctionUuid"].Value <string>()); node = manager.CreateCustomNodeInstance(functionId); RemapPorts(node, inPorts, outPorts, resolver); } else if (type.ToString() == "CoreNodeModels.Formula") { node = (NodeModel)obj.ToObject(type); RemapPorts(node, inPorts, outPorts, resolver); } else { node = (NodeModel)obj.ToObject(type); } //cannot set Lacing directly as property is protected node.UpdateValue(new UpdateValueParams("ArgumentLacing", replication)); node.GUID = guid; // Add references to the node and the ports to the reference resolver, // so that they are available for entities which are deserialized later. serializer.ReferenceResolver.AddReference(serializer.Context, node.GUID.ToString(), node); foreach (var p in node.InPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } foreach (var p in node.OutPorts) { serializer.ReferenceResolver.AddReference(serializer.Context, p.GUID.ToString(), p); } return(node); }