Skip to content

Commit 3edfc35

Browse files
authored
Merge pull request WolvenKit#2664 from WolvenKit/feature/wscript-debugger
Debugging for WScripts
2 parents 978bc74 + b0848d4 commit 3edfc35

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

WolvenKit.App/Services/AppScriptService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
44
using System.Threading.Tasks;
@@ -64,10 +64,10 @@ public AppScriptService(
6464
return _projectExplorerViewModel;
6565
}
6666

67-
public async Task ExecuteAsync(string code)
67+
public async Task ExecuteAsync(string code, bool enableDebugging = false)
6868
{
6969
GetProjectExplorerViewModel()?.SuspendFileWatcher();
70-
await ExecuteAsync(code, DefaultHostObject);
70+
await ExecuteAsync(code, DefaultHostObject, null, enableDebugging);
7171
GetProjectExplorerViewModel()?.ResumeFileWatcher();
7272
}
7373

@@ -88,11 +88,11 @@ public IList<ScriptFile> GetScripts()
8888
return result;
8989
}
9090

91-
protected override V8ScriptEngine GetScriptEngine(Dictionary<string, object>? hostObjects = null, List<string>? searchPaths = null)
91+
protected override V8ScriptEngine GetScriptEngine(Dictionary<string, object>? hostObjects = null, List<string>? searchPaths = null, bool enableDebugging = false)
9292
{
9393
searchPaths ??= new List<string> { ISettingsManager.GetWScriptDir(), Path.GetFullPath(@"Resources\Scripts") };
9494

95-
var engine = base.GetScriptEngine(hostObjects, searchPaths);
95+
var engine = base.GetScriptEngine(hostObjects, searchPaths, enableDebugging);
9696

9797
engine.AddHostType(typeof(EUncookExtension));
9898
engine.AddHostType(typeof(MeshExporterType));

WolvenKit.App/ViewModels/Documents/WScriptDocumentViewModel.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.IO;
@@ -36,6 +36,7 @@ private void _scriptService_PropertyChanged(object? sender, PropertyChangedEvent
3636
if(e.PropertyName == nameof(ScriptService.IsRunning))
3737
{
3838
RunCommand.NotifyCanExecuteChanged();
39+
DebugCommand.NotifyCanExecuteChanged();
3940
StopCommand.NotifyCanExecuteChanged();
4041
}
4142
}
@@ -69,6 +70,9 @@ private void _scriptService_PropertyChanged(object? sender, PropertyChangedEvent
6970
[RelayCommand(CanExecute = nameof(CanRun))]
7071
private async Task Run() => await _scriptService.ExecuteAsync(Text);
7172

73+
[RelayCommand(CanExecute = nameof(CanRun))]
74+
private async Task Debug() => await _scriptService.ExecuteAsync(Text, true);
75+
7276
private bool CanStop() => _scriptService.IsRunning;
7377
[RelayCommand(CanExecute = nameof(CanStop))]
7478
private void Stop() => _scriptService.Stop();
@@ -178,4 +182,4 @@ private void GetScriptType(string fileName)
178182
IsNormalScript = true;
179183
}
180184
}
181-
}
185+
}

WolvenKit.Modkit/Scripting/ScriptService.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Diagnostics;
@@ -29,7 +29,7 @@ public partial class ScriptService : ObservableObject
2929

3030
public ScriptService(ILoggerService loggerService) => _loggerService = loggerService;
3131

32-
public async Task ExecuteAsync(string code, Dictionary<string, object>? hostObjects = null, List<string>? searchPaths = null)
32+
public async Task ExecuteAsync(string code, Dictionary<string, object>? hostObjects = null, List<string>? searchPaths = null, bool enableDebugging = false)
3333
{
3434
if (_mainEngine != null)
3535
{
@@ -41,7 +41,7 @@ public async Task ExecuteAsync(string code, Dictionary<string, object>? hostObje
4141

4242
var sw = Stopwatch.StartNew();
4343

44-
_mainEngine = GetScriptEngine(hostObjects, searchPaths);
44+
_mainEngine = GetScriptEngine(hostObjects, searchPaths, enableDebugging);
4545

4646
try
4747
{
@@ -91,9 +91,16 @@ public void Stop()
9191
IsRunning = false;
9292
}
9393

94-
protected virtual V8ScriptEngine GetScriptEngine(Dictionary<string, object>? hostObjects = null, List<string>? searchPaths = null)
94+
protected virtual V8ScriptEngine GetScriptEngine(Dictionary<string, object>? hostObjects = null, List<string>? searchPaths = null, bool enableDebugging = false)
9595
{
96-
var engine = new V8ScriptEngine();
96+
var flags = V8ScriptEngineFlags.None;
97+
if (enableDebugging)
98+
{
99+
flags |= V8ScriptEngineFlags.EnableDebugging;
100+
flags |= V8ScriptEngineFlags.AwaitDebuggerAndPauseOnStart;
101+
}
102+
103+
var engine = new V8ScriptEngine(flags);
97104

98105
engine.AddHostType(typeof(OpenAs));
99106
engine.AddHostObject("logger", _loggerService);
@@ -151,4 +158,4 @@ public virtual IList<ScriptFile> GetScripts(string path)
151158
}
152159

153160
public void RemoveFromCache(string path) => _scriptCache.Remove(path, out _);
154-
}
161+
}

WolvenKit/Views/Documents/WScriptDocumentView.xaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<reactiveUi:ReactiveUserControl
1+
<reactiveUi:ReactiveUserControl
22
x:Class="WolvenKit.Views.Documents.WScriptDocumentView"
33
x:TypeArguments="documents:WScriptDocumentViewModel"
44
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
@@ -69,6 +69,21 @@
6969
</MenuItem.Icon>
7070
</MenuItem>
7171

72+
<MenuItem
73+
Visibility="{Binding IsNormalScript,
74+
Converter={StaticResource BooleanToVisibilityConverter}}"
75+
Command="{Binding DebugCommand}"
76+
CommandParameter="{Binding Document.Text}"
77+
Header="Debug"
78+
ToolTip="Waits for debugger on port 9222">
79+
<MenuItem.Icon>
80+
<templates:IconBox
81+
IconPack="Material"
82+
Kind="BugPlay"
83+
Foreground="{StaticResource WolvenKitGreen}" />
84+
</MenuItem.Icon>
85+
</MenuItem>
86+
7287
<MenuItem
7388
Visibility="{Binding IsNormalScript,
7489
Converter={StaticResource BooleanToVisibilityConverter}}"

0 commit comments

Comments
 (0)