コード例 #1
0
ファイル: FileManagerXML.cs プロジェクト: 675492062/behaviac
        /// <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;
            }
        }