public void GetPreferredInstance_WithEmptyAutoStartAndMultipleInstances_ReturnsNull() { using (var tidy2 = new DisposableKSP()) { win32_reg.Instances.Add(new Tuple <string, string>("tidy2", tidy2.KSP.GameDir())); manager.LoadInstancesFromRegistry(); manager.ClearAutoStart(); Assert.That(manager.GetPreferredInstance(), Is.Null); } }
/// <summary> /// Initialize the screen. /// </summary> /// <param name="mgr">KSP manager object for getting hte Instances</param> /// <param name="first">If true, this is the first screen after the splash, so Ctrl+Q exits, else Esc exits</param> public KSPListScreen(KSPManager mgr, bool first = false) { manager = mgr; AddObject(new ConsoleLabel( 1, 2, -1, () => "Select a game instance:" )); kspList = new ConsoleListBox <KSP>( 1, 4, -1, -2, manager.Instances.Values, new List <ConsoleListBoxColumn <KSP> >() { new ConsoleListBoxColumn <KSP>() { Header = "Default", Width = 7, Renderer = StatusSymbol }, new ConsoleListBoxColumn <KSP>() { Header = "Name", Width = 20, Renderer = k => k.Name }, new ConsoleListBoxColumn <KSP>() { Header = "Version", Width = 12, Renderer = k => k.Version()?.ToString() ?? noVersion, Comparer = (a, b) => a.Version()?.CompareTo(b.Version() ?? KspVersion.Any) ?? 1 }, new ConsoleListBoxColumn <KSP>() { Header = "Path", Width = 70, Renderer = k => k.GameDir() } }, 1, 0, ListSortDirection.Descending ); if (first) { AddTip("Ctrl+Q", "Quit"); AddBinding(Keys.AltX, (object sender) => false); AddBinding(Keys.CtrlQ, (object sender) => false); } else { AddTip("Esc", "Quit"); AddBinding(Keys.Escape, (object sender) => false); } AddTip("Enter", "Select"); AddBinding(Keys.Enter, (object sender) => { ConsoleMessageDialog d = new ConsoleMessageDialog( $"Loading instance {kspList.Selection.Name}...", new List <string>() ); if (TryGetInstance(kspList.Selection, () => { d.Run(() => {}); })) { try { manager.SetCurrentInstance(kspList.Selection.Name); } catch (Exception ex) { // This can throw if the previous current instance had an error, // since it gets destructed when it's replaced. RaiseError(ex.Message); } return(false); } else { return(true); } }); kspList.AddTip("A", "Add"); kspList.AddBinding(Keys.A, (object sender) => { LaunchSubScreen(new KSPAddScreen(manager)); kspList.SetData(manager.Instances.Values); return(true); }); kspList.AddTip("R", "Remove"); kspList.AddBinding(Keys.R, (object sender) => { manager.RemoveInstance(kspList.Selection.Name); kspList.SetData(manager.Instances.Values); return(true); }); kspList.AddTip("E", "Edit"); kspList.AddBinding(Keys.E, (object sender) => { ConsoleMessageDialog d = new ConsoleMessageDialog( $"Loading instance {kspList.Selection.Name}...", new List <string>() ); TryGetInstance(kspList.Selection, () => { d.Run(() => {}); }); // Still launch the screen even if the load fails, // because you need to be able to fix the name/path. LaunchSubScreen(new KSPEditScreen(manager, kspList.Selection)); return(true); }); kspList.AddTip("D", "Default"); kspList.AddBinding(Keys.D, (object sender) => { string name = kspList.Selection.Name; if (name == manager.AutoStartInstance) { manager.ClearAutoStart(); } else { try { manager.SetAutoStart(name); } catch (NotKSPDirKraken k) { ConsoleMessageDialog errd = new ConsoleMessageDialog( $"Error loading {k.path}:\n{k.Message}", new List <string>() { "OK" } ); errd.Run(); } } return(true); }); AddObject(kspList); mainMenu = kspList.SortMenu(); }