/// <summary>
        /// Creates a directory for the ModNodes destination.
        /// </summary>
        /// <param name="node">The ModNode to get the destination of.</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        private static void CreateDirectory(ModNode node, bool silent)
        {
            string destination = node.Destination;

            if (!string.IsNullOrEmpty(destination))
            {
                destination = KSPPathHelper.GetAbsolutePath(destination);
            }

            node.IsInstalled = false;
            if (!Directory.Exists(destination))
            {
                try
                {
                    Directory.CreateDirectory(destination);
                    node.IsInstalled = true;

                    if (!silent)
                    {
                        Messenger.AddInfo(string.Format(Messages.MSG_DIR_CREATED_0, destination));
                    }
                }
                catch
                {
                    Messenger.AddInfo(string.Format(Messages.MSG_DIR_CREATED_ERROR_0, destination));
                }
            }

            node.NodeType = (node.IsKSPFolder) ? NodeType.KSPFolderInstalled : NodeType.UnknownFolderInstalled;
        }
        /// <summary>
        /// Removes the file the ModNodes destination points to.
        /// </summary>
        /// <param name="node">The ModNode to get the destination of.</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        private static void RemoveFile(ModNode node, bool silent)
        {
            string destination = node.Destination;

            if (!string.IsNullOrEmpty(destination))
            {
                destination = KSPPathHelper.GetAbsolutePath(destination);
            }

            node.IsInstalled = false;
            bool installedByOtherMod = ModRegister.GetCollisionModFiles(node).Any(n => n.IsInstalled);

            if (File.Exists(destination) && !installedByOtherMod)
            {
                try
                {
                    File.Delete(destination);
                    node.IsInstalled = false;
                    if (!silent)
                    {
                        Messenger.AddInfo(string.Format(Messages.MSG_FILE_DELETED_0, destination));
                    }
                }
                catch (Exception ex)
                {
                    Messenger.AddError(string.Format(Messages.MSG_FILE_DELETED_ERROR_0, destination), ex);
                }
            }

            node.NodeType = NodeType.UnknownFile;
        }
        /// <summary>
        /// Try to delete all not processed directories.
        /// </summary>
        /// <param name="node">The dir node to delete.</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        /// <returns>True on success.</returns>
        private static bool DeleteDirectory(ModNode node, bool silent = false)
        {
            string destination = node.Destination;

            if (!string.IsNullOrEmpty(destination))
            {
                destination = KSPPathHelper.GetAbsolutePath(destination);
            }

            try
            {
                if (!Directory.Exists(destination))
                {
                    Messenger.AddInfo(string.Format(Messages.MSG_DIR_0_NOT_EXISTS, destination));
                    return(false);
                }

                if (KSPPathHelper.IsKSPDir(destination))
                {
                    Messenger.AddInfo(string.Format(Messages.MSG_DIR_0_IS_KSPDIR, destination));
                    return(false);
                }

                if (Directory.GetDirectories(destination).Any() ||
                    Directory.GetFiles(destination).Any())
                {
                    Messenger.AddInfo(string.Format(Messages.MSG_DIR_0_IS_NOT_EMPTY, destination));
                    return(false);
                }

                Directory.Delete(destination, true);
                if (!silent)
                {
                    Messenger.AddInfo(string.Format(Messages.MSG_DIR_DELETED_0, destination));
                }

                return(true);
            }
            catch (Exception ex)
            {
                Messenger.AddError(string.Format(Messages.MSG_DIR_DELETED_ERROR_0, destination), ex);
            }

            return(false);
        }
示例#4
0
        /// <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>
        /// Removes the directory the ModNodes destination points to.
        /// </summary>
        /// <param name="node">The ModNode to get the destination of.</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        private static void RemoveDirectory(ModNode node, bool silent)
        {
            if (!node.IsKSPFolder)
            {
                string destination = node.Destination;
                if (!string.IsNullOrEmpty(destination))
                {
                    destination = KSPPathHelper.GetAbsolutePath(destination);
                }

                node.IsInstalled = false;
                if (Directory.Exists(destination))
                {
                    if (!Directory.GetDirectories(destination).Any() && !Directory.GetFiles(destination).Any())
                    {
                        try
                        {
                            Directory.Delete(destination, true);
                            node.IsInstalled = false;
                            if (!silent)
                            {
                                Messenger.AddInfo(string.Format(Messages.MSG_DIR_DELETED_0, destination));
                            }
                        }
                        catch
                        {
                            mNotDeletedDirs.Add(node);
                        }
                    }
                    else
                    {
                        // add dir for later try to delete
                        mNotDeletedDirs.Add(node);
                    }
                }
            }
            else
            {
                mNotDeletedDirs.Add(node);
            }

            node.NodeType = (node.IsKSPFolder) ? NodeType.KSPFolder : NodeType.UnknownFolder;
        }
        /// <summary>
        /// Extracts the file from the archive with the passed key.
        /// </summary>
        /// <param name="node">The node to install the file from.</param>
        /// <param name="path">The path to install the file to.</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        private static void ExtractFile(ModNode node, string path, bool silent = false, bool overrideOn = false)
        {
            if (node == null)
            {
                return;
            }

            string destination = path;

            if (!string.IsNullOrEmpty(destination))
            {
                destination = KSPPathHelper.GetAbsolutePath(destination);
            }

            using (IArchive archive = ArchiveFactory.Open(node.ZipRoot.Key))
            {
                IArchiveEntry entry = archive.Entries.FirstOrDefault(e => e.FilePath.Equals(node.Key, StringComparison.CurrentCultureIgnoreCase));
                if (entry == null)
                {
                    return;
                }

                node.IsInstalled = false;
                if (!File.Exists(destination))
                {
                    try
                    {
                        // create new file.
                        entry.WriteToFile(destination);
                        node.IsInstalled = true;

                        if (!silent)
                        {
                            Messenger.AddInfo(string.Format(Messages.MSG_FILE_EXTRACTED_0, destination));
                        }
                    }
                    catch (Exception ex)
                    {
                        Messenger.AddError(string.Format(Messages.MSG_FILE_EXTRACTED_ERROR_0, destination), ex);
                    }
                }
                else if (overrideOn)
                {
                    try
                    {
                        // delete old file
                        File.Delete(destination);

                        // create new file.
                        entry.WriteToFile(destination);

                        if (!silent)
                        {
                            Messenger.AddInfo(string.Format(Messages.MSG_FILE_EXTRACTED_0, destination));
                        }
                    }
                    catch (Exception ex)
                    {
                        Messenger.AddError(string.Format(Messages.MSG_FILE_EXTRACTED_ERROR_0, destination), ex);
                    }
                }

                node.IsInstalled = File.Exists(destination);
                node.NodeType    = (node.IsInstalled) ? NodeType.UnknownFileInstalled : NodeType.UnknownFile;
            }
        }