Skip to content

Commit

Permalink
Update AutoSwitchService.cs
Browse files Browse the repository at this point in the history
Updated AutoSwitchService.cs to all a * wildcard, for example "Rocket*" will now match RocketLeague.
  • Loading branch information
xNightWulf69 authored Jul 5, 2024
1 parent b2b7ee9 commit 89ceadb
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Sonar.AutoSwitch/Services/AutoSwitchService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using Sonar.AutoSwitch.ViewModels;

namespace Sonar.AutoSwitch.Services;
Expand All @@ -10,8 +11,8 @@ public class AutoSwitchService
{
private readonly HomeViewModel _homeViewModel;
private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
private SonarGamingConfiguration _selectedGamingConfiguration;
private CancellationTokenSource _cancellationTokenSource;
private SonarGamingConfiguration? _selectedGamingConfiguration;
private CancellationTokenSource? _cancellationTokenSource;

public AutoSwitchService()
{
Expand All @@ -38,6 +39,7 @@ private async void InstanceOnForegroundWindowChanged(object? sender, WindowInfo
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = new CancellationTokenSource();

await _semaphoreSlim.WaitAsync();
try
{
Expand All @@ -52,18 +54,22 @@ private async void InstanceOnForegroundWindowChanged(object? sender, WindowInfo
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)));
(string.IsNullOrEmpty(p.Title) || MatchesWildcard(e.Title, p.Title)));

SonarGamingConfiguration? sonarGamingConfiguration = autoSwitchProfileViewModel?.SonarGamingConfiguration;
sonarGamingConfiguration ??= _homeViewModel.DefaultSonarGamingConfiguration;

if (string.IsNullOrEmpty(sonarGamingConfiguration.Id) ||
_selectedGamingConfiguration == sonarGamingConfiguration)
return;

string selectedGamingConfigurationId =
SteelSeriesSonarService.Instance.GetSelectedGamingConfiguration();
_selectedGamingConfiguration = sonarGamingConfiguration;

if (sonarGamingConfiguration.Id == selectedGamingConfigurationId)
return;

await SteelSeriesSonarService.Instance.ChangeSelectedGamingConfiguration(sonarGamingConfiguration,
_cancellationTokenSource.Token);
}
Expand All @@ -72,4 +78,10 @@ await SteelSeriesSonarService.Instance.ChangeSelectedGamingConfiguration(sonarGa
_semaphoreSlim.Release();
}
}
}

private bool MatchesWildcard(string input, string pattern)
{
string regexPattern = "^" + Regex.Escape(pattern).Replace("\\*", ".*") + "$";
return Regex.IsMatch(input, regexPattern, RegexOptions.IgnoreCase);
}
}

0 comments on commit 89ceadb

Please sign in to comment.