diff --git a/Sonar.AutoSwitch/Services/AutoSwitchService.cs b/Sonar.AutoSwitch/Services/AutoSwitchService.cs index 04a5f64..e50f44f 100644 --- a/Sonar.AutoSwitch/Services/AutoSwitchService.cs +++ b/Sonar.AutoSwitch/Services/AutoSwitchService.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Threading; +using Avalonia.Logging; using Sonar.AutoSwitch.ViewModels; namespace Sonar.AutoSwitch.Services; @@ -48,11 +50,11 @@ private async void InstanceOnForegroundWindowChanged(object? sender, WindowInfo autoSwitchProfileViewModels.Concat(AutoSwitchProfilesDatabase.Instance.GithubProfiles); } - AutoSwitchProfileViewModel? autoSwitchProfileViewModel = - autoSwitchProfileViewModels.FirstOrDefault(p => + AutoSwitchProfileViewModel? autoSwitchProfileViewModel = autoSwitchProfileViewModels.FirstOrDefault(p => (string.IsNullOrEmpty(p.ExeName) || string.Equals(p.ExeName, windowExeName, StringComparison.OrdinalIgnoreCase)) && - (string.IsNullOrEmpty(p.Title) || e.Title.Contains(p.Title, StringComparison.OrdinalIgnoreCase))); + p.MatchTitle(e.Title)); + SonarGamingConfiguration? sonarGamingConfiguration = autoSwitchProfileViewModel?.SonarGamingConfiguration; sonarGamingConfiguration ??= _homeViewModel.DefaultSonarGamingConfiguration; if (string.IsNullOrEmpty(sonarGamingConfiguration.Id) || diff --git a/Sonar.AutoSwitch/Services/Win32/Win32WindowEventManager.cs b/Sonar.AutoSwitch/Services/Win32/Win32WindowEventManager.cs index 0b447e1..62203e0 100644 --- a/Sonar.AutoSwitch/Services/Win32/Win32WindowEventManager.cs +++ b/Sonar.AutoSwitch/Services/Win32/Win32WindowEventManager.cs @@ -62,12 +62,15 @@ private void WindowEventCallback(nint hWinEventHook, uint eventType, nint hwnd, int idChild, uint dwEventThread, uint dwmsEventTime) { string windowTitle = GetWindowTitle(hwnd); + string? exeName = null; if (GetWindowThreadProcessId(hwnd, out var pid) == 0) + { + OnForegroundWindowChanged(new WindowInfo(exeName, windowTitle)); return; - string? exeName = null; + } try { - using var processById = Process.GetProcessById((int) pid); + using var processById = Process.GetProcessById((int)pid); string? fileName = processById.MainModule?.FileName; if (string.IsNullOrEmpty(fileName)) return; diff --git a/Sonar.AutoSwitch/ViewModels/AutoSwitchProfileViewModel.cs b/Sonar.AutoSwitch/ViewModels/AutoSwitchProfileViewModel.cs index 5298158..2a5ba83 100644 --- a/Sonar.AutoSwitch/ViewModels/AutoSwitchProfileViewModel.cs +++ b/Sonar.AutoSwitch/ViewModels/AutoSwitchProfileViewModel.cs @@ -1,11 +1,14 @@ using Sonar.AutoSwitch.Services; using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; namespace Sonar.AutoSwitch.ViewModels; public class AutoSwitchProfileViewModel : ViewModelBase { + private static readonly RegexOptions REGEX_OPTIONS = RegexOptions.IgnoreCase; private string _exeName = "MyGame"; private string _title = ""; + private Regex _titleRegex = new Regex("", REGEX_OPTIONS); private SonarGamingConfiguration _sonarGamingConfiguration = new(null, "Unset"); public string Title @@ -14,12 +17,18 @@ public string Title set { if (value == _title) return; - _title = value; + _title = value ??= ""; + _titleRegex = new Regex(_title, REGEX_OPTIONS); OnPropertyChanged(); OnPropertyChanged(nameof(DisplayName)); // Notify that DisplayName has changed } } + public bool MatchTitle(string title) + { + return _titleRegex.IsMatch(title); + } + public string ExeName { get => _exeName;