public override int Handle(GameInstanceManager manager, IUser user) { int exitCode = base.Handle(manager, user); if (exitCode == Exit.OK) { // User provided game instance if (Gamedir != null && Instance != null) { user.RaiseMessage("--instance and --gamedir can't be specified at the same time"); return(Exit.BADOPT); } try { if (!string.IsNullOrEmpty(Instance)) { // Set a game directory by its alias. manager.SetCurrentInstance(Instance); } else if (!string.IsNullOrEmpty(Gamedir)) { // Set a game directory by its path manager.SetCurrentInstanceByPath(Gamedir); } } catch (NotKSPDirKraken k) { user.RaiseMessage("Sorry, {0} does not appear to be a game instance", k.path); return(Exit.BADOPT); } catch (InvalidKSPInstanceKraken k) { user.RaiseMessage("Invalid game instance specified \"{0}\", use '--gamedir' to specify by path, or 'instance list' to see known game instances", k.instance); return(Exit.BADOPT); } } return(exitCode); }
/// <summary> /// Initialize the screen. /// </summary> /// <param name="mgr">Game instance 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 GameInstanceListScreen(GameInstanceManager mgr, bool first = false) { manager = mgr; AddObject(new ConsoleLabel( 1, 2, -1, () => "Select or add a game instance:" )); instanceList = new ConsoleListBox <GameInstance>( 1, 4, -1, -2, manager.Instances.Values, new List <ConsoleListBoxColumn <GameInstance> >() { new ConsoleListBoxColumn <GameInstance>() { Header = "Default", Width = 7, Renderer = StatusSymbol }, new ConsoleListBoxColumn <GameInstance>() { Header = "Name", Width = 20, Renderer = k => k.Name }, new ConsoleListBoxColumn <GameInstance>() { Header = "Game", Width = 5, Renderer = k => k.game.ShortName }, new ConsoleListBoxColumn <GameInstance>() { Header = "Version", Width = 12, Renderer = k => k.Version()?.ToString() ?? noVersion, Comparer = (a, b) => a.Version()?.CompareTo(b.Version() ?? GameVersion.Any) ?? 1 }, new ConsoleListBoxColumn <GameInstance>() { Header = "Path", Width = 70, Renderer = k => k.GameDir() } }, 1, 0, ListSortDirection.Descending ); if (first) { AddTip("Ctrl+Q", "Quit"); AddBinding(Keys.AltX, (object sender, ConsoleTheme theme) => false); AddBinding(Keys.CtrlQ, (object sender, ConsoleTheme theme) => false); } else { AddTip("Esc", "Quit"); AddBinding(Keys.Escape, (object sender, ConsoleTheme theme) => false); } AddTip("Enter", "Select"); AddBinding(Keys.Enter, (object sender, ConsoleTheme theme) => { ConsoleMessageDialog d = new ConsoleMessageDialog( $"Loading instance {instanceList.Selection.Name}...", new List <string>() ); if (TryGetInstance(theme, instanceList.Selection, (ConsoleTheme th) => { d.Run(th, (ConsoleTheme thm) => {}); })) { try { manager.SetCurrentInstance(instanceList.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); } }); instanceList.AddTip("A", "Add"); instanceList.AddBinding(Keys.A, (object sender, ConsoleTheme theme) => { LaunchSubScreen(theme, new GameInstanceAddScreen(manager)); instanceList.SetData(manager.Instances.Values); return(true); }); instanceList.AddTip("R", "Remove"); instanceList.AddBinding(Keys.R, (object sender, ConsoleTheme theme) => { manager.RemoveInstance(instanceList.Selection.Name); instanceList.SetData(manager.Instances.Values); return(true); }); instanceList.AddTip("E", "Edit"); instanceList.AddBinding(Keys.E, (object sender, ConsoleTheme theme) => { ConsoleMessageDialog d = new ConsoleMessageDialog( $"Loading instance {instanceList.Selection.Name}...", new List <string>() ); TryGetInstance(theme, instanceList.Selection, (ConsoleTheme th) => { d.Run(theme, (ConsoleTheme thm) => {}); }); // Still launch the screen even if the load fails, // because you need to be able to fix the name/path. LaunchSubScreen(theme, new GameInstanceEditScreen(manager, instanceList.Selection)); return(true); }); instanceList.AddTip("D", "Default"); instanceList.AddBinding(Keys.D, (object sender, ConsoleTheme theme) => { string name = instanceList.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(theme); } } return(true); }); AddObject(instanceList); mainMenu = instanceList.SortMenu(); }
public void SetCurrentInstance_NameNotInRepo_Throws() { Assert.Throws <InvalidKSPInstanceKraken>(() => manager.SetCurrentInstance("invalid")); }