using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Reactive.Disposables; using System.Reactive.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Media.Imaging; using Wabbajack.Common; using Wabbajack.Lib; namespace Wabbajack { public class CompilerVM : ViewModel { public MainWindowVM MWVM { get; } [Reactive] public string Mo2Folder { get; set; } [Reactive] public string MOProfile { get; set; } [Reactive] public string ModListName { get; set; } [Reactive] public string Location { get; set; } [Reactive] public bool UIReady { get; set; } = true; [Reactive] public string AuthorName { get; set; } [Reactive] public string Summary { get; set; } = "Description (700 characters max)"; [Reactive] public string ImagePath { get; set; } private readonly ObservableAsPropertyHelper _Image; public BitmapImage Image => _Image.Value; [Reactive] public string NexusSiteURL { get; set; } [Reactive] public string ReadMeText { get; set; } [Reactive] public string HTMLReport { get; set; } [Reactive] public string DownloadLocation { get; set; } public IReactiveCommand BeginCommand { get; } public CompilerVM(MainWindowVM mainWindowVM, string source) { this.MWVM = mainWindowVM; this.Location = source; this.BeginCommand = ReactiveCommand.CreateFromTask( execute: this.ExecuteBegin, canExecute: this.WhenAny(x => x.UIReady) .ObserveOnGuiThread()); this._Image = this.WhenAny(x => x.ImagePath) .Select(path => { if (string.IsNullOrWhiteSpace(path)) return UIUtils.BitmapImageFromResource("Wabbajack.Resources.Banner_Dark.png"); if (UIUtils.TryGetBitmapImageFromFile(path, out var image)) { return image; } return UIUtils.BitmapImageFromResource("Wabbajack.Resources.none.png"); }) .ToProperty(this, nameof(this.Image)); ConfigureForBuild(source); } private void ConfigureForBuild(string location) { var profile_folder = Path.GetDirectoryName(location); this.Mo2Folder = Path.GetDirectoryName(Path.GetDirectoryName(profile_folder)); if (!File.Exists(Path.Combine(this.Mo2Folder, "ModOrganizer.exe"))) { this.Log().Error($"Error! No ModOrganizer2.exe found in {this.Mo2Folder}"); } this.MOProfile = Path.GetFileName(profile_folder); this.ModListName = this.MOProfile; var tmp_compiler = new Compiler(this.Mo2Folder); this.DownloadLocation = tmp_compiler.MO2DownloadsFolder; } private async Task ExecuteBegin() { if (this.Mo2Folder != null) { var compiler = new Compiler(this.Mo2Folder) { MO2Profile = this.MOProfile, ModListName = this.ModListName, ModListAuthor = this.AuthorName, ModListDescription = this.Summary, ModListImage = this.ImagePath, ModListWebsite = this.NexusSiteURL, ModListReadme = this.ReadMeText, }; await Task.Run(() => { UIReady = false; try { compiler.Compile(); if (compiler.ModList?.ReportHTML != null) { this.HTMLReport = compiler.ModList.ReportHTML; } } catch (Exception ex) { while (ex.InnerException != null) ex = ex.InnerException; this.Log().Warn(ex, "Can't continue"); } finally { UIReady = true; } }); } else { this.Log().Warn("Cannot compile modlist: no valid Mod Organizer profile directory selected."); UIReady = true; } } } }