Beispiel #1
0
        private void printCacheInfo()
        {
            int  fileCount;
            long bytes;

            manager.Cache.GetSizeInfo(out fileCount, out bytes);
            user.RaiseMessage($"{fileCount} files, {CkanModule.FmtSize(bytes)}");
        }
Beispiel #2
0
        private int ShowCacheSizeLimit(CommonOptions options)
        {
            IWin32Registry winReg = new Win32Registry();

            if (winReg.CacheSizeLimit.HasValue)
            {
                user.RaiseMessage(CkanModule.FmtSize(winReg.CacheSizeLimit.Value));
            }
            else
            {
                user.RaiseMessage("Unlimited");
            }
            return(Exit.OK);
        }
Beispiel #3
0
        private int ShowCacheSizeLimit(CommonOptions options)
        {
            IConfiguration cfg = ServiceLocator.Container.Resolve <IConfiguration>();

            if (cfg.CacheSizeLimit.HasValue)
            {
                user.RaiseMessage(CkanModule.FmtSize(cfg.CacheSizeLimit.Value));
            }
            else
            {
                user.RaiseMessage("Unlimited");
            }
            return(Exit.OK);
        }
Beispiel #4
0
 private string getLength(FileSystemInfo fi)
 {
     if (isDir(fi))
     {
         return(dirSize);
     }
     else
     {
         FileInfo file = fi as FileInfo;
         if (file != null)
         {
             return(CkanModule.FmtSize(file.Length));
         }
         else
         {
             return(dirSize);
         }
     }
 }
Beispiel #5
0
        /// <summary>
        /// Initialize the screen
        /// </summary>
        /// <param name="mgr">Game instance manager object containing the current instance</param>
        /// <param name="dbg">True if debug options should be available, false otherwise</param>
        public ModListScreen(GameInstanceManager mgr, bool dbg)
        {
            debug    = dbg;
            manager  = mgr;
            registry = RegistryManager.Instance(manager.CurrentInstance).registry;

            moduleList = new ConsoleListBox <CkanModule>(
                1, 4, -1, -2,
                GetAllMods(),
                new List <ConsoleListBoxColumn <CkanModule> >()
            {
                new ConsoleListBoxColumn <CkanModule>()
                {
                    Header   = "",
                    Width    = 1,
                    Renderer = StatusSymbol
                }, new ConsoleListBoxColumn <CkanModule>()
                {
                    Header   = "Name",
                    Width    = 44,
                    Renderer = m => m.name ?? ""
                }, new ConsoleListBoxColumn <CkanModule>()
                {
                    Header   = "Version",
                    Width    = 10,
                    Renderer = m => ModuleInstaller.StripEpoch(m.version?.ToString() ?? ""),
                    Comparer = (a, b) => a.version.CompareTo(b.version)
                }, new ConsoleListBoxColumn <CkanModule>()
                {
                    Header   = "Max game version",
                    Width    = 17,
                    Renderer = m => registry.LatestCompatibleKSP(m.identifier)?.ToString() ?? "",
                    Comparer = (a, b) => registry.LatestCompatibleKSP(a.identifier).CompareTo(registry.LatestCompatibleKSP(b.identifier))
                }
            },
                1, 0, ListSortDirection.Descending,
                (CkanModule m, string filter) => {
                // Search for author
                if (filter.StartsWith("@"))
                {
                    string authorFilt = filter.Substring(1);
                    if (string.IsNullOrEmpty(authorFilt))
                    {
                        return(true);
                    }
                    else
                    {
                        // Remove special characters from search term
                        authorFilt = CkanModule.nonAlphaNums.Replace(authorFilt, "");
                        return(m.SearchableAuthors.Any((author) => author.IndexOf(authorFilt, StringComparison.CurrentCultureIgnoreCase) == 0));
                    }
                    // Other special search params: installed, updatable, new, conflicting and dependends
                }
                else if (filter.StartsWith("~"))
                {
                    if (filter.Length <= 1)
                    {
                        // Don't blank the list for just "~" by itself
                        return(true);
                    }
                    else
                    {
                        switch (filter.Substring(1, 1))
                        {
                        case "i":
                            return(registry.IsInstalled(m.identifier, false));

                        case "u":
                            return(registry.HasUpdate(m.identifier, manager.CurrentInstance.VersionCriteria()));

                        case "n":
                            // Filter new
                            return(recent.Contains(m.identifier));

                        case "c":
                            if (m.conflicts != null)
                            {
                                string conflictsWith = filter.Substring(2);
                                // Search for mods depending on a given mod
                                foreach (var rel in m.conflicts)
                                {
                                    if (rel.StartsWith(conflictsWith))
                                    {
                                        return(true);
                                    }
                                }
                            }
                            return(false);

                        case "d":
                            if (m.depends != null)
                            {
                                string dependsOn = filter.Substring(2);
                                // Search for mods depending on a given mod
                                foreach (var rel in m.depends)
                                {
                                    if (rel.StartsWith(dependsOn))
                                    {
                                        return(true);
                                    }
                                }
                            }
                            return(false);
                        }
                    }
                    return(false);
                }
                else
                {
                    filter = CkanModule.nonAlphaNums.Replace(filter, "");

                    return(m.SearchableIdentifier.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
                           m.SearchableName.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
                           m.SearchableAbstract.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
                           m.SearchableDescription.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0);
                }
            }
                );

            searchBox = new ConsoleField(-searchWidth, 2, -1)
            {
                GhostText = () => Focused() == searchBox
                    ? "<Type to search>"
                    : "<Ctrl+F to search>"
            };
            searchBox.OnChange += (ConsoleField sender, string newValue) => {
                moduleList.FilterString = newValue;
            };

            AddObject(new ConsoleLabel(
                          1, 2, -searchWidth - 2,
                          () => $"{moduleList.VisibleRowCount()} mods"
                          ));
            AddObject(searchBox);
            AddObject(moduleList);

            AddBinding(Keys.CtrlQ, (object sender, ConsoleTheme theme) => false);
            AddBinding(Keys.AltX, (object sender, ConsoleTheme theme) => false);
            AddBinding(Keys.F1, (object sender, ConsoleTheme theme) => Help(theme));
            AddBinding(Keys.AltH, (object sender, ConsoleTheme theme) => Help(theme));
            AddBinding(Keys.F5, (object sender, ConsoleTheme theme) => UpdateRegistry(theme));
            AddBinding(Keys.CtrlR, (object sender, ConsoleTheme theme) => UpdateRegistry(theme));
            AddBinding(Keys.CtrlU, (object sender, ConsoleTheme theme) => UpgradeAll(theme));

            // Now a bunch of convenience shortcuts so you don't get stuck in the search box
            searchBox.AddBinding(Keys.PageUp, (object sender, ConsoleTheme theme) => {
                SetFocus(moduleList);
                return(true);
            });
            searchBox.AddBinding(Keys.PageDown, (object sender, ConsoleTheme theme) => {
                SetFocus(moduleList);
                return(true);
            });
            searchBox.AddBinding(Keys.Enter, (object sender, ConsoleTheme theme) => {
                SetFocus(moduleList);
                return(true);
            });

            moduleList.AddBinding(Keys.CtrlF, (object sender, ConsoleTheme theme) => {
                SetFocus(searchBox);
                return(true);
            });
            moduleList.AddBinding(Keys.Escape, (object sender, ConsoleTheme theme) => {
                searchBox.Clear();
                return(true);
            });

            moduleList.AddTip("Enter", "Details",
                              () => moduleList.Selection != null
                              );
            moduleList.AddBinding(Keys.Enter, (object sender, ConsoleTheme theme) => {
                if (moduleList.Selection != null)
                {
                    LaunchSubScreen(theme, new ModInfoScreen(manager, plan, moduleList.Selection, debug));
                }
                return(true);
            });

            // Conditionally show only one of these based on selected mod status

            moduleList.AddTip("+", "Install",
                              () => moduleList.Selection != null && !moduleList.Selection.IsDLC &&
                              !registry.IsInstalled(moduleList.Selection.identifier, false)
                              );
            moduleList.AddTip("+", "Upgrade",
                              () => moduleList.Selection != null && !moduleList.Selection.IsDLC &&
                              registry.HasUpdate(moduleList.Selection.identifier, manager.CurrentInstance.VersionCriteria())
                              );
            moduleList.AddTip("+", "Replace",
                              () => moduleList.Selection != null &&
                              registry.GetReplacement(moduleList.Selection.identifier, manager.CurrentInstance.VersionCriteria()) != null
                              );
            moduleList.AddBinding(Keys.Plus, (object sender, ConsoleTheme theme) => {
                if (moduleList.Selection != null && !moduleList.Selection.IsDLC)
                {
                    if (!registry.IsInstalled(moduleList.Selection.identifier, false))
                    {
                        plan.ToggleInstall(moduleList.Selection);
                    }
                    else if (registry.IsInstalled(moduleList.Selection.identifier, false) &&
                             registry.HasUpdate(moduleList.Selection.identifier, manager.CurrentInstance.VersionCriteria()))
                    {
                        plan.ToggleUpgrade(moduleList.Selection);
                    }
                    else if (registry.GetReplacement(moduleList.Selection.identifier, manager.CurrentInstance.VersionCriteria()) != null)
                    {
                        plan.ToggleReplace(moduleList.Selection.identifier);
                    }
                }
                return(true);
            });

            moduleList.AddTip("-", "Remove",
                              () => moduleList.Selection != null && !moduleList.Selection.IsDLC &&
                              registry.IsInstalled(moduleList.Selection.identifier, false) &&
                              !registry.IsAutodetected(moduleList.Selection.identifier)
                              );
            moduleList.AddBinding(Keys.Minus, (object sender, ConsoleTheme theme) => {
                if (moduleList.Selection != null && !moduleList.Selection.IsDLC &&
                    registry.IsInstalled(moduleList.Selection.identifier, false) &&
                    !registry.IsAutodetected(moduleList.Selection.identifier))
                {
                    plan.ToggleRemove(moduleList.Selection);
                }
                return(true);
            });

            moduleList.AddTip("F8", "Mark auto-installed",
                              () => moduleList.Selection != null && !moduleList.Selection.IsDLC &&
                              (!registry.InstalledModule(moduleList.Selection.identifier)?.AutoInstalled ?? false)
                              );
            moduleList.AddTip("F8", "Mark user-selected",
                              () => moduleList.Selection != null && !moduleList.Selection.IsDLC &&
                              (registry.InstalledModule(moduleList.Selection.identifier)?.AutoInstalled ?? false)
                              );
            moduleList.AddBinding(Keys.F8, (object sender, ConsoleTheme theme) => {
                InstalledModule im = registry.InstalledModule(moduleList.Selection.identifier);
                if (im != null && !moduleList.Selection.IsDLC)
                {
                    im.AutoInstalled = !im.AutoInstalled;
                    RegistryManager.Instance(manager.CurrentInstance).Save(false);
                }
                return(true);
            });

            AddTip("F9", "Apply changes", plan.NonEmpty);
            AddBinding(Keys.F9, (object sender, ConsoleTheme theme) => {
                ApplyChanges(theme);
                return(true);
            });

            // Show total download size of all installed mods
            AddObject(new ConsoleLabel(
                          1, -1, searchWidth,
                          () => $"{CkanModule.FmtSize(totalInstalledDownloadSize())} installed",
                          null,
                          th => th.DimLabelFg
                          ));

            AddObject(new ConsoleLabel(
                          -searchWidth, -1, -2,
                          () => {
                int days = daysSinceUpdated(registryFilePath());
                return(days < 1 ? ""
                        :  days == 1 ? $"Updated at least {days} day ago"
                        :              $"Updated at least {days} days ago");
            },
                          null,
                          (ConsoleTheme th) => {
                int daysSince = daysSinceUpdated(registryFilePath());
                if (daysSince < daysTillStale)
                {
                    return(th.RegistryUpToDate);
                }
                else if (daysSince < daystillVeryStale)
                {
                    return(th.RegistryStale);
                }
                else
                {
                    return(th.RegistryVeryStale);
                }
            }
                          ));

            List <ConsoleMenuOption> opts = new List <ConsoleMenuOption>()
            {
                new ConsoleMenuOption("Sort...", "",
                                      "Change the sorting of the list of mods",
                                      true, null, null, moduleList.SortMenu()),
                null,
                new ConsoleMenuOption("Refresh mod list", "F5, Ctrl+R",
                                      "Refresh the list of mods",
                                      true, UpdateRegistry),
                new ConsoleMenuOption("Upgrade all", "Ctrl+U",
                                      "Mark all available updates for installation",
                                      true, UpgradeAll, null, null, HasAnyUpgradeable()),
                new ConsoleMenuOption("Audit recommendations", "",
                                      "List mods suggested and recommended by installed mods",
                                      true, ViewSuggestions),
                new ConsoleMenuOption("Import downloads...", "",
                                      "Select manually downloaded mods to import into CKAN",
                                      true, ImportDownloads),
                new ConsoleMenuOption("Export installed...", "",
                                      "Save your mod list",
                                      true, ExportInstalled),
                null,
                new ConsoleMenuOption("Select game instance...", "",
                                      "Switch to a different game instance",
                                      true, SelectInstall),
                new ConsoleMenuOption("Authentication tokens...", "",
                                      "Edit authentication tokens sent to download servers",
                                      true, EditAuthTokens),
                null,
                new ConsoleMenuOption("Help", helpKey,
                                      "Tips & tricks",
                                      true, Help),
                null,
                new ConsoleMenuOption("Quit", "Ctrl+Q",
                                      "Exit to DOS",
                                      true, (ConsoleTheme th) => false)
            };

            if (debug)
            {
                opts.Add(null);
                opts.Add(new ConsoleMenuOption("DEBUG: Capture key...", "",
                                               "Print details of how your system reports a keystroke for debugging",
                                               true, CaptureKey));
            }
            mainMenu = new ConsolePopupMenu(opts);
        }
Beispiel #6
0
        /// <summary>
        /// Initialize the Screen
        /// </summary>
        /// <param name="mgr">KSP manager containing game instances</param>
        /// <param name="cp">Plan of other mods to be added or removed</param>
        /// <param name="m">The module to display</param>
        /// <param name="dbg">True if debug options should be available, false otherwise</param>
        public ModInfoScreen(KSPManager mgr, ChangePlan cp, CkanModule m, bool dbg)
        {
            debug    = dbg;
            mod      = m;
            manager  = mgr;
            plan     = cp;
            registry = RegistryManager.Instance(manager.CurrentInstance).registry;

            int midL = Console.WindowWidth / 2 - 1;

            AddObject(new ConsoleLabel(
                          1, 1, -1,
                          () => mod.name == mod.identifier ? mod.name : $"{mod.name} ({mod.identifier})",
                          null,
                          () => ConsoleTheme.Current.ActiveFrameFg
                          ));
            AddObject(new ConsoleLabel(
                          1, 2, -1,
                          () => $"By {string.Join(", ", mod.author)}"
                          ));

            AddObject(new ConsoleFrame(
                          1, 3, midL, 7,
                          () => "",
                          () => ConsoleTheme.Current.NormalFrameFg,
                          false
                          ));
            AddObject(new ConsoleLabel(
                          3, 4, 11,
                          () => "License:",
                          null,
                          () => ConsoleTheme.Current.DimLabelFg
                          ));
            AddObject(new ConsoleLabel(
                          13, 4, midL - 2,
                          () => string.Join(", ", Array.ConvertAll <License, string>(
                                                mod.license.ToArray(), (l => l.ToString())))
                          ));
            AddObject(new ConsoleLabel(
                          3, 5, 12,
                          () => "Download:",
                          null,
                          () => ConsoleTheme.Current.DimLabelFg
                          ));
            AddObject(new ConsoleLabel(
                          13, 5, midL - 2,
                          () => CkanModule.FmtSize(mod.download_size)
                          ));
            AddObject(new ConsoleLabel(
                          3, 6, midL - 2,
                          HostedOn
                          ));

            int depsBot = addDependencies();
            int versBot = addVersionDisplay();

            AddObject(new ConsoleFrame(
                          1, Math.Max(depsBot, versBot) + 1, -1, -1,
                          () => "Description",
                          () => ConsoleTheme.Current.NormalFrameFg,
                          false
                          ));
            ConsoleTextBox tb = new ConsoleTextBox(
                3, Math.Max(depsBot, versBot) + 2, -3, -2, false,
                TextAlign.Left,
                () => ConsoleTheme.Current.MainBg,
                () => ConsoleTheme.Current.LabelFg
                );

            tb.AddLine(mod.@abstract);
            if (!string.IsNullOrEmpty(mod.description) &&
                mod.description != mod.@abstract)
            {
                tb.AddLine(mod.description);
            }
            AddObject(tb);
            if (!ChangePlan.IsAnyAvailable(registry, mod.identifier))
            {
                tb.AddLine("\r\nNOTE: This mod is installed but no longer available.");
                tb.AddLine("If you uninstall it, CKAN will not be able to re-install it.");
            }
            tb.AddScrollBindings(this);

            AddTip("Esc", "Back");
            AddBinding(Keys.Escape, (object sender) => false);

            AddTip("Ctrl+D", "Download",
                   () => !manager.Cache.IsMaybeCachedZip(mod)
                   );
            AddBinding(Keys.CtrlD, (object sender) => {
                Download();
                return(true);
            });

            if (mod.resources != null)
            {
                List <ConsoleMenuOption> opts = new List <ConsoleMenuOption>();

                if (mod.resources.homepage != null)
                {
                    opts.Add(new ConsoleMenuOption(
                                 "Home page", "", "Open the home page URL in a browser",
                                 true,
                                 () => LaunchURL(mod.resources.homepage)
                                 ));
                }
                if (mod.resources.repository != null)
                {
                    opts.Add(new ConsoleMenuOption(
                                 "Repository", "", "Open the repository URL in a browser",
                                 true,
                                 () => LaunchURL(mod.resources.repository)
                                 ));
                }
                if (mod.resources.bugtracker != null)
                {
                    opts.Add(new ConsoleMenuOption(
                                 "Bugtracker", "", "Open the bug tracker URL in a browser",
                                 true,
                                 () => LaunchURL(mod.resources.bugtracker)
                                 ));
                }
                if (mod.resources.spacedock != null)
                {
                    opts.Add(new ConsoleMenuOption(
                                 "SpaceDock", "", "Open the SpaceDock URL in a browser",
                                 true,
                                 () => LaunchURL(mod.resources.spacedock)
                                 ));
                }
                if (mod.resources.curse != null)
                {
                    opts.Add(new ConsoleMenuOption(
                                 "Curse", "", "Open the Curse URL in a browser",
                                 true,
                                 () => LaunchURL(mod.resources.curse)
                                 ));
                }
                if (debug)
                {
                    opts.Add(null);
                    opts.Add(new ConsoleMenuOption(
                                 "DEBUG: View metadata", "", "Display the full registry data for this mod",
                                 true,
                                 ViewMetadata
                                 ));
                }

                if (opts.Count > 0)
                {
                    mainMenu = new ConsolePopupMenu(opts);
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Initialize the popup.
        /// </summary>
        /// <param name="title">String to be shown in the top center of the popup</param>
        /// <param name="startPath">Path of directory to start in</param>
        /// <param name="filPat">Glob-style wildcard string for matching files to show</param>
        /// <param name="toggleHeader">Header for the column with checkmarks for selected files</param>
        public ConsoleFileMultiSelectDialog(string title, string startPath, string filPat, string toggleHeader)
            : base()
        {
            CenterHeader = () => title;
            curDir       = new DirectoryInfo(startPath);
            filePattern  = filPat;

            int w = (Console.WindowWidth > idealW + 2 * hPad)
                ? idealW
                : Console.WindowWidth - 2 * hPad;
            int left  = (Console.WindowWidth - w) / 2;
            int right = -left;

            SetDimensions(left, top, right, bottom);

            AddObject(new ConsoleLabel(
                          left + 2, top + 2, left + 2 + labelW - 1,
                          () => $"Directory:",
                          () => ConsoleTheme.Current.PopupBg,
                          () => ConsoleTheme.Current.PopupFg
                          ));

            pathField = new ConsoleField(
                left + 2 + labelW, top + 2, right - 2,
                curDir.FullName
                );
            pathField.OnChange += pathFieldChanged;
            AddObject(pathField);

            AddObject(new ConsoleLabel(
                          left + 2, bottom - 1, right - 2,
                          () => $"{chosenFiles.Count} selected, {CkanModule.FmtSize(totalChosenSize())}",
                          () => ConsoleTheme.Current.PopupBg,
                          () => ConsoleTheme.Current.PopupFg
                          ));

            // ListBox showing zip files in current dir
            fileList = new ConsoleListBox <FileSystemInfo>(
                left + 2, top + 4, right - 2, bottom - 2,
                getFileList(),
                new List <ConsoleListBoxColumn <FileSystemInfo> >()
            {
                new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header   = toggleHeader,
                    Width    = 8,
                    Renderer = getRowSymbol
                }, new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header   = "Name",
                    Width    = 36,
                    Renderer = getRowName,
                    Comparer = compareNames
                }, new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header = "Size",
                    // Longest: "1023.1 KB"
                    Width    = 9,
                    Renderer = (FileSystemInfo fi) => getLength(fi),
                    Comparer = (a, b) => {
                        FileInfo fa = a as FileInfo, fb = b as FileInfo;
                        return(fa == null
                                ? (fb == null ? 0 : -1)
                                : (fb == null ? 1 : fa.Length.CompareTo(fb.Length)));
                    }
                }, new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header   = "Accessed",
                    Width    = 10,
                    Renderer = (FileSystemInfo fi) => fi.LastWriteTime.ToString("yyyy-MM-dd"),
                    Comparer = (a, b) => a.LastWriteTime.CompareTo(b.LastWriteTime)
                }
            },
                1, 1, ListSortDirection.Ascending
                );
            AddObject(fileList);

            AddTip("Esc", "Cancel");
            AddBinding(Keys.Escape, (object sender) => {
                chosenFiles.Clear();
                return(false);
            });

            AddTip("F10", "Sort");
            AddBinding(Keys.F10, (object sender) => {
                fileList.SortMenu().Run(right - 2, top + 2);
                DrawBackground();
                return(true);
            });

            AddTip("Enter", "Change directory", () => fileList.Selection != null && isDir(fileList.Selection));
            AddTip("Enter", "Select", () => fileList.Selection != null && !isDir(fileList.Selection));
            AddBinding(Keys.Enter, (object sender) => selectRow());
            AddBinding(Keys.Space, (object sender) => selectRow());

            AddTip("Ctrl+A", "Select all");
            AddBinding(Keys.CtrlA, (object sender) => {
                foreach (FileSystemInfo fi in contents)
                {
                    if (!isDir(fi))
                    {
                        FileInfo file = fi as FileInfo;
                        if (file != null)
                        {
                            chosenFiles.Add(file);
                        }
                    }
                }
                return(true);
            });

            AddTip("Ctrl+D", "Deselect all", () => chosenFiles.Count > 0);
            AddBinding(Keys.CtrlD, (object sender) => {
                if (chosenFiles.Count > 0)
                {
                    chosenFiles.Clear();
                }
                return(true);
            });

            AddTip("F9", "Import", () => chosenFiles.Count > 0);
            AddBinding(Keys.F9, (object sender) => {
                return(false);
            });
        }