Esempio n. 1
0
        public static int RunCommandLine(string[] args, Func <string[], GuiOptions, int> showGuiFunc = null)
        {
            // Launch debugger if the "--debugger" flag is present in the command line arguments.
            // We want to do this as early as possible so just check the flag manually, rather than doing the
            // more robust argument parsing.
            if (args.Any(i => i == "--debugger"))
            {
                Debugger.Launch();
            }

            if (args.Length == 1 && args.Any(i => i == "--verbose" || i == "--debug"))
            {
                // Start the gui with logging enabled #437
                List <string> guiCommand = args.ToList();
                guiCommand.Insert(0, "gui");
                guiCommand.Add("--show-console");
                args = guiCommand.ToArray();
            }

            BasicConfigurator.Configure();
            LogManager.GetRepository().Threshold = Level.Warn;
            log.Debug("CFAN started");

            Options cmdline;

            // If we're starting with no options then invoke the GUI instead (if compiled with GUI)
            if (args.Length == 0)
            {
                if (showGuiFunc != null)
                {
                    return(showGuiFunc(args, null));
                }
                args = new[] { "--help" };
            }

            IUser user;

            try
            {
                cmdline = new Options(args);
            }
            catch (BadCommandKraken)
            {
                // Our help screen will already be shown. Let's add some extra data.
                user = new ConsoleUser(false);
                user.RaiseMessage("You are using CFAN version {0}", Meta.Version());

                return(Exit.BADOPT);
            }

            // Process commandline options.

            var options = (CommonOptions)cmdline.options;

            user = new ConsoleUser(options.Headless);
            CheckMonoVersion(user, 3, 1, 0);

            if ((Platform.IsUnix || Platform.IsMac) && CmdLineUtil.GetUID() == 0)
            {
                if (!options.AsRoot)
                {
                    user.RaiseError(@"You are trying to run CFAN as root.
This is a bad idea and there is absolutely no good reason to do it. Please run CFAN from a user account (or use --asroot if you are feeling brave).");
                    return(Exit.ERROR);
                }
                user.RaiseMessage("Warning: Running CFAN as root!");
            }

            if (options.Debug)
            {
                LogManager.GetRepository().Threshold = Level.Debug;
                log.Info("Debug logging enabled");
            }
            else if (options.Verbose)
            {
                LogManager.GetRepository().Threshold = Level.Info;
                log.Info("Verbose logging enabled");
            }

            // Assign user-agent string if user has given us one
            if (options.NetUserAgent != null)
            {
                Net.UserAgentString = options.NetUserAgent;
            }

            // User provided KSP instance

            if (options.FactorioDirectory != null && options.FactorioInstallName != null)
            {
                user.RaiseMessage("--factorio and --factorio-dir can't be specified at the same time");
                return(Exit.BADOPT);
            }
            KSPManager manager = new KSPManager(user);

            if (options.FactorioInstallName != null)
            {
                // Set a KSP directory by its alias.

                try
                {
                    manager.SetCurrentInstance(options.FactorioInstallName);
                }
                catch (InvalidKSPInstanceKraken)
                {
                    user.RaiseMessage("Invalid Factorio installation specified \"{0}\", use '--factorio-dir' to specify by path, or 'list-installs' to see known Factorio installations", options.FactorioInstallName);
                    return(Exit.BADOPT);
                }
            }
            else if (options.FactorioDirectory != null)
            {
                // Set a KSP directory by its path
                manager.SetCurrentInstanceByPath(options.FactorioDirectory);
            }
            else if (cmdline.action != "factorio" && cmdline.action != "version" && cmdline.action != "gui")
            {
                // Find whatever our preferred instance is.
                // We don't do this on `ksp/version` commands, they don't need it.
                CKAN.KSP ksp = manager.GetPreferredInstance();

                if (ksp == null)
                {
                    user.RaiseMessage("I don't know where Factorio is installed.");
                    user.RaiseMessage("Use 'cfan factorio help' for assistance on setting this.");
                    return(Exit.ERROR);
                }
                log.InfoFormat("Using Factorio install at {0} with data dir set to {1}", ksp.GameDir(), ksp.GameData());

                if (ksp.lacksFactorioAuthData())
                {
                    user.RaiseError(
                        "Your config file located in {0} does not contain Factorio authorization data. Mods from official factorio.com mod portal will not be shown.\n\rYou can fix it by using in-game mod portal once. For headless you can copy values of service-username and service-token from your regular Factorio install.",
                        new object[] { ksp.getFactorioAuthDataPath() }
                        );
                }
            }

            switch (cmdline.action)
            {
            case "gui":
                return(ShowGui(args, user, (GuiOptions)options, showGuiFunc));

            case "version":
                return(Version(user));

            case "update":
                return((new Update(user)).RunCommand(manager.CurrentInstance, (UpdateOptions)cmdline.options));

            case "available":
                return(Available(manager.CurrentInstance, user));

            case "install":
                Scan(manager.CurrentInstance, user, cmdline.action);
                return((new Install(user)).RunCommand(manager.CurrentInstance, (InstallOptions)cmdline.options));

            case "scan":
                return(Scan(manager.CurrentInstance, user));

            case "list":
                return((new List(user)).RunCommand(manager.CurrentInstance, (ListOptions)cmdline.options));

            case "show":
                return((new Show(user)).RunCommand(manager.CurrentInstance, (ShowOptions)cmdline.options));

            case "search":
                return((new Search(user)).RunCommand(manager.CurrentInstance, options));

            case "remove":
                return((new Remove(user)).RunCommand(manager.CurrentInstance, cmdline.options));

            case "upgrade":
                Scan(manager.CurrentInstance, user, cmdline.action);
                return((new Upgrade(user)).RunCommand(manager.CurrentInstance, cmdline.options));

            case "clean":
                return(Clean(manager.CurrentInstance));

            case "repair":
                var repair = new Repair(manager.CurrentInstance, user);
                return(repair.RunSubCommand((SubCommandOptions)cmdline.options));

            case "factorio":
                var ksp = new KSP(manager, user);
                return(ksp.RunSubCommand((SubCommandOptions)cmdline.options));

            case "repo":
                var repo = new Repo(manager, user);
                return(repo.RunSubCommand((SubCommandOptions)cmdline.options));

            case "compare":
                return((new Compare(user)).RunCommand(manager.CurrentInstance, cmdline.options));

            default:
                user.RaiseMessage("Unknown command, try --help");
                return(Exit.BADOPT);
            }
        }