using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Versioning;
using System.Text.Json;
using System.Threading.Tasks;
using MessagePipe;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using StabilityMatrix.Core.Helper;
using URIScheme;
namespace StabilityMatrix.Avalonia.Helpers;
///
/// Custom URI scheme handler for the stabilitymatrix:// protocol
///
/// Need to call on App startup
public class UriHandler
{
public const string IpcKeySend = "uri_handler_send";
public string Scheme { get; }
public string Description { get; }
public UriHandler(string scheme, string description)
{
Scheme = scheme;
Description = description;
}
///
/// Send a received Uri over MessagePipe and exits
///
///
[DoesNotReturn]
public void SendAndExit(Uri uri)
{
var services = new ServiceCollection();
services.AddMessagePipe();
services.AddMessagePipeNamedPipeInterprocess("StabilityMatrix");
var provider = services.BuildServiceProvider();
var publisher = provider.GetRequiredService>();
var sendTask = Task.Run(async () => await publisher.PublishAsync(IpcKeySend, uri));
sendTask.Wait();
var info = JsonSerializer.Serialize(new Dictionary { [IpcKeySend] = uri });
Debug.WriteLine(info);
Console.WriteLine(info);
Environment.Exit(0);
}
public void RegisterUriScheme()
{
// Not supported on macos
if (Compat.IsWindows)
{
RegisterUriSchemeWin();
}
else if (Compat.IsLinux)
{
// Try to register on linux but ignore errors
// Library does not support some distros
try
{
RegisterUriSchemeLinux();
}
catch (Exception e)
{
Debug.WriteLine(e);
Console.WriteLine(e);
}
}
}
[SupportedOSPlatform("windows")]
private void RegisterUriSchemeWin()
{
using var key = Registry.CurrentUser.CreateSubKey(@$"SOFTWARE\Classes\{Scheme}");
key.SetValue("", "URL:" + Description);
key.SetValue(null, Description);
key.SetValue("URL Protocol", "");
using (var defaultIcon = key.CreateSubKey("DefaultIcon"))
{
defaultIcon.SetValue("", Compat.AppCurrentPath.FullPath + ",1");
}
using (var commandKey = key.CreateSubKey(@"shell\open\command"))
{
commandKey.SetValue("", "\"" + Compat.AppCurrentPath.FullPath + "\" --uri \"%1\"");
}
}
[SupportedOSPlatform("linux")]
private void RegisterUriSchemeLinux()
{
var service = URISchemeServiceFactory.GetURISchemeSerivce(
Scheme,
Description,
Compat.AppCurrentPath.FullPath
);
service.Set();
}
}