2021-12-26 21:56:44 +00:00
|
|
|
|
using DynamicData;
|
|
|
|
|
using DynamicData.Binding;
|
|
|
|
|
using Microsoft.WindowsAPICodePack.Dialogs;
|
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using System;
|
2021-12-27 05:13:28 +00:00
|
|
|
|
using System.Diagnostics;
|
2021-12-26 21:56:44 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using Wabbajack.Common;
|
2021-12-27 05:53:39 +00:00
|
|
|
|
using Wabbajack.Hashing.xxHash64;
|
2021-12-27 05:13:28 +00:00
|
|
|
|
using Wabbajack.Lib.Extensions;
|
|
|
|
|
using Wabbajack.Paths;
|
2021-12-27 05:53:39 +00:00
|
|
|
|
using Wabbajack.Paths.IO;
|
2021-12-26 21:56:44 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public static class UIUtils
|
|
|
|
|
{
|
|
|
|
|
public static BitmapImage BitmapImageFromResource(string name) => BitmapImageFromStream(System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Wabbajack;component/" + name)).Stream);
|
|
|
|
|
|
|
|
|
|
public static BitmapImage BitmapImageFromStream(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
var img = new BitmapImage();
|
|
|
|
|
img.BeginInit();
|
|
|
|
|
img.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
|
|
img.StreamSource = stream;
|
|
|
|
|
img.EndInit();
|
|
|
|
|
img.Freeze();
|
|
|
|
|
return img;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool TryGetBitmapImageFromFile(AbsolutePath path, out BitmapImage bitmapImage)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-27 05:53:39 +00:00
|
|
|
|
if (!path.FileExists())
|
2021-12-26 21:56:44 +00:00
|
|
|
|
{
|
|
|
|
|
bitmapImage = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-12-27 05:53:39 +00:00
|
|
|
|
bitmapImage = new BitmapImage(new Uri(path.ToString(), UriKind.RelativeOrAbsolute));
|
2021-12-26 21:56:44 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
bitmapImage = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-27 05:13:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void OpenWebsite(Uri url)
|
|
|
|
|
{
|
|
|
|
|
Process.Start(new ProcessStartInfo("cmd.exe", $"/c start {url}")
|
|
|
|
|
{
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OpenFolder(AbsolutePath path)
|
|
|
|
|
{
|
2021-12-27 05:53:39 +00:00
|
|
|
|
Process.Start(new ProcessStartInfo(KnownFolders.Windows.Combine("explorer.exe").ToString(), path.ToString())
|
2021-12-27 05:13:28 +00:00
|
|
|
|
{
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:56:44 +00:00
|
|
|
|
|
|
|
|
|
public static AbsolutePath OpenFileDialog(string filter, string initialDirectory = null)
|
|
|
|
|
{
|
|
|
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
|
|
|
ofd.Filter = filter;
|
|
|
|
|
ofd.InitialDirectory = initialDirectory;
|
|
|
|
|
if (ofd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
return (AbsolutePath)ofd.FileName;
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IObservable<BitmapImage> DownloadBitmapImage(this IObservable<string> obs, Action<Exception> exceptionHandler)
|
|
|
|
|
{
|
|
|
|
|
return obs
|
|
|
|
|
.ObserveOn(RxApp.TaskpoolScheduler)
|
|
|
|
|
.SelectTask(async url =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var (found, mstream) = await FindCachedImage(url);
|
|
|
|
|
if (found) return mstream;
|
|
|
|
|
|
|
|
|
|
var ret = new MemoryStream();
|
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
|
await using (var stream = await client.GetStreamAsync(url))
|
|
|
|
|
{
|
|
|
|
|
await stream.CopyToAsync(ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
await WriteCachedImage(url, ret.ToArray());
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
exceptionHandler(ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.Select(memStream =>
|
|
|
|
|
{
|
|
|
|
|
if (memStream == null) return default;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return BitmapImageFromStream(memStream);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
exceptionHandler(ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
memStream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.ObserveOnGuiThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task WriteCachedImage(string url, byte[] data)
|
|
|
|
|
{
|
2021-12-27 05:53:39 +00:00
|
|
|
|
var folder = KnownFolders.WabbajackAppLocal.Combine("ModListImages");
|
|
|
|
|
if (!folder.DirectoryExists()) folder.CreateDirectory();
|
2021-12-26 21:56:44 +00:00
|
|
|
|
|
2021-12-28 06:11:39 +00:00
|
|
|
|
var path = folder.Combine((await Encoding.UTF8.GetBytes(url).Hash()).ToHex());
|
2021-12-26 21:56:44 +00:00
|
|
|
|
await path.WriteAllBytesAsync(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<(bool Found, MemoryStream data)> FindCachedImage(string uri)
|
|
|
|
|
{
|
2021-12-27 05:53:39 +00:00
|
|
|
|
var folder = KnownFolders.WabbajackAppLocal.Combine("ModListImages");
|
|
|
|
|
if (!folder.DirectoryExists()) folder.CreateDirectory();
|
2021-12-26 21:56:44 +00:00
|
|
|
|
|
2021-12-28 06:11:39 +00:00
|
|
|
|
var path = folder.Combine((await Encoding.UTF8.GetBytes(uri).Hash()).ToHex());
|
2021-12-27 05:53:39 +00:00
|
|
|
|
return path.FileExists() ? (true, new MemoryStream(await path.ReadAllBytesAsync())) : (false, default);
|
2021-12-26 21:56:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Format bytes to a greater unit
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">number of bytes</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string FormatBytes(long bytes)
|
|
|
|
|
{
|
|
|
|
|
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
|
|
|
|
|
int i;
|
|
|
|
|
double dblSByte = bytes;
|
|
|
|
|
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
|
|
|
|
|
{
|
|
|
|
|
dblSByte = bytes / 1024.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|