2020-05-06 07:43:50 +12:00
|
|
|
using System;
|
2020-05-20 05:47:45 +12:00
|
|
|
using System.Collections.Generic;
|
2020-05-06 07:43:50 +12:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2020-05-20 05:47:45 +12:00
|
|
|
using System.Net.Http.Headers;
|
2020-05-06 07:43:50 +12:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Windows;
|
2020-05-20 05:47:45 +12:00
|
|
|
using static ModAssistant.Http;
|
2020-05-06 07:43:50 +12:00
|
|
|
|
|
|
|
namespace ModAssistant.API
|
|
|
|
{
|
|
|
|
public class Utils
|
|
|
|
{
|
|
|
|
public static readonly string BeatSaberPath = App.BeatSaberInstallDirectory;
|
|
|
|
|
2020-05-20 05:47:45 +12:00
|
|
|
public static void SetMessage(string message)
|
|
|
|
{
|
|
|
|
if (App.window == null)
|
|
|
|
{
|
|
|
|
OneClickStatus.Instance.MainText = message;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MainWindow.Instance.MainText = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 04:28:39 +12:00
|
|
|
public static async Task DownloadAsset(string link, string folder, bool showNotifcation, string fileName = null)
|
|
|
|
{
|
|
|
|
await DownloadAsset(link, folder, fileName, null, showNotifcation);
|
|
|
|
}
|
|
|
|
|
2020-05-06 07:43:50 +12:00
|
|
|
public static async Task DownloadAsset(string link, string folder, string fileName = null, string displayName = null)
|
|
|
|
{
|
2020-05-10 04:28:39 +12:00
|
|
|
await DownloadAsset(link, folder, fileName, displayName, true);
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:47:45 +12:00
|
|
|
public static async Task DownloadAsset(string link, string folder, string fileName, string displayName, bool showNotification, bool beatsaver = false)
|
2020-05-10 04:28:39 +12:00
|
|
|
{
|
2020-05-06 07:43:50 +12:00
|
|
|
if (string.IsNullOrEmpty(BeatSaberPath))
|
|
|
|
{
|
|
|
|
ModAssistant.Utils.SendNotify((string)Application.Current.FindResource("OneClick:InstallDirNotFound"));
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.Combine(BeatSaberPath, folder));
|
|
|
|
if (string.IsNullOrEmpty(fileName))
|
|
|
|
{
|
|
|
|
fileName = WebUtility.UrlDecode(Path.Combine(BeatSaberPath, folder, new Uri(link).Segments.Last()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileName = WebUtility.UrlDecode(Path.Combine(BeatSaberPath, folder, fileName));
|
|
|
|
}
|
2020-05-10 04:28:39 +12:00
|
|
|
if (string.IsNullOrEmpty(displayName))
|
|
|
|
{
|
|
|
|
displayName = Path.GetFileNameWithoutExtension(fileName);
|
|
|
|
}
|
2020-05-06 07:43:50 +12:00
|
|
|
|
2020-05-20 05:47:45 +12:00
|
|
|
if (beatsaver) await BeatSaver.Download(link, fileName);
|
|
|
|
else await ModAssistant.Utils.Download(link, fileName);
|
|
|
|
|
2020-05-10 04:28:39 +12:00
|
|
|
if (showNotification)
|
|
|
|
{
|
2020-05-20 05:47:45 +12:00
|
|
|
SetMessage(string.Format((string)Application.Current.FindResource("OneClick:InstalledAsset"), displayName));
|
2020-05-10 04:28:39 +12:00
|
|
|
}
|
2020-05-06 07:43:50 +12:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2020-05-20 05:47:45 +12:00
|
|
|
SetMessage((string)Application.Current.FindResource("OneClick:AssetInstallFailed"));
|
2020-05-20 10:48:35 +12:00
|
|
|
App.CloseWindowOnFinish = false;
|
2020-05-06 07:43:50 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|