Esempio n. 1
0
        /// <summary>
        /// Takes a collection of directories and adds all parent directories within the GameData structure.
        /// </summary>
        /// <param name="directories">The collection of directory path strings to examine</param>
        public HashSet <string> AddParentDirectories(HashSet <string> directories)
        {
            if (directories == null || directories.Count == 0)
            {
                return(new HashSet <string>());
            }

            var gameDir = KSPPathUtils.NormalizePath(ksp.GameDir());

            return(directories
                   .Where(dir => !string.IsNullOrWhiteSpace(dir))
                   // normalize all paths before deduplicate
                   .Select(KSPPathUtils.NormalizePath)
                   // remove any duplicate paths
                   .Distinct()
                   .SelectMany(dir =>
            {
                var results = new HashSet <string>();
                // adding in the DirectorySeparatorChar fixes attempts on Windows
                // to parse "X:" which resolves to Environment.CurrentDirectory
                var dirInfo = new DirectoryInfo(dir + Path.DirectorySeparatorChar);

                // if this is a parentless directory (Windows)
                // or if the Root equals the current directory (Mono)
                if (dirInfo.Parent == null || dirInfo.Root == dirInfo)
                {
                    return results;
                }

                if (!dir.StartsWith(gameDir, StringComparison.CurrentCultureIgnoreCase))
                {
                    dir = KSPPathUtils.ToAbsolute(dir, gameDir);
                }

                // remove the system paths, leaving the path under the instance directory
                var relativeHead = KSPPathUtils.ToRelative(dir, gameDir);
                var pathArray = relativeHead.Split('/');
                var builtPath = string.Empty;
                foreach (var path in pathArray)
                {
                    builtPath += path + '/';
                    results.Add(KSPPathUtils.ToAbsolute(builtPath, gameDir));
                }

                return results;
            })
                   .Where(dir => !IsReservedDirectory(dir))
                   .ToHashSet());
        }
Esempio n. 2
0
File: KSP.cs Progetto: alibode/CKAN
 /// <summary>
 /// Returns path relative to this KSP's GameDir.
 /// </summary>
 public string ToRelativeGameDir(string path)
 {
     return(KSPPathUtils.ToRelative(path, GameDir()));
 }