wabbajack/Wabbajack/View Models/ModListVM.cs

121 lines
4.4 KiB
C#
Raw Normal View History

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;
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
{
public ModList SourceModList { get; }
public Exception Error { get; }
2019-11-03 06:01:19 +00:00
public string ModListPath { get; }
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; }
public ModListVM(string modListPath)
2019-11-03 06:01:19 +00:00
{
2019-11-11 11:38:45 +00:00
ModListPath = modListPath;
try
{
SourceModList = AInstaller.LoadFromFile(modListPath);
}
catch (Exception ex)
{
Error = ex;
}
2019-11-03 06:01:19 +00:00
ImageObservable = Observable.Return(Unit.Default)
2019-11-03 06:01:19 +00:00
.ObserveOn(RxApp.TaskpoolScheduler)
.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();
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)
{
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 =>
{
if (memStream == null) return default(BitmapImage);
2019-11-03 06:01:19 +00:00
try
{
return UIUtils.BitmapImageFromStream(memStream);
2019-11-03 06:01:19 +00:00
}
catch (Exception ex)
{
Utils.Error(ex, $"Exception while caching Mod List image {Name}");
2019-11-03 06:01:19 +00:00
return default(BitmapImage);
}
})
.Replay(1)
.RefCount();
}
public void OpenReadmeWindow()
{
if (string.IsNullOrEmpty(Readme)) return;
2019-12-29 22:57:01 +00:00
if (false) //SourceModList.ReadmeIsWebsite)
{
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 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-11-03 06:01:19 +00:00
}
}