Skip to content

Commit

Permalink
Better TypeScript debugging, support for alternate workflows other th…
Browse files Browse the repository at this point in the history
…an MC's gulp template. (#13)
  • Loading branch information
chmeyer-ms authored Jun 21, 2022
1 parent 5f3422b commit a94ee75
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 142 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
## Version 0.2.0 (April 2022)

- Support for TypeScript debugging.

## Version 0.3.0 (April 2022)

- Better TypeScript support.
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "minecraft-debugger",
"displayName": "Minecraft Bedrock Edition Debugger",
"description": "Debug your JavaScript code running as part of the GameTest Framework experimental feature in Minecraft Bedrock Edition.",
"version": "0.2.0",
"version": "0.3.0",
"publisher": "mojang-studios",
"author": {
"name": "Mojang Studios"
Expand All @@ -12,7 +12,7 @@
"type": "git",
"url": "https://github.com/Mojang/minecraft-debugger.git"
},
"icon": "bedrock-icon.png",
"icon": "bedrock-icon.png",
"engines": {
"vscode": "^1.55.0"
},
Expand Down Expand Up @@ -64,14 +64,13 @@
"description": "The local root of the Minecraft Add-On.",
"default": "${workspaceFolder}/"
},
"remoteRoot": {
"sourceMapRoot": {
"type": "string",
"description": "The remote root of the Minecraft Add-On."
"description": "The location of the source maps."
},
"sourceMapRoot": {
"generatedSourceRoot": {
"type": "string",
"description": "The local output folder for source maps.",
"default": "{workspaceFolder}/"
"description": "The location of the generated source files (js). Not required if same as source maps."
},
"host": {
"type": "string",
Expand Down
9 changes: 1 addition & 8 deletions src/MCConfigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,8 @@ export class MCConfigProvider implements vscode.DebugConfigurationProvider {
if (!config.localRoot) {
config.localRoot = "${workspaceFolder}/";
}

// default to local root
if (!config.remoteRoot) {
config.remoteRoot = config.localRoot;
}

// if no port, then set a command string that will trigger a user input dialog
if (!config.port) {
config.inputPort = "${command:PromptForPort}";
config.inputPort = "${command:PromptForPort}"; // prompt user for port
}

return config;
Expand Down
42 changes: 30 additions & 12 deletions src/MCDebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DebugProtocol } from 'vscode-debugprotocol';
import { LogOutputEvent, LogLevel } from 'vscode-debugadapter/lib/logger';
import { MCMessageStreamParser } from './MCMessageStreamParser';
import { MCSourceMaps } from './MCSourceMaps';
import { window } from 'vscode';
import { FileSystemWatcher, window, workspace } from 'vscode';
import * as path from 'path';

interface PendingResponse {
Expand All @@ -20,7 +20,7 @@ interface PendingResponse {
interface IAttachRequestArguments extends DebugProtocol.AttachRequestArguments {
mode: string;
localRoot: string;
remoteRoot: string;
generatedSourceRoot: string;
sourceMapRoot: string;
host: string;
port: number;
Expand All @@ -39,8 +39,8 @@ export class MCDebugSession extends DebugSession {
private _terminated: boolean = false;
private _threads = new Set<number>();
private _requests = new Map<number, PendingResponse>();
private _sourceMaps: MCSourceMaps = new MCSourceMaps("", "");
private _localRoot: string = "";
private _sourceMaps: MCSourceMaps = new MCSourceMaps("");
private _fileWatcher?: FileSystemWatcher;
private _activeThreadId: number = 0; // the one being debugged

public constructor() {
Expand Down Expand Up @@ -72,11 +72,6 @@ export class MCDebugSession extends DebugSession {

// VSCode wants to attach to a debugee (MC), create socket connection on specified port
protected async attachRequest(response: DebugProtocol.AttachResponse, args: IAttachRequestArguments, request?: DebugProtocol.Request) {
// capture arguments from launch.json
this._localRoot = path.normalize(args.localRoot);
// init source maps
this._sourceMaps = new MCSourceMaps(args.localRoot, args.remoteRoot, args.sourceMapRoot);

this.closeSession();

const host = args.host || 'localhost';
Expand All @@ -101,6 +96,12 @@ export class MCDebugSession extends DebugSession {
return;
}

// init source maps
this._sourceMaps = new MCSourceMaps(args.localRoot, args.sourceMapRoot, args.generatedSourceRoot);

// watch for source map changes
this.createSourceMapFileWatcher(args.sourceMapRoot);

// tell VSCode that attach is complete
this.sendResponse(response);
}
Expand Down Expand Up @@ -194,8 +195,7 @@ export class MCDebugSession extends DebugSession {
line: line || 0,
column: column || 0
});
let localOriginalAbsolutePath = path.join(this._localRoot, originalLocation.source);
const source = new Source(path.basename(originalLocation.source), localOriginalAbsolutePath);
const source = new Source(path.basename(originalLocation.source), originalLocation.source);
stackFrames.push(new StackFrame(id, name, source, originalLocation.line, originalLocation.column));
}
catch (e) {
Expand Down Expand Up @@ -380,6 +380,11 @@ export class MCDebugSession extends DebugSession {
this._connectionSocket.destroy();
}
this._connectionSocket = undefined;

if (this._fileWatcher) {
this._fileWatcher.dispose();
this._fileWatcher = undefined;
}
}

// close and terminate session (could be from debugee request)
Expand Down Expand Up @@ -439,7 +444,7 @@ export class MCDebugSession extends DebugSession {
// json = 1 line json + new line
let messageLength = jsonBuffer.byteLength + 1;
let length = '00000000' + messageLength.toString(16) + '\n';
length = length.substr(length.length - 9);
length = length.substring(length.length - 9);
let lengthBuffer = Buffer.from(length);
let newline = Buffer.from('\n');
let buffer = Buffer.concat([lengthBuffer, jsonBuffer, newline]);
Expand Down Expand Up @@ -517,6 +522,19 @@ export class MCDebugSession extends DebugSession {
}
}

private createSourceMapFileWatcher(sourceMapRoot?: string) {
if (this._fileWatcher) {
this._fileWatcher.dispose();
this._fileWatcher = undefined
}
if (sourceMapRoot) {
this._fileWatcher = workspace.createFileSystemWatcher('**/*.{map}', false, false, false);
this._fileWatcher.onDidChange(uri => { this._sourceMaps.reset() });
this._fileWatcher.onDidCreate(uri => { this._sourceMaps.reset() });
this._fileWatcher.onDidDelete(uri => { this._sourceMaps.reset() });
}
}

// ------------------------------------------------------------------------

private log(message: string, logLevel: LogLevel) {
Expand Down
Loading

0 comments on commit a94ee75

Please sign in to comment.