2021-10-27 04:28:01 +00:00
|
|
|
|
|
|
|
using System;
|
2021-09-30 22:15:31 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reactive;
|
2021-09-30 04:03:43 +00:00
|
|
|
using System.Reactive.Disposables;
|
|
|
|
using System.Reactive.Linq;
|
2021-09-30 22:15:31 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-09-30 04:03:43 +00:00
|
|
|
using Avalonia.Media.Imaging;
|
2021-09-30 22:15:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-09-30 04:03:43 +00:00
|
|
|
using ReactiveUI;
|
|
|
|
using ReactiveUI.Fody.Helpers;
|
|
|
|
using Wabbajack.App.Extensions;
|
|
|
|
using Wabbajack.App.Messages;
|
|
|
|
using Wabbajack.App.Models;
|
|
|
|
using Wabbajack.App.ViewModels;
|
2021-09-30 22:15:31 +00:00
|
|
|
using Wabbajack.Common;
|
|
|
|
using Wabbajack.DTOs;
|
2021-09-30 04:03:43 +00:00
|
|
|
using Wabbajack.DTOs.SavedSettings;
|
|
|
|
using Wabbajack.Paths;
|
2021-09-30 22:15:31 +00:00
|
|
|
using Wabbajack.Paths.IO;
|
2021-09-30 04:03:43 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.App.Screens;
|
|
|
|
|
2021-10-27 04:28:01 +00:00
|
|
|
public class LauncherViewModel : ViewModelBase
|
2021-09-30 04:03:43 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
private readonly ILogger<LauncherViewModel> _logger;
|
2021-09-30 22:18:58 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public ReactiveCommand<Unit, Unit> PlayButton;
|
2021-09-30 12:23:43 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public LauncherViewModel(ILogger<LauncherViewModel> logger, InstallationStateManager manager)
|
|
|
|
{
|
|
|
|
Activator = new ViewModelActivator();
|
|
|
|
PlayButton = ReactiveCommand.Create(() => { StartGame().FireAndForget(); });
|
|
|
|
_logger = logger;
|
2021-09-30 22:15:31 +00:00
|
|
|
|
2021-10-27 04:28:01 +00:00
|
|
|
MessageBus.Current.Listen<ConfigureLauncher>()
|
2021-11-04 03:43:20 +00:00
|
|
|
.Subscribe(Receive)
|
2021-10-27 04:28:01 +00:00
|
|
|
.DisposeWith(VMDisposables);
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
this.WhenActivated(disposables =>
|
2021-09-30 04:03:43 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
this.WhenAnyValue(v => v.InstallFolder)
|
2021-11-04 03:43:20 +00:00
|
|
|
.SelectMany(async folder => await manager.GetByInstallFolder(folder))
|
|
|
|
.OnUIThread()
|
2021-10-23 16:51:17 +00:00
|
|
|
.Where(v => v != null)
|
|
|
|
.BindTo(this, vm => vm.Setting)
|
|
|
|
.DisposeWith(disposables);
|
2021-09-30 04:03:43 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
this.WhenAnyValue(v => v.Setting)
|
2021-11-02 13:40:59 +00:00
|
|
|
.Where(v => v != default && v!.Image != default && v!.Image.FileExists())
|
2021-10-23 16:51:17 +00:00
|
|
|
.Select(v => new Bitmap(v!.Image.ToString()))
|
|
|
|
.BindTo(this, vm => vm.Image)
|
|
|
|
.DisposeWith(disposables);
|
2021-09-30 12:23:43 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
this.WhenAnyValue(v => v.Setting)
|
|
|
|
.Where(v => v is {Metadata: { }})
|
|
|
|
.Select(v => $"{v!.Metadata!.Title} v{v!.Metadata.Version}")
|
|
|
|
.BindTo(this, vm => vm.Title)
|
|
|
|
.DisposeWith(disposables);
|
|
|
|
});
|
|
|
|
}
|
2021-09-30 12:23:43 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
[Reactive] public AbsolutePath InstallFolder { get; set; }
|
2021-09-30 04:03:43 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
[Reactive] public IBitmap Image { get; set; }
|
2021-09-30 22:15:31 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
[Reactive] public InstallationConfigurationSetting? Setting { get; set; }
|
|
|
|
|
|
|
|
[Reactive] public string Title { get; set; }
|
|
|
|
|
|
|
|
public void Receive(ConfigureLauncher val)
|
|
|
|
{
|
|
|
|
InstallFolder = val.InstallFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task StartGame()
|
|
|
|
{
|
|
|
|
var mo2Path = InstallFolder.Combine("ModOrganizer.exe");
|
|
|
|
var gamePath = GameRegistry.Games.Values.Select(g => g.MainExecutable)
|
|
|
|
.Where(ge => ge != null)
|
|
|
|
.Select(ge => InstallFolder.Combine(ge!))
|
|
|
|
.FirstOrDefault(ge => ge.FileExists());
|
|
|
|
if (mo2Path.FileExists())
|
|
|
|
Process.Start(mo2Path.ToString());
|
|
|
|
else if (gamePath.FileExists())
|
|
|
|
Process.Start(gamePath.ToString());
|
|
|
|
else
|
|
|
|
_logger.LogError("No way to launch game, no acceptable executable found");
|
2021-09-30 04:03:43 +00:00
|
|
|
}
|
|
|
|
}
|