Example #1
0
        /// <summary>
        /// Saves a node to the XML file.
        /// </summary>
        /// <param name="root">The XML node we want to attach the node to.</param>
        /// <param name="node">The node we want to save.</param>
        protected void SaveNode(XmlElement root, Node node) {
            try {
                // allow the node to process its attributes in preparation of the save
                node.PreSave(_behavior);

                // store the class we have to create when loading
                XmlElement elem = _xmlfile.CreateElement("Node");
                elem.SetAttribute("Class", node.GetType().FullName);

                bool isPrefabInstance = !string.IsNullOrEmpty(node.PrefabName) && !node.IsPrefabDataDirty();

                // save attributes
                IList<DesignerPropertyInfo> properties = node.GetDesignerProperties();

                for (int p = 0; p < properties.Count; ++p) {
                    if (!properties[p].Attribute.HasFlags(DesignerProperty.DesignerFlags.NoSave)) {
                        bool bDo = !isPrefabInstance || properties[p].Attribute.HasFlags(DesignerProperty.DesignerFlags.NotPrefabRelated);

                        if (bDo) {
                            if (bDo) {
                                elem.SetAttribute(properties[p].Property.Name, properties[p].GetSaveValue(node));
                            }
                        }
                    }
                }

                // append node to root
                root.AppendChild(elem);

                // save comment
                if (node.CommentObject != null) {
                    XmlElement comment = _xmlfile.CreateElement("Comment");

                    properties = node.CommentObject.GetDesignerProperties();

                    for (int p = 0; p < properties.Count; ++p) {
                        if (!properties[p].Attribute.HasFlags(DesignerProperty.DesignerFlags.NoSave))
                        { comment.SetAttribute(properties[p].Property.Name, properties[p].GetSaveValue(node.CommentObject)); }
                    }

                    elem.AppendChild(comment);
                }

                if (!isPrefabInstance) {
                    // save parameters
                    if (node.LocalVars != null) {
                        Debug.Check(node is Behavior || node is ReferencedBehavior) ;
                        SaveParameters(elem, node.LocalVars);
                    }

                    // save DescriptorRefs
                    if (node is Behavior) {
#if QUERY_EANBLED
                        Behavior b = node as Behavior;
                        SaveDescriptorRefs(elem, b);
#endif//#if QUERY_EANBLED
                    }

                    // save attachments
                    foreach(Attachments.Attachment attach in node.Attachments) {
                        XmlElement attelem = _xmlfile.CreateElement("Attachment");
                        attelem.SetAttribute("Class", attach.GetType().FullName);

                        // save attributes
                        properties = attach.GetDesignerProperties();

                        for (int p = 0; p < properties.Count; ++p) {
                            if (!properties[p].Attribute.HasFlags(DesignerProperty.DesignerFlags.NoSave))
                            { attelem.SetAttribute(properties[p].Property.Name, properties[p].GetSaveValue(attach)); }
                        }

                        if (attach.LocalVars != null && attach.LocalVars.Count > 0) {
                            Debug.Check(attach is Attachments.Event);
                            SaveParameters(attelem, attach.LocalVars);
                        }

                        elem.AppendChild(attelem);
                    }

                    // save children if allowed. Disallowed for referenced behaviours.
                    if (node.SaveChildren) {
                        // save connectors
                        foreach(Nodes.Node.Connector connector in node.Connectors) {
                            // if we have no children to store we can skip the connector
                            if (connector.ChildCount < 1)
                            { continue; }

                            XmlElement conn = _xmlfile.CreateElement("Connector");
                            conn.SetAttribute("Identifier", connector.Identifier);
                            elem.AppendChild(conn);

                            // save their children
                            for (int i = 0; i < connector.ChildCount; ++i)
                            { SaveNode(conn, (Node)connector.GetChild(i)); }
                        }

                        // save fsm nodes
                        if (node.FSMNodes.Count > 0) {
                            XmlElement fsmNodesElement = _xmlfile.CreateElement("FSMNodes");
                            fsmNodesElement.SetAttribute("ScreenLocationX", node.ScreenLocation.X.ToString());
                            fsmNodesElement.SetAttribute("ScreenLocationY", node.ScreenLocation.Y.ToString());
                            elem.AppendChild(fsmNodesElement);

                            foreach(Nodes.Node fsmNode in node.FSMNodes) {
                                SaveNode(fsmNodesElement, fsmNode);
                            }
                        }
                    }
                }

            } catch (Exception e) {
                MessageBox.Show(e.Message, Resources.SaveError, MessageBoxButtons.OK);

                throw;
            }
        }
Example #2
0
        //if there is a 'Predicate' attachment, convert it to a Condition node and attach it to the '_custom_condition' connector.
        private void AutoRestruct(List <Node.ErrorCheck> result, int version, Behaviac.Design.Attachments.Attachment a, Node node)
        {
            if (version <= 1)
            {
                string attachClass = a.GetType().FullName;
                if (attachClass.IndexOf("PluginBehaviac.Events.Predicate") >= 0)
                {
                    DesignerPropertyInfo propInfo = DesignerProperty.GetDesignerProperty(a.GetType(), "Opl");
                    RightValueDef        opl      = propInfo.GetValue(a) as RightValueDef;
                    propInfo = DesignerProperty.GetDesignerProperty(a.GetType(), "Opr");
                    RightValueDef opr = propInfo.GetValue(a) as RightValueDef;
                    propInfo = DesignerProperty.GetDesignerProperty(a.GetType(), "Operator");
                    OperatorType  oprr    = (OperatorType)propInfo.GetValue(a);
                    OperatorTypes oprType = (OperatorTypes)((int)OperatorTypes.Equal - (int)OperatorType.Equal + (int)oprr);
                    propInfo = DesignerProperty.GetDesignerProperty(a.GetType(), "BinaryOperator");
                    Behaviac.Design.Attachments.BinaryOperator binaryOpr = (Behaviac.Design.Attachments.BinaryOperator)propInfo.GetValue(a);

                    string clss      = node.GetType().FullName;
                    bool   bIsSeqSel = (node.GetType().IsSubclassOf(typeof(Sequence)) ||
                                        node.GetType().IsSubclassOf(typeof(Selector)));

                    bool bCare = (bIsSeqSel ||
                                  node.GetType().IsSubclassOf(typeof(Impulse))
                                  );

                    if (bCare ||
                        clss == "PluginBehaviac.Nodes.Query" ||
                        clss == "PluginBehaviac.Nodes.DecoratorCountLimit")
                    {
                        node.RemoveAttachment(a);
                        node.Behavior.TriggerWasModified(node);

                        Type newType = Plugin.GetType("PluginBehaviac.Nodes.Condition");

                        Behaviac.Design.Nodes.Node           newNode   = Behaviac.Design.Nodes.Node.Create(newType);
                        Behaviac.Design.Nodes.Node.Connector connector = node.GetConnector(Node.Connector.kInterupt);

                        if (connector != null && connector.Identifier == Node.Connector.kInterupt && connector.ChildCount > 0)
                        {
                            //it has multiple Predicates, so insert all of them to a newly created Sequence
                            Node oldOne = (Node)connector.GetChild(0);
                            if (oldOne.GetType().IsSubclassOf(typeof(Condition)))
                            {
                                AddAfterConditions(node, binaryOpr, newNode, connector, oldOne);
                            }
                            else
                            {
                                if (bIsSeqSel)
                                {
                                    Debug.Check(oldOne.GetType().IsSubclassOf(typeof(Decorator)));
                                    Decorator d = oldOne as Decorator;
                                    node      = oldOne;
                                    connector = node.GetConnector(BaseNode.Connector.kGeneric);
                                    oldOne    = (Node)d.Children[0];
                                }

                                if (oldOne.GetType() == typeof(PluginBehaviac.Nodes.And))
                                {
                                    if (binaryOpr == Behaviac.Design.Attachments.BinaryOperator.Or)
                                    {
                                        node.RemoveChild(connector, oldOne);
                                        Type selType1 = Plugin.GetType("PluginBehaviac.Nodes.Or");

                                        Behaviac.Design.Nodes.Node sel = Behaviac.Design.Nodes.Node.Create(selType1);
                                        sel.AddChild(BaseNode.Connector.kGeneric, oldOne);
                                        sel.AddChild(BaseNode.Connector.kGeneric, newNode);

                                        node.AddChild(BaseNode.Connector.kInterupt, sel);
                                    }
                                    else
                                    {
                                        oldOne.AddChild(BaseNode.Connector.kGeneric, newNode);
                                    }
                                }
                                else if (oldOne.GetType() == typeof(PluginBehaviac.Nodes.Or))
                                {
                                    if (binaryOpr == Behaviac.Design.Attachments.BinaryOperator.And)
                                    {
                                        node.RemoveChild(connector, oldOne);
                                        Type selType1 = Plugin.GetType("PluginBehaviac.Nodes.And");

                                        Behaviac.Design.Nodes.Node sel = Behaviac.Design.Nodes.Node.Create(selType1);
                                        sel.AddChild(BaseNode.Connector.kGeneric, oldOne);
                                        sel.AddChild(BaseNode.Connector.kGeneric, newNode);

                                        node.AddChild(BaseNode.Connector.kInterupt, sel);
                                    }
                                    else
                                    {
                                        oldOne.AddChild(BaseNode.Connector.kGeneric, newNode);
                                    }
                                }
                                else if (oldOne.GetType().IsSubclassOf(typeof(Condition)))
                                {
                                    AddAfterConditions(node, binaryOpr, newNode, connector, oldOne);
                                }
                                else
                                {
                                    Debug.Check(false);
                                }
                            }
                        }
                        else
                        {
                            //the first condition
                            Behaviac.Design.Nodes.Node notNode = null;
                            if (bIsSeqSel)
                            {
                                //for sequence/selector, it is reverted
                                Type notType = Plugin.GetType("PluginBehaviac.Nodes.DecoratorNot");

                                notNode = Behaviac.Design.Nodes.Node.Create(notType);
                                node.AddChild(BaseNode.Connector.kInterupt, notNode);
                                notNode.AddChild(BaseNode.Connector.kGeneric, newNode);
                            }
                            else
                            {
                                node.AddChild(BaseNode.Connector.kInterupt, newNode);
                            }
                        }

                        // initialise the attachments properties
                        IList <DesignerPropertyInfo> lp = newNode.GetDesignerProperties();
                        for (int p = 0; p < lp.Count; ++p)
                        {
                            if (lp[p].Property.Name == "Opl")
                            {
                                lp[p].Property.SetValue(newNode, opl, null);
                            }
                            else if (lp[p].Property.Name == "Opr")
                            {
                                lp[p].Property.SetValue(newNode, opr, null);
                            }
                            else if (lp[p].Property.Name == "Operator")
                            {
                                lp[p].Property.SetValue(newNode, oprr, null);
                            }
                        }

                        // update attacheent with attributes
                        newNode.OnPropertyValueChanged(false);
                    }
                    else if (clss == "PluginBehaviac.Nodes.Action")
                    {
                        Type newType = Plugin.GetType("PluginBehaviac.Events.Precondition");

                        Behaviac.Design.Attachments.Attachment newNode = Behaviac.Design.Attachments.Attachment.Create(newType, node);
                        node.AddAttachment(newNode);
                        node.RemoveAttachment(a);
                        node.Behavior.TriggerWasModified(node);

                        // initialise the attachments properties
                        IList <DesignerPropertyInfo> lp = newNode.GetDesignerProperties();
                        for (int p = 0; p < lp.Count; ++p)
                        {
                            if (lp[p].Property.Name == "BinaryOperator")
                            {
                                lp[p].Property.SetValue(newNode, binaryOpr, null);
                            }
                            else if (lp[p].Property.Name == "Opl")
                            {
                                lp[p].Property.SetValue(newNode, opl, null);
                            }
                            else if (lp[p].Property.Name == "Opr2")
                            {
                                lp[p].Property.SetValue(newNode, opr, null);
                            }
                            else if (lp[p].Property.Name == "Operator")
                            {
                                lp[p].Property.SetValue(newNode, oprType, null);
                            }
                            else if (lp[p].Property.Name == "IsAlive")
                            {
                                lp[p].SetValueFromString(result, newNode, "true");
                            }
                        }

                        // update attacheent with attributes
                        newNode.OnPropertyValueChanged(false);
                    }
                    else
                    {
                        Debug.Check(false);
                    }
                }
            } // if (version <= 1)
        }
Example #3
0
        /// <summary>
        /// Exports all the properties of a ode and assigns them.
        /// </summary>
        /// <param name="file">The file we are exporting to.</param>
        /// <param name="nodeName">The name of the node we are setting the properties for.</param>
        /// <param name="node">The node whose properties we are exporting.</param>
        /// <param name="indent">The indent for the currently generated code.</param>
        private void ExportProperties(BsonSerializer file, Node n) {
            IList<DesignerPropertyInfo> properties = n.GetDesignerProperties();

            file.WriteStartElement("properties");

            foreach(DesignerPropertyInfo p in properties) {
                // we skip properties which are not marked to be exported
                if (p.Attribute.HasFlags(DesignerProperty.DesignerFlags.NoExport))
                { continue; }

                object v = p.Property.GetValue(n, null);
                bool bExport = !Plugin.IsExportArray(v); ;

                if (bExport) {

                    // create the code which assigns the value to the node's property
                    //file.Write(string.Format("{0}\t{1}.{2} = {3};\r\n", indent, nodeName, properties[p].Property.Name, properties[p].GetExportValue(node)));
                    string propValue = p.GetExportValue(n);

                    if (propValue != string.Empty && propValue != "\"\"") {
                        WriteProperty(file, p, n);
                    }
                }
            }

            if (n is Task) {
                Task task = n as Task;

                file.WriteAttributeString("IsHTN", task.IsHTN ? "true" : "false");
            }

#if QUERY_EANBLED
            Behavior b = n as Behavior;

            if (b != null) {
                this.ExportDescritorRefs(file, b);
            }

#endif

            file.WriteEndElement();
        }
Example #4
0
        private void setSubItems(Node node)
        {
            // Add all listed properties
            IList<DesignerPropertyInfo> properties = node.GetDesignerProperties(DesignerProperty.SortByDisplayOrder);

            for (int p = 0; p < properties.Count; ++p) {
            DesignerProperty att = properties[p].Attribute;

            if (att.Display == DesignerProperty.DisplayMode.List) {
                object pValue = properties[p].Property.GetValue(node, null);
                bool bDo = pValue != null;

                if (bDo) {
                    AddSubItem(new SubItemProperty(node, properties[p].Property, att));
                }
            }
            }

            // Add all attachments
            foreach(Attachment attach in node.Attachments) {
            AddSubItem(attach.CreateSubItem());
            }
        }
Example #5
0
        /// <summary>
        /// Exports all the properties of a ode and assigns them.
        /// </summary>
        /// <param name="file">The file we are exporting to.</param>
        /// <param name="nodeName">The name of the node we are setting the properties for.</param>
        /// <param name="node">The node whose properties we are exporting.</param>
        /// <param name="indent">The indent for the currently generated code.</param>
        private void ExportProperties(BsonSerializer file, Node n)
        {
            IList<DesignerPropertyInfo> properties = n.GetDesignerProperties();

            file.WriteStartElement("properties");

            foreach (DesignerPropertyInfo p in properties)
            {
                // we skip properties which are not marked to be exported
                if (p.Attribute.HasFlags(DesignerProperty.DesignerFlags.NoExport))
                    continue;

                bool bDo = true;
                if (p.Attribute.HasFlags(DesignerProperty.DesignerFlags.BeValid))
                {
                    object obj = p.Property.GetValue(n, null);

                    if (obj == null || obj.ToString() == "null_method")
                    {
                        bDo = false;
                    }
                }

                if (bDo)
                {

                    // create the code which assigns the value to the node's property
                    //file.Write(string.Format("{0}\t{1}.{2} = {3};\r\n", indent, nodeName, properties[p].Property.Name, properties[p].GetExportValue(node)));
                    string propValue = p.GetExportValue(n);
                    if (propValue != string.Empty && propValue != "\"\"")
                    {
                        WriteProperty(file, p, n);
                    }
                }
            }

            #if QUERY_EANBLED
            Behavior b = n as Behavior;
            if (b != null)
            {
                this.ExportDescritorRefs(file, b);
            }
            #endif

            file.WriteEndElement();
        }
Example #6
0
        private void setSubItems(Node node)
        {
            // Add all listed properties
            IList<DesignerPropertyInfo> properties = node.GetDesignerProperties(DesignerProperty.SortByDisplayOrder);
            for (int p = 0; p < properties.Count; ++p)
            {
                DesignerProperty att = properties[p].Attribute;

                if (att.Display == DesignerProperty.DisplayMode.List)
                {
                    bool bDo = true;
                    if (att.HasFlags(DesignerProperty.DesignerFlags.BeValid))
                    {
                        object obj = properties[p].Property.GetValue(node, null);

                        if (obj == null || obj.ToString() == "null_method")
                        {
                            bDo = false;
                        }
                    }

                    if (bDo)
                    {
                        AddSubItem(new SubItemProperty(node, properties[p].Property, att));
                    }
                }
            }

            // Add all attachments
            foreach (Attachment attach in node.Attachments)
            {
                AddSubItem(attach.CreateSubItem());
            }
        }