2025-11-19 19:16:38 +08:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2025-11-25 10:10:53 +08:00
|
|
|
using ManagedCommon;
|
2025-11-19 19:16:38 +08:00
|
|
|
using Microsoft.CommandPalette.Extensions;
|
|
|
|
|
|
2025-11-26 16:02:41 +08:00
|
|
|
namespace PowerToysExtension;
|
2025-11-19 19:16:38 +08:00
|
|
|
|
2025-11-26 16:02:41 +08:00
|
|
|
public class Program
|
2025-11-19 19:16:38 +08:00
|
|
|
{
|
|
|
|
|
[MTAThread]
|
2025-11-26 16:02:41 +08:00
|
|
|
public static void Main(string[] args)
|
2025-11-19 19:16:38 +08:00
|
|
|
{
|
2025-11-25 10:10:53 +08:00
|
|
|
try
|
|
|
|
|
{
|
2025-11-26 16:02:41 +08:00
|
|
|
// Initialize per-extension log under CmdPal/PowerToysExtension.
|
2025-11-25 10:10:53 +08:00
|
|
|
Logger.InitializeLogger("\\CmdPal\\PowerToysExtension\\Logs");
|
2025-11-27 09:21:55 +08:00
|
|
|
Logger.LogInfo("PowerToysExtension starting (args: " + string.Join(' ', args) + ")");
|
2025-11-25 10:10:53 +08:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2025-11-26 16:02:41 +08:00
|
|
|
// Continue even if logging fails.
|
2025-11-25 10:10:53 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 09:21:55 +08:00
|
|
|
try
|
2025-11-19 19:16:38 +08:00
|
|
|
{
|
2025-11-27 09:21:55 +08:00
|
|
|
if (args.Length > 0 && args[0] == "-RegisterProcessAsComServer")
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo("RegisterProcessAsComServer mode detected.");
|
|
|
|
|
using ExtensionServer server = new();
|
2025-11-25 15:49:50 +08:00
|
|
|
|
2025-11-27 09:21:55 +08:00
|
|
|
ManualResetEvent extensionDisposedEvent = new(false);
|
2025-11-19 19:16:38 +08:00
|
|
|
|
2025-11-27 09:21:55 +08:00
|
|
|
// We are instantiating an extension instance once above, and returning it every time the callback in RegisterExtension below is called.
|
|
|
|
|
// This makes sure that only one instance of SampleExtension is alive, which is returned every time the host asks for the IExtension object.
|
|
|
|
|
// If you want to instantiate a new instance each time the host asks, create the new instance inside the delegate.
|
|
|
|
|
PowerToysExtension extensionInstance = new(extensionDisposedEvent);
|
|
|
|
|
server.RegisterExtension(() => extensionInstance);
|
|
|
|
|
Logger.LogInfo("Extension instance registered; waiting for disposal signal.");
|
2025-11-19 19:16:38 +08:00
|
|
|
|
2025-11-27 09:21:55 +08:00
|
|
|
// This will make the main thread wait until the event is signalled by the extension class.
|
|
|
|
|
// Since we have single instance of the extension object, we exit as soon as it is disposed.
|
|
|
|
|
extensionDisposedEvent.WaitOne();
|
|
|
|
|
Logger.LogInfo("Extension disposed signal received; exiting server loop.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Not being launched as a Extension... exiting.");
|
|
|
|
|
Logger.LogInfo("Exited: not launched with -RegisterProcessAsComServer.");
|
|
|
|
|
}
|
2025-11-26 16:02:41 +08:00
|
|
|
}
|
2025-11-27 09:21:55 +08:00
|
|
|
finally
|
2025-11-26 16:02:41 +08:00
|
|
|
{
|
|
|
|
|
}
|
2025-11-19 19:16:38 +08:00
|
|
|
}
|
|
|
|
|
}
|