Exemplo n.º 1
0
        static void AssignGraphAttr(BaseGraph graph, BaseGraphCommand command, string inputCommand)
        {
            var field = graph.GetType().GetField(command.graphFieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            if (field == null)
            {
                throw new InvalidOperationException("Can't assign graph field '" + command.graphFieldName + "': not found");
            }

            field.SetValue(graph, command.graphFieldValue);
        }
Exemplo n.º 2
0
        public static void Execute(BaseGraph graph, string inputCommand)
        {
            BaseGraphCommand command = Parse(inputCommand);

            if (commandTypeFunctions.ContainsKey(command.type))
            {
                commandTypeFunctions[command.type](graph, command, inputCommand);
            }
            else
            {
                throw new InvalidOperationException("Command type not handled: " + command.type);
            }
        }
Exemplo n.º 3
0
        static void GetNodes(BaseGraph graph, BaseGraphCommand command, out BaseNode fromNode, out BaseNode toNode, string inputCommand)
        {
            fromNode = graph.FindNodeByName(command.fromNodeName);
            toNode   = graph.FindNodeByName(command.toNodeName);

            if (fromNode == null)
            {
                throw new InvalidOperationException("Node " + command.fromNodeName + " not found in graph while parsing: '" + inputCommand + "'");
            }
            if (toNode == null)
            {
                throw new InvalidOperationException("Node " + command.toNodeName + " not found in graph while parsing: '" + inputCommand + "'");
            }
        }
Exemplo n.º 4
0
        static void     CreateNode(BaseGraph graph, BaseGraphCommand command, string inputCommand)
        {
            Vector2  position = command.position;
            BaseNode node     = null;

            //if we receive a CreateNode with input/output graph nodes, we assign them so we don't have multiple inout/output nodes
            if (NodeTypeProvider.inputGraphTypes.Contains(command.nodeType))
            {
                node = graph.inputNode;
            }
            else if (NodeTypeProvider.outputGraphTypes.Contains(command.nodeType))
            {
                node = graph.outputNode;
            }
            else
            {
                node = graph.CreateNewNode(command.nodeType, position);
            }

            //set the position again for input/output nodes
            node.rect.position = position;

            Type nodeType = node.GetType();

            if (!String.IsNullOrEmpty(command.attributes))
            {
                foreach (var attr in Jsonizer.Parse(command.attributes))
                {
                    FieldInfo attrField = nodeType.GetField(attr.first, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

                    if (attrField != null)
                    {
                        attrField.SetValue(node, attr.second);
                    }
                    else
                    {
                        Debug.LogError("Attribute " + attr.first + " can be found in node " + node);
                    }
                }
            }

            node.name = command.name;
        }
        public void     ProcessOnce(BaseGraph graph)
        {
            if (graph.GetComputeSortedNodes() == null)
            {
                graph.UpdateComputeOrder();
            }

            currentGraph = graph;

            foreach (var node in graph.GetComputeSortedNodes())
            {
                //ignore unlinked nodes
                if (node.computeOrder < 0)
                {
                    continue;
                }

                node.OnNodeProcessOnce();

                ProcessNodeLinks(node, graph.IsRealMode());
            }
        }
        public float ProcessNodes(BaseGraph graph, List <BaseNode> nodes)
        {
            float calculTime = 0f;
            bool  realMode   = graph.IsRealMode();

            currentGraph = graph;

            //sort nodes by compute order:
            nodes.Sort((n1, n2) => n1.computeOrder.CompareTo(n2.computeOrder));

            foreach (var node in nodes)
            {
                if (node.computeOrder < 0)
                {
                    continue;
                }

                calculTime += ProcessNode(node, realMode);
            }

            return(calculTime);
        }
        //Durty Clone cauz unity ScriptableObject does not implement a Clone method >.<
        public BaseGraph Clone()
        {
            //Instancing a new graph fmor this one will duplicate the object but not the nodes
            // so the OnEnable function will bind this new graph to our nodes, to revert
            // that we need to clone each nodes and assign them to this new graph.
            BaseGraph clonedGraph = Object.Instantiate(this);

            clonedGraph.name += "(Clone)";

            //clean and add copies of nodes into the cloned graph
            clonedGraph.nodes.Clear();
            foreach (var node in nodes)
            {
                clonedGraph.nodes.Add(Object.Instantiate(node));
            }

            clonedGraph.inputNode = Object.Instantiate(inputNode);
            clonedGraph.AddInitializedNode(clonedGraph.inputNode, true, false);

            clonedGraph.outputNode = Object.Instantiate(outputNode);
            clonedGraph.AddInitializedNode(clonedGraph.outputNode, true, false);

            //reenable the new graph so the new nodes are taken in account
            clonedGraph.OnDisable();
            clonedGraph.OnEnable();

            //reenable all cloned nodes
            foreach (var node in clonedGraph.allNodes)
            {
                node.OnDisable();
                node.OnEnable();
            }

            //reenable our graph to rebind our nodes to our graph
            OnDisable();
            OnEnable();

            return(clonedGraph);
        }
Exemplo n.º 8
0
        public static void Import(BaseGraph graph, string filePath, bool wipeDatas = false)
        {
            if (wipeDatas)
            {
                while (graph.nodes.Count != 0)
                {
                    Debug.Log("removing node: " + graph.nodes.First());
                    graph.RemoveNode(graph.nodes.First());
                }
            }

            string[] commands = File.ReadAllLines(filePath);
            foreach (var command in commands)
            {
                //ignore empty lines:
                if (String.IsNullOrEmpty(command.Trim()))
                {
                    continue;
                }

                Execute(graph, command);
            }
        }
Exemplo n.º 9
0
        static void CreateLinkAnchorName(BaseGraph graph, BaseGraphCommand command, string inputCommand)
        {
            BaseNode fromNode, toNode;

            GetNodes(graph, command, out fromNode, out toNode, inputCommand);

            var fromAnchorField = fromNode.outputAnchorFields.Find(af => af.fieldName == command.fromAnchorFieldName);
            var toAnchorField   = toNode.inputAnchorFields.Find(af => af.fieldName == command.toAnchorFieldName);

            if (fromAnchorField == null)
            {
                throw new InvalidOperationException("Anchor " + command.fromAnchorFieldName + " not found in node: " + fromNode);
            }
            if (toAnchorField == null)
            {
                throw new InvalidOperationException("Anchor " + command.toAnchorFieldName + " not found in node: " + toNode);
            }

            Anchor fromAnchor, toAnchor;

            FindAnchors(fromAnchorField, toAnchorField, out fromAnchor, out toAnchor);

            graph.SafeCreateLink(fromAnchor, toAnchor, false);
        }
Exemplo n.º 10
0
        public static void Export(BaseGraph graph, string filePath)
        {
            List <string> commands      = new List <string>();
            List <string> nodeNames     = new List <string>();
            var           nodeToNameMap = new Dictionary <BaseNode, string>();

            //GraphAttr commands
            var fields = graph.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            foreach (var field in fields)
            {
                var attrs = field.GetCustomAttributes(true);

                if (attrs.Any(a => a is TextSerializeField))
                {
                    string s = GenerateGraphAttributeCommand(field.Name, field.GetValue(graph));

                    if (s != null)
                    {
                        commands.Add(s);
                    }
                }
            }

            //CreateNode commands
            foreach (var node in graph.allNodes)
            {
                var attrs = new BaseGraphCLIAttributes();

                //load node attributes
                FieldInfo[] attrFields = node.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                foreach (var field in attrFields)
                {
                    var attributes = field.GetCustomAttributes(false);

                    bool isInput = attributes.Any(a => {
                        return(a.GetType() == typeof(InputAttribute));
                    });

                    if (isInput)
                    {
                        continue;
                    }

                    //if the field can't be jsonified, we skip it
                    if (Jsonizer.allowedJsonTypes.FindIndex(j => j.type == field.FieldType) == -1)
                    {
                        continue;
                    }

                    attrs.Add(field.Name, field.GetValue(node));
                }

                string nodeName = node.name;
                int    i        = 0;

                //unique name generation
                while (nodeNames.Contains(nodeName))
                {
                    nodeName = node.name + i++;
                }

                nodeNames.Add(nodeName);
                nodeToNameMap[node] = nodeName;

                commands.Add(GenerateNewNodeCommand(node.GetType(), nodeName, node.rect.position, attrs));
            }

            //Link commands
            foreach (var link in graph.nodeLinkTable.GetLinks())
            {
                if (link.fromNode == null || link.toNode == null)
                {
                    continue;
                }

                var fromName       = nodeToNameMap[link.fromNode];
                var toName         = nodeToNameMap[link.toNode];
                var fromAnchorName = link.fromAnchor.fieldName;
                var toAnchorName   = link.toAnchor.fieldName;

                commands.Add(GenerateLinkAnchorNameCommand(fromName, fromAnchorName, toName, toAnchorName));
            }

            File.WriteAllLines(filePath, commands.ToArray());
        }