/// <summary> /// Adds a related craft to this part. /// </summary> /// <param name="craft">The related craft to add.</param> public void AddRelatedCraft(CraftNode craft) { if (!ContainsCraft(craft.Name)) { PartNode cNode = new PartNode() { Title = craft.Name, FilePath = craft.FilePath }; cNode.Tag = craft; Nodes.Add(cNode); } }
/// <summary> /// Removes the relation part. /// </summary> /// <param name="partNode">The part to remove.</param> public void RemovePartRelation(PartNode partNode) { foreach (CraftNode part in Nodes) { if (part.RelatedPart != null && part.RelatedPart.Title == partNode.Title) { part.Text = part.FilePath; // FilePath is here the name of the part. part.RelatedPart = null; } } }
/// <summary> /// Removes a craft relation. /// </summary> /// <param name="craftNode">The CraftNode to remove.</param> public void RemoveCraft(CraftNode craftNode) { PartNode temp = null; foreach (PartNode craft in Nodes) { if (craft.Tag != null && ((CraftNode)craft.Tag).FilePath == craftNode.FilePath) { temp = craft; break; } } if (temp != null) { Nodes.Remove(temp); } }
/// <summary> /// Opens the Part Editor with the passed PartNode. /// </summary> /// <param name="partNode">The part node to edit.</param> public static void EditPart(PartNode partNode) { frmPartEditor dlg = new frmPartEditor(); dlg.Title = partNode.Title; dlg.PartName = partNode.Name; dlg.Category = partNode.Category; dlg.KnownNames = (from PartNode part in allNodes select part.Name).ToList(); if (dlg.ShowDialog(View.ParentForm) == DialogResult.OK) { string fullPath = KSPPathHelper.GetAbsolutePath(partNode.FilePath); if (File.Exists(fullPath)) { string allText = File.ReadAllText(fullPath); if (partNode.Name != dlg.NewName) { if (!ChangeParameter(ref allText, partNode.Name, NAME, partNode.Name, dlg.NewName)) return; Messenger.AddInfo(string.Format(Messages.MSG_NAME_OF_PART_0_CHANGED_1, partNode.Name, dlg.NewName)); partNode.Name = dlg.NewName; } if (partNode.Title != dlg.NewTitle) { if (!ChangeParameter(ref allText, partNode.Name, TITLE, partNode.Title, dlg.NewTitle)) return; Messenger.AddInfo(string.Format(Messages.MSG_TITLE_OF_PART_0_CHANGED_FROM_1_TO_2, partNode.Name, partNode.Title, dlg.NewTitle)); partNode.Title = dlg.NewTitle; } if (partNode.Category != dlg.NewCategory) { if (!ChangeParameter(ref allText, partNode.Name, CATEGORY, partNode.Category, dlg.NewCategory)) return; Messenger.AddInfo(string.Format(Messages.MSG_CATEGORY_OF_PART_0_CHANGED_FROM_1_TO_2, partNode.Name, partNode.Category, dlg.NewCategory)); partNode.Category = dlg.NewCategory; foreach (var node in partNode.Nodes) { if (node.Text.StartsWith("Category = ")) { node.Text = "Category = " + partNode.Category; break; } } } File.WriteAllText(fullPath, allText); } } }
/// <summary> /// Removes the part from KSP and unchecks it in the mod selection. /// </summary> /// <param name="partNode">The part node to remove.</param> public static void RemovePart(PartNode partNode) { if (partNode == null) return; string partPath = Path.GetDirectoryName(KSPPathHelper.GetAbsolutePath(partNode.FilePath)); ModNode node = ModSelectionTreeModel.SearchNodeByDestination(partNode.FilePath, ModSelectionController.Model); DialogResult dlgResult = DialogResult.Cancel; if (node == null) dlgResult = MessageBox.Show(View.ParentForm, Messages.MSG_PART_NOT_FROM_MOD_DELETE_WARNING, string.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (partNode.Nodes != null && partNode.Nodes.Count > 0) { StringBuilder sb = new StringBuilder(); sb.AppendLine(Messages.MSG_PART_USED_DELETE_WARNING); foreach (PartNode tempNode in partNode.Nodes) sb.AppendFormat("- {0}{1}", tempNode.Title, Environment.NewLine); sb.AppendLine(); sb.AppendLine(Messages.MSG_DELETE_ANYWAY); dlgResult = MessageBox.Show(View.ParentForm, sb.ToString(), string.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } if ((node != null || dlgResult == DialogResult.Yes) && Directory.Exists(partPath)) { Messenger.AddInfo(string.Format(Messages.MSG_DIR_0_OF_PART_1_DELETED, partPath, Path.GetFileName(partPath))); Directory.Delete(partPath, true); if (partNode.Nodes != null) { foreach (var n in partNode.Nodes) { var craft = n.Tag as CraftNode; if (craft == null) continue; craft.RemovePartRelation(partNode); } } if (node != null) { node = node.Parent as ModNode; node.SetChecked(false); node.IsInstalled = false; node.NodeType = NodeType.UnknownFolder; Messenger.AddInfo(string.Format(Messages.MSG_MODNODE_0_UNCHECKED, node.Name)); foreach (ModNode child in node.Nodes) { child.SetChecked(false); child.IsInstalled = false; child.NodeType = child.IsFile ? NodeType.UnknownFile : NodeType.UnknownFolder; Messenger.AddInfo(string.Format(Messages.MSG_MODNODE_0_UNCHECKED, child.Name)); } } model.Nodes.Remove(partNode); allNodes.Remove(partNode); } }
/// <summary> /// Parses the line for part informations. /// </summary> /// <param name="file">Full path to the part cfg file.</param> /// <param name="line">The line to parse.</param> /// <param name="partNode">The node to write the informations to.</param> private static void ParsePartLine(string file, string line, ref PartNode partNode) { string tempLine = line.Trim(); // TODO: change name to title! if (tempLine.ToLower().StartsWith("name =") || tempLine.ToLower().StartsWith("name=")) { string[] nameValuePair = tempLine.Split('='); if (nameValuePair.Length != 2) Messenger.AddError(string.Format(Messages.MSG_ERROR_DURING_PART_READING_0_NAME_TITLE_MISSMATCH, file)); else { string name = nameValuePair[1].Trim(); partNode.Title = name; partNode.Name = name; } } else if (tempLine.ToLower().StartsWith("title =") || tempLine.ToLower().StartsWith("title=")) { string[] nameValuePair = tempLine.Split('='); if (nameValuePair.Length != 2) Messenger.AddError(string.Format(Messages.MSG_ERROR_DURING_PART_READING_0_NAME_TITLE_MISSMATCH, file)); else { string name = nameValuePair[1].Trim(); partNode.Title = name; } } else if (tempLine.ToLower().StartsWith("category =") || tempLine.ToLower().StartsWith("category=")) { string[] nameValuePair = tempLine.Split('='); if (nameValuePair.Length != 2) Messenger.AddError(string.Format(Messages.MSG_ERROR_DURING_PART_READING_0_NAME_TITLE_MISSMATCH, file)); else { int categoryIndex = -1; string category = nameValuePair[1].Trim(); if (int.TryParse(category, out categoryIndex)) category = TranslateCategoryIndex(categoryIndex); partNode.Category = category; } } }
/// <summary> /// Creates a new default TreeNodePart. /// </summary> /// <param name="file">The full path of the part cfg file.</param> /// <returns>The new created PartNode from the passed file.</returns> private static PartNode CreateNewPartNode(string file) { PartNode partNode = new PartNode(); partNode.FilePath = KSPPathHelper.GetRelativePath(file); if (file.Contains(Constants.GAMEDATA)) { string mod = file.Substring(file.IndexOf(Constants.GAMEDATA) + 9); mod = mod.Substring(0, mod.IndexOf(Path.DirectorySeparatorChar)); partNode.Mod = mod; if (!allModFilter.Contains(mod)) allModFilter.Add(mod); } return partNode; }
/// <summary> /// Adds a node to a list if the node and node name is not null and if the list doesn't contains the node already. /// </summary> /// <param name="node">The node to add.</param> /// <param name="list">The list to add to.</param> private static void AddNode(PartNode node, List<PartNode> list) { if (node != null && !string.IsNullOrEmpty(node.Name) && !list.Contains(node)) { Messenger.AddInfo(string.Format(Messages.MSG_PART_FOUND_AND_ADDED_0, node.Name)); list.Add(node); } }