@@ -9,6 +9,7 @@ import { MessageStreamParser } from './MessageStreamParser';
9
9
import { SourceMaps } from './SourceMaps' ;
10
10
import { FileSystemWatcher , window , workspace } from 'vscode' ;
11
11
import * as path from 'path' ;
12
+ import * as fs from 'fs' ;
12
13
13
14
interface PendingResponse {
14
15
resolve : Function ;
@@ -42,6 +43,9 @@ export class Session extends DebugSession {
42
43
private _sourceMaps : SourceMaps = new SourceMaps ( "" ) ;
43
44
private _fileWatcher ?: FileSystemWatcher ;
44
45
private _activeThreadId : number = 0 ; // the one being debugged
46
+ private _localRoot : string = "" ;
47
+ private _sourceMapRoot ?: string ;
48
+ private _generatedSourceRoot ?: string ;
45
49
46
50
public constructor ( ) {
47
51
super ( ) ;
@@ -81,6 +85,10 @@ export class Session extends DebugSession {
81
85
return ;
82
86
}
83
87
88
+ this . _localRoot = args . localRoot ? path . normalize ( args . localRoot ) : "" ;
89
+ this . _sourceMapRoot = args . sourceMapRoot ? path . normalize ( args . sourceMapRoot ) : undefined ;
90
+ this . _generatedSourceRoot = args . generatedSourceRoot ? path . normalize ( args . generatedSourceRoot ) : undefined ;
91
+
84
92
// Listen or connect (default), depending on mode.
85
93
// Attach makes more sense to use connect, but some MC platforms require using listen.
86
94
try {
@@ -96,13 +104,7 @@ export class Session extends DebugSession {
96
104
return ;
97
105
}
98
106
99
- // init source maps
100
- this . _sourceMaps = new SourceMaps ( args . localRoot , args . sourceMapRoot , args . generatedSourceRoot ) ;
101
-
102
- // watch for source map changes
103
- this . createSourceMapFileWatcher ( args . sourceMapRoot ) ;
104
-
105
- // tell VSCode that attach is complete
107
+ // tell VSCode that attach has been received
106
108
this . sendResponse ( response ) ;
107
109
}
108
110
@@ -357,9 +359,22 @@ export class Session extends DebugSession {
357
359
// connect socket to stream parser
358
360
socket . pipe ( socketStreamParser as any ) ;
359
361
362
+ //
363
+ // Now wait for the debugee protocol event which will call onConnectionComplete if accepted.
364
+ //
365
+ }
366
+
367
+ private onConnectionComplete ( ) {
368
+ // success
360
369
this . showNotification ( "Success! Debugger is now connected." , LogLevel . Log ) ;
361
370
362
- // Now that a connection is established, send this event to
371
+ // init source maps
372
+ this . _sourceMaps = new SourceMaps ( this . _localRoot , this . _sourceMapRoot , this . _generatedSourceRoot ) ;
373
+
374
+ // watch for source map changes
375
+ this . createSourceMapFileWatcher ( this . _sourceMapRoot ) ;
376
+
377
+ // Now that a connection is established, and capabilities have been delivered, send this event to
363
378
// tell VSCode to ask Minecraft/debugee for config data (breakpoints etc).
364
379
// When config is complete VSCode calls 'configurationDoneRequest' and the DA
365
380
// sends a 'resume' message to the debugee, which had paused following the attach.
@@ -498,18 +513,42 @@ export class Session extends DebugSession {
498
513
499
514
// ------------------------------------------------------------------------
500
515
501
- private handleProtocolEvent ( eventMessage : any ) {
502
- let mcVer = eventMessage . version ;
503
- let extVer = Session . DEBUGGER_PROTOCOL_VERSION ;
504
- if ( mcVer < extVer ) {
505
- // Minecraft protocol is behind the extension
506
- let errorMessage = `Minecraft's debugger protocol [${ mcVer } ] is not compatible with this extension's protocol [${ extVer } ], please update Minecraft.` ;
507
- this . showNotification ( errorMessage , LogLevel . Error ) ;
508
- }
509
- else if ( mcVer > extVer ) {
510
- // Extension protocol is behind Minecraft
511
- let errorMessage = `This extension's protocol [${ extVer } ] is not compatible with Minecraft's debugger protocol [${ mcVer } ], please update the extension.` ;
512
- this . showNotification ( errorMessage , LogLevel . Error ) ;
516
+ private handleProtocolEvent ( protocolCapabilities : any ) {
517
+ //
518
+ // handle protocol capabilities here...
519
+ // can fail connection on errors
520
+ //
521
+
522
+ // MC isn't using pack root, check that localRoot is pointing to js files
523
+ if ( protocolCapabilities . disablePackRoot ) {
524
+ this . handleCapabilityDisablePackRoot ( ) ; // warn but don't fail connection
525
+ }
526
+
527
+ // success
528
+ this . onConnectionComplete ( ) ;
529
+ }
530
+
531
+ // MC source files are rooted to scripts/ folder, not the pack root.
532
+ // check that the localRoot property of launch.json contains source files.
533
+ // message to user if no source files are found.
534
+ private handleCapabilityDisablePackRoot ( ) {
535
+ let sourcePath = this . _sourceMapRoot ? this . _sourceMapRoot : this . _localRoot ;
536
+ let foundSourceFiles = false ;
537
+ try {
538
+ let fileNames = fs . readdirSync ( sourcePath ) ;
539
+ for ( let fn of fileNames ) {
540
+ let ext = path . extname ( fn ) ;
541
+ if ( ext == ".js" || ext == ".ts" ) {
542
+ foundSourceFiles = true ;
543
+ break ;
544
+ }
545
+ }
546
+ }
547
+ catch ( e ) {
548
+ }
549
+
550
+ if ( ! foundSourceFiles ) {
551
+ this . showNotification ( "Failed to find source files. Check that launch.json 'localRoot' contains scripts or if using source maps that 'sourceMapRoot' is correct." , LogLevel . Warn ) ;
513
552
}
514
553
}
515
554
0 commit comments