示例#1
0
        /// <summary>
        /// Initialize the screen
        /// </summary>
        /// <param name="mgr">KSP manager object containing the current instance</param>
        /// <param name="dbg">True if debug options should be available, false otherwise</param>
        public ModListScreen(KSPManager 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 => Formatting.StripEpoch(m.version?.ToString() ?? ""),
                    Comparer = (a, b) => a.version.CompareTo(b.version)
                }, new ConsoleListBoxColumn <CkanModule>()
                {
                    Header   = "Max KSP 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) => {
                if (filter.StartsWith("@"))
                {
                    string authorFilt = filter.Substring(1);
                    if (string.IsNullOrEmpty(authorFilt))
                    {
                        return(true);
                    }
                    else if (m.author != null)
                    {
                        foreach (string auth in m.author)
                        {
                            if (auth.IndexOf(authorFilt, StringComparison.CurrentCultureIgnoreCase) == 0)
                            {
                                return(true);
                            }
                        }
                    }
                    return(false);
                }
                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.name.IndexOf(conflictsWith, StringComparison.CurrentCultureIgnoreCase) == 0)
                                    {
                                        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.name.IndexOf(dependsOn, StringComparison.CurrentCultureIgnoreCase) == 0)
                                    {
                                        return(true);
                                    }
                                }
                            }
                            return(false);
                        }
                    }
                    return(false);
                }
                else
                {
                    return(m.identifier.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
                           m.name.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
                           [email protected](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) => false);
            AddBinding(Keys.AltX, (object sender) => false);
            AddBinding(Keys.F1, (object sender) => Help());
            AddBinding(Keys.AltH, (object sender) => Help());
            AddBinding(Keys.F5, (object sender) => UpdateRegistry());
            AddBinding(Keys.CtrlR, (object sender) => UpdateRegistry());
            AddBinding(Keys.CtrlU, (object sender) => UpgradeAll());

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

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

            moduleList.AddTip("Enter", "Details",
                              () => moduleList.Selection != null
                              );
            moduleList.AddBinding(Keys.Enter, (object sender) => {
                if (moduleList.Selection != null)
                {
                    LaunchSubScreen(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 &&
                              !registry.IsInstalled(moduleList.Selection.identifier, false)
                              );
            moduleList.AddTip("+", "Upgrade",
                              () => moduleList.Selection != null &&
                              registry.HasUpdate(moduleList.Selection.identifier, manager.CurrentInstance.VersionCriteria())
                              );
            moduleList.AddBinding(Keys.Plus, (object sender) => {
                if (moduleList.Selection != null)
                {
                    if (!registry.IsInstalled(moduleList.Selection.identifier, false))
                    {
                        plan.ToggleInstall(moduleList.Selection.identifier);
                    }
                    else if (registry.IsInstalled(moduleList.Selection.identifier, false) &&
                             registry.HasUpdate(moduleList.Selection.identifier, manager.CurrentInstance.VersionCriteria()))
                    {
                        plan.ToggleUpgrade(moduleList.Selection.identifier);
                    }
                }
                return(true);
            });

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

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

            // Show total download size of all installed mods
            AddObject(new ConsoleLabel(
                          1, -1, searchWidth,
                          () => $"{Formatting.FmtSize(totalInstalledDownloadSize())} installed",
                          null,
                          () => ConsoleTheme.Current.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,
                          () => {
                int daysSince = daysSinceUpdated(registryFilePath());
                if (daysSince < daysTillStale)
                {
                    return(ConsoleTheme.Current.RegistryUpToDate);
                }
                else if (daysSince < daystillVeryStale)
                {
                    return(ConsoleTheme.Current.RegistryStale);
                }
                else
                {
                    return(ConsoleTheme.Current.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),
                new ConsoleMenuOption("Audit recommendations", "",
                                      "List mods suggested and recommended by installed mods",
                                      true, ViewSuggestions),
                new ConsoleMenuOption("Scan KSP dir", "",
                                      "Check for manually installed mods",
                                      true, ScanForMods),
                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 KSP install...", "",
                                      "Switch to a different game instance",
                                      true, SelectInstall),
                null,
                new ConsoleMenuOption("Help", helpKey,
                                      "Tips & tricks",
                                      true, Help),
                null,
                new ConsoleMenuOption("Quit", "Ctrl+Q",
                                      "Exit to DOS",
                                      true, () => 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);

            LeftHeader   = () => $"CKAN {Meta.GetVersion()}";
            CenterHeader = () => $"KSP {manager.CurrentInstance.Version().ToString()} ({manager.CurrentInstance.Name})";
        }
示例#2
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,
                          () => Formatting.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.CurrentInstance.Cache.IsCachedZip(mod.download)
                   );
            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);
                }
            }

            LeftHeader   = () => $"CKAN {Meta.GetVersion()}";
            CenterHeader = () => "Mod Details";
        }