Example #1
0
        //This test builds a migration for a zero touch node from DynamoCore
        public void CanReadFileWithZeroTouchMigrationOfFunctionSignatureWithoutParameters()
        {
            string libraryPath = "DSCoreNodes.dll";
            string XmlPath     = "DSCoreNodes.Migrations.xml";
            string migrations  = "<?xml version=\"1.0\"?>" + System.Environment.NewLine +
                                 "<migrations>" + System.Environment.NewLine +
                                 "<priorNameHint>" + System.Environment.NewLine +
                                 "<oldName>DSCore.DateTime.Now</oldName>" + System.Environment.NewLine +
                                 "<newName>DSCore.DateTime.Never</newName>" + System.Environment.NewLine +
                                 "</priorNameHint>" + System.Environment.NewLine +
                                 "</migrations>" + System.Environment.NewLine;


            string xmlstring = @"<Dynamo.Nodes.DSFunction guid=""f05953f3-6ead-44f7-b872-1e0203c784cc""
            type=""Dynamo.Nodes.DSFunction"" nickname=""DateTime.Now"" x=""259.5"" y=""260.5"" 
            isVisible=""true"" isUpstreamVisible=""true"" lacing=""Shortest""
            isSelectedInput=""False"" assembly=""DSCoreNodes.dll"" function=""DSCore.DateTime.Now"" />";

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xmlstring);
            var xmlElement = doc.DocumentElement;

            string tempXMLPath     = Path.Combine(TempFolder, XmlPath);
            string tempLibraryPath = Path.Combine(TempFolder, libraryPath);

            System.IO.File.WriteAllText(tempXMLPath, migrations);

            libraryServices.LoadLibraryMigrations(tempLibraryPath);
            Assert.DoesNotThrow(() => { libraryServices.AddAdditionalAttributesToNode("DSCore.DateTime.Now", xmlElement); });
            Assert.DoesNotThrow(() => { libraryServices.AddAdditionalElementsToNode("DSCore.DateTime.Now", xmlElement); });
        }
Example #2
0
        public NodeModel CreateNodeFromXml(XmlElement nodeElement, SaveContext context, ElementResolver resolver)
        {
            string assembly = "";
            string function;
            var    nickname = nodeElement.Attributes["nickname"].Value;

            FunctionDescriptor descriptor;

            Trace.Assert(nodeElement.Attributes != null, "nodeElement.Attributes != null");

            if (nodeElement.Attributes["assembly"] == null)
            {
                assembly = DetermineAssemblyName(nodeElement);
                function = nickname.Replace(".get", ".");
            }
            else
            {
                string xmlSignature = nodeElement.Attributes["function"].Value;

                string hintedSigniture =
                    libraryServices.FunctionSignatureFromFunctionSignatureHint(xmlSignature);

                if (hintedSigniture != null)
                {
                    nodeElement.Attributes["nickname"].Value =
                        libraryServices.NicknameFromFunctionSignatureHint(xmlSignature);
                    function = hintedSigniture;

                    // if the node needs additional parameters, add them here
                    libraryServices.AddAdditionalAttributesToNode(xmlSignature, nodeElement);
                    libraryServices.AddAdditionalElementsToNode(xmlSignature, nodeElement);
                }
                else
                {
                    function = xmlSignature;
                }

                var xmlAttribute = nodeElement.Attributes["assembly"];
                if (xmlAttribute != null)
                {
                    assembly = Uri.UnescapeDataString(xmlAttribute.Value);
                }
            }

            if (context == SaveContext.File && !string.IsNullOrEmpty(assembly))
            {
                var document = nodeElement.OwnerDocument;
                var docPath  = Nodes.Utilities.GetDocumentXmlPath(document);
                assembly = Nodes.Utilities.MakeAbsolutePath(docPath, assembly);

                if (libraryServices.IsLibraryLoaded(assembly))
                {
                    descriptor = libraryServices.GetFunctionDescriptor(assembly, function);
                }
                else
                {
                    // If the desired assembly is not loaded already. Check if it belongs to BuiltInFunctionGroup.
                    if (libraryServices.IsFunctionBuiltIn(assembly, nickname))
                    {
                        descriptor = libraryServices.GetFunctionDescriptor(function);
                    }
                    else
                    {
                        // If neither of these, Dynamo need to import the library.
                        try
                        {
                            libraryServices.ImportLibrary(assembly);
                            descriptor = libraryServices.GetFunctionDescriptor(assembly, function);
                        }
                        catch (LibraryLoadFailedException)
                        {
                            descriptor = libraryServices.GetFunctionDescriptor(function);
                        }
                    }
                }
            }
            else
            {
                descriptor = libraryServices.GetFunctionDescriptor(function);
            }

            if (null == descriptor)
            {
                var inputcount = DetermineFunctionInputCount(nodeElement);

                return(new DummyNode(
                           inputcount,
                           1,
                           nickname,
                           nodeElement,
                           assembly,
                           DummyNode.Nature.Unresolved));
            }

            DSFunctionBase result;

            if (descriptor.IsVarArg)
            {
                result = new DSVarArgFunction(descriptor);

                var akas = typeof(DSVarArgFunction).GetCustomAttribute <AlsoKnownAsAttribute>().Values;
                if (nodeElement.Name != typeof(DSVarArgFunction).FullName &&
                    akas.All(aka => aka != nodeElement.Name))
                {
                    VariableInputNodeController.SerializeInputCount(
                        nodeElement,
                        descriptor.Parameters.Count());
                }
            }
            else
            {
                result = new DSFunction(descriptor);
            }

            result.Deserialize(nodeElement, context);

            // In case of input parameters mismatch, use default arguments for parameters that have one
            if (!descriptor.MangledName.EndsWith(function))
            {
                string[] oldSignature = function.Split('@');
                string[] inputTypes = oldSignature.Length > 1 ? oldSignature[1].Split(',') : new string[] {};
                int      i = 0, j = 0;
                foreach (var param in descriptor.InputParameters)
                {
                    if (i >= inputTypes.Length || param.Item2 != inputTypes[i])
                    {
                        result.InPorts[j].UsingDefaultValue = result.InPorts[j].DefaultValue != null;
                    }
                    else
                    {
                        i++;
                    }
                    j++;
                }
            }

            return(result);
        }
        public NodeModel CreateNodeFromXml(XmlElement nodeElement, SaveContext context)
        {
            string assembly = "";
            string function;
            var    nickname = nodeElement.Attributes["nickname"].Value;

            FunctionDescriptor descriptor;

            Trace.Assert(nodeElement.Attributes != null, "nodeElement.Attributes != null");

            if (nodeElement.Attributes["assembly"] == null)
            {
                assembly = DetermineAssemblyName(nodeElement);
                function = nickname.Replace(".get", ".");
            }
            else
            {
                string xmlSignature = nodeElement.Attributes["function"].Value;

                string hintedSigniture =
                    libraryServices.FunctionSignatureFromFunctionSignatureHint(xmlSignature);

                if (hintedSigniture != null)
                {
                    nodeElement.Attributes["nickname"].Value =
                        libraryServices.NicknameFromFunctionSignatureHint(xmlSignature);
                    function = hintedSigniture;

                    // if the node needs additional parameters, add them here
                    if (libraryServices.FunctionSignatureNeedsAdditionalAttributes(xmlSignature))
                    {
                        libraryServices.AddAdditionalAttributesToNode(xmlSignature, nodeElement);
                    }

                    if (libraryServices.FunctionSignatureNeedsAdditionalElements(xmlSignature))
                    {
                        libraryServices.AddAdditionalElementsToNode(xmlSignature, nodeElement);
                    }
                }
                else
                {
                    function = xmlSignature;
                }

                var xmlAttribute = nodeElement.Attributes["assembly"];
                if (xmlAttribute != null)
                {
                    assembly = Uri.UnescapeDataString(xmlAttribute.Value);
                }
            }

            if (context == SaveContext.File && !string.IsNullOrEmpty(assembly))
            {
                var document = nodeElement.OwnerDocument;
                var docPath  = Nodes.Utilities.GetDocumentXmlPath(document);
                assembly = Nodes.Utilities.MakeAbsolutePath(docPath, assembly);

                descriptor = libraryServices.IsLibraryLoaded(assembly) || libraryServices.ImportLibrary(assembly)
                    ? libraryServices.GetFunctionDescriptor(assembly, function)
                    : libraryServices.GetFunctionDescriptor(function);
            }
            else
            {
                descriptor = libraryServices.GetFunctionDescriptor(function);
            }

            if (null == descriptor)
            {
                var inputcount = DetermineFunctionInputCount(nodeElement);

                return(new DummyNode(
                           inputcount,
                           1,
                           nickname,
                           nodeElement,
                           assembly,
                           DummyNode.Nature.Unresolved));
            }

            DSFunctionBase result;

            if (descriptor.IsVarArg)
            {
                result = new DSVarArgFunction(descriptor);
                if (nodeElement.Name != typeof(DSVarArgFunction).FullName)
                {
                    VariableInputNodeController.SerializeInputCount(
                        nodeElement,
                        descriptor.Parameters.Count());
                }
            }
            else
            {
                result = new DSFunction(descriptor);
            }

            result.Deserialize(nodeElement, context);
            return(result);
        }