@@ -27,7 +27,7 @@ const MinecraftModules = [
27
27
{ module_name : "@minecraft/server-editor" , tag : "editor" } ,
28
28
{ module_name : "@minecraft/server-editor-bindings" , tag : "bindings" }
29
29
] ;
30
- enum OutputType {
30
+ export enum OutputType {
31
31
SyntaxError ,
32
32
Error ,
33
33
Successfull
@@ -46,14 +46,14 @@ function formatView(type: OutputType, object: any): string{
46
46
return getView [ typeof object ] ( ViewStyle . Full , object ) ;
47
47
}
48
48
export async function TerminalInput < s > ( source : s , message : string , o :( this : s , type : LogTypes , ...params : any [ ] ) => void = consoleLike ) {
49
- const a = await RunCode ( message , true , { console :{ log :o . bind ( source , LogTypes . log ) , warn :o . bind ( source , LogTypes . warn ) , error :o . bind ( source , LogTypes . error ) } , print :o . bind ( source , LogTypes . log ) , self :source , setTimeout, setInterval, clearInterval :clearRun , clearTimeout :clearRun } ) ;
50
- const multicommand = a . multicommand ;
51
- if ( a . syntaxError ) return { type : OutputType . SyntaxError , value : a . syntaxError , formatView : formatView ( OutputType . SyntaxError , a . syntaxError ) , multicommand} ;
49
+ const a = await RunCode ( message , true , { console :{ log :o . bind ( source , LogTypes . log ) , Map , Set , warn :o . bind ( source , LogTypes . warn ) , error :o . bind ( source , LogTypes . error ) } , print :o . bind ( source , LogTypes . log ) , self :source , setTimeout, setInterval, clearInterval :clearRun , clearTimeout :clearRun } ) ;
50
+ const { multicommand, startTime } = a ;
51
+ if ( a . syntaxError ) return { type : OutputType . SyntaxError , value : a . syntaxError , formatView : formatView ( OutputType . SyntaxError , a . syntaxError ) , multicommand, startTime } ;
52
52
const output = await a . promise ;
53
- if ( typeof output === "object" && symbolError in output ) return { type : OutputType . Error , value : output [ symbolError ] , formatView : formatView ( OutputType . Error , output [ symbolError ] ) , multicommand} ;
54
- return { type : OutputType . Successfull , value : output , formatView : formatView ( OutputType . Successfull , output ) , multicommand} ;
53
+ if ( typeof output === "object" && output !== null && symbolError in output ) return { type : OutputType . Error , value : output [ symbolError ] , formatView : formatView ( OutputType . Error , output [ symbolError ] ) , multicommand, startTime } ;
54
+ return { type : OutputType . Successfull , value : output , formatView : formatView ( OutputType . Successfull , output ) , multicommand, startTime } ;
55
55
}
56
- async function RunCode ( code : string , useModules = true , ...scopes : any [ ] ) : Promise < { syntaxError ?:any , promise ?: Promise < any | never > , multicommand : boolean } > {
56
+ async function RunCode ( code : string , useModules = true , ...scopes : any [ ] ) : Promise < { syntaxError ?:any , promise ?: Promise < any | never > , multicommand : boolean , startTime : number } > {
57
57
let func , output : any = { syntaxError : undefined , promise : undefined , multicommand : false } ;
58
58
const modules = useModules ?( await BuildAPIScope ( ...MinecraftModules ) ) :[ ] ;
59
59
try {
@@ -64,12 +64,13 @@ async function RunCode(code: string, useModules = true, ...scopes: any[]): Promi
64
64
return output ;
65
65
}
66
66
output . multicommand = func . multicommand ?? false ;
67
+ output . startTime = Date . now ( ) ;
67
68
output . promise = Promise . resolve ( func ( ) ) . catch ( er => ( { [ symbolError ] : er } ) ) ;
68
69
return output ;
69
70
}
70
71
async function BuildAPIScope ( ...modules : { module_name :string , tag :string } [ ] ) {
71
72
let promises : Promise < any > [ ] = [ ] ;
72
- modules . forEach ( m => promises . push ( import ( m . module_name ) . catch ( ( ) => ( { } ) ) ) ) ;
73
+ modules . forEach ( m => promises . push ( import ( m . module_name ) . catch ( ( ) => null ) ) ) ;
73
74
const dlls = await Promise . all ( promises ) ;
74
75
return dlls . map ( ( m , i ) => ( { [ modules [ i ] . tag ] : m } ) ) ;
75
76
}
@@ -105,7 +106,20 @@ const getView: {[key: string]:(style: ViewStyle, n: any)=>string} = {
105
106
else if ( style === ViewStyle . Short ) {
106
107
const names = Object . getOwnPropertyNames ( any ) , symbols = Object . getOwnPropertySymbols ( any ) ;
107
108
//@ts -ignore
108
- const keys = names . map ( k => `§7${ k } §r§7: ${ getView [ typeof any [ k ] ] ( ViewStyle . Primitive , any [ k ] ) } §r` ) . concat ( symbols . map ( s => `§r${ getView [ "symbol" ] ( ViewStyle . Primitive , s ) } §r§7: ${ getView [ typeof any [ s ] ] ( ViewStyle . Primitive , any [ s ] ) } ` ) ) ;
109
+ const keys = names . map ( k => {
110
+ try {
111
+ return `§7${ k } §r§7: ${ getView [ typeof any [ k ] ] ( ViewStyle . Primitive , any [ k ] ) } §r` ;
112
+ } catch ( error ) {
113
+ return `§7${ k } §r§7: §o(...)§r` ;
114
+ }
115
+ } ) . concat ( symbols . map ( s => {
116
+ try {
117
+ return `§r${ getView [ "symbol" ] ( ViewStyle . Primitive , s ) } §r§7: ${ getView [ typeof any [ s ] ] ( ViewStyle . Primitive , any [ s ] ) } ` ;
118
+ } catch ( error ) {
119
+ return `§r${ getView [ "symbol" ] ( ViewStyle . Primitive , s ) } §r§7: §o(...)§r` ;
120
+ }
121
+ } ) ) ;
122
+ Object . getOwnPropertyDescriptor
109
123
const realKeys = keys . slice ( 0 , 5 ) , typeOf = getTypeOfObject ( any ) ;
110
124
return `§7${ ( typeOf == "Object" || typeOf == '' ) ?"" :typeOf + " " } {${ realKeys . join ( "§7, " ) } ${ keys . length > 5 ?"§r§7, ..." :"§r§7" } }` ;
111
125
}
@@ -149,10 +163,15 @@ function buildCompoudView(array: string[], base: any, object: any, offSet = " "
149
163
array . push ( `${ off } §r${ typeof key == "string" ?key :getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r${ getView [ typeof value ] ( ViewStyle . Short , value ) } §r` ) ;
150
164
} else {
151
165
if ( get != undefined ) {
152
- const v = get . call ( base ) ;
153
- array . push ( `${ off } §r§7get§r ${ typeof key == "string" ?key :getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r${ getView [ typeof v ] ( ViewStyle . Short , v ) } §r` ) ;
166
+ let v ;
167
+ try {
168
+ v = get . call ( base ) ;
169
+ array . push ( `${ off } §r§7get§r ${ typeof key == "string" ?key :getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r${ getView [ typeof v ] ( ViewStyle . Short , v ) } §r` ) ;
170
+ } catch ( error ) {
171
+ array . push ( `${ off } §r§7get§r ${ typeof key == "string" ?key :getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §o(...)§r` ) ;
172
+ }
154
173
}
155
- if ( set != undefined ) array . push ( `${ off } §r§7set§r ${ typeof key == "string" ?key :getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: (...)§r` ) ;
174
+ if ( set != undefined ) array . push ( `${ off } §r§7set§r ${ typeof key == "string" ?key :getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §o (...)§r` ) ;
156
175
}
157
176
}
158
177
if ( func ) array . push ( `${ off } §7[[Native Function]]: §7${ getView [ "boolean" ] ( ViewStyle . Primitive , object . toString ( ) . endsWith ( nativeFunction ) ) } §r` ) ;
0 commit comments