/// <summary>
        /// Splits the filename (at '\') and searches the tree for the Node where name and path matches.
        /// </summary>
        /// <param name="nodePath">Tree path of the node.</param>
        /// <param name="startNode">Node to start the search from.</param>
        /// <param name="pathSeparator">Separator of the path in filename.</param>
        /// <returns>The matching TreeNodeMod.</returns>
        public static ModNode SearchNodeByPathNew(string nodePath, ModNode startNode, char pathSeparator)
        {
            string[] pathNodeNames = nodePath.Split(new[] { pathSeparator }, StringSplitOptions.RemoveEmptyEntries);
            var      node          = SearchNodeByPath(pathNodeNames, startNode);

            return(node != null && node.GetFullTreePath().Contains(nodePath) ? node : null);
        }
Example #2
0
        /// <summary>
        /// Checks all patents if value is true,
        /// otherwise parents will be unchecked if no sibling is checked.
        /// </summary>
        /// <param name="value">The value to set to the Checked property to.</param>
        public void CheckParents(bool value)
        {
            if (!HasParent)
            {
                return;
            }

            ModNode parent = (Parent as ModNode);

            if (parent.Checked == value)
            {
                return;
            }

            if (value)
            {
                parent.SetChecked(true);
                parent.CheckParents(true);
            }
            else
            {
                if (!IsSiblingChecked)
                {
                    parent.SetChecked(false);
                    parent.CheckParents(false);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Loads the config.
        /// </summary>
        public static void Load(string path, ref List<ModNode> modNodes)
        {
            ModNode root = new ModNode() { Key = Constants.ROOT };
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNodeList moVersion = doc.GetElementsByTagName(Constants.VERSION);
                if (moVersion.Count > 0)
                {
                    switch (moVersion[0].InnerText.ToLower())
                    {
                        case "v1.0":
                            root = LoadV1_0(doc);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Messenger.AddError(string.Format("Error during KSPMods.cfg. \"{0}\"", ex.Message), ex);
            }

            modNodes.AddRange(root.Nodes.Cast<ModNode>());
        }
        /// <summary>
        /// Splits the filename (at '\') and searches the tree for the Node where name and path matches.
        /// </summary>
        /// <param name="filename">Zip-File path of the node to search for.</param>
        /// <param name="startNode">Node to start the search from.</param>
        /// <param name="pathSeparator">Separator of the path in filename.</param>
        /// <returns>The matching TreeNodeMod.</returns>
        public static ModNode SearchNodeByPath(string filename, Node startNode, char pathSeparator)
        {
            string[] dirs = filename.Split(new[] { pathSeparator }, StringSplitOptions.RemoveEmptyEntries);

            ModNode result = null;

            foreach (string dir in dirs)
            {
                startNode = SearchNode(dir, startNode);

                if (startNode == null)
                {
                    break;
                }

                result = startNode as ModNode;
            }

            if (result != null && result.GetFullTreePath().Contains(filename))
            {
                return(result);
            }

            return(null);
        }
Example #5
0
        /// <summary>
        /// Unchecks the modNode and all child and child child ... ModNodes.
        /// </summary>
        private void UncheckAllInternal(ModNode modNode)
        {
            modNode.SetChecked(false);

            foreach (ModNode child in modNode.Nodes)
            {
                UncheckAllInternal(child);
            }
        }
Example #6
0
        /// <summary>
        /// v1.0 load function.
        /// </summary>
        /// <returns>True on success.</returns>
        private static ModNode LoadV1_0(XmlDocument doc)
        {
            ModNode result = new ModNode() { Key = Constants.ROOT };
            XmlNodeList nodeList = doc.GetElementsByTagName(Constants.DOWNLOAD_PATH);
            if (nodeList.Count >= 1 && nodeList[0].Attributes != null)
            {
                foreach (XmlAttribute att in nodeList[0].Attributes)
                {
                    if (att.Name == Constants.NAME)
                        OptionsController.DownloadPath = att.Value;
                }
            }

            nodeList = doc.GetElementsByTagName(Constants.LAUNCHPARAMETER);
            if (nodeList.Count >= 1 && nodeList[0].Attributes != null)
            {
                foreach (XmlAttribute att in nodeList[0].Attributes)
                {
                    if (att.Name == Constants.USE64BIT)
                        MainController.LaunchPanel.Use64Bit = att.Value.Equals(Constants.TRUE, StringComparison.CurrentCultureIgnoreCase);
                    else if (att.Name == Constants.FORCEOPENGL)
                        MainController.LaunchPanel.ForceOpenGL = att.Value.Equals(Constants.TRUE, StringComparison.CurrentCultureIgnoreCase);
                }
            }

            ////nodeList = doc.GetElementsByTagName(Constants.OVERRRIDE);
            ////if (nodeList.Count >= 1 && nodeList[0].Attributes != null)
            ////{
            ////    foreach (XmlAttribute att in nodeList[0].Attributes)
            ////    {
            ////        if (att.Name == Constants.VALUE)
            ////            mOverride = false; // mOverride = (att.Value.ToLower() == Constants.TRUE);
            ////    }
            ////}

            ////nodeList = doc.GetElementsByTagName(Constants.KSPSTARTUPOPTIONS);
            ////if (nodeList.Count >= 1 && nodeList[0].Attributes != null)
            ////{
            ////    foreach (XmlAttribute att in nodeList[0].Attributes)
            ////    {
            ////        if (att.Name == Constants.BORDERLESSWINDOW)
            ////            mStartWithBorderlessWindow = (att.Value.ToLower() == Constants.TRUE);
            ////    }
            ////}

            result.Nodes.Clear();
            XmlNodeList mods = doc.GetElementsByTagName(Constants.MOD);
            foreach (XmlNode mod in mods)
            {
                ModNode newNode = new ModNode();
                result.Nodes.Add(newNode);
                FillModTreeNode(mod, ref newNode);
            }

            return result;
        }
        /// <summary>
        /// Registers all mod files that have a destination.
        /// </summary>
        /// <param name="modRoot">The root node of the mod to register the file nodes from.</param>
        /// <returns>True if a collision with another mod was detected.</returns>
        public static bool RegisterMod(ModNode modRoot)
        {
            bool collisionDetected = false;
            List<ModNode> fileNodes = GetAllNodesWithDestination(modRoot);
            foreach (ModNode fileNode in fileNodes)
                if (RegisterModFile(fileNode))
                    collisionDetected = true;

            return collisionDetected;
        }
        /// <summary>
        /// Invokes the BeforeCheckedChange event.
        /// </summary>
        /// <param name="invokingModNode">The invoking ModNode that will be passed as sender.</param>
        /// <param name="newCheckedState">The new checked state that should be applied.</param>
        /// <returns>True if continue with the change.</returns>
        internal static BeforeCheckedChangeEventArgs InvokeBeforeCheckedChange(ModNode invokingModNode, bool newCheckedState)
        {
            var args = new BeforeCheckedChangeEventArgs(invokingModNode, newCheckedState);

            if (BeforeCheckedChange != null)
            {
                BeforeCheckedChange(invokingModNode, args);
            }

            return(args);
        }
        /// <summary>
        /// Creates a instance of the ConflictInfoNode.
        /// This node will be a child ConflictInfoNode and represents one conflicting ModNode file.
        /// </summary>
        /// <param name="conflictingNode">The conflicting ModNode file.</param>
        private ConflictInfoNode(ModNode conflictingNode)
        {
            FileName    = LINE;
            Destination = LINE;

            ConflictingNode = conflictingNode;
            ModName         = ConflictingNode.ZipRoot.Name;
            ModVersion      = ConflictingNode.ZipRoot.Version;
            TreePath        = ConflictingNode.GetFullTreePath();
            ArchivePath     = ConflictingNode.ZipRoot.ArchivePath;
        }
        /// <summary>
        /// Checks if the possibleChild is a child of one of the ModNodes in modNodes
        /// </summary>
        /// <param name="possibleChild">The possible child ModNode.</param>
        /// <param name="modNodes">A list of possible parent ModNodes of possibleChild.</param>
        /// <returns>True if possible child is a child of one of the ModNodes in modNodes.</returns>
        protected static bool IsChildOfAny(ModNode possibleChild, List <ModNode> modNodes)
        {
            foreach (var modNode in modNodes)
            {
                if (IsParentOf(modNode, possibleChild))
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Returns the count of all nodes and sub node and sub sub...
        /// </summary>
        /// <param name="nodeList">The list to count the nodes from.</param>
        /// <returns>The count of nodes.</returns>
        public static int GetFullNodeCount(List <ModNode> nodeList)
        {
            if (nodeList == null || nodeList.Count == 0)
            {
                return(0);
            }

            ModNode[] nodes = new ModNode[nodeList.Count];
            for (int i = 0; i < nodeList.Count; ++i)
            {
                nodes[i] = nodeList[i];
            }

            return(GetFullNodeCount(nodes));
        }
        public static ModNode SearchNodeByDestination(string searchText, TreeModel model)
        {
            ModNode node = null;

            foreach (var startNode in model.Nodes)
            {
                node = SearchNodeByDestination(searchText, startNode as ModNode);
                if (node != null)
                {
                    break;
                }
            }

            return(node);
        }
        private static ModNode SearchNodeByPath(string[] pathNodeNames, ModNode startNode, int depth = 0, bool parentMatches = false)
        {
            // Are we deeper than we should search?
            if (depth >= pathNodeNames.Length)
            {
                return(null);
            }

            // Does the node match?
            bool thisMatches = startNode.Text.Equals(pathNodeNames[depth], StringComparison.CurrentCultureIgnoreCase);

            // if yes and we are at the lowest level of the search, we have found our match!
            if (thisMatches && depth == pathNodeNames.Length - 1)
            {
                return(startNode);
            }

            // if parent matches to last pathNodeNames entry and this pathNodeNames entry doesn't match with child, all child childes will mismatch too!
            if (parentMatches && !thisMatches)
            {
                return(null);
            }

            // Move to next search pathNodeName if node matches
            int newdpeth = thisMatches ? depth += 1 : depth;

            foreach (ModNode child in startNode.Nodes)
            {
                var node = SearchNodeByPath(pathNodeNames, child, newdpeth, thisMatches);
                if (node != null)
                {
                    return(node);
                }

                // if childs don't match then search deeper for conplete path.
                if (thisMatches)
                {
                    node = SearchNodeByPath(pathNodeNames, child, newdpeth - 1, false);
                    if (node != null)
                    {
                        return(node);
                    }
                }
            }

            return(null);
        }
        /// <summary>
        /// Searches the passed node for ksp folder.
        /// </summary>
        /// <param name="node">Node to start the search from.</param>
        /// <param name="kspFolders">List of found KSP folders.</param>
        /// <param name="craftFiles">List of found craft files.</param>
        public static void GetAllKSPFolders(ModNode node, ref List <ModNode> kspFolders, ref List <ModNode> craftFiles)
        {
            if (node.IsKSPFolder && !IsChildOfAny(node, kspFolders))
            {
                kspFolders.Add(node);
            }

            if (node.Text.EndsWith(Constants.EXT_CRAFT, StringComparison.CurrentCultureIgnoreCase))
            {
                craftFiles.Add(node);
            }

            foreach (ModNode child in node.Nodes)
            {
                GetAllKSPFolders(child, ref kspFolders, ref craftFiles);
            }
        }
        public static ModNode SearchNodeByDestination(string searchText, ModNode startNode)
        {
            ModNode node = null;

            if (startNode.Destination.Equals(searchText, StringComparison.CurrentCultureIgnoreCase))
            {
                node = startNode as ModNode;
            }
            else
            {
                foreach (ModNode child in startNode.Nodes)
                {
                    node = SearchNodeByDestination(searchText, child);
                    if (node != null)
                    {
                        break;
                    }
                }
            }
            return(node);
        }
        /// <summary>
        /// Registers the mod file if it has a destination.
        /// </summary>
        /// <param name="fileNode">The file node to register.</param>
        /// <returns>True if a collision with another mod was detected.</returns>
        public static bool RegisterModFile(ModNode fileNode)
        {
            if (string.IsNullOrEmpty(fileNode.Destination))
                return false;

            if (!mRegisterdModFiles.ContainsKey(fileNode.Destination.ToLower()))
            {
                // Add fileNode to register
                List<ModNode> list = new List<ModNode> { fileNode };
                mRegisterdModFiles.Add(fileNode.Destination.ToLower(), list);
            }
            else
            {
                if (!mRegisterdModFiles[fileNode.Destination.ToLower()].Contains(fileNode)) ////!AlreadyKnown(fileNode))
                {
                    mRegisterdModFiles[fileNode.Destination.ToLower()].Add(fileNode);

                    // Set collision flag
                    if (fileNode.IsFile || fileNode.HasChildCollision)
                    {
                        foreach (ModNode node in mRegisterdModFiles[fileNode.Destination.ToLower()])
                            node.HasCollision = true;
                    }
                    // Ignore folders for now ...
                    ////else
                    ////{
                    ////    if (HaveCollisionsSameRoot(fileNode) || fileNode.Text.Trim().Equals(Constants.GAMEDATA, StringComparison.CurrentCultureIgnoreCase))
                    ////        return false;

                    ////    foreach (ModNode node in mRegisterdModFiles[fileNode.Destination.ToLower()])
                    ////        node.HasCollision = true;
                    ////}

                    // Only true wenn collision detection is on
                    return ConflictDetectionOnOff;
                }
            }

            return false;
        }
        /// <summary>
        /// Checks if the parent ModeNode or any of its childes is a parent of child ModeNode.
        /// </summary>
        /// <param name="parent">The parent ModNode.</param>
        /// <param name="child">The child ModNode.</param>
        /// <returns>true if the parent ModeNode or any of its childes is a parent of child ModeNode.</returns>
        /// <remarks>Recursive method.</remarks>
        private static bool IsParentOf(ModNode parent, ModNode child)
        {
            if (parent == child)
            {
                return(false);
            }

            if (parent == child.Parent)
            {
                return(true);
            }

            foreach (ModNode newParent in parent.Nodes)
            {
                if (IsParentOf(newParent, child))
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Returns the count of all nodes and sub node and sub sub...
        /// </summary>
        /// <param name="nodeArray">The array to count the nodes from.</param>
        /// <returns>The count of nodes.</returns>
        /// <remarks>Recursive method.</remarks>
        public static int GetFullNodeCount(ModNode[] nodeArray)
        {
            if (nodeArray == null || nodeArray.Length == 0)
            {
                return(0);
            }

            int count = 0;

            foreach (ModNode node in nodeArray)
            {
                ++count;

                ModNode[] nodes = new ModNode[node.Nodes.Count];
                for (int i = 0; i < node.Nodes.Count; ++i)
                {
                    nodes[i] = (ModNode)node.Nodes[i];
                }

                count += GetFullNodeCount(nodes);
            }

            return(count);
        }
 public static ModNode SearchNodeByDestination(string searchText, ModNode startNode)
 {
     ModNode node = null;
     if (startNode.Destination.Equals(searchText, StringComparison.CurrentCultureIgnoreCase))
         node = startNode as ModNode;
     else
     {
         foreach (ModNode child in startNode.Nodes)
         {
             node = SearchNodeByDestination(searchText, child);
             if (node != null)
                 break;
         }
     }
     return node;
 }
Example #20
0
        /// <summary>
        /// Unchecks the modNode and all child and child child ... ModNodes.
        /// </summary>
        private void UncheckAllInternal(ModNode modNode)
        {
            modNode.SetChecked(false);

            foreach (ModNode child in modNode.Nodes)
                UncheckAllInternal(child);
        }
Example #21
0
        /// <summary>
        /// Adds a ModNode as a child to this node.
        /// </summary>
        /// <param name="node">the node to add as child.</param>
        /// <returns>The added child node.</returns>
        public ModNode AddChild(ModNode node)
        {
            Nodes.Add(node);

            return node;
        }
        /// <summary>
        /// Creates a XmlNode for the child nodes of the mod.
        /// </summary>
        /// <param name="doc">The XmlDocument for XmlNode creation.</param>
        /// <param name="mod">The mod node to get the information for the XMLNode.</param>
        /// <returns>A XmlNode with the infos from the mod.</returns>
        private static XmlNode CreateModChildEntryXmlNode(XmlDocument doc, ModNode mod)
        {
            XmlNode modEntryNode = doc.CreateElement(Constants.MOD_ENTRY);

            XmlAttribute nodeAttribute = doc.CreateAttribute(Constants.NAME);
            nodeAttribute.Value = mod.Text;
            modEntryNode.Attributes.Append(nodeAttribute);

            nodeAttribute = doc.CreateAttribute(Constants.ISFILE);
            nodeAttribute.Value = mod.IsFile ? ONE : ZERO;
            modEntryNode.Attributes.Append(nodeAttribute);

            nodeAttribute = doc.CreateAttribute(Constants.INSTALL);
            nodeAttribute.Value = mod.IsInstalled ? ONE : ZERO;
            modEntryNode.Attributes.Append(nodeAttribute);

            nodeAttribute = doc.CreateAttribute(Constants.INSTALLDIR);
            nodeAttribute.Value = mod.Destination;
            modEntryNode.Attributes.Append(nodeAttribute);

            foreach (ModNode child in mod.Nodes)
                modEntryNode.AppendChild(CreateModChildEntryXmlNode(doc, child));

            return modEntryNode;
        }
        private void InstallParentIfNecessary(ModNode modNode)
        {
            var parent = modNode.Parent as ModNode;
            if (parent == null)
                return;

            if (parent.Checked && !parent.IsInstalled)
            {
                Messenger.AddInfo(string.Format(Messages.MSG_CONFLICT_SOLVER_INSTALL_PARENT_FOLDER_0_1, modNode.Name, modNode.ZipRoot.Name));
                parent._Checked = true;
                ModNodeHandler.ProcessMod(parent, true);
                InstallParentIfNecessary(parent);
            }
        }
        /// <summary>
        /// Checks if the parent ModeNode or any of its childes is a parent of child ModeNode.
        /// </summary>
        /// <param name="parent">The parent ModNode.</param>
        /// <param name="child">The child ModNode.</param>
        /// <returns>true if the parent ModeNode or any of its childes is a parent of child ModeNode.</returns>
        /// <remarks>Recursive method.</remarks>
        private static bool IsParentOf(ModNode parent, ModNode child)
        {
            if (parent == child)
                return false;

            if (parent == child.Parent)
                return true;

            foreach (ModNode newParent in parent.Nodes)
            {
                if (IsParentOf(newParent, child))
                    return true;
            }

            return false;
        }
        /// <summary>
        /// Searches the passed node for ksp folder.
        /// </summary>
        /// <param name="node">Node to start the search from.</param>
        /// <param name="kspFolders">List of found KSP folders.</param>
        /// <param name="craftFiles">List of found craft files.</param>
        public static void GetAllKSPFolders(ModNode node, ref List<ModNode> kspFolders, ref List<ModNode> craftFiles)
        {
            if (node.IsKSPFolder && !IsChildOfAny(node, kspFolders))
                kspFolders.Add(node);

            if (node.Text.EndsWith(Constants.EXT_CRAFT, StringComparison.CurrentCultureIgnoreCase))
                craftFiles.Add(node);

            foreach (ModNode child in node.Nodes)
                GetAllKSPFolders(child, ref kspFolders, ref craftFiles);
        }
        /// <summary>
        /// Adds a ModNode to the ModSelection (model).
        /// </summary>
        /// <param name="modNode">The ModNode to add.</param>
        /// <returns>The added ModNode.</returns>
        public ModNode AddMod(ModNode modNode)
        {
            Nodes.Add(modNode);

            return modNode;
        }
        /// <summary>
        /// Returns the count of all nodes and sub node and sub sub...
        /// </summary>
        /// <param name="nodeArray">The array to count the nodes from.</param>
        /// <returns>The count of nodes.</returns>
        /// <remarks>Recursive method.</remarks>
        public static int GetFullNodeCount(ModNode[] nodeArray)
        {
            if (nodeArray == null || nodeArray.Length == 0) return 0;

            int count = 0;
            foreach (ModNode node in nodeArray)
            {
                ++count;

                ModNode[] nodes = new ModNode[node.Nodes.Count];
                for (int i = 0; i < node.Nodes.Count; ++i)
                    nodes[i] = (ModNode)node.Nodes[i];

                count += GetFullNodeCount(nodes);
            }

            return count;
        }
 /// <summary>
 /// Removes a ModNode from the ModSelection (model).
 /// </summary>
 /// <param name="modNode">The ModNode to add.</param>
 public void RemoveMod(ModNode modNode)
 {
     Nodes.Remove(modNode);
 }
        /// <summary>
        /// Adds a ModNode to the ModSelection (model).
        /// </summary>
        /// <param name="modNode">The ModNode to add.</param>
        /// <returns>The added ModNode.</returns>
        public ModNode AddMod(ModNode modNode)
        {
            Nodes.Add(modNode);

            return(modNode);
        }
        private void ResetPatentDestinationIfNecessary(ModNode modNode)
        {
            var parent = modNode.Parent as ModNode;
            if (parent == null)
                return;

            if (!parent.HasDestinationForChilds)
            {
                Messenger.AddInfo(string.Format(Messages.MSG_CONFLICT_SOLVER_RESET_DESTINATION_PARENT_FOLDER_0_1, modNode.Name, modNode.ZipRoot.Name));
                parent._Checked = false;
                ModNodeHandler.SetDestinationPaths(parent, string.Empty);
            }
        }
 /// <summary>
 /// Splits the filename (at '\') and searches the tree for the Node where name and path matches.
 /// </summary>
 /// <param name="nodePath">Tree path of the node.</param>
 /// <param name="startNode">Node to start the search from.</param>
 /// <param name="pathSeparator">Separator of the path in filename.</param>
 /// <returns>The matching TreeNodeMod.</returns>
 public static ModNode SearchNodeByPathNew(string nodePath, ModNode startNode, char pathSeparator)
 {
     string[] pathNodeNames = nodePath.Split(new[] { pathSeparator }, StringSplitOptions.RemoveEmptyEntries);
     var node = SearchNodeByPath(pathNodeNames, startNode);
     return node != null && node.GetFullTreePath().Contains(nodePath) ? node : null;
 }
        private static ModNode SearchNodeByPath(string[] pathNodeNames, ModNode startNode, int depth = 0, bool parentMatches = false)
        {
            // Are we deeper than we should search?
            if (depth >= pathNodeNames.Length)
                return null;

            // Does the node match?
            bool thisMatches = startNode.Text.Equals(pathNodeNames[depth], StringComparison.CurrentCultureIgnoreCase);

            // if yes and we are at the lowest level of the search, we have found our match!
            if (thisMatches && depth == pathNodeNames.Length - 1)
                return startNode;

            // if parent matches to last pathNodeNames entry and this pathNodeNames entry doesn't match with child, all child childes will mismatch too!
            if (parentMatches && !thisMatches)
                return null;

            // Move to next search pathNodeName if node matches
            int newdpeth = thisMatches ? depth += 1 : depth;
            foreach (ModNode child in startNode.Nodes)
            {
                var node = SearchNodeByPath(pathNodeNames, child, newdpeth, thisMatches);
                if (node != null)
                    return node;
                
                // if childs don't match then search deeper for conplete path.
                if (thisMatches)
                {
                    node = SearchNodeByPath(pathNodeNames, child, newdpeth - 1, false);
                    if (node != null)
                        return node;
                }
            }

            return null;
        }
        /// <summary>
        /// Creates a ZipEntry of the passed node and its childs to the passed zip file.
        /// </summary>
        /// <param name="zip">The zip file to add the new zip entry to.</param>
        /// <param name="node">The node to create a zip entry for.</param>
        /// <param name="processedNodeCount">Count of the processed nodes (for recursive calls only).</param>
        /// <returns>Count of the processed nodes.</returns>
        private static int CreateZipEntry(ZipArchive zip, ModNode node, int processedNodeCount = 0)
        {
            string absPath = KSPPathHelper.GetAbsolutePath(node.Destination);
            string gameDataPath = KSPPathHelper.GetPath(KSPPaths.GameData);
            string path = absPath.Replace(gameDataPath + Path.DirectorySeparatorChar, string.Empty);
            if (node.IsFile && node.IsInstalled)
                zip.AddEntry(path, absPath);

            foreach (ModNode child in node.Nodes)
                processedNodeCount = CreateZipEntry(zip, child, processedNodeCount);

            return processedNodeCount;
        }
        /// <summary>
        /// Checks if the possibleChild is a child of one of the ModNodes in modNodes
        /// </summary>
        /// <param name="possibleChild">The possible child ModNode.</param>
        /// <param name="modNodes">A list of possible parent ModNodes of possibleChild.</param>
        /// <returns>True if possible child is a child of one of the ModNodes in modNodes.</returns>
        protected static bool IsChildOfAny(ModNode possibleChild, List<ModNode> modNodes)
        {
            foreach (var modNode in modNodes)
            {
                if (IsParentOf(modNode, possibleChild))
                    return true;
            }

            return false;
        }
Example #35
0
        /// <summary>
        /// Saves the config.
        /// </summary>
        /// <returns>True on success.</returns>
        public static bool Save(string path, ModNode[] nodeArray)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            XmlNode root = doc.CreateElement(Constants.ROOTNODE);
            doc.AppendChild(root);

            XmlNode versionNode = doc.CreateElement(Constants.VERSION);
            versionNode.InnerText = mVersion;
            root.AppendChild(versionNode);

            XmlNode generalNode = doc.CreateElement(Constants.GENERAL);
            root.AppendChild(generalNode);

            XmlNode node = ConfigHelper.CreateConfigNode(doc, Constants.DOWNLOAD_PATH, Constants.NAME, OptionsController.DownloadPath);
            generalNode.AppendChild(node);

            node = ConfigHelper.CreateConfigNode(doc, Constants.LAUNCHPARAMETER, new string[,]
            {
                { Constants.USE64BIT, MainController.LaunchPanel.Use64Bit.ToString() }, 
                { Constants.FORCEOPENGL, MainController.LaunchPanel.ForceOpenGL.ToString() }
            });
            generalNode.AppendChild(node);

            ////node = CreateKSPConfigNode(doc, Constants.OVERRRIDE, Constants.VALUE, mOverride.ToString());
            ////generalNode.AppendChild(node);

            ////node = CreateKSPConfigNode(doc, Constants.KSPSTARTUPOPTIONS, Constants.BORDERLESSWINDOW, mStartWithBorderlessWindow.ToString());
            ////generalNode.AppendChild(node);

            XmlNode modsNode = doc.CreateElement(Constants.MODS);
            root.AppendChild(modsNode);

            foreach (ModNode mod in nodeArray)
                modsNode.AppendChild(CreateXmlNode(Constants.MOD, mod, modsNode));

            doc.Save(path);

            return true;
        }
        /// <summary>
        /// Returns the count of all nodes and sub node and sub sub...
        /// </summary>
        /// <param name="nodeList">The list to count the nodes from.</param>
        /// <returns>The count of nodes.</returns>
        public static int GetFullNodeCount(List<ModNode> nodeList)
        {
            if (nodeList == null || nodeList.Count == 0) return 0;

            ModNode[] nodes = new ModNode[nodeList.Count];
            for (int i = 0; i < nodeList.Count; ++i)
                nodes[i] = nodeList[i];

            return GetFullNodeCount(nodes);
        }
Example #37
0
        /// <summary>
        /// Saves the node and all its child nodes.
        /// </summary>
        /// <param name="nodeName">Name of the node.</param>
        /// <param name="child">The node to create the XmlNode from.</param>
        /// <param name="parent">The parent XmlNode of the new created XmlNode.</param>
        /// <returns>The new created XmlNode.</returns>
        private static XmlNode CreateXmlNode(string nodeName, ModNode child, XmlNode parent)
        {
            XmlDocument doc = parent.OwnerDocument;
            XmlNode modNode = doc.CreateElement(nodeName);
            modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.KEY, child.Key));
            modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.NAME, child.Name));
            modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.NODETYPE, ((int)child.NodeType).ToString()));
            modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.CHECKED, child.Checked.ToString()));

            if (nodeName == Constants.MOD)
            { 
                if (!string.IsNullOrEmpty(child.AddDate))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.ADDDATE, child.AddDate));
                if (!string.IsNullOrEmpty(child.Version))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.VERSION, child.Version));
                if (!string.IsNullOrEmpty(child.KSPVersion))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.GAMEVERSION, child.KSPVersion));
                if (!string.IsNullOrEmpty(child.Note))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.NOTE, child.Note));
                if (!string.IsNullOrEmpty(child.ProductID))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.PRODUCTID, child.ProductID));
                if (!string.IsNullOrEmpty(child.CreationDate))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.CREATIONDATE, child.CreationDate));
                if (!string.IsNullOrEmpty(child.ChangeDate))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.CHANGEDATE, child.ChangeDate));
                if (!string.IsNullOrEmpty(child.Author))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.AUTHOR, child.Author));
                if (!string.IsNullOrEmpty(child.Rating))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.RATING, child.Rating));
                if (!string.IsNullOrEmpty(child.Downloads))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.DOWNLOADS, child.Downloads));
                if (!string.IsNullOrEmpty(child.ModURL))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.MODURL, child.ModURL));
                if (!string.IsNullOrEmpty(child.AvcURL))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.AVCURL, child.AvcURL));
                if (!string.IsNullOrEmpty(child.AdditionalURL))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.ADDITIONALURL, child.AdditionalURL));
                if (!string.IsNullOrEmpty(child.SiteHandlerName))
                    modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.VERSIONCONTROLERNAME, child.SiteHandlerName));
            }

            if (!string.IsNullOrEmpty(child.Destination))
                modNode.Attributes.Append(ConfigHelper.CreateXMLAttribute(doc, Constants.DESTINATION, child.Destination));

            foreach (ModNode childchild in child.Nodes)
                modNode.AppendChild(CreateXmlNode(Constants.MOD_ENTRY, childchild, modNode));

            return modNode;
        }
        /// <summary>
        /// Invokes the BeforeCheckedChange event.
        /// </summary>
        /// <param name="invokingModNode">The invoking ModNode that will be passed as sender.</param>
        /// <param name="newCheckedState">The new checked state that should be applied.</param>
        /// <returns>True if continue with the change.</returns>
        internal static BeforeCheckedChangeEventArgs InvokeBeforeCheckedChange(ModNode invokingModNode, bool newCheckedState)
        {
            var args = new BeforeCheckedChangeEventArgs(invokingModNode, newCheckedState);

            if (BeforeCheckedChange != null)
                BeforeCheckedChange(invokingModNode, args);

            return args;
        }
        /// <summary>
        /// Adds a source folder to the CB_Source for the passed node and all its childs.
        /// </summary>
        /// <param name="node">The node to add as source folder.</param>
        /// <param name="depth">The depth of the recursive call.</param>
        /// <note>Recursive function!</note>
        private void AddSrcFolder(ModNode node, int depth = 0)
        {
            if (!cbListFoldersOnly.Checked || (cbListFoldersOnly.Checked && !node.IsFile))
                CB_Source.Items.Add(node);

            foreach (ModNode child in node.Nodes)
                if (!cbListFoldersOnly.Checked || (cbListFoldersOnly.Checked && !child.IsFile))
                    AddSrcFolder(child, depth + 1);
        }
 /// <summary>
 /// Removes a ModNode from the ModSelection (model).
 /// </summary>
 /// <param name="modNode">The ModNode to add.</param>
 public void RemoveMod(ModNode modNode)
 {
     Nodes.Remove(modNode);
 }
        /// <summary>
        /// Updates the ModInfo
        /// </summary>
        public static void Check4ModUpdates(ModNode[] mods, bool silent = false)
        {
            ModSelectionController.CheckForModUpdates(mods);

            #region OldCode

            ////mTaskAction = TaskAction.ModsUpdateCheck;
            ////EventDistributor.InvokeAsyncTaskStarted(Instance);
            ////AsyncTask<bool>.DoWork(
            ////    delegate()
            ////    {
            ////        bool result = false;
            ////        foreach (ModNode mod in mods)
            ////        {
            ////            Messenger.AddInfo(string.Format("\"{0}\" checking ...", mod.Text));
            ////            ModInfo modInfo = null;
            ////            if (mod.VersionControl == VersionControl.KSPForum && KSPForum.IsValidURL(mod.KSPForumURL))
            ////                modInfo = KSPForum.GetModInfo(mod.KSPForumURL);
            ////            else if (mod.VersionControl == VersionControl.CurseForge && CurseForge.IsValidURL(CurseForge.GetCurseForgeModURL(mod.CurseForgeURL)))
            ////                modInfo = CurseForge.GetModInfo(CurseForge.GetCurseForgeModURL(mod.CurseForgeURL));

            ////            if (modInfo != null)
            ////            {
            ////                modInfo.LocalPath = string.Empty;

            ////                DateTime oldDate = DateTime.MinValue;
            ////                DateTime newDate = DateTime.MinValue;
            ////                if (!DateTime.TryParse(mod.AddDate, out oldDate))
            ////                    oldDate = DateTime.MinValue;

            ////                if (!DateTime.TryParse(modInfo.CreationDate, out newDate))
            ////                    newDate = DateTime.MinValue;

            ////                bool updateAvailable = false;
            ////                if (oldDate < newDate)
            ////                    updateAvailable = true;
            ////                else
            ////                {
            ////                    if (!DateTime.TryParse(mod.CreationDate, out oldDate))
            ////                        continue;

            ////                    if (oldDate < newDate)
            ////                        updateAvailable = true;
            ////                }

            ////                if (updateAvailable)
            ////                {
            ////                    Messenger.AddInfo(string.Format("\"{0}\" is outdated", mod.Text));
            ////                    result = true;
            ////                }
            ////                else
            ////                {
            ////                    Messenger.AddInfo(string.Format("\"{0}\" is up to date.", mod.Text));
            ////                }

            ////                mod.IsOutdated = updateAvailable;
            ////                //mod.CreationDate = modInfo.CreationDate;
            ////                mod.Rating = modInfo.Rating;
            ////                mod.Downloads = modInfo.Downloads;
            ////                mod.SpaceportURL = modInfo.SpaceportURL;
            ////                mod.KSPForumURL = modInfo.ForumURL;
            ////                mod.CurseForgeURL = modInfo.CurseForgeURL;
            ////                mod.Author = modInfo.Author;
            ////            }
            ////            else
            ////                Messenger.AddInfo(string.Format("\"{0}\" has no valid CurseForge or KSP Forum URL", mod.Text));
            ////        }

            ////    return result;
            ////},
            ////delegate(bool result, Exception ex)
            ////{
            ////    EventDistributor.InvokeAsyncTaskDone(Instance);

            ////    if (ex != null)
            ////    {
            ////        MessageBox.Show(View.ParentForm, ex.Message);
            ////        Messenger.AddError("Error during update check.", ex);
            ////    }
            ////    else
            ////    {
            ////        if (result && !silent)
            ////        {
            ////            string msg = "One or more mods are outdated.";
            ////            MessageBox.Show(View.ParentForm, msg, "Update Info");
            ////            // TODO: Ask to switch to ModSelection.
            ////            //MessageBoxButtons buttons = MessageBoxButtons.OK;
            ////            //if (MainForm.tabControl1.SelectedTab != MainForm.tabPageMods)
            ////            //{
            ////            //    msg += "\n\rSwitch to ModSelection?";
            ////            //    buttons = MessageBoxButtons.YesNo;
            ////            //}

            ////            //if (MessageBox.Show(View.ParentForm, msg, "Update Info", buttons) == DialogResult.Yes)
            ////            //{
            ////            //    MainForm.tabControl1.SelectedTab = MainForm.tabPageMods;
            ////            //    MainForm.tabControl1.Refresh();
            ////            //}
            ////        }

            ////        LastModUpdateTry = DateTime.Now;
            ////    }

            ////    Messenger.AddInfo("Update check done.");
            ////});

            #endregion
        }
Example #42
0
        /// <summary>
        /// Creates a TreeNode for the XmlNode information.
        /// </summary>
        private static void FillModTreeNode(XmlNode mod, ref ModNode node)
        {
            string kspForumURL = string.Empty;
            string curseForgeURL = string.Empty;
            node.AddDate = DateTime.Now.ToString();
            foreach (XmlAttribute att in mod.Attributes)
            {
                if (att.Name == Constants.NAME)
                    node.Name = att.Value;
                else if (att.Name == Constants.KEY)
                    node.Key = att.Value;
                else if (att.Name == Constants.ADDDATE)
                    node.AddDate = att.Value;
                else if (att.Name == Constants.VERSION)
                    node.Version = att.Value;
                else if (att.Name == Constants.GAMEVERSION)
                    node.KSPVersion = att.Value;
                else if (att.Name == Constants.NOTE)
                    node.Note = att.Value;
                else if (att.Name == Constants.PRODUCTID)
                    node.ProductID = att.Value;
                else if (att.Name == Constants.CREATIONDATE)
                    node.CreationDate = att.Value;
                else if (att.Name == Constants.CHANGEDATE)
                    node.ChangeDate = att.Value;
                else if (att.Name == Constants.AUTHOR)
                    node.Author = att.Value;
                else if (att.Name == Constants.RATING)
                    node.Rating = att.Value;
                else if (att.Name == Constants.DOWNLOADS)
                    node.Downloads = att.Value;
                else if (att.Name == Constants.MODURL)
                    node.ModURL = att.Value;
                else if (att.Name == Constants.AVCURL)
                    node.AvcURL = att.Value;
                else if (att.Name == Constants.ADDITIONALURL)
                    node.AdditionalURL = att.Value;
                else if (att.Name == Constants.CHECKED)
                    node.SetChecked((att.Value.Equals(Constants.TRUE, StringComparison.CurrentCultureIgnoreCase)), true);
                else if (att.Name == Constants.NODETYPE)
                    node.NodeType = (NodeType)int.Parse(att.Value);
                else if (att.Name == Constants.DESTINATION)
                    node.Destination = att.Value;
                else if (att.Name == Constants.FORUMURL)
                    kspForumURL = att.Value;
                else if (att.Name == Constants.CURSEFORGEURL)
                    curseForgeURL = att.Value;
                else if (att.Name == Constants.VERSIONCONTROLERNAME)
                {
                    node.SiteHandlerName = att.Value;

                    switch (att.Value)
                    {
                        case "KSPForum": // KSPForum
                            if (string.IsNullOrEmpty(node.ModURL))
                                node.ModURL = kspForumURL;
                            if (string.IsNullOrEmpty(node.AdditionalURL))
                                node.AdditionalURL = curseForgeURL;
                            break;
                        case "CurseForge": // CurseForge
                            if (string.IsNullOrEmpty(node.ModURL))
                                node.ModURL = curseForgeURL;
                            if (string.IsNullOrEmpty(node.AdditionalURL))
                                node.AdditionalURL = kspForumURL;
                            break;
                    }
                }
            }

            foreach (XmlNode modEntry in mod.ChildNodes)
            {
                ModNode newNode = new ModNode();
                node.Nodes.Add(newNode);
                FillModTreeNode(modEntry, ref newNode);
            }
        }
 private void OpenFile(ModNode node)
 {
     if (node != null && node.IsFile)
         ModSelectionController.OpenTextDisplayer(node);
 }
 /// <summary>
 /// Handles the CheckedChanged event of the CB_ListFoldersOnly.
 /// </summary>
 private void CB_ListFoldersOnly_CheckedChanged(object sender, EventArgs e)
 {
     CB_Source.Items.Clear();
     SrcFolders = new ModNode[] { mSourceNode };
 }
 private void OpenFolder(ModNode node)
 {
     if (node != null && !node.IsFile && node.IsInstalled)
         OptionsController.OpenFolder(KSPPathHelper.GetAbsolutePath(SelectedNode.Destination));
 }
Example #46
0
        /// <summary>
        /// Adds a ModNode as a child to this node.
        /// </summary>
        /// <param name="node">the node to add as child.</param>
        /// <returns>The added child node.</returns>
        public ModNode AddChild(ModNode node)
        {
            Nodes.Add(node);

            return(node);
        }
        /// <summary>
        /// Creates a XmlNode for the mods and all its childes from an XmlFile.
        /// </summary>
        /// <param name="doc">The XmlDocument for XmlNode creation.</param>
        /// <param name="mod">The mod node to get the information for the XMLNode.</param>
        /// <returns>A XmlNode with the infos from the mod.</returns>
        private static XmlNode CreateModXmlNode(XmlDocument doc, ModNode mod)
        {
            XmlNode modNode = doc.CreateElement(Constants.MOD);

            XmlAttribute nodeAttribute = doc.CreateAttribute(Constants.KEY);
            nodeAttribute.Value = Path.GetFileName(mod.Key);
            modNode.Attributes.Append(nodeAttribute);

            nodeAttribute = doc.CreateAttribute(Constants.NAME);
            nodeAttribute.Value = mod.Name;
            modNode.Attributes.Append(nodeAttribute);

            nodeAttribute = doc.CreateAttribute(Constants.VERSIONCONTROLERNAME);
            nodeAttribute.Value = mod.SiteHandlerName;
            modNode.Attributes.Append(nodeAttribute);

            if (!string.IsNullOrEmpty(mod.ProductID))
            {
                nodeAttribute = doc.CreateAttribute(Constants.PRODUCTID);
                nodeAttribute.Value = mod.ProductID;
                modNode.Attributes.Append(nodeAttribute);
            }

            if (!string.IsNullOrEmpty(mod.ModURL))
            {
                nodeAttribute = doc.CreateAttribute(Constants.MODURL);
                nodeAttribute.Value = mod.ModURL;
                modNode.Attributes.Append(nodeAttribute);
            }

            if (!string.IsNullOrEmpty(mod.AdditionalURL))
            {
                nodeAttribute = doc.CreateAttribute(Constants.ADDITIONALURL);
                nodeAttribute.Value = mod.AdditionalURL;
                modNode.Attributes.Append(nodeAttribute);
            }

            foreach (ModNode child in mod.Nodes)
                modNode.AppendChild(CreateModChildEntryXmlNode(doc, child));

            return modNode;
        }