Save CompilerFileState changes in file manager to disk, create base compiler class

This commit is contained in:
trawzified 2024-06-24 19:50:46 +02:00
parent 3cc9cf9856
commit 30ab06a304
3 changed files with 88 additions and 92 deletions

View File

@ -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<BaseCompilerVM> _logger;
protected readonly Client _wjClient;
[Reactive] public CompilerSettingsVM Settings { get; set; } = new();
public BaseCompilerVM(DTOSerializer dtos, SettingsManager settingsManager, ILogger<BaseCompilerVM> 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<List<AbsolutePath>>(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);
}
}
}

View File

@ -38,15 +38,11 @@ namespace Wabbajack
Completed, Completed,
Errored 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 IServiceProvider _serviceProvider;
private readonly ILogger<CompilerDetailsVM> _logger;
private readonly ResourceMonitor _resourceMonitor; private readonly ResourceMonitor _resourceMonitor;
private readonly CompilerSettingsInferencer _inferencer; private readonly CompilerSettingsInferencer _inferencer;
private readonly Client _wjClient;
[Reactive] public string StatusText { get; set; } [Reactive] public string StatusText { get; set; }
[Reactive] public Percent StatusProgress { get; set; } [Reactive] public Percent StatusProgress { get; set; }
@ -62,8 +58,6 @@ namespace Wabbajack
public FilePickerVM DownloadLocation { get; } public FilePickerVM DownloadLocation { get; }
public FilePickerVM OutputLocation { get; } public FilePickerVM OutputLocation { get; }
[Reactive] public CompilerSettingsVM Settings { get; set; } = new();
public FilePickerVM ModListImageLocation { get; } = new(); public FilePickerVM ModListImageLocation { get; } = new();
/* public ReactiveCommand<Unit, Unit> ExecuteCommand { get; } */ /* public ReactiveCommand<Unit, Unit> ExecuteCommand { get; } */
@ -78,16 +72,12 @@ namespace Wabbajack
public CompilerDetailsVM(ILogger<CompilerDetailsVM> logger, DTOSerializer dtos, SettingsManager settingsManager, public CompilerDetailsVM(ILogger<CompilerDetailsVM> logger, DTOSerializer dtos, SettingsManager settingsManager,
IServiceProvider serviceProvider, LogStream loggerProvider, ResourceMonitor resourceMonitor, 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; _serviceProvider = serviceProvider;
LoggerProvider = loggerProvider; LoggerProvider = loggerProvider;
_resourceMonitor = resourceMonitor; _resourceMonitor = resourceMonitor;
_inferencer = inferencer; _inferencer = inferencer;
_wjClient = wjClient;
MessageBus.Current.Listen<LoadCompilerSettings>() MessageBus.Current.Listen<LoadCompilerSettings>()
.Subscribe(msg => { .Subscribe(msg => {
@ -105,7 +95,7 @@ namespace Wabbajack
BackCommand = ReactiveCommand.CreateFromTask(async () => BackCommand = ReactiveCommand.CreateFromTask(async () =>
{ {
await SaveSettingsFile(); await SaveSettings();
NavigateToGlobal.Send(ScreenType.Home); NavigateToGlobal.Send(ScreenType.Home);
}); });
@ -179,7 +169,7 @@ namespace Wabbajack
this.WhenAnyValue(x => x.Settings) this.WhenAnyValue(x => x.Settings)
.Throttle(TimeSpan.FromSeconds(2)) .Throttle(TimeSpan.FromSeconds(2))
.Subscribe(_ => SaveSettingsFile().FireAndForget()) .Subscribe(_ => SaveSettings().FireAndForget())
.DisposeWith(disposables); .DisposeWith(disposables);
/* /*
@ -264,7 +254,7 @@ namespace Wabbajack
private async Task NextPage() private async Task NextPage()
{ {
await SaveSettingsFile(); await SaveSettings();
NavigateToGlobal.Send(ScreenType.CompilerFileManager); NavigateToGlobal.Send(ScreenType.CompilerFileManager);
LoadCompilerSettings.Send(Settings.ToCompilerSettings()); LoadCompilerSettings.Send(Settings.ToCompilerSettings());
} }
@ -275,7 +265,7 @@ namespace Wabbajack
{ {
try try
{ {
await SaveSettingsFile(); await SaveSettings();
var token = CancellationToken.None; var token = CancellationToken.None;
State = CompilerState.Compiling; State = CompilerState.Compiling;
@ -369,33 +359,6 @@ namespace Wabbajack
return true; 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<List<AbsolutePath>>(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<List<AbsolutePath>>(Consts.AllSavedCompilerSettingsPaths);
if (allSavedCompilerSettings.Any())
lastPath = allSavedCompilerSettings[0];
if (lastPath == default || !lastPath.FileExists() || lastPath.FileName.Extension != Ext.CompilerSettings) return;
ModlistLocation.TargetPath = lastPath;
}
#region ListOps #region ListOps
public void AddOtherProfile(string profile) public void AddOtherProfile(string profile)

View File

@ -3,32 +3,22 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reactive;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Wabbajack.Messages; using Wabbajack.Messages;
using ReactiveUI; using ReactiveUI;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media;
using DynamicData;
using Microsoft.WindowsAPICodePack.Dialogs;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Wabbajack.Common; using Wabbajack.Common;
using Wabbajack.Compiler; using Wabbajack.Compiler;
using Wabbajack.DTOs;
using Wabbajack.DTOs.JsonConverters; using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Extensions;
using Wabbajack.Installer;
using Wabbajack.Models; using Wabbajack.Models;
using Wabbajack.Networking.WabbajackClientApi; using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.Paths; using Wabbajack.Paths;
using Wabbajack.Paths.IO; using Wabbajack.Paths.IO;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated; using Wabbajack.Services.OSIntegrated;
using NexusMods.Paths.FileTree;
using System.Windows.Controls; using System.Windows.Controls;
using FluentIcons.Common; using FluentIcons.Common;
using System.Windows.Input; using System.Windows.Input;
@ -114,31 +104,22 @@ namespace Wabbajack
_disposable.Dispose(); _disposable.Dispose();
} }
} }
public class CompilerFileManagerVM : BackNavigatingVM public class CompilerFileManagerVM : BaseCompilerVM
{ {
private readonly DTOSerializer _dtos;
private readonly SettingsManager _settingsManager;
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly ILogger<CompilerFileManagerVM> _logger;
private readonly ResourceMonitor _resourceMonitor; private readonly ResourceMonitor _resourceMonitor;
private readonly CompilerSettingsInferencer _inferencer; private readonly CompilerSettingsInferencer _inferencer;
private readonly Client _wjClient;
[Reactive] public CompilerSettingsVM Settings { get; set; } = new();
public ObservableCollection<FileTreeViewItem> Files { get; set; } public ObservableCollection<FileTreeViewItem> Files { get; set; }
public ICommand PrevCommand { get; set; } public ICommand PrevCommand { get; set; }
public CompilerFileManagerVM(ILogger<CompilerFileManagerVM> logger, DTOSerializer dtos, SettingsManager settingsManager, public CompilerFileManagerVM(ILogger<CompilerFileManagerVM> logger, DTOSerializer dtos, SettingsManager settingsManager,
IServiceProvider serviceProvider, LogStream loggerProvider, ResourceMonitor resourceMonitor, 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; _serviceProvider = serviceProvider;
_resourceMonitor = resourceMonitor; _resourceMonitor = resourceMonitor;
_inferencer = inferencer; _inferencer = inferencer;
_wjClient = wjClient;
MessageBus.Current.Listen<LoadCompilerSettings>() MessageBus.Current.Listen<LoadCompilerSettings>()
.Subscribe(msg => { .Subscribe(msg => {
@ -152,6 +133,7 @@ namespace Wabbajack
{ {
var fileTree = GetDirectoryContents(new DirectoryInfo(Settings.Source.ToString())); var fileTree = GetDirectoryContents(new DirectoryInfo(Settings.Source.ToString()));
Files = LoadFiles(new DirectoryInfo(Settings.Source.ToString())); Files = LoadFiles(new DirectoryInfo(Settings.Source.ToString()));
Disposable.Create(() => { }).DisposeWith(disposables); Disposable.Create(() => { }).DisposeWith(disposables);
}); });
} }
@ -202,11 +184,11 @@ namespace Wabbajack
.Select(file => new FileTreeViewItem(file))); .Select(file => new FileTreeViewItem(file)));
} }
private void Header_PropertyChanged(object sender, PropertyChangedEventArgs e) private async void Header_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == nameof(FileTreeItemVM.SpecialFileState))
{ {
var updatedItem = (FileTreeItemVM)sender; var updatedItem = (FileTreeItemVM)sender;
if(e.PropertyName == nameof(FileTreeItemVM.SpecialFileState))
{
IEnumerable<FileTreeViewItem> currentEnumerable = null; IEnumerable<FileTreeViewItem> currentEnumerable = null;
for (int i = 0; i < updatedItem.PathRelativeToRoot.Depth - 1; i++) for (int i = 0; i < updatedItem.PathRelativeToRoot.Depth - 1; i++)
{ {
@ -218,6 +200,30 @@ namespace Wabbajack
currentEnumerable = (IEnumerable<FileTreeViewItem>)currentItem.ItemsSource; currentEnumerable = (IEnumerable<FileTreeViewItem>)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) private void LoadingItem_Expanded(object sender, System.Windows.RoutedEventArgs e)
@ -246,31 +252,5 @@ namespace Wabbajack
NavigateToGlobal.Send(ScreenType.CompilerFileManager); NavigateToGlobal.Send(ScreenType.CompilerFileManager);
LoadCompilerSettings.Send(Settings.ToCompilerSettings()); 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<List<AbsolutePath>>(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<List<AbsolutePath>>(Consts.AllSavedCompilerSettingsPaths);
if (allSavedCompilerSettings.Any())
lastPath = allSavedCompilerSettings[0];
if (lastPath == default || !lastPath.FileExists() || lastPath.FileName.Extension != Ext.CompilerSettings) return;
}
} }
} }