Пример #1
0
        public int RunCommand(CKAN.KSP ksp, object raw_options)
        {
            UpgradeOptions options = (UpgradeOptions)raw_options;

            if (options.ckan_file != null)
            {
                options.modules.Add(LoadCkanFromFile(ksp, options.ckan_file).identifier);
            }

            if (options.modules.Count == 0 && !options.upgrade_all)
            {
                // What? No files specified?
                User.RaiseMessage("Usage: cfan upgrade Mod [Mod2, ...]");
                User.RaiseMessage("  or   cfan upgrade --all");
                User.RaiseMessage("  or   cfan upgrade ckan");
                return(Exit.BADOPT);
            }

            if (!options.upgrade_all && options.modules[0] == "cfan")
            {
                User.RaiseMessage("Querying the latest CFAN version");
                AutoUpdate.Instance.FetchLatestReleaseInfo();
                var latestVersion  = AutoUpdate.Instance.LatestVersion;
                var currentVersion = new ModVersion(Meta.Version());

                if (latestVersion.IsGreaterThan(currentVersion))
                {
                    User.RaiseMessage("New CFAN version available - " + latestVersion);
                    var releaseNotes = AutoUpdate.Instance.ReleaseNotes;
                    User.RaiseMessage(releaseNotes);
                    User.RaiseMessage("\n");

                    if (User.RaiseYesNoDialog("Proceed with install?"))
                    {
                        User.RaiseMessage("Upgrading CFAN, please wait..");
                        AutoUpdate.Instance.StartUpdateProcess(false);
                    }
                }
                else
                {
                    User.RaiseMessage("You already have the latest version.");
                }

                return(Exit.OK);
            }

            User.RaiseMessage("\nUpgrading modules...\n");

            try
            {
                if (options.upgrade_all)
                {
                    var installed  = new Dictionary <string, AbstractVersion>(ksp.Registry.Installed(true));
                    var to_upgrade = new List <CfanModule>();

                    foreach (KeyValuePair <string, AbstractVersion> mod in installed)
                    {
                        AbstractVersion current_version = mod.Value;

                        if ((current_version is ProvidedVersion) || (current_version is AutodetectedVersion))
                        {
                            continue;
                        }
                        try
                        {
                            // Check if upgrades are available
                            CfanModule latest = ksp.Registry.LatestAvailable(mod.Key, ksp.Version());

                            // This may be an unindexed mod. If so,
                            // skip rather than crash. See KSP-CKAN/CKAN#841.
                            if (latest == null)
                            {
                                continue;
                            }

                            if (latest.modVersion.IsGreaterThan(mod.Value))
                            {
                                // Upgradable
                                log.InfoFormat("New version {0} found for {1}",
                                               latest.modVersion, latest.identifier);
                                to_upgrade.Add(latest);
                            }
                        }
                        catch (ModuleNotFoundKraken)
                        {
                            log.InfoFormat("{0} is installed, but no longer in the registry",
                                           mod.Key);
                        }
                    }

                    ModuleInstaller.GetInstance(ksp, User).Upgrade(to_upgrade, new NetAsyncModulesDownloader(User, ksp.tryGetFactorioAuthData()));
                }
                else
                {
                    // TODO: These instances all need to go.
                    ModuleInstaller.GetInstance(ksp, User).Upgrade(options.modules, new NetAsyncModulesDownloader(User, ksp.tryGetFactorioAuthData()));
                }
            }
            catch (ModuleNotFoundKraken kraken)
            {
                User.RaiseMessage("Module {0} not found", kraken.module);
                return(Exit.ERROR);
            }
            User.RaiseMessage("\nDone!\n");

            return(Exit.OK);
        }