Example #1
0
 /// <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>
 /// 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);
     }
 }
Example #3
0
        /// <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>
        /// 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>
        /// 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));
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Opens the Craft Editor with the selected craft.
 /// </summary>
 public static void EditSelectedCraft(CraftNode craftNode)
 {
     MessageBox.Show("Not implemented yet!", string.Empty);
 }
        /// <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;
        }
        /// <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>
        /// 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('=')[1];
                        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;
        }