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,
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<CompilerDetailsVM> _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<Unit, Unit> ExecuteCommand { get; } */
@ -78,16 +72,12 @@ namespace Wabbajack
public CompilerDetailsVM(ILogger<CompilerDetailsVM> 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<LoadCompilerSettings>()
.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<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
public void AddOtherProfile(string profile)

View File

@ -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<CompilerFileManagerVM> _logger;
private readonly ResourceMonitor _resourceMonitor;
private readonly CompilerSettingsInferencer _inferencer;
private readonly Client _wjClient;
[Reactive] public CompilerSettingsVM Settings { get; set; } = new();
public ObservableCollection<FileTreeViewItem> Files { get; set; }
public ICommand PrevCommand { get; set; }
public CompilerFileManagerVM(ILogger<CompilerFileManagerVM> 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<LoadCompilerSettings>()
.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)
{
if(e.PropertyName == nameof(FileTreeItemVM.SpecialFileState))
private async void Header_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var updatedItem = (FileTreeItemVM)sender;
if(e.PropertyName == nameof(FileTreeItemVM.SpecialFileState))
{
IEnumerable<FileTreeViewItem> currentEnumerable = null;
for (int i = 0; i < updatedItem.PathRelativeToRoot.Depth - 1; i++)
{
@ -218,6 +200,30 @@ namespace Wabbajack
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)
@ -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<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;
}
}
}