VRCMelonAssistant/ModAssistant/Classes/OneClickInstaller.cs

152 lines
5.4 KiB
C#
Raw Normal View History

2020-02-02 21:11:06 +13:00
using System;
2019-04-22 18:41:43 +12:00
using System.Linq;
using System.Threading.Tasks;
2020-02-05 17:58:35 +13:00
using System.Windows;
2020-11-17 02:22:30 +13:00
using Microsoft.Win32;
2019-04-22 18:41:43 +12:00
namespace ModAssistant
{
class OneClickInstaller
{
2020-05-10 04:28:39 +12:00
private static readonly string[] Protocols = new[] { "modelsaber", "beatsaver", "bsplaylist" };
public static OneClickStatus Status = new OneClickStatus();
2019-04-22 18:41:43 +12:00
2020-02-28 21:28:02 +13:00
public static async Task InstallAsset(string link)
2019-04-22 18:41:43 +12:00
{
Uri uri = new Uri(link);
if (!Protocols.Contains(uri.Scheme)) return;
2019-04-30 04:34:41 +12:00
switch (uri.Scheme)
{
case "modelsaber":
await ModelSaber(uri);
2019-04-30 04:34:41 +12:00
break;
case "beatsaver":
await BeatSaver(uri);
break;
2020-05-10 04:28:39 +12:00
case "bsplaylist":
await Playlist(uri);
break;
}
2020-06-22 18:17:30 +12:00
if (App.OCIWindow != "No")
{
Status.StopRotation();
API.Utils.SetMessage((string)Application.Current.FindResource("OneClick:Done"));
}
2020-06-22 16:05:53 +12:00
if (App.OCIWindow == "Close")
{
Application.Current.Shutdown();
}
}
private static async Task BeatSaver(Uri uri)
{
string Key = uri.Host;
2020-05-06 07:43:50 +12:00
await API.BeatSaver.GetFromKey(Key);
}
private static async Task ModelSaber(Uri uri)
2019-04-30 04:34:41 +12:00
{
2020-06-22 16:05:53 +12:00
if (App.OCIWindow != "No") Status.Show();
API.Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Installing"), System.Web.HttpUtility.UrlDecode(uri.Segments.Last()))}");
2020-05-06 07:43:50 +12:00
await API.ModelSaber.GetModel(uri);
2019-04-30 04:34:41 +12:00
}
2019-04-22 18:41:43 +12:00
2020-05-10 04:28:39 +12:00
private static async Task Playlist(Uri uri)
{
2020-06-22 16:05:53 +12:00
if (App.OCIWindow != "No") Status.Show();
2020-05-10 04:28:39 +12:00
await API.Playlists.DownloadAll(uri);
}
public static void Register(string Protocol, bool Background = false, string Description = null)
{
if (IsRegistered(Protocol) == true)
return;
try
{
if (Utils.IsAdmin)
{
RegistryKey ProtocolKey = Registry.ClassesRoot.OpenSubKey(Protocol, true);
if (ProtocolKey == null)
ProtocolKey = Registry.ClassesRoot.CreateSubKey(Protocol, true);
RegistryKey CommandKey = ProtocolKey.CreateSubKey(@"shell\open\command", true);
if (CommandKey == null)
CommandKey = Registry.ClassesRoot.CreateSubKey(@"shell\open\command", true);
if (ProtocolKey.GetValue("OneClick-Provider", "").ToString() != "ModAssistant")
{
if (Description != null)
{
ProtocolKey.SetValue("", Description, RegistryValueKind.String);
}
ProtocolKey.SetValue("URL Protocol", "", RegistryValueKind.String);
ProtocolKey.SetValue("OneClick-Provider", "ModAssistant", RegistryValueKind.String);
CommandKey.SetValue("", $"\"{Utils.ExePath}\" \"--install\" \"%1\"");
}
2020-02-02 19:14:12 +13:00
Utils.SendNotify(string.Format((string)Application.Current.FindResource("OneClick:ProtocolHandler:Registered"), Protocol));
}
else
{
Utils.StartAsAdmin($"\"--register\" \"{Protocol}\" \"{Description}\"");
}
}
2020-02-03 00:04:30 +13:00
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
if (Background)
Application.Current.Shutdown();
else
Pages.Options.Instance.UpdateHandlerStatus();
}
public static void Unregister(string Protocol, bool Background = false)
2019-05-05 04:08:54 +12:00
{
if (IsRegistered(Protocol) == false)
return;
try
2019-05-05 04:08:54 +12:00
{
if (Utils.IsAdmin)
{
using (RegistryKey ProtocolKey = Registry.ClassesRoot.OpenSubKey(Protocol, true))
{
if (ProtocolKey != null
&& ProtocolKey.GetValue("OneClick-Provider", "").ToString() == "ModAssistant")
{
Registry.ClassesRoot.DeleteSubKeyTree(Protocol);
}
}
2020-02-02 19:14:12 +13:00
Utils.SendNotify(string.Format((string)Application.Current.FindResource("OneClick:ProtocolHandler:Unregistered"), Protocol));
}
else
{
Utils.StartAsAdmin($"\"--unregister\" \"{Protocol}\"");
}
2019-05-05 04:08:54 +12:00
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
if (Background)
Application.Current.Shutdown();
else
Pages.Options.Instance.UpdateHandlerStatus();
2019-05-05 04:08:54 +12:00
}
public static bool IsRegistered(string Protocol)
2019-05-05 04:08:54 +12:00
{
RegistryKey ProtocolKey = Registry.ClassesRoot.OpenSubKey(Protocol);
if (ProtocolKey != null
&& ProtocolKey.GetValue("OneClick-Provider", "").ToString() == "ModAssistant")
return true;
else
return false;
2019-05-05 04:08:54 +12:00
}
2019-04-22 18:41:43 +12:00
}
}