Skip to main content
The GameDetectionService monitors running processes and automatically starts recording when supported games are detected. It uses Windows Management Instrumentation (WMI) events, foreground window hooks, and periodic process scanning.

Overview

Implemented in Backend/Games/GameDetectionService.cs, the service provides:
  • Automatic game detection from multiple sources (games.json, Steam, EA, Epic, Ubisoft)
  • WMI-based process monitoring (start/stop events)
  • Foreground window change detection
  • Periodic process scanning for games that don’t trigger events
  • Whitelist and blacklist support
  • Anti-cheat and launcher filtering

Core Methods

StartAsync

Initializes and starts the game detection service.
Behavior:
  • Initializes game database from games.json
  • Starts WMI watchers for process creation and deletion
  • Starts periodic process check timer (runs every 10 seconds)
  • Runs on a background thread
This method is automatically called by OBSService.InitializeAsync() after OBS initialization completes.
Example

Detection Strategies

The service uses multiple strategies to detect games:

1. Whitelist (Highest Priority)

Games in Settings.Instance.Whitelist are always recorded, regardless of other criteria.

2. Blacklist (Second Priority)

Games in Settings.Instance.Blacklist are never recorded.

3. Global auto-record toggle

If Settings.Instance.AutoRecordGames is false, the service stops here and does not record. Explicit per-game entries above still apply, and manual recording is unaffected.

4. Known Games Database

The service checks if the executable path matches entries in games.json:

5. Launcher-Based Detection

Automatically detects games from popular launchers: Steam:
  • Pattern: */steamapps/common/{GameFolder}/*.exe
  • Resolves game name from .acf manifest files
EA Games:
  • Pattern: */EA Games/{GameFolder}/*.exe
  • Uses folder name as game name
Epic Games:
  • Pattern: */Epic Games/{GameFolder}/*.exe
  • Resolves game name from manifest JSON files in %ProgramData%/Epic/EpicGamesLauncher/Data/Manifests
Ubisoft:
  • Pattern: */Ubisoft/{GameFolder}/*.exe
  • Uses file description from EXE metadata

6. Filtering

The service filters out: System Processes:
  • Paths starting with C:/Windows/System32/
  • Paths starting with C:/Windows/SysWOW64/
  • Paths starting with C:/Program Files/Git/
Blacklisted Path Text:
Anti-Cheat Clients:
Checked against file description metadata. Splash Screen Detection: If a known game executable exists in the same folder but differs from the current process, the current process is skipped (likely a splash screen or launcher).

Process Monitoring

WMI Event Watchers

The service uses two WMI watchers: Process Start Watcher:
Triggered when a new process starts. Checks if it’s a recordable game. Process Stop Watcher:
Triggered when a process exits. Stops recording if it matches the currently recorded game.

Foreground Window Hook

The ForegroundHook subclass monitors foreground window changes:
Behavior:
  • Sets up a Windows event hook for EVENT_SYSTEM_FOREGROUND
  • Runs on a dedicated STA thread with a message loop
  • Resets PreventRetryRecording flag when foreground changes
  • Checks if the new foreground window is a recordable game
The foreground hook runs on a separate thread and must be properly stopped to avoid resource leaks:

Periodic Process Check

A timer runs every 10 seconds to check for games that might not trigger other events:
Process:
  1. Verifies the currently recording process is still alive
  2. Gets the foreground window and its process ID
  3. Resolves the executable path
  4. Checks if it should record the game
  5. Starts recording if criteria are met
This timer catches games that launch without triggering WMI events or don’t change the foreground window immediately.

Process Path Resolution

The service uses multiple strategies to resolve process executable paths:

Strategy 1: QueryFullProcessImageName (Preferred)

Works for most processes, including elevated ones.

Strategy 2: Process.MainModule

Standard .NET approach (may fail for elevated processes).

Strategy 3: GetProcessImageFileName (Device Path)

Converts device paths like \Device\HarddiskVolume1\... to C:\...

Strategy 4: WMI Query (Slowest)

Fallback for processes that can’t be accessed via other methods.

Game Name Extraction

The service attempts to extract friendly game names:
Priority Order:
  1. games.json lookup: GameUtils.GetGameNameFromExePath(exePath)
  2. Steam ACF lookup: Reads .acf manifest files to find game name
  3. EA Games lookup: Uses folder name after /EA Games/
  4. Epic Games lookup: Reads manifest JSON files for DisplayName
  5. Ubisoft lookup: Reads FileDescription from EXE metadata
  6. Fallback: Uses filename without extension

Configuration Properties

bool
default:"false"
When true, prevents the periodic process check from starting a new recording. Automatically reset when foreground window changes.

Integration with Recording Service

When a game is detected, the service creates a pre-recording state on the runtime AppState singleton:
Recording and pre-recording state used to live under Settings.Instance.State. They are now exposed on AppState.Instance (in Backend/Core/Models/AppState.cs) and pushed to the frontend in a separate State WebSocket message rather than nested under Settings.

Process Lifecycle

  1. Process Start: WMI detects new process
  2. Path Resolution: Resolve executable path using multiple strategies
  3. Game Detection: Check whitelist, blacklist, known games, launchers
  4. Name Extraction: Determine friendly game name
  5. Pre-Recording: Set state and notify frontend
  6. Recording Start: Call OBSService.StartRecording()
  7. Process Stop: WMI detects process exit
  8. Recording Stop: Call OBSService.StopRecording()

Error Handling

The service includes robust error handling:

Path Resolution Failures

If all strategies fail, returns empty string and skips the process.

Access Denied

Elevated processes may not be accessible - uses limited-access APIs (PROCESS_QUERY_LIMITED_INFORMATION).

Process Exited

Handles ArgumentException and InvalidOperationException when process exits during detection.

WMI Exceptions

Catches and logs WMI event handler exceptions to prevent service interruption.

Performance Considerations

  • WMI Events: Near-instant detection (1-second polling interval)
  • Foreground Hook: Immediate detection on window change
  • Process Check: 10-second intervals (low overhead)
  • Path Resolution: Cached drive mappings for device-to-drive conversion
The service initializes drive mappings once at startup:
This caches \Device\HarddiskVolume1C:\ mappings for fast conversion.

Logging

The service provides detailed logging:

Thread Safety

The service uses multiple threads:
  • Main Thread: Service initialization
  • Background Thread: WMI event handling
  • Hook Thread: Foreground window events (STA)
  • Timer Thread: Periodic process checks
No shared mutable state requires locking - each handler independently checks AppState.Instance.