57 lines
2.6 KiB
C#
57 lines
2.6 KiB
C#
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text.Json;
|
|
using Windows.ApplicationModel;
|
|
|
|
internal static class Program
|
|
{
|
|
[DllImport("user32.dll")] static extern bool SetProcessDpiAwarenessContext(IntPtr context);
|
|
static readonly object OutputLock = new();
|
|
static void Write(object data) { lock (OutputLock) { Console.WriteLine(JsonSerializer.Serialize(data)); Console.Out.Flush(); } }
|
|
|
|
[STAThread]
|
|
static void Main(string[] args)
|
|
{
|
|
SetProcessDpiAwarenessContext(new IntPtr(-4));
|
|
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
|
|
using var dispatch = new Control();
|
|
dispatch.CreateControl();
|
|
_ = Task.Run(() => {
|
|
string? line;
|
|
while ((line = Console.ReadLine()) != null) {
|
|
string request = line;
|
|
dispatch.BeginInvoke(new Action(async () => {
|
|
int id = 0;
|
|
try {
|
|
using var doc = JsonDocument.Parse(request);
|
|
var value = doc.RootElement;
|
|
id = value.GetProperty("id").GetInt32();
|
|
string command = value.GetProperty("command").GetString() ?? "";
|
|
object result;
|
|
switch (command) {
|
|
case "selection": result = Selection(); break;
|
|
case "identity":
|
|
try { result = new { packaged = true, family = Package.Current.Id.FamilyName }; }
|
|
catch { result = new { packaged = false, family = "" }; }
|
|
break;
|
|
case "startup":
|
|
var task = await StartupTask.GetAsync("FileBowStartup");
|
|
if (value.TryGetProperty("enabled", out var enabled)) {
|
|
if (enabled.GetBoolean()) await task.RequestEnableAsync(); else task.Disable();
|
|
}
|
|
result = task.State.ToString(); break;
|
|
default: throw new InvalidOperationException("Unknown command.");
|
|
}
|
|
Write(new { id, result });
|
|
} catch (Exception ex) { Write(new { id, error = ex.Message }); }
|
|
}));
|
|
}
|
|
dispatch.BeginInvoke(new Action(Application.Exit));
|
|
});
|
|
Write(new { type = "ready" });
|
|
Application.Run();
|
|
}
|
|
|
|
static string[] Selection() => ExplorerSelection.Read();
|
|
}
|