diff --git a/Emscripten.Debugger.VSCodeDwarfDebug/AdapterLauncher.cs b/Emscripten.Debugger.VSCodeDwarfDebug/AdapterLauncher.cs index ef1d289..26bf72a 100644 --- a/Emscripten.Debugger.VSCodeDwarfDebug/AdapterLauncher.cs +++ b/Emscripten.Debugger.VSCodeDwarfDebug/AdapterLauncher.cs @@ -1,33 +1,78 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Shell.Interop; -using Task = System.Threading.Tasks.Task; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Runtime; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Emscripten.DebuggerLauncher; +using Microsoft.VisualStudio.Debugger.DebugAdapterHost.Interfaces; +using Microsoft.VisualStudio.ProjectSystem.Debug; using Microsoft.VisualStudio.ProjectSystem.VS.Debug; -using StreamJsonRpc; +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Threading; +using Newtonsoft.Json; +using static System.Windows.Forms.Design.AxImporter; namespace Kamenokosoft.Emscripten.Debugger.VSCodeDwarfDebug { - internal sealed class DebugEngineLauncher + public class AdapterLauncher : IAdapterLauncher, IDebugAdapterHostComponent { - static public async Task LaunchAsync(DebugLaunchSettings setting) + private IDebugAdapterHostContext context; + + private Process debugServerProcess; + + public void Initialize(IDebugAdapterHostContext context) { - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + // Save the context object provided by the Debug Adapter Host so we can use it to access services later + this.context = context; + + context.Events.DebuggingEnded += OnDebugSessionEnded; + } + + public void UpdateLaunchOptions(IAdapterLaunchInfo launchInfo) + { + if (launchInfo.LaunchJson.Contains("__pendingTarget")) + { + // child target. ignore + } + else + { + LaunchDebugServer(launchInfo); + } + } - var obj = Package.GetGlobalService(typeof(SVsShellDebugger)) as IVsDebugger4; - var vsDebugTargetInfo = new VsDebugTargetInfo4 + private void OnDebugSessionEnded(object sender, EventArgs e) + { + if (debugServerProcess != null && !debugServerProcess.HasExited) { - LaunchFlags = (uint)setting.LaunchOptions, - dlo = (uint)setting.LaunchOperation, - guidLaunchDebugEngine = setting.LaunchDebugEngineGuid, - bstrExe = setting.Executable, - bstrOptions = setting.Options - }; - var _unused = new VsDebugTargetProcessInfo[1]; - - obj.LaunchDebugTargets4(1, new VsDebugTargetInfo4[1] { vsDebugTargetInfo }, _unused); + debugServerProcess.Kill(); + } } + + private void LaunchDebugServer(IAdapterLaunchInfo launchInfo) + { + var AdapterExecutable = launchInfo.GetMetricString("Adapter"); + var AdapterArguments = launchInfo.GetMetricString("AdapterArgs"); + + var AdapterServerProcess = new Process(); + + AdapterServerProcess.StartInfo.FileName = AdapterExecutable; + AdapterServerProcess.StartInfo.Arguments = AdapterArguments; + AdapterServerProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + // AdapterServerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + + AdapterServerProcess.Start(); + + debugServerProcess = AdapterServerProcess; + } + + public ITargetHostProcess LaunchAdapter(IAdapterLaunchInfo launchInfo, ITargetHostInterop targetInterop) + { + // nop + return null; + } } -} \ No newline at end of file +} diff --git a/Emscripten.Debugger.VSCodeDwarfDebug/CustomMessageHandler.cs b/Emscripten.Debugger.VSCodeDwarfDebug/CustomMessageHandler.cs index 0266c55..9e7a4ab 100644 --- a/Emscripten.Debugger.VSCodeDwarfDebug/CustomMessageHandler.cs +++ b/Emscripten.Debugger.VSCodeDwarfDebug/CustomMessageHandler.cs @@ -12,8 +12,6 @@ namespace Kamenokosoft.Emscripten.Debugger.VSCodeDwarfDebug { - - public class StartDebuggingRequestHandler : ICustomProtocolExtension { private IDebugAdapterHostContext context; @@ -46,8 +44,9 @@ private void OnStartDebuggingRequest(IRequestResponder { @@ -59,8 +58,14 @@ private void OnStartDebuggingRequest(IRequestResponder{var n;let t=new kr;for(this._rawData=Buffer.concat([this._rawData,e]);;){if(this._contentLength>=0){if(this._rawData.length>=this._contentLength){let i=this._rawData.toString("utf8",0,this._contentLength);if(this._rawData=this._rawData.slice(this._contentLength),this._contentLength=-1,i.length>0)try{let o=JSON.parse(i);(n=this.logger)==null||n.verbose("dap.receive",void 0,{connectionId:this._connectionId,message:o}),this.msgEmitter.fire({message:o,receivedTime:t})}catch(o){console.error("Error handling data: "+(o&&o.message))}continue}}else{let i=this._rawData.indexOf(L1);if(i!==-1){let s=this._rawData.toString("utf8",0,i).split(`\r `);for(let a=0;athis.endedEmitter.fire()),e.on("data",this._handleData)}send(e,t=!0,n){var s,a;let i=JSON.stringify(e);if(t){let c=e;e.type==="response"&&e.command==="source"&&(c={...e,body:{...e.body,content:"