public LaunchManagerResult LaunchGameHandlingDelaysAndTitles(BackgroundWorker worker)
        {
            var result = new LaunchManagerResult();

            if (worker.CancellationPending)
            {
                return(result);
            }
            DateTime lastLaunchUtc = (_accountLaunchTimes.ContainsKey(_launchItem.AccountName)
                                       ? _accountLaunchTimes[_launchItem.AccountName]
                                       : DateTime.MinValue);
            // Add a 5 second delay before launching the same account. EMU servers won't have cross-server timouts like Live servers did.
            TimeSpan         delay            = new TimeSpan(0, 0, 5) - (DateTime.UtcNow - lastLaunchUtc);
            GameLaunchResult gameLaunchResult = null;

            while (delay.TotalMilliseconds > 0)
            {
                if (worker.CancellationPending)
                {
                    return(result);
                }
                string context = string.Format("Waiting {0} sec", (int)delay.TotalSeconds + 1);
                ReportStatus(context, _launchItem);

                System.Threading.Thread.Sleep(1000);
                delay = new TimeSpan(0, 0, 5) - (DateTime.UtcNow - lastLaunchUtc);
            }

            ReportStatus("Launching", _launchItem);
            _accountLaunchTimes[_launchItem.AccountName] = DateTime.UtcNow;

            var launcher = new GameLauncher();

            launcher.ReportGameStatusEvent += (o) => { ReportStatus(o, _launchItem); };
            launcher.StopLaunchEvent       += (o, eventArgs) => { return(worker.CancellationPending); };
            try
            {
                var finder = new ThwargUtils.WindowFinder();
                finder.RecordExistingWindows();
                string launcherPath = GetLaunchItemLauncherLocation(_launchItem);
                OverridePreferenceFile(_launchItem.CustomPreferencePath);
                gameLaunchResult = launcher.LaunchGameClient(
                    launcherPath,
                    _launchItem.ServerName,
                    accountName: _launchItem.AccountName,
                    password: _launchItem.Password,
                    ipAddress: _launchItem.IpAndPort,
                    gameApiUrl: _launchItem.GameApiUrl,
                    loginServerUrl: _launchItem.LoginServerUrl,
                    discordurl: _launchItem.DiscordUrl,
                    emu: _launchItem.EMU,
                    desiredCharacter: _launchItem.CharacterSelected,
                    rodatSetting: _launchItem.RodatSetting,
                    secureSetting: _launchItem.SecureSetting,
                    simpleLaunch: _launchItem.IsSimpleLaunch
                    );
                if (!gameLaunchResult.Success)
                {
                    return(result);
                }
                string gameCaptionPattern = ConfigSettings.GetConfigString("GameCaptionPattern", null);
                if (gameCaptionPattern != null)
                {
                    var    regex = new System.Text.RegularExpressions.Regex(gameCaptionPattern);
                    IntPtr hwnd  = finder.FindNewWindow(regex);
                    if (hwnd != IntPtr.Zero)
                    {
                        string newGameTitle = GetNewGameTitle(_launchItem);
                        if (!string.IsNullOrEmpty(newGameTitle))
                        {
                            finder.SetWindowTitle(hwnd, newGameTitle);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                ReportStatus("Exception launching game launcher: " + exc.Message, _launchItem);
                return(result);
            }
            if (gameLaunchResult != null && gameLaunchResult.Success)
            {
                result.Success   = true;
                result.ProcessId = gameLaunchResult.ProcessId;
            }
            return(result);
        }
Пример #2
0
        public LaunchManagerResult LaunchGameHandlingDelaysAndTitles(BackgroundWorker worker)
        {
            var result = new LaunchManagerResult();

            if (worker.CancellationPending)
            {
                return(result);
            }

            GameLaunchResult gameLaunchResult = null;

            ReportStatus("Launching", _launchItem);
            _accountLaunchTimes[_launchItem.AccountName] = DateTime.UtcNow;

            var launcher = new GameLauncher();

            launcher.ReportGameStatusEvent += (o) => { ReportStatus(o, _launchItem); };
            launcher.StopLaunchEvent       += (o, eventArgs) => { return(worker.CancellationPending); };
            try
            {
                var    finder       = new ThwargUtils.WindowFinder();
                string launcherPath = GetLaunchItemLauncherLocation(_launchItem);
                OverridePreferenceFile(_launchItem.CustomPreferencePath);
                gameLaunchResult = launcher.LaunchGameClient(
                    launcherPath,
                    _launchItem.ServerName,
                    accountName: _launchItem.AccountName,
                    password: _launchItem.Password,
                    ipAddress: _launchItem.IpAndPort,
                    gameApiUrl: _launchItem.GameApiUrl,
                    loginServerUrl: _launchItem.LoginServerUrl,
                    discordurl: _launchItem.DiscordUrl,
                    emu: _launchItem.EMU,
                    desiredCharacter: _launchItem.CharacterSelected,
                    rodatSetting: _launchItem.RodatSetting,
                    secureSetting: _launchItem.SecureSetting,
                    simpleLaunch: _launchItem.IsSimpleLaunch
                    );
                if (!gameLaunchResult.Success)
                {
                    return(result);
                }
                var regex = GetGameWindowCaptionRegex();
                if (regex != null)
                {
                    IntPtr hwnd = finder.FindWindowByCaptionAndProcessId(regex, newWindow: true, processId: gameLaunchResult.ProcessId);
                    if (hwnd != IntPtr.Zero)
                    {
                        result.Hwnd = hwnd;
                        string newGameTitle = GetNewGameTitle(_launchItem);
                        if (!string.IsNullOrEmpty(newGameTitle))
                        {
                            Logger.WriteDebug("Found hwnd: " + newGameTitle);
                            finder.SetWindowTitle(hwnd, newGameTitle);
                        }
                    }
                    else
                    {
                        Logger.WriteDebug("Unable to find hwnd");
                    }
                }
            }
            catch (Exception exc)
            {
                ReportStatus("Exception launching game launcher: " + exc.Message, _launchItem);
                return(result);
            }
            if (gameLaunchResult != null && gameLaunchResult.Success)
            {
                result.Success   = true;
                result.ProcessId = gameLaunchResult.ProcessId;
            }
            return(result);
        }