bloxstrap/Bloxstrap/ProtocolHandler.cs
pizzaboxer 765cccf89e
Refactor arg parser + channel handling
this commit changes way too many things to be stable. be warned of bugs if using this
2024-08-19 15:37:56 +01:00

79 lines
2.8 KiB
C#

using System.Web;
using System.Windows;
using Microsoft.Win32;
namespace Bloxstrap
{
static class ProtocolHandler
{
private const string RobloxPlaceKey = "Roblox.Place";
public static void Register(string key, string name, string handler, string handlerParam = "%1")
{
string handlerArgs = $"\"{handler}\" {handlerParam}";
using var uriKey = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{key}");
using var uriIconKey = uriKey.CreateSubKey("DefaultIcon");
using var uriCommandKey = uriKey.CreateSubKey(@"shell\open\command");
if (uriKey.GetValue("") is null)
{
uriKey.SetValue("", $"URL: {name} Protocol");
uriKey.SetValue("URL Protocol", "");
}
if (uriCommandKey.GetValue("") as string != handlerArgs)
{
uriIconKey.SetValue("", handler);
uriCommandKey.SetValue("", handlerArgs);
}
}
public static void RegisterRobloxPlace(string handler)
{
const string keyValue = "Roblox Place";
string handlerArgs = $"\"{handler}\" -ide \"%1\"";
string iconValue = $"{handler},0";
using RegistryKey uriKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + RobloxPlaceKey);
using RegistryKey uriIconKey = uriKey.CreateSubKey("DefaultIcon");
using RegistryKey uriOpenKey = uriKey.CreateSubKey(@"shell\Open");
using RegistryKey uriCommandKey = uriOpenKey.CreateSubKey(@"command");
if (uriKey.GetValue("") as string != keyValue)
uriKey.SetValue("", keyValue);
if (uriCommandKey.GetValue("") as string != handlerArgs)
uriCommandKey.SetValue("", handlerArgs);
if (uriOpenKey.GetValue("") as string != "Open")
uriOpenKey.SetValue("", "Open");
if (uriIconKey.GetValue("") as string != iconValue)
uriIconKey.SetValue("", iconValue);
}
public static void RegisterExtension(string key)
{
using RegistryKey uriKey = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{key}");
uriKey.CreateSubKey(RobloxPlaceKey + @"\ShellNew");
if (uriKey.GetValue("") as string != RobloxPlaceKey)
uriKey.SetValue("", RobloxPlaceKey);
}
public static void Unregister(string key)
{
try
{
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{key}");
}
catch (Exception ex)
{
App.Logger.WriteLine("Protocol::Unregister", $"Failed to unregister {key}: {ex}");
}
}
}
}