From 2c099efd2b8294b01152eca53427bbd52ed69853 Mon Sep 17 00:00:00 2001 From: 4sval Date: Sat, 6 May 2023 22:45:58 +0200 Subject: [PATCH] do as much as possible simultaneously without soft locking the UI --- FModel/MainWindow.xaml.cs | 23 ++++-- FModel/ViewModels/ApplicationViewModel.cs | 8 +- FModel/ViewModels/CUE4ParseViewModel.cs | 97 +++++++++++++---------- FModel/ViewModels/Commands/LoadCommand.cs | 71 ++++++++--------- 4 files changed, 103 insertions(+), 96 deletions(-) diff --git a/FModel/MainWindow.xaml.cs b/FModel/MainWindow.xaml.cs index 22722330..5c8d11dc 100644 --- a/FModel/MainWindow.xaml.cs +++ b/FModel/MainWindow.xaml.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Linq; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -63,17 +64,23 @@ public partial class MainWindow await _applicationView.CUE4Parse.Initialize(); await _applicationView.AesManager.InitAes(); + await _applicationView.UpdateProvider(true); #if !DEBUG await _applicationView.CUE4Parse.InitInformation(); #endif - await _applicationView.UpdateProvider(true); - await _applicationView.CUE4Parse.InitMappings(); - await _applicationView.InitImGuiSettings(newOrUpdated); - await _applicationView.InitVgmStream(); - await _applicationView.InitOodle(); - - if (UserSettings.Default.DiscordRpc == EDiscordRpc.Always) - _discordHandler.Initialize(_applicationView.GameDisplayName); + await Task.WhenAll( + Task.Run(() => _applicationView.CUE4Parse.VerifyCva()), + Task.Run(() => _applicationView.CUE4Parse.VerifyVfc()), + _applicationView.CUE4Parse.InitMappings(), + _applicationView.InitImGuiSettings(newOrUpdated), + _applicationView.InitVgmStream(), + _applicationView.InitOodle(), + Task.Run(() => + { + if (UserSettings.Default.DiscordRpc == EDiscordRpc.Always) + _discordHandler.Initialize(_applicationView.GameDisplayName); + }) + ).ConfigureAwait(false); #if DEBUG // await _threadWorkerView.Begin(cancellationToken => diff --git a/FModel/ViewModels/ApplicationViewModel.cs b/FModel/ViewModels/ApplicationViewModel.cs index 3a4b8263..3fa2afcf 100644 --- a/FModel/ViewModels/ApplicationViewModel.cs +++ b/FModel/ViewModels/ApplicationViewModel.cs @@ -113,13 +113,7 @@ public class ApplicationViewModel : ViewModel await ApplicationService.ThreadWorkerView.Begin(cancellationToken => { CUE4Parse.LoadVfs(cancellationToken, AesManager.AesKeys); - CUE4Parse.VerifyCva(); - var vfcCount = CUE4Parse.Provider.LoadVirtualCache(); - if (vfcCount > 0) - { - FLogger.AppendInformation(); - FLogger.AppendText($"VFC loaded {vfcCount} cached packages", Constants.WHITE, true); - } + CUE4Parse.Provider.LoadIniConfigs(); AesManager.SetAesKeys(); }); RaisePropertyChanged(nameof(GameDisplayName)); diff --git a/FModel/ViewModels/CUE4ParseViewModel.cs b/FModel/ViewModels/CUE4ParseViewModel.cs index c9a8bbd7..afcd4bc7 100644 --- a/FModel/ViewModels/CUE4ParseViewModel.cs +++ b/FModel/ViewModels/CUE4ParseViewModel.cs @@ -119,9 +119,6 @@ public class CUE4ParseViewModel : ViewModel public AssetsFolderViewModel AssetsFolder { get; } public SearchViewModel SearchVm { get; } public TabControlViewModel TabControl { get; } - public int LocalizedResourcesCount { get; set; } - public bool HotfixedResourcesDone { get; set; } - public int VirtualPathCount { get; set; } public CUE4ParseViewModel(string gameDirectory) { @@ -333,19 +330,6 @@ public class CUE4ParseViewModel : ViewModel Game = Helper.IAmThePanda(Provider.GameName) ? FGame.PandaGame : Provider.GameName.ToEnum(Game); } - public void VerifyCva() - { - Provider.LoadIniConfigs(); - - var inst = new List(); - Provider.DefaultEngine.FindPropertyInstructions("ConsoleVariables", "a.StripAdditiveRefPose", inst); - if (inst.Count > 0 && inst[0].Value.Equals("1")) - { - FLogger.AppendWarning(); - FLogger.AppendText("Additive animations have their reference pose stripped, which will lead to inaccurate preview and export", Constants.WHITE, true); - } - } - public void ClearProvider() { if (Provider == null) return; @@ -387,15 +371,15 @@ public class CUE4ParseViewModel : ViewModel }); } - public async Task InitMappings() + public Task InitMappings() { if (!UserSettings.IsEndpointValid(Game, EEndpointType.Mapping, out var endpoint)) { Provider.MappingsContainer = null; - return; + return Task.CompletedTask; } - await _threadWorkerView.Begin(cancellationToken => + return Task.Run(() => { if (endpoint.Overwrite && File.Exists(endpoint.FilePath)) { @@ -406,7 +390,7 @@ public class CUE4ParseViewModel : ViewModel else if (endpoint.IsValid) { var mappingsFolder = Path.Combine(UserSettings.Default.OutputDirectory, ".data"); - var mappings = _apiEndpointView.DynamicApi.GetMappings(cancellationToken, endpoint.Url, endpoint.Path); + var mappings = _apiEndpointView.DynamicApi.GetMappings(default, endpoint.Url, endpoint.Path); if (mappings is { Length: > 0 }) { foreach (var mapping in mappings) @@ -440,11 +424,38 @@ public class CUE4ParseViewModel : ViewModel }); } + private bool _cvaVerifDone { get; set; } + public void VerifyCva() + { + if (_cvaVerifDone) return; + _cvaVerifDone = true; + + var inst = new List(); + Provider.DefaultEngine.FindPropertyInstructions("ConsoleVariables", "a.StripAdditiveRefPose", inst); + if (inst.Count > 0 && inst[0].Value.Equals("1")) + { + FLogger.AppendWarning(); + FLogger.AppendText("Additive animations have their reference pose stripped, which will lead to inaccurate preview and export", Constants.WHITE, true); + } + } + + private int _vfcCount { get; set; } + public void VerifyVfc() + { + if (_vfcCount > 0) return; + + _vfcCount = Provider.LoadVirtualCache(); + if (_vfcCount > 0) + { + FLogger.AppendInformation(); + FLogger.AppendText($"VFC loaded {_vfcCount} cached packages", Constants.WHITE, true); + } + } + + public int LocalizedResourcesCount { get; set; } public async Task LoadLocalizedResources() { - await LoadGameLocalizedResources(); - await LoadHotfixedLocalizedResources(); - await _threadWorkerView.Begin(_ => Utils.Typefaces = new Typefaces(this)); + await Task.WhenAll(LoadGameLocalizedResources(), LoadHotfixedLocalizedResources()).ConfigureAwait(false); if (LocalizedResourcesCount > 0) { FLogger.AppendInformation(); @@ -457,37 +468,34 @@ public class CUE4ParseViewModel : ViewModel } } - private async Task LoadGameLocalizedResources() + private bool _localResourcesDone { get; set; } + private Task LoadGameLocalizedResources() { - if (LocalizedResourcesCount > 0) return; - await _threadWorkerView.Begin(cancellationToken => + if (_localResourcesDone) return Task.CompletedTask; + return Task.Run(() => { - LocalizedResourcesCount = Provider.LoadLocalization(UserSettings.Default.AssetLanguage, cancellationToken); + LocalizedResourcesCount += Provider.LoadLocalization(UserSettings.Default.AssetLanguage); + _localResourcesDone = true; }); } - /// - /// Load hotfixed localized resources - /// - /// Functions only when LoadLocalizedResources is used prior to this. - private async Task LoadHotfixedLocalizedResources() + private bool _hotfixedResourcesDone { get; set; } + private Task LoadHotfixedLocalizedResources() { - if (Game != FGame.FortniteGame || HotfixedResourcesDone) return; - await _threadWorkerView.Begin(cancellationToken => + if (Game != FGame.FortniteGame || _hotfixedResourcesDone) return Task.CompletedTask; + return Task.Run(() => { - var hotfixes = ApplicationService.ApiEndpointView.CentralApi.GetHotfixes(cancellationToken, Provider.GetLanguageCode(UserSettings.Default.AssetLanguage)); + var hotfixes = ApplicationService.ApiEndpointView.CentralApi.GetHotfixes(default, Provider.GetLanguageCode(UserSettings.Default.AssetLanguage)); if (hotfixes == null) return; - HotfixedResourcesDone = true; + _hotfixedResourcesDone = true; foreach (var entries in hotfixes) { - cancellationToken.ThrowIfCancellationRequested(); if (!Provider.LocalizedResources.ContainsKey(entries.Key)) Provider.LocalizedResources[entries.Key] = new Dictionary(); foreach (var keyValue in entries.Value) { - cancellationToken.ThrowIfCancellationRequested(); Provider.LocalizedResources[entries.Key][keyValue.Key] = keyValue.Value; LocalizedResourcesCount++; } @@ -495,16 +503,17 @@ public class CUE4ParseViewModel : ViewModel }); } - public async Task LoadVirtualPaths() + private int _virtualPathCount { get; set; } + public Task LoadVirtualPaths() { - if (VirtualPathCount > 0) return; - await _threadWorkerView.Begin(cancellationToken => + if (_virtualPathCount > 0) return Task.CompletedTask; + return Task.Run(() => { - VirtualPathCount = Provider.LoadVirtualPaths(UserSettings.Default.OverridedGame[Game].GetVersion(), cancellationToken); - if (VirtualPathCount > 0) + _virtualPathCount = Provider.LoadVirtualPaths(UserSettings.Default.OverridedGame[Game].GetVersion()); + if (_virtualPathCount > 0) { FLogger.AppendInformation(); - FLogger.AppendText($"{VirtualPathCount} virtual paths loaded", Constants.WHITE, true); + FLogger.AppendText($"{_virtualPathCount} virtual paths loaded", Constants.WHITE, true); } else { diff --git a/FModel/ViewModels/Commands/LoadCommand.cs b/FModel/ViewModels/Commands/LoadCommand.cs index 631aec0b..217cfb08 100644 --- a/FModel/ViewModels/Commands/LoadCommand.cs +++ b/FModel/ViewModels/Commands/LoadCommand.cs @@ -6,9 +6,11 @@ using System.Globalization; using System.IO; using System.Linq; using System.Threading; +using System.Threading.Tasks; using AdonisUI.Controls; using CUE4Parse.UE4.Readers; using CUE4Parse.UE4.VirtualFileSystem; +using FModel.Creator; using FModel.Extensions; using FModel.Framework; using FModel.Services; @@ -43,59 +45,54 @@ public class LoadCommand : ViewModelCommand return; } - if (_applicationView.CUE4Parse.Game == FGame.FortniteGame && - _applicationView.CUE4Parse.Provider.MappingsContainer == null) - { - FLogger.AppendError(); - FLogger.AppendText("Mappings could not get pulled, extracting packages might not work properly. If so, either press F12, restart, or come back later.", Constants.WHITE, true); - } - #if DEBUG var loadingTime = Stopwatch.StartNew(); #endif _applicationView.CUE4Parse.AssetsFolder.Folders.Clear(); _applicationView.CUE4Parse.SearchVm.SearchResults.Clear(); MainWindow.YesWeCats.LeftTabControl.SelectedIndex = 1; // folders tab - - await _applicationView.CUE4Parse.LoadLocalizedResources(); // load locres if not already loaded - await _applicationView.CUE4Parse.LoadVirtualPaths(); // load virtual paths if not already loaded Helper.CloseWindow("Search View"); // close search window if opened - await _threadWorkerView.Begin(cancellationToken => - { - // filter what to show - switch (UserSettings.Default.LoadingMode) + await Task.WhenAll( + _applicationView.CUE4Parse.LoadLocalizedResources(), // load locres if not already loaded, + _applicationView.CUE4Parse.LoadVirtualPaths(), // load virtual paths if not already loaded + Task.Run(() => Utils.Typefaces = new Typefaces(_applicationView.CUE4Parse)), + _threadWorkerView.Begin(cancellationToken => { - case ELoadingMode.Single: - case ELoadingMode.Multiple: + // filter what to show + switch (UserSettings.Default.LoadingMode) { - var l = (IList) parameter; - if (l.Count < 1) return; + case ELoadingMode.Single: + case ELoadingMode.Multiple: + { + var l = (IList) parameter; + if (l.Count < 1) return; - var directoryFilesToShow = l.Cast(); - FilterDirectoryFilesToDisplay(cancellationToken, directoryFilesToShow); - break; + var directoryFilesToShow = l.Cast(); + FilterDirectoryFilesToDisplay(cancellationToken, directoryFilesToShow); + break; + } + case ELoadingMode.All: + { + FilterDirectoryFilesToDisplay(cancellationToken, null); + break; + } + case ELoadingMode.AllButNew: + case ELoadingMode.AllButModified: + { + FilterNewOrModifiedFilesToDisplay(cancellationToken); + break; + } + default: throw new ArgumentOutOfRangeException(); } - case ELoadingMode.All: - { - FilterDirectoryFilesToDisplay(cancellationToken, null); - break; - } - case ELoadingMode.AllButNew: - case ELoadingMode.AllButModified: - { - FilterNewOrModifiedFilesToDisplay(cancellationToken); - break; - } - default: throw new ArgumentOutOfRangeException(); - } - _discordHandler.UpdatePresence(_applicationView.CUE4Parse); - }); + _discordHandler.UpdatePresence(_applicationView.CUE4Parse); + }) + ).ConfigureAwait(false); #if DEBUG loadingTime.Stop(); FLogger.AppendDebug(); - FLogger.AppendText($"{_applicationView.CUE4Parse.SearchVm.SearchResults.Count} packages, {_applicationView.CUE4Parse.LocalizedResourcesCount} localized resources, and {_applicationView.CUE4Parse.VirtualPathCount} virtual paths loaded in {loadingTime.Elapsed.TotalSeconds.ToString("F3", CultureInfo.InvariantCulture)} seconds", Constants.WHITE, true); + FLogger.AppendText($"{_applicationView.CUE4Parse.SearchVm.SearchResults.Count} packages loaded in {loadingTime.Elapsed.TotalSeconds.ToString("F3", CultureInfo.InvariantCulture)} seconds", Constants.WHITE, true); #endif }