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 inBackend/Services/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.- 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 inSettings.Instance.Whitelist are always recorded, regardless of other criteria.
2. Blacklist (Second Priority)
Games inSettings.Instance.Blacklist are never recorded.
3. Known Games Database
The service checks if the executable path matches entries ingames.json:
4. Launcher-Based Detection
Automatically detects games from popular launchers: Steam:- Pattern:
*/steamapps/common/{GameFolder}/*.exe - Resolves game name from
.acfmanifest files
- Pattern:
*/EA Games/{GameFolder}/*.exe - Uses folder name as game name
- Pattern:
*/Epic Games/{GameFolder}/*.exe - Resolves game name from manifest JSON files in
%ProgramData%/Epic/EpicGamesLauncher/Data/Manifests
- Pattern:
*/Ubisoft/{GameFolder}/*.exe - Uses file description from EXE metadata
5. 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\
Process Monitoring
WMI Event Watchers
The service uses two WMI watchers: Process Start Watcher:Foreground Window Hook
TheForegroundHook subclass monitors foreground window changes:
- Sets up a Windows event hook for
EVENT_SYSTEM_FOREGROUND - Runs on a dedicated STA thread with a message loop
- Resets
PreventRetryRecordingflag when foreground changes - Checks if the new foreground window is a recordable game
Periodic Process Check
A timer runs every 10 seconds to check for games that might not trigger other events:- Verifies the currently recording process is still alive
- Gets the foreground window and its process ID
- Resolves the executable path
- Checks if it should record the game
- 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)
Strategy 2: Process.MainModule
Strategy 3: GetProcessImageFileName (Device Path)
\Device\HarddiskVolume1\... to C:\...
Strategy 4: WMI Query (Slowest)
Game Name Extraction
The service attempts to extract friendly game names:- games.json lookup:
GameUtils.GetGameNameFromExePath(exePath) - Steam ACF lookup: Reads
.acfmanifest files to find game name - EA Games lookup: Uses folder name after
/EA Games/ - Epic Games lookup: Reads manifest JSON files for
DisplayName - Ubisoft lookup: Reads
FileDescriptionfrom EXE metadata - Fallback: Uses filename without extension
Configuration Properties
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:Process Lifecycle
- Process Start: WMI detects new process
- Path Resolution: Resolve executable path using multiple strategies
- Game Detection: Check whitelist, blacklist, known games, launchers
- Name Extraction: Determine friendly game name
- Pre-Recording: Set state and notify frontend
- Recording Start: Call
OBSService.StartRecording() - Process Stop: WMI detects process exit
- 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
HandlesArgumentException 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\HarddiskVolume1 → C:\ 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
Settings.Instance.State.