private void OpenFolder(ModNode node)
 {
     if (node != null && !node.IsFile && node.IsInstalled)
     {
         OptionsController.OpenFolder(KSPPathHelper.GetAbsolutePath(SelectedNode.Destination));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Swaps the start building of the craft.
        /// </summary>
        public static void SwapBuildingOfSelectedCraft(CraftNode craftNode)
        {
            string fullPath = KSPPathHelper.GetAbsolutePath(craftNode.FilePath);

            if (File.Exists(fullPath))
            {
                string newType = GetOtherBuilding(craftNode.Type);
                string newPath = GetNewPath(craftNode, newType);
                string allText = File.ReadAllText(fullPath);
                if (!ChangeParameter(ref allText, craftNode.Name, "type", craftNode.Type, newType))
                {
                    return;
                }

                File.WriteAllText(fullPath, allText);
                File.Move(fullPath, newPath);

                Messenger.AddInfo(string.Format(Messages.MSG_BUILDING_OF_CRAFT_0_SWAPPED_1_2, craftNode.Name, craftNode.Type, newType));

                craftNode.Type     = newType;
                craftNode.FilePath = newPath;

                View.InvalidateView();
            }
        }
        /// <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);
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Checks if the ModNode is installed.
 /// </summary>
 /// <returns>True if Mod Node is installed.</returns>
 public bool IsModNodeInstalled()
 {
     if (IsFile)
     {
         return(!string.IsNullOrEmpty(Destination) && File.Exists(KSPPathHelper.GetAbsolutePath(Destination)));
     }
     else
     {
         return(!string.IsNullOrEmpty(Destination) && Directory.Exists(KSPPathHelper.GetAbsolutePath(Destination)));
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the new path for the craft.
        /// </summary>
        /// <param name="craftNode">The CraftNode to get a new path for.</param>
        /// <param name="newType">the new type of the craft.</param>
        /// <returns>The new path for the craft.</returns>
        private static string GetNewPath(CraftNode craftNode, string newType)
        {
            string fullPath = KSPPathHelper.GetAbsolutePath(craftNode.FilePath);
            int    index    = fullPath.ToLower().IndexOf(Path.DirectorySeparatorChar + craftNode.Type.ToLower() + Path.DirectorySeparatorChar);

            if (index > -1)
            {
                string start = fullPath.Substring(0, index + 1);
                string end   = fullPath.Substring(index + 5);

                return(Path.Combine(Path.Combine(start, newType.ToUpper()), end));
            }
            else
            {
                return(fullPath);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Removes the selected craft
        /// </summary>
        public static void RemoveSelectedCraft(CraftNode craftNode)
        {
            string  craftPath = KSPPathHelper.GetAbsolutePath(craftNode.FilePath);
            ModNode node      = ModSelectionTreeModel.SearchNodeByDestination(craftNode.FilePath, ModSelectionController.Model);

            DialogResult dlgResult = DialogResult.Cancel;

            if (node == null)
            {
                dlgResult = MessageBox.Show(View.ParentForm, Messages.MSG_CRAFT_NOT_FROM_MOD_DELETE_WARNING, string.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }

            if (node != null || dlgResult == DialogResult.Yes)
            {
                if (File.Exists(craftPath))
                {
                    File.Delete(craftPath);
                    Messenger.AddInfo(string.Format(Messages.MSG_CRAFT_0_DELETED, craftPath));

                    if (node != null)
                    {
                        Messenger.AddInfo(string.Format(Messages.MSG_MODSELECTION_UPDATED_PART_0, node.Name));
                        node.Checked  = false;
                        node.NodeType = NodeType.UnknownFile;
                    }

                    model.Nodes.Remove(craftNode);

                    foreach (CraftNode pNode in craftNode.Nodes)
                    {
                        if (pNode.RelatedPart != null)
                        {
                            pNode.RelatedPart.RemoveCraft(craftNode);
                            Messenger.AddInfo(string.Format(Messages.MSG_PARTTAB_UPDATED_PART_0, pNode.RelatedPart.Name));
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Creates crafts from the passed file.
        /// </summary>
        /// <param name="file">Full path to the craft file.</param>
        /// <returns>The crafts from the passed file.</returns>
        private static List <CraftNode> CreateCraftEntry(string file)
        {
            string adjustedPath = KSPPathHelper.GetAbsolutePath(file);

            var result = new List <CraftNode>();

            if (string.IsNullOrEmpty(file) || !File.Exists(adjustedPath))
            {
                return(result);
            }

            CraftNode craftNode = new CraftNode();

            craftNode.Name     = file;
            craftNode.FilePath = file;
            craftNode.Folder   = GetCraftFolder(adjustedPath);
            result.Add(craftNode);

            bool partInfo    = false;
            int  bracetCount = 0;

            string[] lines = File.ReadLines(file).ToArray <string>();
            foreach (string line in lines)
            {
                if (line == null)
                {
                    Messenger.AddError(string.Format(Messages.MSG_ERROR_DURING_CRAFT_READING_0_UNEXPECTED_EMPTY_LINE, file));
                    continue;
                }

                string tempLine = line.Trim();
                if (!partInfo)
                {
                    if (tempLine.ToLower().StartsWith("ship =") || tempLine.ToLower().StartsWith("ship="))
                    {
                        string name = tempLine.Split('=').Last();
                        craftNode.Text = name.Trim();
                        craftNode.Name = name.Trim();
                    }

                    else if (tempLine.ToLower().StartsWith("type =") || tempLine.ToLower().StartsWith("type="))
                    {
                        string type = tempLine.Split('=')[1];
                        craftNode.Type = type.Trim();
                    }

                    else if (tempLine.ToLower().StartsWith("version =") || tempLine.ToLower().StartsWith("version="))
                    {
                        string version = tempLine.Split('=')[1];
                        craftNode.Version = version.Trim();
                    }

                    else if (tempLine.ToLower().StartsWith("part"))
                    {
                        partInfo = true;
                    }
                }
                else
                {
                    if (tempLine.StartsWith("{"))
                    {
                        ++bracetCount;
                    }

                    else if (tempLine.StartsWith("}"))
                    {
                        --bracetCount;
                        if (bracetCount < 1)
                        {
                            partInfo = false;
                        }
                    }

                    else if (tempLine.ToLower().StartsWith("part =") || tempLine.ToLower().StartsWith("part="))
                    {
                        string partName = tempLine.Split('=')[1].Trim();
                        partName = partName.Substring(0, partName.LastIndexOf("_"));
                        if (!craftNode.ContainsPart(partName))
                        {
                            Messenger.AddInfo(string.Format(Messages.MSG_PART_0_ADDED_TO_CRAFT_1, partName, craftNode.Name));
                            craftNode.Nodes.Add(new CraftNode()
                            {
                                Name = partName + " (1)", FilePath = partName
                            });
                        }
                        else
                        {
                            try
                            {
                                CraftNode part = craftNode.GetPart(partName);
                                int       i1   = part.Name.LastIndexOf('(') + 1;
                                if (i1 < 0)
                                {
                                    continue;
                                }

                                int length = part.Name.Length - part.Name.LastIndexOf(')');
                                if (length < 0)
                                {
                                    continue;
                                }

                                string str = part.Name.Substring(i1, length);
                                int    i   = int.Parse(str) + 1;
                                part.Name = string.Format("{0} ({1})", partName, i);
                                Messenger.AddInfo(string.Format(Messages.MSG_PARTCOUNT_FOR_PART_0_IN_CRAFT_1_CHANGED_TO_2, partName, craftNode.Name, i));
                            }
                            catch (Exception ex)
                            {
                                Messenger.AddError(string.Format(Messages.MSG_ERROR_DURING_CRAFT_READING_0, file), ex);
                            }
                        }
                    }
                }
            }
            ////craftNode.SortPartsByDisplayText();

            return(result);
        }
        /// <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);
            }
        }