Beispiel #1
0
        private void OnProcessAcquired(ProcessMonitor m, ProcessEventArgs e)
        {
            ProcessUtils.Logger($"MONITOR@{ProcessName}",
                                $"Process acquired in {ProcessUtils.ElapsedToString(e.Elapsed)}: {e.ProcessName}.exe [{e.TargetProcess.Id}]");

            HasAcquired = true;
            MonitorTimer.Change(Timeout.Infinite, Timeout.Infinite);
            SearchTimer.Change(0, Interval);
            ProcessAcquired?.Invoke(m, e);
        }
        private void OnProcessHardExit(ProcessMonitor m, ProcessEventArgs e)
        {
            if (e.TargetProcess != null)
            {
                ProcessUtils.Logger($"MONITOR@{ProcessName}",
                                    $"Timed out after {ProcessUtils.ElapsedToString(e.Elapsed)} searching for a matching process: {e.ProcessName}.exe");
            }
            else
            {
                ProcessUtils.Logger($"MONITOR@{ProcessName}",
                                    $"Could not detect a running process after waiting {ProcessUtils.ElapsedToString(e.Elapsed)}...");
            }

            Stop();
            ProcessHardExit?.Invoke(m, e);
        }
Beispiel #3
0
        private void OnGameAcquired(object sender, ProcessEventArgs e)
        {
            if (SetHnd.Options.GameProcessAffinity > 0)
            {
                GamePL.ProcWrapper.Proc.ProcessorAffinity = (IntPtr)SetHnd.Options.GameProcessAffinity;
                ProcessUtils.Logger("OSOL",
                                    $"Setting game process CPU affinity to: {BitmaskExtensions.AffinityToCoreString(SetHnd.Options.GameProcessAffinity)}");
            }

            if (SetHnd.Options.GameProcessPriority.ToString() != "Normal")
            {
                GamePL.ProcWrapper.Proc.PriorityClass = SetHnd.Options.GameProcessPriority;
                ProcessUtils.Logger("OSOL",
                                    $"Setting game process priority to: {SetHnd.Options.GameProcessPriority.ToString()}");
            }
        }
        private void OnProcessSoftExit(ProcessMonitor m, ProcessEventArgs e)
        {// attempt to gracefully switch modes (monitor -> search)
            if (HasAcquired && !string.IsNullOrWhiteSpace(e.ProcessName))
            {
                ProcessUtils.Logger($"MONITOR@{ProcessName}", $"Process exited, attempting to reacquire within {e.Timeout}s: {e.ProcessName}.exe");
            }
            else if (HasAcquired)  // can't get process details?
            {
                ProcessUtils.Logger($"MONITOR@{ProcessName}", $"Process exited, attempting to reacquire within {e.Timeout}s");
            }

            HasAcquired = false;
            // transition from monitoring -> searching
            MonitorTimer.Change(Timeout.Infinite, Timeout.Infinite);
            SearchTimer.Change(0, Interval);

            ProcessSoftExit?.Invoke(m, e);
        }
Beispiel #5
0
 private void OnLauncherExited(object sender, ProcessEventArgs e)
 {// edge case if launcher times out (or closes) before game launches
     ProcessUtils.Logger("OSOL",
                         $"Launcher could not be acquired within {ProcessUtils.ElapsedToString(e.Elapsed)}, cleaning up...");
     OnClosing();
 }
Beispiel #6
0
        private async void OnLauncherAcquired(object sender, ProcessEventArgs e)
        {
            // collect launcher information for collision avoidance
            int  _type    = LauncherPL?.ProcWrapper?.ProcessType ?? -1;
            bool _running = (bool)LauncherMonitor?.IsRunning();
            int  _aPID    = e?.AvoidPID ?? 0;

            // MinimizeWindow after acquisition to prevent issues with ProcessType() fetch
            if (SetHnd.Options.MinimizeLauncher && LauncherPL.ProcWrapper.IsRunning())
            {
                WindowUtils.MinimizeWindow(LauncherPL.ProcWrapper.Hwnd);
            }

            if (!SetHnd.Options.SkipLauncher && LauncherPathValid && LauncherPL != null)
            {// pause to let the launcher process stabilize after being hooked
                ProcessUtils.Logger("OSOL",
                                    $"Launcher detected (type {_type}), preparing to launch game in {SetHnd.Options.PreGameLauncherWaitTime}s...");
                await Task.Delay(SetHnd.Options.PreGameLauncherWaitTime * 1000);
            }

            if (SetHnd.Options.SkipLauncher)
            {                        // ignore AutoGameLaunch option explicitly here
                if (LauncherURIMode) // URI mode
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.LauncherURI, "",
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        monitorName: GameName
                        );
                }
                else  // normal SkipLauncher behavior
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.GamePath,
                        SetHnd.Paths.GameArgs,
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        monitorName: MonitorName
                        );
                }
                await GamePL.Launch();
            }
            else
            {
                if (_running && LauncherURIMode) // URIs
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.LauncherURI, "",
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        avoidPID: _aPID,
                        monitorName: GameName
                        );
                }
                else if (_running && _type == 1) // Battle.net (relaunch LauncherArgs)
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.LauncherPath,
                        SetHnd.Paths.LauncherArgs,
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        avoidPID: _aPID,
                        monitorName: GameName
                        );
                }
                else if (LauncherPathValid && _running) // normal behavior
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.GamePath,
                        SetHnd.Paths.GameArgs,
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        avoidPID: _aPID,
                        monitorName: MonitorName
                        );
                }
                if (GamePL != null && (LauncherPathValid && _running || SetHnd.Options.AutoGameLaunch))
                {
                    await GamePL?.Launch(); // only launch if safe to do so
                }
                else if (LauncherPathValid && LauncherMonitor.IsRunning())
                {
                    ProcessUtils.Logger("OSOL", "AutoGameLaunch is false, waiting for user to launch game before timing out...");
                }
            }

            GameMonitor = new ProcessMonitor(
                GamePL,
                SetHnd.Options.ProcessAcquisitionTimeout,
                SetHnd.Options.InterProcessAcquisitionTimeout
                );
            GameMonitor.ProcessAcquired += OnGameAcquired;
            GameMonitor.ProcessHardExit += OnGameExited;
        }
Beispiel #7
0
        private async void OnLauncherAcquired(object sender, ProcessEventArgs e)
        {
            int  _type    = -1;
            bool _running = false;
            int  _aPID    = 0;

            if (LauncherPL != null && LauncherMonitor != null)
            {// collect launcher information for collision avoidance
                _type    = LauncherPL?.ProcWrapper?.ProcessType ?? -1;
                _running = LauncherMonitor.IsRunning();
                _aPID    = e?.AvoidPID ?? 0;
            }

            if (LauncherPL != null && LauncherPL.ProcWrapper.IsRunning())
            {
                // MinimizeWindow after acquisition to prevent issues with ProcessType() fetch
                if (SetHnd.Options.MinimizeLauncher)
                {
                    WindowUtils.MinimizeWindow(LauncherPL.ProcWrapper.Hwnd);
                }

                // pause to let the launcher process stabilize after being hooked
                if (!SetHnd.Options.SkipLauncher)
                {
                    if (GamePathValid && SetHnd.Options.AutoGameLaunch)
                    {
                        ProcessUtils.Logger("OSOL",
                                            $"Launcher detected (type {_type}), preparing to launch game in {SetHnd.Options.PreGameLauncherWaitTime}s...");
                    }
                    else
                    {
                        ProcessUtils.Logger("OSOL",
                                            $"Launcher detected (type {_type}), skipping GamePath, monitoring...");
                    }
                    await Task.Delay(SetHnd.Options.PreGameLauncherWaitTime * 1000);
                }
            }

            if (SetHnd.Options.SkipLauncher && GamePathValid || LauncherURIMode)
            {                        // ignore AutoGameLaunch option explicitly here
                if (LauncherURIMode) // URI mode
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.LauncherURI, "",
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        monitorName: GameName
                        );
                }
                else  // normal SkipLauncher behavior
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.GamePath,
                        SetHnd.Paths.GameArgs,
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        monitorName: MonitorName
                        );
                }

                if (GamePL != null)
                {
                    await GamePL.Launch();
                }
            }
            else
            {
                if (_running && LauncherURIMode) // URIs
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.LauncherURI, "",
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        avoidPID: _aPID,
                        monitorName: GameName
                        );
                }
                else if (_running && _type == 1) // Battle.net (relaunch LauncherArgs)
                {
                    // Battle.net v1
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.LauncherPath,
                        SetHnd.Paths.LauncherArgs,
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        avoidPID: _aPID,
                        monitorName: GameName
                        );

                    // Battle.net v2 doesn't launch via LauncherArgs so send Enter to the launcher
                    if (ProcessUtils.OrdinalContains("productcode=", SetHnd.Paths.LauncherArgs))
                    {
                        WindowUtils.SendEnterToForeground(LauncherPL.ProcWrapper.Hwnd);
                    }
                }
                else if (GamePathValid) // normal behavior
                {
                    GamePL = new ProcessLauncher(
                        SetHnd.Paths.GamePath,
                        SetHnd.Paths.GameArgs,
                        avoidProcName: LauncherName,
                        delayTime: SetHnd.Options.PreGameWaitTime,
                        avoidPID: _aPID,
                        monitorName: MonitorName
                        );
                }
                else if (!_running)
                { // edge case for !AutoGameLaunch while LauncherPathValid/GamePathValid
                    ProcessUtils.Logger("FATAL", "Fell through all launch attempts, this should not happen!");
                    Environment.Exit(0);
                }

                if (GamePL != null && GamePathValid && !SetHnd.Options.AutoGameLaunch)
                {
                    await GamePL?.Launch(NoLaunch : true); // monitor passively
                }
                else if (GamePL != null && (LauncherPathValid && _running || SetHnd.Options.AutoGameLaunch))
                {
                    await GamePL?.Launch(); // launch if safe to do so
                }
                else if (LauncherPathValid && LauncherMonitor.IsRunning())
                {
                    ProcessUtils.Logger("OSOL", $"AutoGameLaunch is false, continuing to monitor existing processes...");
                }
            }

            if (GamePL != null)
            { // monitor only if safe to do so
                GameMonitor = new ProcessMonitor(
                    GamePL,
                    SetHnd.Options.ProcessAcquisitionTimeout,
                    SetHnd.Options.InterProcessAcquisitionTimeout
                    );
                GameMonitor.ProcessAcquired += OnGameAcquired;
                GameMonitor.ProcessHardExit += OnGameExited;
            }
        }