コード例 #1
0
        /// <summary>
        /// Creates an Overwatch process using the currently logged in battle.net account.
        /// </summary>
        /// <param name="processInfo">Parameters for creating the process.</param>
        /// <returns>The created Overwatch process.</returns>
        public static Process StartOverwatch(OverwatchInfoAuto processInfo = null)
        {
            // Return any Overwatch process that might already be running.
            Process foundProcess = GetOverwatchProcess();

            if (foundProcess != null)
            {
                return(foundProcess);
            }

            #region Argument check
            if (processInfo == null)
            {
                processInfo = new OverwatchInfoAuto();
            }

            // Check if the files in processInfo exist.
            if (!File.Exists(processInfo.BattlenetExecutableFilePath))
            {
                throw new FileNotFoundException($"Battle.net.exe's executable at {processInfo.BattlenetExecutableFilePath} was not found. " +
                                                "Change OverwatchInfoAuto.BattlenetExecutableFilePath to the location of the battle.net.exe executable.");
            }

            if (!File.Exists(processInfo.OverwatchSettingsFilePath))
            {
                throw new FileNotFoundException($"Overwatch's settings file at {processInfo.OverwatchSettingsFilePath} was not found. " +
                                                "Change OverwatchInfoAuto.OverwatchSettingsFilePath to the location of Overwatch's settings file.");
            }
            #endregion

            #region Start battle.net
            // If battle.net is not started, start it.
            if (Process.GetProcessesByName("battle.net").Length == 0)
            {
                CustomGameDebug.WriteLine("No battle.net process found, starting battle.net.");

                Process battlenet = new Process();
                battlenet.StartInfo.FileName = processInfo.BattlenetExecutableFilePath;
                battlenet.Start();

                // The battle.net app is fully started when there are 3 battle.net processes. Loop while there are less than 3.
                if (!SpinWait.SpinUntil(() =>
                {
                    return(Process.GetProcessesByName("battle.net").Length >= 3);
                }, processInfo.MaxBattlenetStartTime))
                {
                    // This code block will run if battle.net isn't finished setting up before the specified maximum time.
                    CustomGameDebug.WriteLine("Error: Battle.net took too long to start.");
                    throw new OverwatchStartFailedException("Battle.net took too long to start.");
                }
                CustomGameDebug.WriteLine("Finished starting Battle.net.");
            }
            else
            {
                CustomGameDebug.WriteLine("Battle.net process found.");
            }
            #endregion

            CustomGameDebug.WriteLine("Starting the Overwatch process.");

            // Set the video settings.
            var initialSettings = ChangeVideoSettings(processInfo.OverwatchSettingsFilePath, VideoSettings.Item1, VideoSettings.Item2);

            try
            {
                try
                {
                    Process battlenetOW = new Process();
                    // The arguments to start the game directly before August 2018:
                    // battlenet.StartInfo.FileName = "battlenet://Pro";
                    // The arguments after:
                    battlenetOW.StartInfo.FileName  = processInfo.BattlenetExecutableFilePath;
                    battlenetOW.StartInfo.Arguments = "--exec=\"launch Pro\"";
                    battlenetOW.Start();

                    Process owProcess = null;

                    TimeSpan overwatchStartTimeSpan = new TimeSpan(0, 0, 0, 0, processInfo.MaxOverwatchStartTime);

                    if (!SpinWait.SpinUntil(() =>
                    {
                        owProcess = GetOverwatchProcess();
                        return(owProcess != null);
                    }, overwatchStartTimeSpan))
                    {
                        CustomGameDebug.WriteLine("Error: Overwatch took too long to start.");
                        throw new OverwatchStartFailedException("Overwatch took too long to start.");
                    }

                    // Wait for the window to be visible.
                    if (!SpinWait.SpinUntil(() =>
                    {
                        owProcess.Refresh();
                        return(!string.IsNullOrEmpty(owProcess.MainWindowTitle));
                    }, overwatchStartTimeSpan))
                    {
                        CustomGameDebug.WriteLine("Error: Overwatch took too long to start.");
                        throw new OverwatchStartFailedException("Overwatch took too long to start.");
                    }

                    RestoreVideoSettings(processInfo.OverwatchSettingsFilePath, initialSettings);
                    initialSettings = null;

                    if (processInfo.AutomaticallyCreateCustomGame)
                    {
                        CustomGame cg = new CustomGame(new CustomGameBuilder()
                        {
                            OpenChatIsDefault = false,
                            ScreenshotMethod  = processInfo.ScreenshotMethod,
                            OverwatchProcess  = owProcess
                        });

                        if (WaitForMainMenu(cg, processInfo.MaxWaitForMenuTime))
                        {
                            cg.CreateCustomGame();
                        }
                        else
                        {
                            CustomGameDebug.WriteLine("Could not start Overwatch, main menu did not load.");

                            if (processInfo.CloseOverwatchProcessOnFailure)
                            {
                                owProcess.CloseMainWindow();
                            }

                            throw new OverwatchStartFailedException("Could not start Overwatch, main menu did not load.");
                        }
                    }

                    CustomGameDebug.WriteLine("Finished starting Overwatch.");
                    return(owProcess);
                }
                finally
                {
                    CustomGameDebug.WriteLine("Restoring video settings.");
                    if (initialSettings != null)
                    {
                        RestoreVideoSettings(processInfo.OverwatchSettingsFilePath, initialSettings);
                    }
                }
            }
            catch (OverwatchClosedException)
            {
                CustomGameDebug.WriteLine("Could not start Overwatch, was closed during initialization.");
                throw new OverwatchStartFailedException("Could not start Overwatch, was closed during initialization.");
            }
        }