2019-10-28 04:36:46 +00:00
|
|
|
using ReactiveUI;
|
2019-10-23 04:15:42 +00:00
|
|
|
using Splat;
|
2019-10-13 19:53:49 +00:00
|
|
|
using System;
|
2019-10-11 10:14:01 +00:00
|
|
|
using System.Collections.Generic;
|
2019-10-14 01:15:41 +00:00
|
|
|
using System.Diagnostics;
|
2019-10-11 10:14:01 +00:00
|
|
|
using System.IO;
|
2019-10-13 19:53:49 +00:00
|
|
|
using System.IO.Compression;
|
2019-10-11 10:14:01 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
2019-10-13 19:53:49 +00:00
|
|
|
using System.Reactive;
|
|
|
|
using System.Reactive.Disposables;
|
|
|
|
using System.Reactive.Linq;
|
2019-10-11 10:14:01 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using Wabbajack.Common;
|
2019-10-16 03:10:34 +00:00
|
|
|
using Wabbajack.Lib;
|
2019-10-28 04:47:17 +00:00
|
|
|
using Wabbajack.Lib.Downloaders;
|
2019-10-16 03:10:34 +00:00
|
|
|
using Wabbajack.Lib.NexusApi;
|
2019-10-11 10:14:01 +00:00
|
|
|
|
2019-10-13 20:24:33 +00:00
|
|
|
namespace Wabbajack
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
2019-10-13 19:12:33 +00:00
|
|
|
public class SlideShow : ViewModel
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
|
|
|
private readonly Random _random;
|
|
|
|
private Slide _lastSlide;
|
|
|
|
private const bool UseSync = false;
|
|
|
|
private const int MaxCacheSize = 10;
|
|
|
|
|
2019-10-13 19:12:33 +00:00
|
|
|
public List<Slide> SlideShowElements { get; set; }
|
|
|
|
|
|
|
|
public Dictionary<string, Slide> CachedSlides { get; }
|
|
|
|
|
|
|
|
public Queue<Slide> SlidesQueue { get; }
|
|
|
|
|
2019-10-25 04:26:29 +00:00
|
|
|
public InstallerVM Installer { get; }
|
2019-10-13 19:12:33 +00:00
|
|
|
|
2019-10-22 01:08:26 +00:00
|
|
|
public BitmapImage NextIcon { get; } = UIUtils.BitmapImageFromResource("Wabbajack.Resources.Icons.next.png");
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-13 19:12:33 +00:00
|
|
|
private bool _ShowNSFW;
|
|
|
|
public bool ShowNSFW { get => _ShowNSFW; set => this.RaiseAndSetIfChanged(ref _ShowNSFW, value); }
|
|
|
|
|
|
|
|
private bool _GCAfterUpdating = true;
|
|
|
|
public bool GCAfterUpdating { get => _GCAfterUpdating; set => this.RaiseAndSetIfChanged(ref _GCAfterUpdating, value); }
|
|
|
|
|
2019-10-13 22:08:50 +00:00
|
|
|
private bool _Enable = true;
|
|
|
|
public bool Enable { get => _Enable; set => this.RaiseAndSetIfChanged(ref _Enable, value); }
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-13 22:08:50 +00:00
|
|
|
private BitmapImage _Image;
|
|
|
|
public BitmapImage Image { get => _Image; set => this.RaiseAndSetIfChanged(ref _Image, value); }
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-13 22:08:50 +00:00
|
|
|
private string _ModName = "Wabbajack";
|
|
|
|
public string ModName { get => _ModName; set => this.RaiseAndSetIfChanged(ref _ModName, value); }
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-13 22:08:50 +00:00
|
|
|
private string _AuthorName = "Halgari & the Wabbajack Team";
|
|
|
|
public string AuthorName { get => _AuthorName; set => this.RaiseAndSetIfChanged(ref _AuthorName, value); }
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-13 22:08:50 +00:00
|
|
|
private string _Summary;
|
|
|
|
public string Summary { get => _Summary; set => this.RaiseAndSetIfChanged(ref _Summary, value); }
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-14 01:15:41 +00:00
|
|
|
private string _NexusSiteURL = "https://github.com/wabbajack-tools/wabbajack";
|
|
|
|
public string NexusSiteURL { get => _NexusSiteURL; set => this.RaiseAndSetIfChanged(ref _NexusSiteURL, value); }
|
|
|
|
|
2019-10-13 19:53:49 +00:00
|
|
|
public IReactiveCommand SlideShowNextItemCommand { get; } = ReactiveCommand.Create(() => { });
|
2019-10-14 01:15:41 +00:00
|
|
|
public IReactiveCommand VisitNexusSiteCommand { get; }
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-25 04:26:29 +00:00
|
|
|
public SlideShow(InstallerVM appState)
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
2019-10-13 19:12:33 +00:00
|
|
|
SlideShowElements = NexusApiClient.CachedSlideShow.ToList();
|
2019-10-11 10:14:01 +00:00
|
|
|
CachedSlides = new Dictionary<string, Slide>();
|
|
|
|
SlidesQueue = new Queue<Slide>();
|
|
|
|
_random = new Random();
|
2019-10-25 04:26:29 +00:00
|
|
|
Installer = appState;
|
2019-10-13 19:53:49 +00:00
|
|
|
|
2019-10-14 01:15:41 +00:00
|
|
|
this.VisitNexusSiteCommand = ReactiveCommand.Create(
|
|
|
|
execute: () => Process.Start(this.NexusSiteURL),
|
|
|
|
canExecute: this.WhenAny(x => x.NexusSiteURL)
|
|
|
|
.Select(x => x?.StartsWith("https://") ?? false)
|
|
|
|
.ObserveOnGuiThread());
|
|
|
|
|
2019-10-13 19:53:49 +00:00
|
|
|
// Apply modlist properties when it changes
|
2019-10-25 04:26:29 +00:00
|
|
|
this.WhenAny(x => x.Installer.ModList)
|
2019-10-13 19:53:49 +00:00
|
|
|
.NotNull()
|
2019-10-28 04:47:17 +00:00
|
|
|
.ObserveOnGuiThread()
|
2019-10-13 19:53:49 +00:00
|
|
|
.Subscribe(modList =>
|
|
|
|
{
|
2019-10-14 01:15:41 +00:00
|
|
|
this.NexusSiteURL = modList.Website;
|
2019-10-13 22:08:50 +00:00
|
|
|
this.ModName = modList.Name;
|
|
|
|
this.AuthorName = modList.Author;
|
|
|
|
this.Summary = modList.Description;
|
2019-10-28 04:47:17 +00:00
|
|
|
|
|
|
|
this.SlideShowElements = modList.Archives
|
|
|
|
.Select(m => m.State)
|
|
|
|
.OfType<NexusDownloader.State>()
|
|
|
|
.Select(m =>
|
|
|
|
new Slide(NexusApiUtils.FixupSummary(m.ModName), m.ModID,
|
|
|
|
NexusApiUtils.FixupSummary(m.Summary), NexusApiUtils.FixupSummary(m.Author),
|
|
|
|
m.Adult, m.NexusURL, m.SlideShowPic)).ToList();
|
|
|
|
|
|
|
|
this.PreloadSlideShow();
|
2019-10-13 19:53:49 +00:00
|
|
|
})
|
|
|
|
.DisposeWith(this.CompositeDisposable);
|
|
|
|
|
|
|
|
/// Wire slideshow updates
|
|
|
|
// Merge all the sources that trigger a slideshow update
|
|
|
|
Observable.Merge(
|
|
|
|
// If the natural timer fires
|
2019-10-28 04:40:26 +00:00
|
|
|
Observable.Interval(TimeSpan.FromSeconds(10))
|
|
|
|
.Unit()
|
|
|
|
// Only if enabled
|
|
|
|
.FilterSwitch(this.WhenAny(x => x.Enable)),
|
2019-10-13 19:53:49 +00:00
|
|
|
// If user requests one manually
|
|
|
|
this.SlideShowNextItemCommand.StartingExecution())
|
2019-10-28 04:40:26 +00:00
|
|
|
// When installing fire an initial signal
|
2019-10-13 19:53:49 +00:00
|
|
|
.StartWith(Unit.Default)
|
2019-10-28 04:40:26 +00:00
|
|
|
// Only subscribe to slideshow triggers if installing
|
|
|
|
.FilterSwitch(this.WhenAny(x => x.Installer.Installing))
|
2019-10-13 19:53:49 +00:00
|
|
|
// Don't ever update more than once every half second. ToDo: Update to debounce
|
|
|
|
.Throttle(TimeSpan.FromMilliseconds(500), RxApp.MainThreadScheduler)
|
|
|
|
.ObserveOn(RxApp.MainThreadScheduler)
|
|
|
|
.Subscribe(_ => this.UpdateSlideShowItem())
|
|
|
|
.DisposeWith(this.CompositeDisposable);
|
2019-10-11 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void PreloadSlideShow()
|
|
|
|
{
|
|
|
|
var turns = 0;
|
|
|
|
for (var i = 0; i < SlideShowElements.Count; i++)
|
|
|
|
{
|
|
|
|
if (turns >= 3)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (QueueRandomSlide(true, false))
|
|
|
|
turns++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateSlideShowItem()
|
|
|
|
{
|
2019-10-12 21:04:14 +00:00
|
|
|
if (SlidesQueue.Count == 0) return;
|
2019-10-11 10:14:01 +00:00
|
|
|
var slide = SlidesQueue.Peek();
|
|
|
|
|
|
|
|
while (CachedSlides.Count >= MaxCacheSize)
|
|
|
|
{
|
|
|
|
var idx = _random.Next(0, SlideShowElements.Count);
|
|
|
|
var randomSlide = SlideShowElements[idx];
|
2019-10-11 17:29:11 +00:00
|
|
|
while (!CachedSlides.ContainsKey(randomSlide.ModID) || SlidesQueue.Contains(randomSlide))
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
|
|
|
idx = _random.Next(0, SlideShowElements.Count);
|
|
|
|
randomSlide = SlideShowElements[idx];
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:29:11 +00:00
|
|
|
//if (SlidesQueue.Contains(randomSlide)) continue;
|
2019-10-11 10:14:01 +00:00
|
|
|
CachedSlides.Remove(randomSlide.ModID);
|
2019-10-13 19:12:33 +00:00
|
|
|
if (this.GCAfterUpdating)
|
2019-10-11 10:14:01 +00:00
|
|
|
GC.Collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slide.IsNSFW || (slide.IsNSFW && ShowNSFW))
|
|
|
|
{
|
2019-10-25 04:26:29 +00:00
|
|
|
this.Image = UIUtils.BitmapImageFromResource("Wabbajack.Resources.none.jpg");
|
2019-10-11 10:14:01 +00:00
|
|
|
if (slide.ImageURL != null && slide.Image != null)
|
|
|
|
{
|
2019-10-12 21:04:14 +00:00
|
|
|
if (!CachedSlides.ContainsKey(slide.ModID)) return;
|
2019-10-13 22:08:50 +00:00
|
|
|
this.Image = slide.Image;
|
2019-10-11 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 22:08:50 +00:00
|
|
|
this.ModName = slide.ModName;
|
|
|
|
this.AuthorName = slide.ModAuthor;
|
|
|
|
this.Summary = slide.ModDescription;
|
2019-10-14 01:15:41 +00:00
|
|
|
this.NexusSiteURL = slide.ModURL;
|
2019-10-11 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SlidesQueue.Dequeue();
|
|
|
|
QueueRandomSlide(false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CacheImage(Slide slide)
|
|
|
|
{
|
2019-10-12 19:15:19 +00:00
|
|
|
Utils.LogToFile($"Caching slide for {slide.ModName} at {slide.ImageURL}");
|
2019-10-11 10:14:01 +00:00
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
{
|
2019-10-12 19:15:19 +00:00
|
|
|
try
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
2019-10-12 19:15:19 +00:00
|
|
|
if (UseSync)
|
|
|
|
{
|
2019-10-13 06:43:02 +00:00
|
|
|
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
2019-10-12 19:15:19 +00:00
|
|
|
{
|
|
|
|
using (var stream = new HttpClient().GetStreamSync(slide.ImageURL))
|
|
|
|
stream.CopyTo(ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
using (Task<Stream> stream = new HttpClient().GetStreamAsync(slide.ImageURL))
|
|
|
|
{
|
|
|
|
stream.Wait();
|
|
|
|
stream.Result.CopyTo(ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.Seek(0, SeekOrigin.Begin);
|
2019-10-11 22:34:59 +00:00
|
|
|
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
2019-10-12 19:15:19 +00:00
|
|
|
var image = new BitmapImage();
|
|
|
|
image.BeginInit();
|
|
|
|
image.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
|
image.StreamSource = ms;
|
|
|
|
image.EndInit();
|
|
|
|
image.Freeze();
|
|
|
|
|
|
|
|
slide.Image = image;
|
2019-10-11 10:14:01 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-12 19:15:19 +00:00
|
|
|
catch (Exception e)
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
2019-10-12 19:15:19 +00:00
|
|
|
Utils.LogToFile($"Exception while caching slide {slide.ModName} ({slide.ModID})\n{e.ExceptionToString()}");
|
2019-10-11 10:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool QueueRandomSlide(bool init, bool checkLast)
|
|
|
|
{
|
|
|
|
var result = false;
|
|
|
|
var idx = _random.Next(0, SlideShowElements.Count);
|
|
|
|
var element = SlideShowElements[idx];
|
|
|
|
|
2019-10-22 00:41:51 +00:00
|
|
|
if (checkLast && SlideShowElements.Count > 1)
|
2019-10-11 10:14:01 +00:00
|
|
|
{
|
|
|
|
while (element == _lastSlide && (!element.IsNSFW || (element.IsNSFW && ShowNSFW)))
|
|
|
|
{
|
|
|
|
idx = _random.Next(0, SlideShowElements.Count);
|
|
|
|
element = SlideShowElements[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.ImageURL == null)
|
|
|
|
{
|
|
|
|
if (!init) SlidesQueue.Enqueue(element);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!CachedSlides.ContainsKey(element.ModID))
|
|
|
|
{
|
|
|
|
CacheImage(element);
|
|
|
|
CachedSlides.Add(element.ModID, element);
|
|
|
|
SlidesQueue.Enqueue(element);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!init) SlidesQueue.Enqueue(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastSlide = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|