Esempio n. 1
0
        public int RunSubCommand(GameInstanceManager manager, CommonOptions opts, SubCommandOptions options)
        {
            var exitCode = Exit.OK;

            Parser.Default.ParseArgumentsStrict(options.options.ToArray(), new CompatOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions comOpts = (CommonOptions)suboptions;
                    comOpts.Merge(opts);
                    _user       = new ConsoleUser(comOpts.Headless);
                    _kspManager = manager ?? new GameInstanceManager(_user);
                    exitCode    = comOpts.Handle(_kspManager, _user);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "list":
                        {
                            var ksp = MainClass.GetGameInstance(_kspManager);

                            const string versionHeader = "Version";
                            const string actualHeader  = "Actual";

                            var output = ksp
                                         .GetCompatibleVersions()
                                         .Select(i => new
                            {
                                Version = i,
                                Actual  = false
                            })
                                         .ToList();

                            output.Add(new
                            {
                                Version = ksp.Version(),
                                Actual  = true
                            });

                            output = output
                                     .OrderByDescending(i => i.Actual)
                                     .ThenByDescending(i => i.Version)
                                     .ToList();

                            var versionWidth = Enumerable
                                               .Repeat(versionHeader, 1)
                                               .Concat(output.Select(i => i.Version.ToString()))
                                               .Max(i => i.Length);

                            var actualWidth = Enumerable
                                              .Repeat(actualHeader, 1)
                                              .Concat(output.Select(i => i.Actual.ToString()))
                                              .Max(i => i.Length);

                            const string columnFormat = "{0}  {1}";

                            _user.RaiseMessage(string.Format(columnFormat,
                                                             versionHeader.PadRight(versionWidth),
                                                             actualHeader.PadRight(actualWidth)
                                                             ));

                            _user.RaiseMessage(string.Format(columnFormat,
                                                             new string('-', versionWidth),
                                                             new string('-', actualWidth)
                                                             ));

                            foreach (var line in output)
                            {
                                _user.RaiseMessage(string.Format(columnFormat,
                                                                 line.Version.ToString().PadRight(versionWidth),
                                                                 line.Actual.ToString().PadRight(actualWidth)
                                                                 ));
                            }
                        }
                        break;

                    case "add":
                        {
                            var ksp        = MainClass.GetGameInstance(_kspManager);
                            var addOptions = (CompatAddOptions)suboptions;

                            GameVersion GameVersion;
                            if (GameVersion.TryParse(addOptions.Version, out GameVersion))
                            {
                                var newCompatibleVersion = ksp.GetCompatibleVersions();
                                newCompatibleVersion.Add(GameVersion);
                                ksp.SetCompatibleVersions(newCompatibleVersion);
                            }
                            else
                            {
                                _user.RaiseError("ERROR: Invalid KSP version.");
                                exitCode = Exit.ERROR;
                            }
                        }
                        break;

                    case "forget":
                        {
                            var ksp        = MainClass.GetGameInstance(_kspManager);
                            var addOptions = (CompatForgetOptions)suboptions;

                            GameVersion GameVersion;
                            if (GameVersion.TryParse(addOptions.Version, out GameVersion))
                            {
                                if (GameVersion != ksp.Version())
                                {
                                    var newCompatibleVersion = ksp.GetCompatibleVersions();
                                    newCompatibleVersion.RemoveAll(i => i == GameVersion);
                                    ksp.SetCompatibleVersions(newCompatibleVersion);
                                }
                                else
                                {
                                    _user.RaiseError("ERROR: Cannot forget actual KSP version.");
                                    exitCode = Exit.ERROR;
                                }
                            }
                            else
                            {
                                _user.RaiseError("ERROR: Invalid KSP version.");
                                exitCode = Exit.ERROR;
                            }
                        }
                        break;

                    default:
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }