11/*
22 * @Author : xuranXYS
3- * @LastEditTime : 2024-03-30 22:18:29
3+ * @LastEditTime : 2024-03-31 16:26:45
44 * @GitHub : www.github.com/xiaoxustudio
55 * @WebSite : www.xiaoxustudio.top
66 * @Description : By xuranXYS
77 */
88import {
9+ ExitedEvent ,
910 Handles ,
1011 InitializedEvent ,
1112 Logger ,
1213 logger ,
1314 LoggingDebugSession ,
14- MemoryEvent ,
1515 Scope ,
1616 StackFrame ,
1717 StoppedEvent ,
1818 Thread ,
1919} from "@vscode/debugadapter" ;
2020import { DebugProtocol } from "@vscode/debugprotocol" ;
2121import { getGameData } from "./utils/utils" ;
22- import { DebugSession } from "vscode" ;
22+ import { debug , DebugSession } from "vscode" ;
2323import XRRuntime , { RuntimeVariable } from "./ws" ;
2424import { FileAccessor } from "./utils/utils_novsc" ;
2525import { WebSocket } from "ws" ;
@@ -37,7 +37,7 @@ export class XRDebugSession extends LoggingDebugSession {
3737 private _socket ! : WebSocket ;
3838 private _socket_real : XRRuntime ;
3939 private _variableHandles = new Handles <
40- "locals" | "globals" | RuntimeVariable
40+ "locals" | "globals" | "env" | "scene" | RuntimeVariable
4141 > ( ) ;
4242 private _valuesInHex = false ;
4343 constructor ( _s : DebugSession , private FileAccess : FileAccessor ) {
@@ -55,20 +55,20 @@ export class XRDebugSession extends LoggingDebugSession {
5555 ) : void {
5656 // 告诉调试器我们支持的功能
5757 response . body = response . body || { } ;
58+ response . body . supportsSetVariable = true ;
5859 response . body . supportsConfigurationDoneRequest = true ;
5960 response . body . supportsStepBack = false ;
6061 response . body . supportsSteppingGranularity = false ;
6162 response . body . supportsEvaluateForHovers = false ;
6263 response . body . supportsStepBack = false ;
63- response . body . supportsSetVariable = false ;
6464 response . body . supportsRestartFrame = false ;
6565 response . body . supportsGotoTargetsRequest = false ;
6666 response . body . supportsStepInTargetsRequest = false ;
6767 response . body . supportsCompletionsRequest = false ;
6868 response . body . supportsCancelRequest = false ;
6969 response . body . supportsBreakpointLocationsRequest = false ;
7070 this . sendResponse ( response ) ;
71- console . log ( "initializing" ) ;
71+ console . log ( "(webgal) initializing" ) ;
7272 this . sendEvent ( new InitializedEvent ( ) ) ;
7373 }
7474 protected attachRequest (
@@ -77,6 +77,14 @@ export class XRDebugSession extends LoggingDebugSession {
7777 ) {
7878 return this . launchRequest ( response , args ) ;
7979 }
80+ protected restartRequest (
81+ response : DebugProtocol . RestartResponse ,
82+ args : DebugProtocol . RestartArguments ,
83+ request ?: DebugProtocol . Request | undefined
84+ ) : void {
85+ this . _socket_real . _clearFunc ( ) ;
86+ this . sendResponse ( response ) ;
87+ }
8088 protected launchRequest (
8189 response : DebugProtocol . LaunchResponse ,
8290 args : ILaunchRequestArguments
@@ -204,6 +212,8 @@ export class XRDebugSession extends LoggingDebugSession {
204212 response . body = {
205213 scopes : [
206214 new Scope ( "Locals" , this . _variableHandles . create ( "locals" ) , false ) ,
215+ new Scope ( "Env" , this . _variableHandles . create ( "env" ) , false ) ,
216+ new Scope ( "Scene" , this . _variableHandles . create ( "scene" ) , false ) ,
207217 ] ,
208218 } ;
209219 this . sendResponse ( response ) ;
@@ -212,30 +222,146 @@ export class XRDebugSession extends LoggingDebugSession {
212222 response : DebugProtocol . VariablesResponse ,
213223 args : DebugProtocol . VariablesArguments
214224 ) : void {
215- let vs = ( this . _socket_real . getLocalVariables ( ) as RuntimeVariable [ ] ) . map (
216- ( v ) => this . convertFromRuntime ( v )
217- ) as DebugProtocol . Variable [ ] ;
225+ const _nfef = this . _variableHandles . get ( args . variablesReference ) ;
226+ let vs : DebugProtocol . Variable [ ] = [ ] ;
227+ if ( _nfef === "locals" ) {
228+ vs = (
229+ this . _socket_real . getLocalVariables ( "var" ) as RuntimeVariable [ ]
230+ ) . map ( ( v ) =>
231+ this . convertFromRuntime ( v , args . variablesReference )
232+ ) as DebugProtocol . Variable [ ] ;
233+ } else if ( _nfef === "scene" ) {
234+ vs = (
235+ this . _socket_real . getLocalVariables ( "scene" ) as RuntimeVariable [ ]
236+ ) . map ( ( v ) =>
237+ this . convertFromRuntime ( v , args . variablesReference )
238+ ) as DebugProtocol . Variable [ ] ;
239+ } else if ( _nfef === "env" ) {
240+ vs = (
241+ this . _socket_real . getLocalVariables ( "env" ) as RuntimeVariable [ ]
242+ ) . map ( ( v ) =>
243+ this . convertFromRuntime ( v , args . variablesReference )
244+ ) as DebugProtocol . Variable [ ] ;
245+ } else {
246+ if ( _nfef instanceof RuntimeVariable ) {
247+ if ( typeof _nfef . value === "object" && _nfef . desc === "Array" ) {
248+ for ( let i in _nfef . value ) {
249+ const _val = _nfef . value [ i ] ;
250+ const _val_name = _val . desc ?? String ( _val . value ) ;
251+ let _var = {
252+ name : _val . name ,
253+ value : _val_name ,
254+ variablesReference : _val . reference ?? 0 ,
255+ } as DebugProtocol . Variable ;
256+ vs . push ( _var ) ;
257+ }
258+ } else if ( typeof _nfef . value === "object" && _nfef . desc === "Object" ) {
259+ const _keys = _nfef . value ;
260+ for ( let i in _keys ) {
261+ const _val = _keys [ i ] as RuntimeVariable ;
262+ const _val_name = _val . desc ?? String ( _val . value ) ;
263+ let _var = {
264+ name : _val . name ,
265+ value : _val_name ,
266+ variablesReference : _val . reference ?? 0 ,
267+ } as DebugProtocol . Variable ;
268+ vs . push ( _var ) ;
269+ }
270+ } else {
271+ let _var = {
272+ name : _nfef . name ,
273+ value : String ( _nfef . value ) ,
274+ variablesReference : 0 ,
275+ } as DebugProtocol . Variable ;
276+ vs . push ( _var ) ;
277+ }
278+ }
279+ }
218280 response . body = {
219281 variables : vs ,
220282 } ;
221283 this . sendResponse ( response ) ;
222284 }
285+ protected setVariableRequest (
286+ response : DebugProtocol . SetVariableResponse ,
287+ args : DebugProtocol . SetVariableArguments ,
288+ request ?: DebugProtocol . Request | undefined
289+ ) : void {
290+ const _origin_var = this . _variableHandles . get ( args . variablesReference ) ;
291+ if ( _origin_var === "locals" ) {
292+ this . _socket . emit ( "runscript" , `setVar:${ args . name } =${ args . value } ;` ) ;
293+ response . body = {
294+ ...args ,
295+ } ;
296+ }
297+ this . sendResponse ( response ) ;
298+ }
223299 private formatNumber ( x : number ) {
224300 return this . _valuesInHex ? "0x" + x . toString ( 16 ) : x . toString ( 10 ) ;
225301 }
226- private convertFromRuntime ( v : RuntimeVariable ) : DebugProtocol . Variable {
302+ private convertFromRuntime (
303+ v : RuntimeVariable ,
304+ vref ?: number
305+ ) : DebugProtocol . Variable {
227306 let dapVariable : DebugProtocol . Variable = {
228307 name : v . name ,
229308 value : "???" ,
230309 type : typeof v . value ,
231310 variablesReference : 0 ,
232311 evaluateName : v . name ,
233312 } ;
234-
235- if ( Array . isArray ( v . value ) ) {
236- dapVariable . value = "Object" ;
313+ if ( v . value instanceof Object ) {
237314 v . reference ??= this . _variableHandles . create ( v ) ;
238315 dapVariable . variablesReference = v . reference ;
316+ const _to_runtimeval = ( _runVal : Array < any > ) : RuntimeVariable [ ] => {
317+ let _arr : Array < any > = [ ] ;
318+ for ( let _rK in _runVal ) {
319+ let _rV = _runVal [ _rK ] ;
320+ const _r = new RuntimeVariable ( _rK , _rV ) ;
321+ if ( Array . isArray ( _rV ) ) {
322+ _r . reference ??= this . _variableHandles . create ( _r ) ;
323+ _r . desc = "Array" ;
324+ _r . value = _to_runtimeval ( _rV ) ;
325+ } else if ( typeof _rV === "object" ) {
326+ _r . reference ??= this . _variableHandles . create ( _r ) ;
327+ _r . desc = "Object" ;
328+ _r . value = _to_runtimeval_obj ( _rV ) ;
329+ }
330+ _arr . push ( _r ) ;
331+ }
332+ return _arr ;
333+ } ;
334+ const _to_runtimeval_obj = ( _runVal : {
335+ [ key : string ] : any ;
336+ } ) : RuntimeVariable [ ] => {
337+ let r : RuntimeVariable [ ] = [ ] ;
338+ const _keys = Object . keys ( _runVal ) ;
339+ for ( let _rK in _keys ) {
340+ const ___r = _keys [ _rK ] ;
341+ let _rV = _runVal [ ___r ] ;
342+ const _r = new RuntimeVariable ( ___r , _rV ) ;
343+ if ( Array . isArray ( _rV ) ) {
344+ _r . reference ??= this . _variableHandles . create ( _r ) ;
345+ _r . desc = "Array" ;
346+ _r . value = _to_runtimeval ( _rV ) ;
347+ } else if ( typeof _rV === "object" ) {
348+ _r . reference ??= this . _variableHandles . create ( _r ) ;
349+ _r . desc = "Object" ;
350+ _r . value = _to_runtimeval_obj ( _rV ) ;
351+ }
352+ r . push ( _r ) ;
353+ }
354+ return r ;
355+ } ;
356+ if ( Array . isArray ( v . value ) ) {
357+ dapVariable . value = "Array " ;
358+ v . value = _to_runtimeval ( v . value ) ;
359+ v . desc = "Array" ;
360+ } else if ( v . value ) {
361+ dapVariable . value = "Object" ;
362+ v . value = _to_runtimeval_obj ( v . value ) ;
363+ v . desc = "Object" ;
364+ }
239365 } else {
240366 switch ( typeof v . value ) {
241367 case "number" :
@@ -270,18 +396,22 @@ export class XRDebugSession extends LoggingDebugSession {
270396 switch ( command ) {
271397 case "updatevar" :
272398 {
273- let vs = (
274- this . _socket_real . getLocalVariables ( ) as RuntimeVariable [ ]
275- ) . map ( ( v ) => this . convertFromRuntime ( v ) ) as DebugProtocol . Variable [ ] ;
276- response . body = {
277- variables : vs ,
278- } ;
279- response . success = true ;
280- response . command = "variables" ;
281- response . type = "response" ;
399+ // let vs = (
400+ // this._socket_real.getLocalVariables() as RuntimeVariable[]
401+ // ).map((v) => this.convertFromRuntime(v)) as DebugProtocol.Variable[];
402+ // response.body = {
403+ // variables: vs,
404+ // };
405+ // response.success = true;
406+ // response.command = "variables";
407+ // response.type = "response";
282408 this . sendResponse ( response ) ;
283409 }
284410 return ;
411+ case "close" :
412+ debug . stopDebugging ( ) ;
413+ this . sendResponse ( response ) ;
414+ break ;
285415 default :
286416 this . sendResponse ( response ) ;
287417 break ;
0 commit comments