2019-11-03 06:01:19 +00:00
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2019-12-03 04:56:06 +00:00
|
|
|
|
using System.Reactive;
|
2019-11-03 06:01:19 +00:00
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.Lib;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public class ModListVM : ViewModel
|
|
|
|
|
{
|
2020-01-16 03:54:06 +00:00
|
|
|
|
public ModList SourceModList { get; private set; }
|
2019-12-03 05:35:51 +00:00
|
|
|
|
public Exception Error { get; }
|
2020-03-28 20:04:22 +00:00
|
|
|
|
public AbsolutePath ModListPath { get; }
|
2019-12-03 05:35:51 +00:00
|
|
|
|
public string Name => SourceModList?.Name;
|
|
|
|
|
public string Readme => SourceModList?.Readme;
|
|
|
|
|
public string Author => SourceModList?.Author;
|
|
|
|
|
public string Description => SourceModList?.Description;
|
2020-03-22 21:55:31 +00:00
|
|
|
|
public Uri Website => SourceModList?.Website;
|
2019-12-03 05:35:51 +00:00
|
|
|
|
public ModManager ModManager => SourceModList?.ModManager ?? ModManager.MO2;
|
2020-04-16 14:15:03 +00:00
|
|
|
|
public Version Version => SourceModList?.Version;
|
2020-04-27 09:58:33 +00:00
|
|
|
|
public bool IsNSFW => SourceModList?.IsNSFW ?? false;
|
2019-11-03 06:01:19 +00:00
|
|
|
|
|
|
|
|
|
// Image isn't exposed as a direct property, but as an observable.
|
|
|
|
|
// This acts as a caching mechanism, as interested parties will trigger it to be created,
|
|
|
|
|
// and the cached image will automatically be released when the last interested party is gone.
|
|
|
|
|
public IObservable<BitmapImage> ImageObservable { get; }
|
|
|
|
|
|
2020-03-28 20:04:22 +00:00
|
|
|
|
public ModListVM(AbsolutePath modListPath)
|
2019-11-03 06:01:19 +00:00
|
|
|
|
{
|
2019-11-11 11:38:45 +00:00
|
|
|
|
ModListPath = modListPath;
|
2019-12-03 05:35:51 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SourceModList = AInstaller.LoadFromFile(modListPath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Error = ex;
|
2020-03-04 12:10:49 +00:00
|
|
|
|
Utils.Error(ex, "Exception while loading the modlist!");
|
2019-12-03 05:35:51 +00:00
|
|
|
|
}
|
2019-11-03 06:01:19 +00:00
|
|
|
|
|
2019-12-03 04:56:06 +00:00
|
|
|
|
ImageObservable = Observable.Return(Unit.Default)
|
2020-01-16 03:19:00 +00:00
|
|
|
|
// Download and retrieve bytes on background thread
|
2019-11-03 06:01:19 +00:00
|
|
|
|
.ObserveOn(RxApp.TaskpoolScheduler)
|
2020-05-25 17:34:25 +00:00
|
|
|
|
.SelectAsync(async filePath =>
|
2019-11-03 06:01:19 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-05-25 17:34:25 +00:00
|
|
|
|
await using var fs = await ModListPath.OpenShared();
|
2020-03-28 20:04:22 +00:00
|
|
|
|
using var ar = new ZipArchive(fs, ZipArchiveMode.Read);
|
|
|
|
|
var ms = new MemoryStream();
|
|
|
|
|
var entry = ar.GetEntry("modlist-image.png");
|
|
|
|
|
if (entry == null) return default(MemoryStream);
|
2020-05-25 17:34:25 +00:00
|
|
|
|
await using var e = entry.Open();
|
|
|
|
|
e.CopyTo(ms);
|
2020-03-28 20:04:22 +00:00
|
|
|
|
return ms;
|
2019-11-03 06:01:19 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-12-05 05:07:44 +00:00
|
|
|
|
Utils.Error(ex, $"Exception while caching Mod List image {Name}");
|
2019-11-03 06:01:19 +00:00
|
|
|
|
return default(MemoryStream);
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-01-16 03:19:00 +00:00
|
|
|
|
// Create Bitmap image on GUI thread
|
2020-01-09 03:22:49 +00:00
|
|
|
|
.ObserveOnGuiThread()
|
2019-11-03 06:01:19 +00:00
|
|
|
|
.Select(memStream =>
|
|
|
|
|
{
|
2019-12-15 19:09:07 +00:00
|
|
|
|
if (memStream == null) return default(BitmapImage);
|
2019-11-03 06:01:19 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-15 19:09:07 +00:00
|
|
|
|
return UIUtils.BitmapImageFromStream(memStream);
|
2019-11-03 06:01:19 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-12-05 05:07:44 +00:00
|
|
|
|
Utils.Error(ex, $"Exception while caching Mod List image {Name}");
|
2019-11-03 06:01:19 +00:00
|
|
|
|
return default(BitmapImage);
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-01-16 03:19:00 +00:00
|
|
|
|
// If ever would return null, show WJ logo instead
|
2020-05-25 17:34:25 +00:00
|
|
|
|
.Select(x => x ?? ResourceLinks.WabbajackLogoNoText.Value)
|
2019-11-03 06:01:19 +00:00
|
|
|
|
.Replay(1)
|
|
|
|
|
.RefCount();
|
|
|
|
|
}
|
2019-12-20 05:09:53 +00:00
|
|
|
|
|
2020-04-15 17:40:41 +00:00
|
|
|
|
public void OpenReadme()
|
2019-12-20 05:09:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(Readme)) return;
|
2020-04-15 17:40:41 +00:00
|
|
|
|
Utils.OpenWebsite(new Uri(Readme));
|
2019-12-20 05:09:53 +00:00
|
|
|
|
}
|
2020-01-16 03:54:06 +00:00
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
|
|
|
|
base.Dispose();
|
|
|
|
|
// Just drop reference explicitly, as it's large, so it can be GCed
|
|
|
|
|
// Even if someone is holding a stale reference to the VM
|
2020-04-15 17:40:41 +00:00
|
|
|
|
SourceModList = null;
|
2020-01-16 03:54:06 +00:00
|
|
|
|
}
|
2019-11-03 06:01:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|