2019-11-03 06:01:19 +00:00
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using System;
|
2019-12-20 07:14:43 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-11-03 06:01:19 +00:00
|
|
|
|
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;
|
2020-01-07 13:03:46 +00:00
|
|
|
|
using Wabbajack.UI;
|
2019-11-03 06:01:19 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public class ModListVM : ViewModel
|
|
|
|
|
{
|
|
|
|
|
public ModList SourceModList { get; }
|
2019-12-03 05:35:51 +00:00
|
|
|
|
public Exception Error { get; }
|
2019-11-03 06:01:19 +00:00
|
|
|
|
public string ModListPath { get; }
|
2019-12-03 05:35:51 +00:00
|
|
|
|
public string Name => SourceModList?.Name;
|
|
|
|
|
public string ReportHTML => SourceModList?.ReportHTML;
|
|
|
|
|
public string Readme => SourceModList?.Readme;
|
|
|
|
|
public string Author => SourceModList?.Author;
|
|
|
|
|
public string Description => SourceModList?.Description;
|
|
|
|
|
public string Website => SourceModList?.Website;
|
|
|
|
|
public ModManager ModManager => SourceModList?.ModManager ?? ModManager.MO2;
|
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; }
|
|
|
|
|
|
2019-12-03 05:35:51 +00:00
|
|
|
|
public ModListVM(string 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;
|
|
|
|
|
}
|
2019-11-03 06:01:19 +00:00
|
|
|
|
|
2019-12-03 04:56:06 +00:00
|
|
|
|
ImageObservable = Observable.Return(Unit.Default)
|
2019-11-03 06:01:19 +00:00
|
|
|
|
.ObserveOn(RxApp.TaskpoolScheduler)
|
2019-12-03 04:56:06 +00:00
|
|
|
|
.Select(filePath =>
|
2019-11-03 06:01:19 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-11-21 15:04:33 +00:00
|
|
|
|
using (var fs = new FileStream(ModListPath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
2019-11-03 06:01:19 +00:00
|
|
|
|
using (var ar = new ZipArchive(fs, ZipArchiveMode.Read))
|
|
|
|
|
{
|
|
|
|
|
var ms = new MemoryStream();
|
2019-12-03 04:56:06 +00:00
|
|
|
|
var entry = ar.GetEntry("modlist-image.png");
|
|
|
|
|
if (entry == null) return default(MemoryStream);
|
2019-11-03 06:01:19 +00:00
|
|
|
|
using (var e = entry.Open())
|
|
|
|
|
{
|
|
|
|
|
e.CopyTo(ms);
|
|
|
|
|
}
|
|
|
|
|
return ms;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.ObserveOn(RxApp.MainThreadScheduler)
|
|
|
|
|
.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);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.Replay(1)
|
|
|
|
|
.RefCount();
|
|
|
|
|
}
|
2019-12-20 05:09:53 +00:00
|
|
|
|
|
|
|
|
|
public void OpenReadmeWindow()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(Readme)) return;
|
2019-12-29 22:57:01 +00:00
|
|
|
|
if (false) //SourceModList.ReadmeIsWebsite)
|
2019-12-20 05:09:53 +00:00
|
|
|
|
{
|
2019-12-20 07:14:43 +00:00
|
|
|
|
Process.Start(Readme);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using (var fs = new FileStream(ModListPath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
|
|
|
using (var ar = new ZipArchive(fs, ZipArchiveMode.Read))
|
|
|
|
|
using (var ms = new MemoryStream())
|
2019-12-20 05:09:53 +00:00
|
|
|
|
{
|
2019-12-20 07:14:43 +00:00
|
|
|
|
var entry = ar.GetEntry(Readme);
|
|
|
|
|
if (entry == null)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Tried to open a non-existant readme: {Readme}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
using (var e = entry.Open())
|
|
|
|
|
{
|
|
|
|
|
e.CopyTo(ms);
|
|
|
|
|
}
|
|
|
|
|
ms.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
using (var reader = new StreamReader(ms))
|
|
|
|
|
{
|
|
|
|
|
var viewer = new TextViewer(reader.ReadToEnd(), Name);
|
|
|
|
|
viewer.Show();
|
|
|
|
|
}
|
2019-12-20 05:09:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-03 06:01:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|