From 30ab06a304d8ae58af13454835cb706f35d90068 Mon Sep 17 00:00:00 2001 From: trawzified <55751269+tr4wzified@users.noreply.github.com> Date: Mon, 24 Jun 2024 19:50:46 +0200 Subject: [PATCH] Save CompilerFileState changes in file manager to disk, create base compiler class --- .../ViewModels/Compiler/BaseCompilerVM.cs | 53 +++++++++++++ .../ViewModels/Compiler/CompilerDetailsVM.cs | 49 ++---------- .../Compiler/CompilerFileManagerVM.cs | 78 +++++++------------ 3 files changed, 88 insertions(+), 92 deletions(-) create mode 100644 Wabbajack.App.Wpf/ViewModels/Compiler/BaseCompilerVM.cs diff --git a/Wabbajack.App.Wpf/ViewModels/Compiler/BaseCompilerVM.cs b/Wabbajack.App.Wpf/ViewModels/Compiler/BaseCompilerVM.cs new file mode 100644 index 00000000..86ab61d4 --- /dev/null +++ b/Wabbajack.App.Wpf/ViewModels/Compiler/BaseCompilerVM.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reactive.Disposables; +using System.Text.Json; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using ReactiveUI; +using ReactiveUI.Fody.Helpers; +using Wabbajack.DTOs; +using Wabbajack.DTOs.JsonConverters; +using Wabbajack.Paths; +using Wabbajack.Services.OSIntegrated; +using Wabbajack.Paths.IO; +using System.Linq; +using Wabbajack.Networking.WabbajackClientApi; + +namespace Wabbajack +{ + public abstract class BaseCompilerVM : BackNavigatingVM + { + protected readonly DTOSerializer _dtos; + protected readonly SettingsManager _settingsManager; + protected readonly ILogger _logger; + protected readonly Client _wjClient; + + [Reactive] public CompilerSettingsVM Settings { get; set; } = new(); + + public BaseCompilerVM(DTOSerializer dtos, SettingsManager settingsManager, ILogger logger, Client wjClient) : base(logger) + { + _dtos = dtos; + _settingsManager = settingsManager; + _logger = logger; + _wjClient = wjClient; + } + + protected async Task SaveSettings() + { + if (Settings.Source == default || Settings.CompilerSettingsPath == default) return; + + await using var st = Settings.CompilerSettingsPath.Open(FileMode.Create, FileAccess.Write, FileShare.None); + await JsonSerializer.SerializeAsync(st, Settings.ToCompilerSettings(), _dtos.Options); + + var allSavedCompilerSettings = await _settingsManager.Load>(Consts.AllSavedCompilerSettingsPaths); + + // Don't simply remove Settings.CompilerSettingsPath here, because WJ sometimes likes to make default compiler settings files + allSavedCompilerSettings.RemoveAll(path => path.Parent == Settings.Source); + allSavedCompilerSettings.Insert(0, Settings.CompilerSettingsPath); + + await _settingsManager.Save(Consts.AllSavedCompilerSettingsPaths, allSavedCompilerSettings); + } + } +} diff --git a/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerDetailsVM.cs b/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerDetailsVM.cs index 30eeb3ee..4372e7c8 100644 --- a/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerDetailsVM.cs +++ b/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerDetailsVM.cs @@ -38,15 +38,11 @@ namespace Wabbajack Completed, Errored } - public class CompilerDetailsVM : BackNavigatingVM, ICpuStatusVM + public class CompilerDetailsVM : BaseCompilerVM, ICpuStatusVM { - private readonly DTOSerializer _dtos; - private readonly SettingsManager _settingsManager; private readonly IServiceProvider _serviceProvider; - private readonly ILogger _logger; private readonly ResourceMonitor _resourceMonitor; private readonly CompilerSettingsInferencer _inferencer; - private readonly Client _wjClient; [Reactive] public string StatusText { get; set; } [Reactive] public Percent StatusProgress { get; set; } @@ -62,8 +58,6 @@ namespace Wabbajack public FilePickerVM DownloadLocation { get; } public FilePickerVM OutputLocation { get; } - [Reactive] public CompilerSettingsVM Settings { get; set; } = new(); - public FilePickerVM ModListImageLocation { get; } = new(); /* public ReactiveCommand ExecuteCommand { get; } */ @@ -78,16 +72,12 @@ namespace Wabbajack public CompilerDetailsVM(ILogger logger, DTOSerializer dtos, SettingsManager settingsManager, IServiceProvider serviceProvider, LogStream loggerProvider, ResourceMonitor resourceMonitor, - CompilerSettingsInferencer inferencer, Client wjClient) : base(logger) + CompilerSettingsInferencer inferencer, Client wjClient) : base(dtos, settingsManager, logger, wjClient) { - _logger = logger; - _dtos = dtos; - _settingsManager = settingsManager; _serviceProvider = serviceProvider; LoggerProvider = loggerProvider; _resourceMonitor = resourceMonitor; _inferencer = inferencer; - _wjClient = wjClient; MessageBus.Current.Listen() .Subscribe(msg => { @@ -105,7 +95,7 @@ namespace Wabbajack BackCommand = ReactiveCommand.CreateFromTask(async () => { - await SaveSettingsFile(); + await SaveSettings(); NavigateToGlobal.Send(ScreenType.Home); }); @@ -179,7 +169,7 @@ namespace Wabbajack this.WhenAnyValue(x => x.Settings) .Throttle(TimeSpan.FromSeconds(2)) - .Subscribe(_ => SaveSettingsFile().FireAndForget()) + .Subscribe(_ => SaveSettings().FireAndForget()) .DisposeWith(disposables); /* @@ -264,7 +254,7 @@ namespace Wabbajack private async Task NextPage() { - await SaveSettingsFile(); + await SaveSettings(); NavigateToGlobal.Send(ScreenType.CompilerFileManager); LoadCompilerSettings.Send(Settings.ToCompilerSettings()); } @@ -275,7 +265,7 @@ namespace Wabbajack { try { - await SaveSettingsFile(); + await SaveSettings(); var token = CancellationToken.None; State = CompilerState.Compiling; @@ -369,33 +359,6 @@ namespace Wabbajack return true; } - private async Task SaveSettingsFile() - { - if (Settings.Source == default || Settings.CompilerSettingsPath == default) return; - - await using var st = Settings.CompilerSettingsPath.Open(FileMode.Create, FileAccess.Write, FileShare.None); - await JsonSerializer.SerializeAsync(st, Settings.ToCompilerSettings(), _dtos.Options); - - var allSavedCompilerSettings = await _settingsManager.Load>(Consts.AllSavedCompilerSettingsPaths); - - // Don't simply remove Settings.CompilerSettingsPath here, because WJ sometimes likes to make default compiler settings files - allSavedCompilerSettings.RemoveAll(path => path.Parent == Settings.Source); - allSavedCompilerSettings.Insert(0, Settings.CompilerSettingsPath); - - await _settingsManager.Save(Consts.AllSavedCompilerSettingsPaths, allSavedCompilerSettings); - } - - private async Task LoadLastSavedSettings() - { - AbsolutePath lastPath = default; - var allSavedCompilerSettings = await _settingsManager.Load>(Consts.AllSavedCompilerSettingsPaths); - if (allSavedCompilerSettings.Any()) - lastPath = allSavedCompilerSettings[0]; - - if (lastPath == default || !lastPath.FileExists() || lastPath.FileName.Extension != Ext.CompilerSettings) return; - ModlistLocation.TargetPath = lastPath; - } - #region ListOps public void AddOtherProfile(string profile) diff --git a/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs b/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs index 5741665f..c4a706b1 100644 --- a/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs +++ b/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs @@ -3,32 +3,22 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; -using System.Reactive; using Microsoft.Extensions.Logging; using Wabbajack.Messages; using ReactiveUI; using System.Reactive.Disposables; using System.Reactive.Linq; using System.Text.Json; -using System.Threading; using System.Threading.Tasks; -using System.Windows.Media; -using DynamicData; -using Microsoft.WindowsAPICodePack.Dialogs; using ReactiveUI.Fody.Helpers; using Wabbajack.Common; using Wabbajack.Compiler; -using Wabbajack.DTOs; using Wabbajack.DTOs.JsonConverters; -using Wabbajack.Extensions; -using Wabbajack.Installer; using Wabbajack.Models; using Wabbajack.Networking.WabbajackClientApi; using Wabbajack.Paths; using Wabbajack.Paths.IO; -using Wabbajack.RateLimiter; using Wabbajack.Services.OSIntegrated; -using NexusMods.Paths.FileTree; using System.Windows.Controls; using FluentIcons.Common; using System.Windows.Input; @@ -114,31 +104,22 @@ namespace Wabbajack _disposable.Dispose(); } } - public class CompilerFileManagerVM : BackNavigatingVM + public class CompilerFileManagerVM : BaseCompilerVM { - private readonly DTOSerializer _dtos; - private readonly SettingsManager _settingsManager; private readonly IServiceProvider _serviceProvider; - private readonly ILogger _logger; private readonly ResourceMonitor _resourceMonitor; private readonly CompilerSettingsInferencer _inferencer; - private readonly Client _wjClient; - [Reactive] public CompilerSettingsVM Settings { get; set; } = new(); public ObservableCollection Files { get; set; } public ICommand PrevCommand { get; set; } public CompilerFileManagerVM(ILogger logger, DTOSerializer dtos, SettingsManager settingsManager, IServiceProvider serviceProvider, LogStream loggerProvider, ResourceMonitor resourceMonitor, - CompilerSettingsInferencer inferencer, Client wjClient) : base(logger) + CompilerSettingsInferencer inferencer, Client wjClient) : base(dtos, settingsManager, logger, wjClient) { - _logger = logger; - _dtos = dtos; - _settingsManager = settingsManager; _serviceProvider = serviceProvider; _resourceMonitor = resourceMonitor; _inferencer = inferencer; - _wjClient = wjClient; MessageBus.Current.Listen() .Subscribe(msg => { @@ -152,6 +133,7 @@ namespace Wabbajack { var fileTree = GetDirectoryContents(new DirectoryInfo(Settings.Source.ToString())); Files = LoadFiles(new DirectoryInfo(Settings.Source.ToString())); + Disposable.Create(() => { }).DisposeWith(disposables); }); } @@ -202,11 +184,11 @@ namespace Wabbajack .Select(file => new FileTreeViewItem(file))); } - private void Header_PropertyChanged(object sender, PropertyChangedEventArgs e) + private async void Header_PropertyChanged(object sender, PropertyChangedEventArgs e) { + var updatedItem = (FileTreeItemVM)sender; if(e.PropertyName == nameof(FileTreeItemVM.SpecialFileState)) { - var updatedItem = (FileTreeItemVM)sender; IEnumerable currentEnumerable = null; for (int i = 0; i < updatedItem.PathRelativeToRoot.Depth - 1; i++) { @@ -218,6 +200,30 @@ namespace Wabbajack currentEnumerable = (IEnumerable)currentItem.ItemsSource; } } + else if(e.PropertyName == nameof(FileTreeItemVM.CompilerFileState)) + { + Settings.NoMatchInclude.Remove(updatedItem.PathRelativeToRoot); + Settings.Include.Remove(updatedItem.PathRelativeToRoot); + Settings.Ignore.Remove(updatedItem.PathRelativeToRoot); + Settings.AlwaysEnabled.Remove(updatedItem.PathRelativeToRoot); + + switch(updatedItem.CompilerFileState) + { + case CompilerFileState.NoMatchInclude: + Settings.NoMatchInclude.Add(updatedItem.PathRelativeToRoot); + break; + case CompilerFileState.Include: + Settings.Include.Add(updatedItem.PathRelativeToRoot); + break; + case CompilerFileState.Ignore: + Settings.Ignore.Add(updatedItem.PathRelativeToRoot); + break; + case CompilerFileState.AlwaysEnabled: + Settings.AlwaysEnabled.Add(updatedItem.PathRelativeToRoot); + break; + }; + await SaveSettings(); + } } private void LoadingItem_Expanded(object sender, System.Windows.RoutedEventArgs e) @@ -246,31 +252,5 @@ namespace Wabbajack NavigateToGlobal.Send(ScreenType.CompilerFileManager); LoadCompilerSettings.Send(Settings.ToCompilerSettings()); } - - private async Task SaveSettingsFile() - { - if (Settings.Source == default || Settings.CompilerSettingsPath == default) return; - - await using var st = Settings.CompilerSettingsPath.Open(FileMode.Create, FileAccess.Write, FileShare.None); - await JsonSerializer.SerializeAsync(st, Settings.ToCompilerSettings(), _dtos.Options); - - var allSavedCompilerSettings = await _settingsManager.Load>(Consts.AllSavedCompilerSettingsPaths); - - // Don't simply remove Settings.CompilerSettingsPath here, because WJ sometimes likes to make default compiler settings files - allSavedCompilerSettings.RemoveAll(path => path.Parent == Settings.Source); - allSavedCompilerSettings.Insert(0, Settings.CompilerSettingsPath); - - await _settingsManager.Save(Consts.AllSavedCompilerSettingsPaths, allSavedCompilerSettings); - } - - private async Task LoadLastSavedSettings() - { - AbsolutePath lastPath = default; - var allSavedCompilerSettings = await _settingsManager.Load>(Consts.AllSavedCompilerSettingsPaths); - if (allSavedCompilerSettings.Any()) - lastPath = allSavedCompilerSettings[0]; - - if (lastPath == default || !lastPath.FileExists() || lastPath.FileName.Extension != Ext.CompilerSettings) return; - } } }