//R. Judge: store item on MissionNode with Missionstatement <-- WTF? /// <summary> /// Create and return new MissionNode from an XmlNode. Will set type appropriately. /// </summary> public static MissionNode NewFromXML(XmlNode item) { MissionNode mn = null; switch (item.Name) { case "event": mn = new MissionNode_Event(); break; case "disabled_event": mn = new MissionNode_Event(); break; case "start": mn = new MissionNode_Start(); break; case "#comment": mn = new MissionNode_Comment(); break; case "folder_arme": mn = new MissionNode_Folder(); break; default: mn = new MissionNode_Unknown(); break; } mn.FromXml(item); return(mn); }
/// <summary> /// Clear mission contents and restore it to "New" state /// </summary> /// <param name="force">If set, ignore pending changes.</param> public void New(bool force = false) { if (!force && ChangesPending) { DialogResult result = MessageBox.Show("Do you want to save unsaved changes?", "Artemis Mission Editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Cancel) return; if (result == DialogResult.Yes) if (!Save()) return; } Clear(); TreeNode newTNode = new TreeNode(); MissionNode_Start newMNode = new MissionNode_Start(); string xml = "<root>" + Settings.Current.NewMissionStartBlock + "</root>"; XmlDocument xDoc = new XmlDocument(); try { xDoc.LoadXml(xml); foreach (XmlNode node in xDoc.ChildNodes[0].ChildNodes) { newMNode.Actions.Add(MissionStatement.NewFromXML(node, newMNode)); } } catch (Exception ex) { Log.Add("Problems while trying to parse new mission start node text:"); Log.Add(ex); } newTNode.Text = newMNode.Name; newTNode.ImageIndex = newMNode.ImageIndex; newTNode.SelectedImageIndex = newMNode.ImageIndex; newTNode.Tag = newMNode; TreeViewNodes.Nodes.Add(newTNode); _startNode = newTNode; FlowLayoutPanel_Clear(); TreeViewNodes.SelectedNode = newTNode; BackgroundNode = newTNode; TruncateUndoStack(0); }
/// <summary> /// Read mission contents from Xml string /// </summary> /// <param name="text">String containing Xml with the mission</param> /// <param name="supressLoadingSignal">Add no loading warnings to the log</param> /// <param name="fromState">True if clearing in order to restore from state, in this case we do not clear file path</param> /// <returns>Success or failure flag</returns> public bool FromXml(string text, bool supressLoadingSignal = false, bool fromState = false) { XmlDocument xDoc = new XmlDocument(); try { xDoc.LoadXml(text); } catch (Exception ex) { MessageBox.Show(ex.StackTrace, ex.Message); Log.Add("Loading mission from Xml failed with the following exception: "); Log.Add(ex); return false; } Clear(fromState); Loading = !supressLoadingSignal; BeginUpdate(); List<TreeNode> NodesToExpand = new List<TreeNode>(); Guid? bgGuid = null; XmlNode root = null; int i; for (i = 0; i < xDoc.ChildNodes.Count; i++) { XmlNode item = xDoc.ChildNodes[i]; if (item.GetType() == typeof(XmlComment)) if (root == null) CommentsBeforeRoot += item.Value + "\r\n"; else CommentsAfterRoot += item.Value + "\r\n"; else root = item; } if (CommentsBeforeRoot.Length >= 2 && CommentsBeforeRoot.Substring(CommentsBeforeRoot.Length - 2, 2) == "\r\n") CommentsBeforeRoot.Substring(0, CommentsBeforeRoot.Length - 2); if (CommentsAfterRoot.Length>=2 && CommentsAfterRoot.Substring(CommentsAfterRoot.Length - 2, 2) == "\r\n") CommentsAfterRoot.Substring(0, CommentsAfterRoot.Length - 2); if (root == null) { Log.Add("No root Xml node found in specified Xml file. Mission was not loaded"); Loading = false; EndUpdate(); return false; } foreach (XmlAttribute att in root.Attributes) { Guid tmp; if (att.Name == "background_id_arme") if (Guid.TryParse(att.Value, out tmp)) bgGuid = tmp; if (att.Name == "playerShipNames_arme") PlayerShipNames = Helper.StringArrayFromLine(att.Value); if (att.Name == "version") VersionNumber = att.Value; } foreach (XmlNode item in root.ChildNodes) { TreeNode newNode = new TreeNode(); MissionNode newMissionNode = MissionNode.NewFromXML(item); newNode.Text = newMissionNode.Name; newNode.Tag = newMissionNode; newNode.ImageIndex = newMissionNode.ImageIndex; newNode.SelectedImageIndex = newMissionNode.ImageIndex; if (newMissionNode is MissionNode_Event) EventCount++; TreeNode parentNode = newMissionNode.ParentID == null ? null : TreeViewNodes.FindNode((TreeNode ii) => ((MissionNode)ii.Tag).ID == newMissionNode.ParentID); if (parentNode != null) parentNode.Nodes.Add(newNode); else TreeViewNodes.Nodes.Add(newNode); if (newMissionNode.ExtraAttributes.Contains("expanded_arme")) NodesToExpand.Add(newNode); //If background id matches then remember this node if (newMissionNode.ID == bgGuid) BackgroundNode = newNode; } _startNode = TreeViewNodes.FindNode((TreeNode ii) => ii.Tag != null && ii.Tag.GetType() == typeof(MissionNode_Start)); if (_startNode == null) { Log.Add("No start node found in the mission! Adding blank start node to the beginning of the mission"); TreeNode newTNode = new TreeNode(); MissionNode_Start newMNode = new MissionNode_Start(); newTNode.Text = newMNode.Name; newTNode.ImageIndex = newMNode.ImageIndex; newTNode.SelectedImageIndex = newMNode.ImageIndex; newTNode.Tag = newMNode; TreeViewNodes.Nodes.Insert(0, newTNode); _startNode = newTNode; } if (BackgroundNode == null) BackgroundNode = _startNode; EndUpdate(); Loading = false; //Expand nodes that are supposed to be expanded foreach (TreeNode node in NodesToExpand) node.Expand(); return true; }
//R. Judge: store item on MissionNode with Missionstatement <-- WTF? /// <summary> /// Create and return new MissionNode from an XmlNode. Will set type appropriately. /// </summary> public static MissionNode NewFromXML(XmlNode item) { MissionNode mn = null; switch (item.Name) { case "event": mn = new MissionNode_Event(); break; case "disabled_event": mn = new MissionNode_Event(); break; case "start": mn = new MissionNode_Start(); break; case "#comment": mn = new MissionNode_Comment(); break; case "folder_arme": mn = new MissionNode_Folder(); break; default: mn = new MissionNode_Unknown(); break; } mn.FromXml(item); return mn; }