wabbajack/Wabbajack/View Models/Compilers/VortexCompilerVM.cs

221 lines
9.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
2019-11-16 23:10:17 +00:00
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using DynamicData.Binding;
using ReactiveUI;
2019-11-16 23:10:17 +00:00
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
using Wabbajack.Lib;
namespace Wabbajack
{
public class VortexCompilerVM : ViewModel, ISubCompilerVM
{
2019-11-17 12:42:31 +00:00
private readonly VortexCompilationSettings _settings;
2019-11-16 23:10:17 +00:00
public IReactiveCommand BeginCommand { get; }
2019-11-17 12:42:31 +00:00
private readonly ObservableAsPropertyHelper<ModlistSettingsEditorVM> _modListSettings;
public ModlistSettingsEditorVM ModlistSettings => _modListSettings.Value;
2019-11-16 23:10:17 +00:00
2019-11-17 12:42:31 +00:00
private static readonly ObservableCollectionExtended<GameVM> _gameOptions = new ObservableCollectionExtended<GameVM>(
EnumExt.GetValues<Game>()
2019-11-18 19:31:55 +00:00
.Where(g => GameRegistry.Games[g].SupportedModManager == ModManager.Vortex && !GameRegistry.Games[g].Disabled)
.Select(g => new GameVM(g))
.OrderBy(g => g.DisplayName));
2019-11-17 12:42:31 +00:00
public ObservableCollectionExtended<GameVM> GameOptions => _gameOptions;
2019-11-21 05:15:47 +00:00
[Reactive]
public ACompiler ActiveCompilation { get; private set; }
2019-11-16 23:10:17 +00:00
[Reactive]
public GameVM SelectedGame { get; set; }
2019-11-16 23:10:17 +00:00
[Reactive]
public FilePickerVM GameLocation { get; set; }
[Reactive]
public FilePickerVM DownloadsLocation { get; set; }
[Reactive]
public FilePickerVM StagingLocation { get; set; }
public ICommand FindGameInSteamCommand { get; }
public ICommand FindGameInGogCommand { get; }
[Reactive]
public StatusUpdateTracker StatusTracker { get; private set; }
2019-11-16 23:10:17 +00:00
public VortexCompilerVM(CompilerVM parent)
{
GameLocation = new FilePickerVM()
2019-11-16 23:10:17 +00:00
{
ExistCheckOption = FilePickerVM.ExistCheckOptions.On,
2019-11-16 23:10:17 +00:00
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select Game Folder Location"
};
DownloadsLocation = new FilePickerVM()
2019-11-16 23:10:17 +00:00
{
ExistCheckOption = FilePickerVM.ExistCheckOptions.On,
2019-11-16 23:10:17 +00:00
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select Downloads Folder"
};
StagingLocation = new FilePickerVM()
2019-11-16 23:10:17 +00:00
{
ExistCheckOption = FilePickerVM.ExistCheckOptions.On,
2019-11-16 23:10:17 +00:00
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select Staging Folder"
};
// Wire start command
BeginCommand = ReactiveCommand.CreateFromTask(
2019-11-16 23:10:17 +00:00
canExecute: Observable.CombineLatest(
this.WhenAny(x => x.GameLocation.InError),
this.WhenAny(x => x.DownloadsLocation.InError),
this.WhenAny(x => x.StagingLocation.InError),
2019-11-17 12:42:31 +00:00
(g, d, s) => !g && !d && !s)
2019-11-16 23:10:17 +00:00
.ObserveOnGuiThread(),
execute: async () =>
{
try
{
2019-11-21 05:15:47 +00:00
this.ActiveCompilation = new VortexCompiler(
2019-11-17 12:42:31 +00:00
SelectedGame.Game,
GameLocation.TargetPath,
VortexCompiler.TypicalVortexFolder(),
DownloadsLocation.TargetPath,
2019-11-17 14:30:06 +00:00
StagingLocation.TargetPath)
{
ModListName = ModlistSettings.ModListName,
ModListAuthor = ModlistSettings.AuthorText,
ModListDescription = ModlistSettings.Description,
ModListImage = ModlistSettings.ImagePath.TargetPath,
ModListWebsite = ModlistSettings.Website,
ModListReadme = ModlistSettings.ReadMeText.TargetPath
};
2019-11-16 23:10:17 +00:00
}
catch (Exception ex)
{
while (ex.InnerException != null) ex = ex.InnerException;
Utils.Log($"Compiler error: {ex.ExceptionToString()}");
return;
}
await Task.Run(async () =>
2019-11-16 23:10:17 +00:00
{
try
{
2019-11-21 05:15:47 +00:00
await this.ActiveCompilation.Begin();
2019-11-16 23:10:17 +00:00
}
catch (Exception ex)
{
while (ex.InnerException != null) ex = ex.InnerException;
Utils.Log($"Compiler error: {ex.ExceptionToString()}");
}
finally
{
this.StatusTracker = null;
2019-11-21 05:15:47 +00:00
this.ActiveCompilation.Dispose();
this.ActiveCompilation = null;
}
2019-11-16 23:10:17 +00:00
});
});
// Load settings
2019-11-17 12:42:31 +00:00
_settings = parent.MWVM.Settings.Compiler.VortexCompilation;
SelectedGame = _gameOptions.FirstOrDefault(x => x.Game == _settings.LastCompiledGame) ?? _gameOptions[0];
2019-11-16 23:10:17 +00:00
parent.MWVM.Settings.SaveSignal
.Subscribe(_ => Unload())
2019-11-17 12:42:31 +00:00
.DisposeWith(CompositeDisposable);
2019-11-16 23:10:17 +00:00
// Load custom game settings when game type changes
this.WhenAny(x => x.SelectedGame)
2019-11-17 12:42:31 +00:00
.Select(game => _settings.ModlistSettings.TryCreate(game.Game))
2019-11-16 23:10:17 +00:00
.Pairwise()
.Subscribe(pair =>
{
// Save old
2019-11-17 12:42:31 +00:00
var (previous, current) = pair;
if (previous != null)
2019-11-16 23:10:17 +00:00
{
2019-11-17 12:42:31 +00:00
previous.GameLocation = GameLocation.TargetPath;
2019-11-16 23:10:17 +00:00
}
// Load new
2019-11-17 12:42:31 +00:00
GameLocation.TargetPath = current?.GameLocation;
if (string.IsNullOrWhiteSpace(GameLocation.TargetPath))
{
2019-11-17 12:42:31 +00:00
SetGameToSteamLocation();
}
2019-11-17 12:42:31 +00:00
if (string.IsNullOrWhiteSpace(GameLocation.TargetPath))
{
2019-11-17 12:42:31 +00:00
SetGameToGogLocation();
}
2019-11-17 12:42:31 +00:00
DownloadsLocation.TargetPath = current?.DownloadLocation;
if (string.IsNullOrWhiteSpace(DownloadsLocation.TargetPath))
{
2019-11-17 12:42:31 +00:00
DownloadsLocation.TargetPath = VortexCompiler.RetrieveDownloadLocation(SelectedGame.Game);
}
2019-11-17 12:42:31 +00:00
StagingLocation.TargetPath = current?.StagingLocation;
if (string.IsNullOrWhiteSpace(StagingLocation.TargetPath))
{
2019-11-17 12:42:31 +00:00
StagingLocation.TargetPath = VortexCompiler.RetrieveStagingLocation(SelectedGame.Game);
}
2019-11-16 23:10:17 +00:00
})
2019-11-17 12:42:31 +00:00
.DisposeWith(CompositeDisposable);
2019-11-16 23:10:17 +00:00
2019-11-17 12:42:31 +00:00
// Load custom ModList settings when game type changes
this._modListSettings = this.WhenAny(x => x.SelectedGame)
2019-11-16 23:10:17 +00:00
.Select(game =>
{
2019-11-17 12:42:31 +00:00
var gameSettings = _settings.ModlistSettings.TryCreate(game.Game);
2019-11-16 23:10:17 +00:00
return new ModlistSettingsEditorVM(gameSettings.ModlistSettings);
})
// Interject and save old while loading new
.Pairwise()
.Do(pair =>
{
2019-11-17 12:42:31 +00:00
var (previous, current) = pair;
previous?.Save();
current?.Init();
2019-11-16 23:10:17 +00:00
})
.Select(x => x.Current)
// Save to property
.ObserveOnGuiThread()
2019-11-17 12:42:31 +00:00
.ToProperty(this, nameof(ModlistSettings));
// Find game commands
2019-11-17 12:42:31 +00:00
FindGameInSteamCommand = ReactiveCommand.Create(SetGameToSteamLocation);
FindGameInGogCommand = ReactiveCommand.Create(SetGameToGogLocation);
// Add additional criteria to download/staging folders
2019-11-17 12:42:31 +00:00
DownloadsLocation.AdditionalError = this.WhenAny(x => x.DownloadsLocation.TargetPath)
.Select(path => path == null ? ErrorResponse.Success : VortexCompiler.IsValidDownloadsFolder(path));
StagingLocation.AdditionalError = this.WhenAny(x => x.StagingLocation.TargetPath)
.Select(path => path == null ? ErrorResponse.Success : VortexCompiler.IsValidBaseStagingFolder(path));
2019-11-16 23:10:17 +00:00
}
public void Unload()
{
2019-11-17 12:42:31 +00:00
_settings.LastCompiledGame = SelectedGame.Game;
ModlistSettings?.Save();
2019-11-16 23:10:17 +00:00
}
private void SetGameToSteamLocation()
{
2019-11-17 12:42:31 +00:00
var steamGame = SteamHandler.Instance.Games.FirstOrDefault(g => g.Game.HasValue && g.Game == SelectedGame.Game);
GameLocation.TargetPath = steamGame?.InstallDir;
}
private void SetGameToGogLocation()
{
2019-11-17 12:42:31 +00:00
var gogGame = GOGHandler.Instance.Games.FirstOrDefault(g => g.Game.HasValue && g.Game == SelectedGame.Game);
GameLocation.TargetPath = gogGame?.Path;
}
}
2019-11-21 05:15:47 +00:00
}