From 76408ce20bce7269b1da8bbf191403deacc19435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Mon, 28 Nov 2022 13:08:35 +0100 Subject: [PATCH] Fix crash on startup when Steam is missing Fixes https://github.com/ayufan/steam-deck-tools/issues/11 --- RELEASE.md | 1 + SteamController/Controller.cs | 2 - SteamController/Helpers/SteamConfiguration.cs | 118 ++++++++++-------- 3 files changed, 70 insertions(+), 51 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 8c136a5..6978d78 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -32,3 +32,4 @@ It does help this project on being supported. - Fix `AccessDenied` in Steam Detection - Properly apply X360 mappings for Back Panel keys - Setting `Keyboard[KEY] = false` and `Mouse[KEY] = false` does not cancel the press +- Fix crash on startup when Steam is missing diff --git a/SteamController/Controller.cs b/SteamController/Controller.cs index 88a5b8a..75328a1 100644 --- a/SteamController/Controller.cs +++ b/SteamController/Controller.cs @@ -42,8 +42,6 @@ namespace SteamController public Controller() { - var blacklist = Helpers.SteamConfiguration.GetControllerBlacklist(); - Instance.RunOnce(TitleWithVersion, "Global\\SteamController"); var contextMenu = new ContextMenuStrip(components); diff --git a/SteamController/Helpers/SteamConfiguration.cs b/SteamController/Helpers/SteamConfiguration.cs index 04b680e..d9a55e4 100644 --- a/SteamController/Helpers/SteamConfiguration.cs +++ b/SteamController/Helpers/SteamConfiguration.cs @@ -87,7 +87,6 @@ namespace SteamController.Helpers { get { - var value = GetValue(ActiveProcessKey, PIDValue); if (value is null) return null; @@ -154,22 +153,29 @@ namespace SteamController.Helpers public static HashSet? GetControllerBlacklist() { - var configPath = SteamConfigPath; - if (configPath is null) - return null; - - foreach (var line in File.ReadLines(configPath)) + try { - var match = ControllerBlacklistRegex.Match(line); - if (!match.Success) - continue; + var configPath = SteamConfigPath; + if (configPath is null) + return null; - // matches `"controller_blacklist" ""` - var value = match.Groups[2].Captures[0].Value; - return value.Split(',', StringSplitOptions.RemoveEmptyEntries).ToHashSet(); + foreach (var line in File.ReadLines(configPath)) + { + var match = ControllerBlacklistRegex.Match(line); + if (!match.Success) + continue; + + // matches `"controller_blacklist" ""` + var value = match.Groups[2].Captures[0].Value; + return value.Split(',', StringSplitOptions.RemoveEmptyEntries).ToHashSet(); + } + + return new HashSet(); + } + catch (IOException) + { + return null; } - - return new HashSet(); } public static bool? IsControllerBlacklisted(ushort vendorId, ushort productId) @@ -182,14 +188,22 @@ namespace SteamController.Helpers return controllers.Contains(id); } - public static void BackupSteamConfig() + public static bool BackupSteamConfig() { var configPath = SteamConfigPath; if (configPath is null) - return; + return true; - var suffix = DateTime.Now.ToString("yyyyMMddHHmmss"); - File.Copy(configPath, String.Format("{0}.{1}.bak", configPath, suffix)); + try + { + var suffix = DateTime.Now.ToString("yyyyMMddHHmmss"); + File.Copy(configPath, String.Format("{0}.{1}.bak", configPath, suffix)); + return true; + } + catch (IOException) + { + return false; + } } public static bool UpdateControllerBlacklist(ushort vendorId, ushort productId, bool add) @@ -201,44 +215,50 @@ namespace SteamController.Helpers if (configPath is null) return false; - var lines = File.ReadLines(configPath).ToList(); - var id = String.Format("{0:x}/{1:x}", vendorId, productId); - - for (int i = 0; i < lines.Count; i++) + try { - if (lines[i] == "}") + var lines = File.ReadLines(configPath).ToList(); + var id = String.Format("{0:x}/{1:x}", vendorId, productId); + + for (int i = 0; i < lines.Count; i++) { - if (add) + if (lines[i] == "}") { - // append controller_blacklist - lines.Insert(i, String.Format("\t\"controller_blacklist\"\t\t\"{0}\"", id)); - break; + if (add) + { + // append controller_blacklist + lines.Insert(i, String.Format("\t\"controller_blacklist\"\t\t\"{0}\"", id)); + break; + } } + + var match = ControllerBlacklistRegex.Match(lines[i]); + if (!match.Success) + continue; + + var value = match.Groups[2].Captures[0].Value; + var controllers = value.Split(',', StringSplitOptions.RemoveEmptyEntries).ToHashSet(); + + if (add) + controllers.Add(id); + else + controllers.Remove(id); + + lines[i] = String.Format("{0}{1}{2}", + match.Groups[1].Captures[0].Value, + String.Join(',', controllers), + match.Groups[3].Captures[0].Value + ); + break; } - var match = ControllerBlacklistRegex.Match(lines[i]); - if (!match.Success) - continue; - - var value = match.Groups[2].Captures[0].Value; - var controllers = value.Split(',', StringSplitOptions.RemoveEmptyEntries).ToHashSet(); - - if (add) - controllers.Add(id); - else - controllers.Remove(id); - - lines[i] = String.Format("{0}{1}{2}", - match.Groups[1].Captures[0].Value, - String.Join(',', controllers), - match.Groups[3].Captures[0].Value - ); - break; + File.WriteAllLines(configPath, lines); + return true; + } + catch (IOException) + { + return false; } - - File.WriteAllLines(configPath, lines); - - return true; } private static T? GetValue(string key, string value) where T : struct