Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions template/framework/__PROJECT_NAME__/DevelopmentLaunch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#if DEBUG
using System;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;

namespace __PROJECT_NAME__
{
internal static class DevelopmentLaunch
{
private const string Option = "--ns-dev-launch-v1";
private const byte Ready = 0x01;
private const byte Commit = 0x02;

internal static async Task<bool> AllowStartupAsync(string[] initialArgs, TimeSpan timeout)
{
int option = Array.IndexOf(initialArgs, Option);
if (option < 0)
return true;

if (option != initialArgs.Length - 2 || Array.LastIndexOf(initialArgs, Option) != option)
return false;

string token = initialArgs[option + 1];
if (token.Length != 32 || !token.All(c => c is >= '0' and <= '9' or >= 'a' and <= 'f'))
return false;

using var deadline = new CancellationTokenSource(timeout);
try
{
using var pipe = new NamedPipeClientStream(".", "NativeScript.DevLaunch.v1." + token, PipeDirection.InOut,
PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly, TokenImpersonationLevel.Identification);
// The STA entry point waits for this gate. Keep its continuations off that thread.
await pipe.ConnectAsync(deadline.Token).ConfigureAwait(false);
await pipe.WriteAsync(new byte[] { Ready }, deadline.Token).ConfigureAwait(false);

var response = new byte[1];
return await pipe.ReadAsync(response, deadline.Token).ConfigureAwait(false) == 1
&& response[0] == Commit && !deadline.IsCancellationRequested;
}
catch (Exception error) when (error is IOException or OperationCanceledException
or UnauthorizedAccessException)
{
return false;
}
}
}
}
#endif
7 changes: 7 additions & 0 deletions template/framework/__PROJECT_NAME__/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public static void Main(string[] args)
{
ComWrappersSupport.InitializeComWrappers();

#if DEBUG
// Keep this on the path that stays in this process, after any activation redirection.
// Only initial Main arguments can acknowledge a development launch.
if (!DevelopmentLaunch.AllowStartupAsync(args, TimeSpan.FromSeconds(15)).GetAwaiter().GetResult())
return;
#endif

Application.Start((_callbackParams) =>
{
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
Expand Down