diff --git a/Project-Aurora/Project-Aurora/Modules/AutomaticGsiPatcher.cs b/Project-Aurora/Project-Aurora/Modules/AutomaticGsiPatcher.cs index e55345ee7..27a7d3046 100644 --- a/Project-Aurora/Project-Aurora/Modules/AutomaticGsiPatcher.cs +++ b/Project-Aurora/Project-Aurora/Modules/AutomaticGsiPatcher.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using System.Windows; using AuroraRgb.Profiles; using Application = AuroraRgb.Profiles.Application; @@ -26,17 +27,20 @@ protected override async Task Initialize() var lightEvent = args.Application; Task.Run(async () => { - var installGsi = await userPromptTcs.Task; - if (!installGsi) - { - return; - } - await InstallAppGsi(lightEvent); + await RunInstallation(userPromptTcs, lightEvent); }); }; + foreach (var application in lsm.Events.Values) + { + _ = Task.Run(async () => + { + await RunInstallation(userPromptTcs, application); + }); + } if (Global.Configuration.AutoInstallGsi != null) { + userPromptTcs.SetResult(Global.Configuration.AutoInstallGsi ?? false); return; } @@ -50,15 +54,50 @@ protected override async Task Initialize() userPromptTcs.SetResult(Global.Configuration.AutoInstallGsi ?? false); } + private static async Task RunInstallation(TaskCompletionSource userPromptTcs, Application lightEvent) + { + try + { + var installGsi = await userPromptTcs.Task; + if (!installGsi) + { + return; + } + + await InstallAppGsi(lightEvent); + } + catch (Exception e) + { + Global.logger.Error(e, "[AutomaticGsiPatcher] An error occured while installing Gsi of {App}", lightEvent.Config.Name); + } + } + private static async Task InstallAppGsi(Application lightEvent) { switch (lightEvent) { case GsiApplication application: - if (application.Settings?.InstallationCompleted ?? false) + var retries = 5; + while (retries-- > 0) { - await application.InstallGsi(); - await application.SaveSettings(); + if (application.Settings != null) + { + if (!application.Settings.InstallationCompleted) + { + Global.logger.Information("[AutomaticGsiPatcher] Installing {App} Gsi", application.Config.Name); + await application.InstallGsi(); + await application.SaveSettings(); + Global.logger.Information("[AutomaticGsiPatcher] {App} Gsi installed", application.Config.Name); + } + } + else + { + Global.logger.Error("[AutomaticGsiPatcher] {App} settings not loaded to determine GSI installation status", application.Config.Name); + await Task.Delay(200); + continue; + } + + break; } break; diff --git a/Project-Aurora/Project-Aurora/Settings/ObjectSettings.cs b/Project-Aurora/Project-Aurora/Settings/ObjectSettings.cs index 75049de13..b592e83e5 100644 --- a/Project-Aurora/Project-Aurora/Settings/ObjectSettings.cs +++ b/Project-Aurora/Project-Aurora/Settings/ObjectSettings.cs @@ -59,19 +59,12 @@ protected virtual async Task LoadSettings(Type settingsType) try { var json = await File.ReadAllTextAsync(SettingsSavePath); - Settings = (T)JsonConvert.DeserializeObject(json, settingsType, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None }); - if (Settings == null) - { - await SaveSettings(settingsType); - } + Settings = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None }); } catch (Exception exc) { Global.logger.Error(exc, "Exception occured while loading \\\"{Name}\\\" Settings", GetType().Name); - await SaveSettings(settingsType); } } - else - await SaveSettings(settingsType); } } \ No newline at end of file diff --git a/Project-Aurora/Project-Aurora/Utils/Steam/SteamUtils.cs b/Project-Aurora/Project-Aurora/Utils/Steam/SteamUtils.cs index d9d83bf97..2e573e7f9 100644 --- a/Project-Aurora/Project-Aurora/Utils/Steam/SteamUtils.cs +++ b/Project-Aurora/Project-Aurora/Utils/Steam/SteamUtils.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading; using System.Threading.Tasks; using Microsoft.Win32; using VdfParser; @@ -12,6 +13,8 @@ namespace AuroraRgb.Utils.Steam; public static class SteamUtils { private static readonly VdfDeserializer VdfDeserializer = new(); + + private static readonly SemaphoreSlim SteamReadLock = new(1, 1); public static async Task InstallGsiFile(int gameId, string gameRelativeFilePath, byte[] gsiFile) { @@ -74,6 +77,7 @@ public static async Task InstallGsiFile(int gameId, string gameRelativeFil if (Directory.Exists(appIdPath)) return appIdPath; } + return null; } catch (Exception exc) @@ -90,6 +94,7 @@ public static async Task InstallGsiFile(int gameId, string gameRelativeFil /// Path to the location of AppID's install public static async Task GetGamePathAsync(int gameId) { + await SteamReadLock.WaitAsync(); Global.logger.Debug("Trying to get game path for: {GameId}", gameId); try @@ -129,6 +134,10 @@ public static async Task InstallGsiFile(int gameId, string gameRelativeFil Global.logger.Error(exc, "SteamUtils: GetGamePath({GameId})", gameId); return null; } + finally + { + SteamReadLock.Release(); + } } private static string? GetSteamPath()