diff --git a/Wabbajack.Common/StoreHandlers/StoreHandler.cs b/Wabbajack.Common/StoreHandlers/StoreHandler.cs index dd1220c5..295c2225 100644 --- a/Wabbajack.Common/StoreHandlers/StoreHandler.cs +++ b/Wabbajack.Common/StoreHandlers/StoreHandler.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace Wabbajack.Common.StoreHandlers { @@ -13,7 +14,7 @@ namespace Wabbajack.Common.StoreHandlers public class StoreHandler { - private static readonly Lazy _instance = new Lazy(() => new StoreHandler(), true); + private static readonly Lazy _instance = new Lazy(() => new StoreHandler(), isThreadSafe: true); public static StoreHandler Instance => _instance.Value; private static readonly Lazy _steamHandler = new Lazy(() => new SteamHandler()); @@ -72,6 +73,11 @@ namespace Wabbajack.Common.StoreHandlers { return StoreGames.FirstOrDefault(g => g.Game == game)?.Path; } + + public static void Warmup() + { + Task.Run(() => _instance.Value).FireAndForget(); + } } public abstract class AStoreGame diff --git a/Wabbajack.Common/Util/TempFolder.cs b/Wabbajack.Common/Util/TempFolder.cs index 9ce128d3..70862216 100644 --- a/Wabbajack.Common/Util/TempFolder.cs +++ b/Wabbajack.Common/Util/TempFolder.cs @@ -18,7 +18,10 @@ namespace Wabbajack.Common _cleanTask = Task.Run(() => "tmp_files".RelativeTo(AbsolutePath.EntryPoint).DeleteDirectory()); } - public static void Init() + /// + /// Starts the initialization in a background task + /// + public static void Warmup() { // Nothing to do, as work is done in static ctor } diff --git a/Wabbajack/App.xaml.cs b/Wabbajack/App.xaml.cs index b2986871..7d8a7492 100644 --- a/Wabbajack/App.xaml.cs +++ b/Wabbajack/App.xaml.cs @@ -1,15 +1,6 @@ using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; using Wabbajack.Common; -using Wabbajack.Common.StoreHandlers; -using Wabbajack.Util; namespace Wabbajack { @@ -23,7 +14,6 @@ namespace Wabbajack CLIOld.ParseOptions(Environment.GetCommandLineArgs()); if (CLIArguments.Help) CLIOld.DisplayHelpText(); - var storeHandler = new StoreHandler(); } } } diff --git a/Wabbajack/Views/MainWindow.xaml.cs b/Wabbajack/Views/MainWindow.xaml.cs index c6572535..8c48ffec 100644 --- a/Wabbajack/Views/MainWindow.xaml.cs +++ b/Wabbajack/Views/MainWindow.xaml.cs @@ -6,6 +6,7 @@ using System.Windows; using MahApps.Metro.Controls; using Newtonsoft.Json; using Wabbajack.Common; +using Wabbajack.Common.StoreHandlers; using Wabbajack.Lib.LibCefHelpers; using Wabbajack.Util; using Application = System.Windows.Application; @@ -23,8 +24,6 @@ namespace Wabbajack public MainWindow() { - TempFolder.Init(); - Helpers.Init(); // Wire any unhandled crashing exceptions to log before exiting AppDomain.CurrentDomain.UnhandledException += (sender, e) => { @@ -44,22 +43,7 @@ namespace Wabbajack Utils.Log( $"System settings - ({p.SystemMemorySize.ToFileSizeString()} RAM), Display: {p.ScreenWidth} x {p.ScreenHeight} ({p.VideoMemorySize.ToFileSizeString()} VRAM - VideoMemorySizeMb={p.EnbLEVRAMSize})"); - // Run logic to associate wabbajack lists with this app in the background - Task.Run(async () => - { - var appPath = System.Reflection.Assembly.GetExecutingAssembly().Location; - try - { - if (!ModListAssociationManager.IsAssociated() || ModListAssociationManager.NeedsUpdating(appPath)) - { - ModListAssociationManager.Associate(appPath); - } - } - catch (Exception e) - { - Utils.Log($"ExtensionManager had an exception:\n{e}"); - } - }).FireAndForget(); + Warmup(); // Load settings if (CLIArguments.NoSettings || !MainSettings.TryLoadTypicalSettings(out var settings)) @@ -100,6 +84,40 @@ namespace Wabbajack _settings = settings; } + /// + /// Starts some background initialization tasks spinning so they're already prepped when actually needed + /// + private void Warmup() + { + TempFolder.Warmup(); + // ToDo + // Currently this is a blocking call. Perhaps upgrade to be run in a background task. + // Would first need to ensure users of CEF properly await the background initialization before use + Helpers.Init(); + StoreHandler.Warmup(); + + Task.Run(AssociateListsWithWabbajack).FireAndForget(); + } + + /// + /// Run logic to associate wabbajack lists with this app in the background + /// + private void AssociateListsWithWabbajack() + { + var appPath = System.Reflection.Assembly.GetExecutingAssembly().Location; + try + { + if (!ModListAssociationManager.IsAssociated() || ModListAssociationManager.NeedsUpdating(appPath)) + { + ModListAssociationManager.Associate(appPath); + } + } + catch (Exception e) + { + Utils.Log($"ExtensionManager had an exception:\n{e}"); + } + } + private void RunWhenLoaded(Action a) { if (IsLoaded)