1
- import { system } from "@minecraft/server" ;
2
1
const symbolError = Symbol ( "RunError" ) ;
3
2
const AsyncFunctionConstructor = ( async function ( ) { } ) . constructor ;
4
3
const GeneratorFunctionConstructor = ( function * ( ) { } ) . constructor ;
5
4
const AsyncGeneratorFunctionConstructor = ( async function * ( ) { } ) . constructor ;
6
- const runInterval = system . runInterval . bind ( system ) ;
7
- const runTimeout = system . runTimeout . bind ( system ) ;
8
- const clearRun = system . clearRun . bind ( system ) ;
9
5
const nativeFunction = "{\n [native code]\n}" ;
10
6
const safePrototypes = [
11
7
Object . prototype ,
@@ -26,35 +22,41 @@ const MinecraftModules = [
26
22
{ module_name : "@minecraft/server-editor" , tag : "editor" } ,
27
23
{ module_name : "@minecraft/server-editor-bindings" , tag : "bindings" }
28
24
] ;
29
- export var OutputType ;
25
+ var OutputType ;
30
26
( function ( OutputType ) {
31
27
OutputType [ OutputType [ "SyntaxError" ] = 0 ] = "SyntaxError" ;
32
28
OutputType [ OutputType [ "Error" ] = 1 ] = "Error" ;
33
29
OutputType [ OutputType [ "Successfull" ] = 2 ] = "Successfull" ;
34
30
} ) ( OutputType || ( OutputType = { } ) ) ;
35
- export var LogTypes ;
31
+ var LogTypes ;
36
32
( function ( LogTypes ) {
37
33
LogTypes [ LogTypes [ "log" ] = 0 ] = "log" ;
38
34
LogTypes [ LogTypes [ "error" ] = 1 ] = "error" ;
39
35
LogTypes [ LogTypes [ "warn" ] = 2 ] = "warn" ;
36
+ LogTypes [ LogTypes [ "info" ] = 3 ] = "info" ;
40
37
} ) ( LogTypes || ( LogTypes = { } ) ) ;
41
- const setInterval = ( callBack , interval = 0 , ...params ) => runInterval ( ( ) => callBack ( ...params ) , interval ) ;
42
- const setTimeout = ( callBack , interval = 0 , ...params ) => runTimeout ( ( ) => callBack ( ...params ) , interval ) ;
38
+ const consoleTamplate = {
39
+ log : consoleLike . bind ( null , LogTypes . log ) ,
40
+ warn : consoleLike . bind ( null , LogTypes . warn ) ,
41
+ error : consoleLike . bind ( null , LogTypes . error ) ,
42
+ info : consoleLike . bind ( null , LogTypes . info )
43
+ } ;
44
+ const runsTemplate = { } ;
43
45
function consoleLike ( type , ...texts ) {
44
46
console . warn ( `[Terminal-${ LogTypes [ type ] } ]: ` , ...texts . map ( a => getView [ typeof a ] ( ViewStyle . Short , a ) ) ) ;
45
47
}
46
- function formatView ( type , object ) {
47
- return getView [ typeof object ] ( ViewStyle . Full , object ) ;
48
+ function formatView ( object , viewStyle = ViewStyle . Full ) {
49
+ return getView [ typeof object ] ( viewStyle , object ) ;
48
50
}
49
- export async function TerminalInput ( source , message , scope = [ ] , o = consoleLike ) {
50
- 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 } , ...scope ) ;
51
+ async function TerminalInput ( source , message , scope = [ ] , console = consoleTamplate ) {
52
+ const a = await RunCode ( message , true , { console : { ... console } , print : consoleLike . bind ( null , LogTypes . log ) , self : source } , runsTemplate , ...scope ) ;
51
53
const { multicommand, startTime } = a ;
52
54
if ( a . syntaxError )
53
- return { type : OutputType . SyntaxError , value : a . syntaxError , formatView : formatView ( OutputType . SyntaxError , a . syntaxError ) , multicommand, startTime } ;
55
+ return { type : OutputType . SyntaxError , value : a . syntaxError , formatView : formatView ( a . syntaxError ) , multicommand, startTime } ;
54
56
const output = await a . promise ;
55
57
if ( typeof output === "object" && output !== null && symbolError in output )
56
- return { type : OutputType . Error , value : output [ symbolError ] , formatView : formatView ( OutputType . Error , output [ symbolError ] ) , multicommand, startTime } ;
57
- return { type : OutputType . Successfull , value : output , formatView : formatView ( OutputType . Successfull , output ) , multicommand, startTime } ;
58
+ return { type : OutputType . Error , value : output [ symbolError ] , formatView : formatView ( output [ symbolError ] ) , multicommand, startTime } ;
59
+ return { type : OutputType . Successfull , value : output , formatView : formatView ( output ) , multicommand, startTime } ;
58
60
}
59
61
async function RunCode ( code , useModules = true , ...scopes ) {
60
62
let func , output = { syntaxError : undefined , promise : undefined , multicommand : false } ;
@@ -101,6 +103,7 @@ var ViewStyle;
101
103
ViewStyle [ ViewStyle [ "Primitive" ] = 0 ] = "Primitive" ;
102
104
ViewStyle [ ViewStyle [ "Short" ] = 1 ] = "Short" ;
103
105
ViewStyle [ ViewStyle [ "Full" ] = 2 ] = "Full" ;
106
+ ViewStyle [ ViewStyle [ "Infinite" ] = 3 ] = "Infinite" ;
104
107
} ) ( ViewStyle || ( ViewStyle = { } ) ) ;
105
108
const getView = {
106
109
"object" ( style , any ) {
@@ -133,7 +136,7 @@ const getView = {
133
136
return `§7${ ( typeOf == "Object" || typeOf == '' ) ? "" : typeOf + " " } {${ realKeys . join ( "§7, " ) } ${ keys . length > 5 ? "§r§7, ..." : "§r§7" } }` ;
134
137
}
135
138
else {
136
- return getView [ "object" ] ( ViewStyle . Short , any ) + "\n" + buildCompoudView ( [ ] , any , any ) . join ( '\n' ) ;
139
+ return getView [ "object" ] ( ViewStyle . Short , any ) + "\n" + buildCompoudView ( style , [ ] , any , any ) . join ( '\n' ) ;
137
140
}
138
141
} ,
139
142
"function" ( n , any ) {
@@ -149,15 +152,15 @@ const getView = {
149
152
else if ( n === ViewStyle . Primitive )
150
153
return "§5§oƒ§r" ;
151
154
else
152
- return getView [ "object " ] ( ViewStyle . Short , any ) + "\n" + buildCompoudView ( [ ] , any , any , " " , true ) . join ( '\n' ) ;
155
+ return getView [ "function " ] ( ViewStyle . Short , any ) + "\n" + buildCompoudView ( n , [ ] , any , any , " " , true ) . join ( '\n' ) ;
153
156
} ,
154
157
"number" ( n , any ) { return `§3${ any . toString ( ) } §r` ; } ,
155
158
"bigint" ( n , any ) { return `§3${ any . toString ( ) } §r` ; } ,
156
159
"boolean" ( n , any ) { return `§3${ any . toString ( ) } §r` ; } ,
157
160
"symbol" ( n , any ) { return `§7Symbol(${ any . description } )§r` ; } ,
158
161
"undefined" ( ) { return "§7§oundefined§r" ; } ,
159
162
"string" ( n , any ) {
160
- if ( n === ViewStyle . Full )
163
+ if ( n === ViewStyle . Full || n == ViewStyle . Infinite )
161
164
return `§3"${ any } "§r` ;
162
165
else if ( n === ViewStyle . Short )
163
166
return `§3"${ any . split ( '\n' ) [ 0 ] } "§r` ;
@@ -167,21 +170,37 @@ const getView = {
167
170
}
168
171
}
169
172
} ;
170
- function buildCompoudView ( array , base , object , offSet = " " , func = false , deepth = 1 ) {
173
+ function buildCompoudView ( viewStyle , array , base , object , offSet = " " , func = false , deepth = 1 , knownSets = [ ] ) {
171
174
const off = offSet . repeat ( deepth ) ;
172
175
const prototype = Object . getPrototypeOf ( object ) ;
173
176
const descriptors = Object . getOwnPropertyDescriptors ( object ) ;
177
+ knownSets . push ( object ) ;
174
178
for ( const key of [ ...Object . getOwnPropertyNames ( descriptors ) , ...Object . getOwnPropertySymbols ( descriptors ) ] ) {
175
- const { value, set, get } = descriptors [ key ] ;
176
- if ( value ) {
177
- array . push ( `${ off } §r${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r${ getView [ typeof value ] ( ViewStyle . Short , value ) } §r` ) ;
179
+ const desc = descriptors [ key ] , { value, set, get } = desc ;
180
+ if ( "value" in desc ) {
181
+ //@ts -ignore
182
+ if ( viewStyle != ViewStyle . Infinite || typeof value !== "object" )
183
+ array . push ( `${ off } §r${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r${ getView [ typeof value ] ( ViewStyle . Short , value ) } §r` ) ;
184
+ else if ( knownSets . includes ( value ) )
185
+ array . push ( `${ off } §r${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §c::Recursive:: §r${ getView [ typeof value ] ( ViewStyle . Short , value ) } §r` ) ;
186
+ else {
187
+ array . push ( `${ off } §r${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r` ) ;
188
+ buildCompoudView ( viewStyle , array , value , value , offSet , typeof value === "function" , deepth + 1 , [ ...knownSets ] ) ;
189
+ }
178
190
}
179
191
else {
180
192
if ( get != undefined ) {
181
- let v ;
182
193
try {
183
- v = get . call ( base ) ;
184
- array . push ( `${ off } §r§7get§r ${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r${ getView [ typeof v ] ( ViewStyle . Short , v ) } §r` ) ;
194
+ const v = get . call ( base ) ;
195
+ //@ts -ignore
196
+ if ( viewStyle != ViewStyle . Infinite || typeof v !== "object" )
197
+ array . push ( `${ off } §r§7get§r ${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §r${ getView [ typeof v ] ( ViewStyle . Short , v ) } §r` ) ;
198
+ else if ( knownSets . includes ( v ) )
199
+ array . push ( `${ off } §r§7get§r ${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §c::Recursive:: §r${ getView [ typeof v ] ( ViewStyle . Short , v ) } §r` ) ;
200
+ else {
201
+ array . push ( `${ off } §r§7get§r ${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7:§r` ) ;
202
+ buildCompoudView ( viewStyle , array , v , v , offSet , typeof v === "function" , deepth + 1 , [ ...knownSets ] ) ;
203
+ }
185
204
}
186
205
catch ( error ) {
187
206
array . push ( `${ off } §r§7get§r ${ typeof key == "string" ? key : getView [ "symbol" ] ( ViewStyle . Primitive , key ) } §7: §o(...)§r` ) ;
@@ -196,94 +215,37 @@ function buildCompoudView(array, base, object, offSet = " ", func = false, deep
196
215
if ( prototype == null )
197
216
return array ;
198
217
const typOf = getTypeOfObject ( prototype ) ;
199
- array . push ( `${ off } §7[[Prototype]]: ${ ( typOf == "" ? "Object" : typOf ) } §r` ) ;
200
- if ( ! safePrototypes . includes ( prototype ) )
201
- buildCompoudView ( array , base , prototype , offSet , typeof prototype === 'function' , deepth + 1 ) ;
202
- return array ;
203
- }
204
- /*
205
- export function toStringPrimitiveFull(any){
206
- switch (typeof any) {
207
- case 'object':
208
- if(any === null){
209
- return "§7§onull";
210
- }else if (any instanceof Error) {
211
- return `§6${any}\n${any.stack}`;
212
- }else{
213
- const base = any, names = Object.getOwnPropertyNames(base), symbols = Object.getOwnPropertySymbols(base);
214
- const keys = names.filter(a=>(base.__lookupGetter__?.(a) == undefined && base.__lookupSetter__?.(a) == undefined)).map(k=>`§7${k}§r§7: ${toStringPrimitiveShort(base[k])}§r`).concat(symbols.map(s=>`§r${toStringPrimitiveShort(s)}§r§7: ${toStringPrimitiveShort(base[s])}`));
215
- const realKeys = keys.slice(0,5), typeOf = getTypeOfObject(base);
216
- let output = `§7${(typeOf == "Object" || typeOf == '')?"":typeOf + " "}{${realKeys.join("§7, ")}${keys.length>5?"§r§7, ...":"§r§7"}}§r`;
217
- function buildLines(base, offSet = " "){
218
- const prototype = Object.getPrototypeOf(base);
219
- for (const keyName of Object.getOwnPropertyNames(base)) {
220
- let getter = base.__lookupGetter__?.(keyName);
221
- let setter = base.__lookupSetter__?.(keyName);
222
- if(getter == undefined&&setter == undefined){
223
- output += `\n${offSet}§r${keyName}§7: §r${toStringPrimitive(base[keyName])}`;
224
- } else {
225
- if(getter != undefined) output += `\n${offSet}§7get§r ${keyName}§7: (...)`;
226
- if(setter != undefined) output += `\n${offSet}§7set§r ${keyName}§7: (...)`;
227
- }
228
- }
229
- for (const keySymbol of Object.getOwnPropertySymbols(base)){
230
- let getter = base.__lookupGetter__?.(keySymbol);
231
- let setter = base.__lookupSetter__?.(keySymbol);
232
- if(getter == undefined&&setter == undefined){
233
- output += `\n${offSet}§r${toStringPrimitiveShort(keySymbol)}§7: §r${toStringPrimitive(base[keySymbol])}`;
234
- } else {
235
- if(getter != undefined) output += `\n${offSet}§7get§r ${toStringPrimitiveShort(keySymbol)}§7: (...)`;
236
- if(setter != undefined) output += `\n${offSet}§7set§r ${toStringPrimitiveShort(keySymbol)}§7: (...)`;
237
- }
238
- }
239
- if(prototype != null){
240
- const typOf = getTypeOfObject(prototype);
241
- output += `\n${offSet}§r[[Prototype]]§r§7: ` + (typOf==""?"Object":typOf)
242
- if(prototype != Object.prototype && prototype != Array.prototype && prototype != Map.prototype){
243
- buildLines(prototype, offSet + " ");
244
- }
245
- }
246
- }
247
- buildLines(base);
248
- return output;
249
- }
250
- case 'function': return any.toString();
251
- case 'symbol': return `§7Symbol(${any.description})`;
252
- case 'bigint':
253
- case 'number':
254
- case 'boolean': return `§3${any.toString()}§r`;
255
- case 'undefined': return "§7§oundefined";
256
- case 'string': return `§3"${any}"§r`;
257
- default:
258
- break;
218
+ if ( ! safePrototypes . includes ( prototype ) ) {
219
+ array . push ( `${ off } §7[[Prototype]]: ${ ( typOf == "" ? "Object" : typOf ) } §r` ) ;
220
+ buildCompoudView ( viewStyle , array , base , prototype , offSet , typeof prototype === 'function' , deepth + 1 , [ ...knownSets ] ) ;
259
221
}
222
+ else
223
+ array . push ( `${ off } §7[[Prototype]]: ${ ( typOf == "" ? "Object" : typOf ) } §r${ getView [ typeof prototype ] ( ViewStyle . Short , prototype ) } §r` ) ;
224
+ return array ;
260
225
}
261
- export function toStringPrimitive(any: any){
262
- switch (typeof any) {
263
- case 'object':
264
- if(any === null){
265
- return "§7§onull§r";
266
- }else if (any instanceof Error) {
267
- return `§6${any}§r`;
268
- }else{
269
- const base = any, names = Object.getOwnPropertyNames(base), symbols = Object.getOwnPropertySymbols(base);
270
- const keys = names.map(k=>`§7${k}§r§7: ${toStringPrimitiveShort(base[k])}§r`).concat(symbols.map(s=>`§r${toStringPrimitiveShort(s)}§r§7: ${toStringPrimitiveShort(base[s])}`));
271
- const realKeys = keys.slice(0,5), typeOf = getTypeOfObject(base);
272
- return `§7${(typeOf == "Object" || typeOf == '')?"":typeOf + " "}{${realKeys.join("§7, ")}${keys.length>5?"§r§7, ...":"§r§7"}}`;
273
- }
274
- case 'function': return toFunctionString(any);
275
- case 'symbol': return `Symbol(${any.description})`;
276
- case 'bigint':
277
- case 'number':
278
- case 'boolean': return `§3${any.toString()}§r`;
279
- case 'undefined': return "§7§oundefined";
280
- case 'string': return `§6"${any}"§r`;
281
- default:
282
- break;
283
- }
284
- }*/
285
226
function getTypeOfObject ( obj ) { return ( obj [ Symbol . toStringTag ] ?? ( ( typeof obj === "function" ? obj . name : undefined ) ?? ( ( obj . constructor ?. prototype == obj ? obj . constructor ?. name : obj . __proto__ ?. constructor ?. name ) ?? "" ) ) ) ; }
227
+ async function timeoutsInit ( ) {
228
+ const a = await import ( "@minecraft/server" ) . catch ( e => null ) ;
229
+ if ( a === null )
230
+ return ;
231
+ if ( a . system ?. runTimeout == undefined )
232
+ return ;
233
+ const system = a . system ;
234
+ const runInterval = system . runInterval . bind ( system ) ;
235
+ const runTimeout = system . runTimeout . bind ( system ) ;
236
+ const clearRun = system . clearRun . bind ( system ) ;
237
+ runsTemplate . setInterval = ( callBack , interval = 0 , ...params ) => runInterval ( ( ) => callBack ( ...params ) , interval ) ;
238
+ runsTemplate . setTimeout = ( callBack , interval = 0 , ...params ) => runTimeout ( ( ) => callBack ( ...params ) , interval ) ;
239
+ runsTemplate . clearInterval = clearRun ;
240
+ runsTemplate . clearTimeout = clearRun ;
241
+ }
242
+ async function timeoutsSupported ( ) {
243
+ const a = await import ( "@minecraft/server" ) . catch ( e => { } ) ;
244
+ return a ?. system ?. runTimeout !== undefined ;
245
+ }
286
246
//@ts -ignore
287
247
globalThis [ Symbol . toStringTag ] = 'GlobalThis' ;
288
248
//@ts -ignore
289
249
globalThis . console [ Symbol . toStringTag ] = "Console" ;
250
+ timeoutsInit ( ) ;
251
+ export { OutputType , formatView as FormatView , TerminalInput , timeoutsSupported } ;
0 commit comments