Skip to content

Commit

Permalink
Fixed an issue where reading steam settings changes could cause a crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Jan 3, 2025
1 parent 202b434 commit ed0bfff
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions HandheldCompanion/Platforms/Steam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Timers;

namespace HandheldCompanion.Platforms;

Expand All @@ -18,6 +19,7 @@ public class Steam : IPlatform

private readonly RegistryWatcher SteamActiveUserWatcher = new(WatchedRegistry.CurrentUser, @"SOFTWARE\\Valve\\Steam\\ActiveProcess\\", "ActiveUser");
private FileSystemWatcher SteamActiveUserFileWatcher = new();
private Timer debounceTimer;

private readonly int SteamAppsId = 413080;

Expand Down Expand Up @@ -60,6 +62,11 @@ public Steam()

// check drivers
IsControllerDriverInstalled = HasXboxDriversInstalled();

// Initialize debounce timer
debounceTimer = new Timer(1000); // 1 second delay
debounceTimer.AutoReset = false; // Trigger only once per activation
debounceTimer.Elapsed += DebounceTimer_Elapsed;
}

public override bool Start()
Expand Down Expand Up @@ -91,13 +98,24 @@ private void ActiveUserWatcher_RegistryChanged(object? sender, RegistryChangedEv
IncludeSubdirectories = true,
};

SteamActiveUserFileWatcher.Changed += (sender, e) => ActiveFileWatch_Changed();
SteamActiveUserFileWatcher.Changed += FileWatcher_Changed;
ActiveFileWatch_Changed();
}

private async void ActiveFileWatch_Changed()
private void FileWatcher_Changed(object sender, FileSystemEventArgs e)
{
// Restart the debounce timer whenever a change is detected
debounceTimer.Stop();
debounceTimer.Start();
}

private void DebounceTimer_Elapsed(object? sender, ElapsedEventArgs e)
{
ActiveFileWatch_Changed();
}

private void ActiveFileWatch_Changed()
{
await Task.Delay(1000).ConfigureAwait(false); // Avoid blocking the synchronization context
int SteamInput = GetUseSteamControllerConfigValue();
base.SettingsValueChaned("UseSteamControllerConfig", SteamInput);
}
Expand Down Expand Up @@ -182,9 +200,12 @@ public override bool Stop(bool kill = false)
{
SteamActiveUserWatcher.StopWatching();

SteamActiveUserFileWatcher.Changed -= (sender, e) => ActiveFileWatch_Changed();
SteamActiveUserFileWatcher.Changed -= FileWatcher_Changed;
SteamActiveUserFileWatcher.Dispose();

debounceTimer.Stop();
debounceTimer.Dispose();

return base.Stop();
}

Expand Down

0 comments on commit ed0bfff

Please sign in to comment.