File size: 31,933 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
const formatMessage = require('format-message');
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const ProjectPermissionManager = require('../../util/project-permissions');
const SandboxRunner = require('../../util/sandboxed-javascript-runner');
const beatgammit = {
    deflate: require('./beatgammit-deflate'),
    inflate: require('./beatgammit-inflate')
};
const {
    validateArray
} = require('../../util/json-block-utilities');
const ArrayBufferUtil = require('../../util/array buffer');
const BufferParser = new ArrayBufferUtil();
const Cast = require('../../util/cast');
// const Cast = require('../../util/cast');

const warningIcon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAC8SURBVDhPpZPBDYMwDEWhJw4MQzdgG0bi0APDlHuPZRv6X2xUaqJWpE8y2Pk/JkRJFVnXtVOMiqdig5yxzm1HJDZu+gWexqcZDCjuqHtcRo/gfTdRkf2yy7kGMG4i/5wlGYSXObqL9MFsRQw06C0voq9ZhxcHasH7m4cV/AUNFkuLWGgwW17EzB5wPB9Wn+aanmoysVGRJAovI5PLydAqzh7l1mWDAUV2JQE8n5P3SORo3xTxOjMWrnNVvQChGZRpEqnWPQAAAABJRU5ErkJggg==";

/**
 * Class for Prism blocks
 * @constructor
 */
class JgPrismBlocks {
    constructor(runtime) {
        /**
         * The runtime instantiating this block package.
         * @type {Runtime}
         */
        this.runtime = runtime;
        this.audioPlayer = new Audio();
        this.isJSPermissionGranted = false;
        this.isCameraScreenshotEnabled = false;

        this.mouseScrollDelta = { x: 0, y: 0, z: 0 };
        addEventListener("wheel", e => {
            this.mouseScrollDelta.x = e.deltaX;
            this.mouseScrollDelta.y = e.deltaY;
            this.mouseScrollDelta.z = e.deltaZ;
        });
        setInterval(() => {
            this.mouseScrollDelta = { x: 0, y: 0, z: 0 };
        }, 65);

        this.encodeCharacterLength = 6;
    }


    /**
     * dummy function for reseting user provided permisions when a save is loaded
     */
    deserialize() {
        this.isJSPermissionGranted = false;
        this.isCameraScreenshotEnabled = false;
    }

    /**
     * @returns {object} metadata for this extension and its blocks.
     */
    getInfo() {
        return {
            id: 'jgPrism',
            name: 'Prism',
            color1: '#BC7FFF',
            color2: '#AD66FF',
            blocks: [
                {
                    opcode: 'playAudioFromUrl',
                    text: formatMessage({
                        id: 'jgPrism.blocks.playAudioFromUrl',
                        default: 'play audio from [URL]',
                        description: 'Plays sound from a URL.'
                    }),
                    blockType: BlockType.COMMAND,
                    hideFromPalette: true,
                    arguments: {
                        URL: {
                            type: ArgumentType.STRING,
                            defaultValue: 'https://extensions.turbowarp.org/meow.mp3'
                        }
                    }
                },
                {
                    opcode: 'setAudioToLooping',
                    text: formatMessage({
                        id: 'jgPrism.blocks.setAudioToLooping',
                        default: 'set audio to loop',
                        description: 'Sets the audio to be looping.'
                    }),
                    hideFromPalette: true,
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'setAudioToNotLooping',
                    text: formatMessage({
                        id: 'jgPrism.blocks.setAudioToNotLooping',
                        default: 'set audio to not loop',
                        description: 'Sets the audio to not be looping.'
                    }),
                    hideFromPalette: true,
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'pauseAudio',
                    text: formatMessage({
                        id: 'jgPrism.blocks.pauseAudio',
                        default: 'pause audio',
                        description: 'Pauses the audio player.'
                    }),
                    hideFromPalette: true,
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'playAudio',
                    text: formatMessage({
                        id: 'jgPrism.blocks.playAudio',
                        default: 'resume audio',
                        description: 'Resumes the audio player.'
                    }),
                    hideFromPalette: true,
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'setAudioPlaybackSpeed',
                    text: formatMessage({
                        id: 'jgPrism.blocks.setAudioPlaybackSpeed',
                        default: 'set audio speed to [SPEED]%',
                        description: 'Sets the speed of the audio player.'
                    }),
                    hideFromPalette: true,
                    blockType: BlockType.COMMAND,
                    arguments: {
                        SPEED: {
                            type: ArgumentType.NUMBER,
                            defaultValue: 100
                        }
                    }
                },
                {
                    opcode: 'getAudioPlaybackSpeed',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.getAudioPlaybackSpeed',
                        default: 'audio speed',
                        description: 'Block that returns the playback speed of the audio player.'
                    }),
                    hideFromPalette: true,
                    disableMonitor: false,
                    blockType: BlockType.REPORTER
                },
                {
                    opcode: 'setAudioPosition',
                    text: formatMessage({
                        id: 'jgPrism.blocks.setAudioPosition',
                        default: 'set audio position to [POSITION] seconds',
                        description: 'Sets the position of the current audio in the audio player.'
                    }),
                    blockType: BlockType.COMMAND,
                    hideFromPalette: true,
                    arguments: {
                        POSITION: {
                            type: ArgumentType.NUMBER,
                            defaultValue: 5
                        }
                    }
                },
                {
                    opcode: 'getAudioPosition',
                    text: 'audio position',
                    disableMonitor: false,
                    hideFromPalette: true,
                    blockType: BlockType.REPORTER
                },
                {
                    opcode: 'setAudioVolume',
                    text: formatMessage({
                        id: 'jgPrism.blocks.setAudioVolume',
                        default: 'set audio volume to [VOLUME]%',
                        description: 'Sets the volume of the current audio in the audio player.'
                    }),
                    blockType: BlockType.COMMAND,
                    hideFromPalette: true,
                    arguments: {
                        VOLUME: {
                            type: ArgumentType.NUMBER,
                            defaultValue: 100
                        }
                    }
                },
                {
                    opcode: 'getAudioVolume',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.getAudioVolume',
                        default: 'audio volume',
                        description: 'Block that returns the volume of the audio player.'
                    }),
                    disableMonitor: false,
                    hideFromPalette: true,
                    blockType: BlockType.REPORTER
                },
                {
                    blockType: BlockType.LABEL,
                    text: "Data URIs"
                },
                {
                    opcode: 'screenshotStage',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.screenshotStage',
                        default: 'screenshot the stage',
                        description: 'Block that screenshots the stage and returns a Data URI of it.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true
                },
                {
                    opcode: 'dataUriOfCostume',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.dataUriOfCostume',
                        default: 'data url of costume #[INDEX]',
                        description: 'Block that returns a Data URI of the costume at the index.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    // these blocks will be replaced in the future
                    // hideFromPalette: true,
                    arguments: {
                        INDEX: {
                            type: ArgumentType.NUMBER,
                            defaultValue: "1"
                        }
                    }
                },
                {
                    opcode: 'dataUriFromImageUrl',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.dataUriFromImageUrl',
                        default: 'data url of image at url: [URL]',
                        description: 'Block that returns a Data URI of the content fetched from the URL.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    // these blocks will be replaced in the future
                    // hideFromPalette: true,
                    arguments: {
                        URL: {
                            type: ArgumentType.STRING,
                            defaultValue: "url"
                        }
                    }
                },
                {
                    opcode: 'dataUriFromArrayBuffer',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.dataUriFromArrayBuffer',
                        default: 'convert array buffer [BUFFER] to data url',
                        description: 'Block that returns a Data URI from an array buffer.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    arguments: {
                        BUFFER: {
                            type: ArgumentType.STRING,
                            defaultValue: "[72,101,108,108,111]"
                        }
                    }
                },
                {
                    opcode: 'arrayBufferFromDataUri',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.arrayBufferFromDataUri',
                        default: 'convert data url [URL] to array buffer',
                        description: 'Block that returns an array buffer from a Data URL.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    arguments: {
                        URL: {
                            type: ArgumentType.STRING,
                            defaultValue: "data:text/plain;base64,SGVsbG8="
                        }
                    }
                },
                // {
                //     blockType: BlockType.LABEL,
                //     text: "More Mouse Inputs"
                // },
                {
                    opcode: 'currentMouseScrollX',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.currentMouseScrollX',
                        default: 'mouse scroll x',
                        description: 'im too lazy to write these anymore tbh'
                    }),
                    disableMonitor: false,
                    hideFromPalette: true,
                    blockIconURI: warningIcon,
                    blockType: BlockType.REPORTER
                },
                {
                    opcode: 'currentMouseScroll',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.currentMouseScroll',
                        default: 'mouse scroll y',
                        description: 'im too lazy to write these anymore tbh'
                    }),
                    disableMonitor: false,
                    hideFromPalette: true,
                    blockIconURI: warningIcon,
                    blockType: BlockType.REPORTER
                },
                {
                    opcode: 'currentMouseScrollZ',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.currentMouseScrollZ',
                        default: 'mouse scroll z',
                        description: 'im too lazy to write these anymore tbh'
                    }),
                    disableMonitor: false,
                    hideFromPalette: true,
                    blockIconURI: warningIcon,
                    blockType: BlockType.REPORTER
                },
                {
                    blockType: BlockType.LABEL,
                    text: "Base64"
                },
                {
                    opcode: 'base64Encode',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.base64Encode',
                        default: 'base64 encode [TEXT]',
                        description: 'Block that encodes and returns the result of it.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    arguments: {
                        TEXT: {
                            type: ArgumentType.STRING,
                            defaultValue: "abc"
                        }
                    }
                },
                {
                    opcode: 'base64Decode',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.base64Decode',
                        default: 'base64 decode [TEXT]',
                        description: 'Block that decodes and returns the result of it.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    arguments: {
                        TEXT: {
                            type: ArgumentType.STRING,
                            defaultValue: "YWJj"
                        }
                    }
                },
                // {
                //     blockType: BlockType.LABEL,
                //     text: "String Character Codes"
                // },
                {
                    opcode: 'fromCharacterCodeString',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.fromCharacterCodeString',
                        default: 'character from character code [TEXT]',
                        description: 'Block that decodes and returns the result of it.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    hideFromPalette: true,
                    blockIconURI: warningIcon,
                    arguments: {
                        TEXT: {
                            type: ArgumentType.NUMBER,
                            defaultValue: 97
                        }
                    }
                },
                {
                    opcode: 'toCharacterCodeString',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.toCharacterCodeString',
                        default: 'character code of [TEXT]',
                        description: 'Block that encodes and returns the result of it.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    hideFromPalette: true,
                    blockIconURI: warningIcon,
                    arguments: {
                        TEXT: {
                            type: ArgumentType.STRING,
                            defaultValue: "a"
                        }
                    }
                },
                "---",
                "---",
                {
                    blockType: BlockType.LABEL,
                    text: "JS Deflate by BeatGammit"
                },
                {
                    opcode: 'lib_deflate_deflateArray',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.lib_deflate_deflateArray',
                        default: 'deflate [ARRAY]',
                        description: 'abc'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    arguments: {
                        ARRAY: {
                            type: ArgumentType.STRING,
                            defaultValue: "[]"
                        }
                    }
                },
                {
                    opcode: 'lib_deflate_inflateArray',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.lib_deflate_inflateArray',
                        default: 'inflate [ARRAY]',
                        description: 'abc'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    arguments: {
                        ARRAY: {
                            type: ArgumentType.STRING,
                            defaultValue: "[]"
                        }
                    }
                },
                {
                    blockType: BlockType.LABEL,
                    text: "Numerical Encoding by cs2627883"
                },
                {
                    opcode: 'NumericalEncode',
                    blockType: BlockType.REPORTER,
                    text: 'encode [DATA] to number',
                    arguments: {
                        DATA: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Hello!'
                        }
                    }
                },
                {
                    opcode: 'NumericalDecode',
                    blockType: BlockType.REPORTER,
                    text: 'decode [ENCODED] from number',
                    arguments: {
                        ENCODED: {
                            type: ArgumentType.STRING,
                            defaultValue: '000072000101000108000108000111000033'
                        }
                    }
                },
                // "---",
                // {
                //     blockType: BlockType.LABEL,
                //     text: "Deprecated blocks"
                // },
                // "---",
                // {
                //     blockType: BlockType.LABEL,
                //     text: "Don't use any of the blocks below."
                // },
                // {
                //     blockType: BlockType.LABEL,
                //     text: "They will be removed soon."
                // },
                // "---",
                // {
                //     blockType: BlockType.LABEL,
                //     text: "(Deprecated) JavaScript"
                // },
                {
                    opcode: 'evaluate',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.evaluate',
                        default: 'eval [JAVASCRIPT]',
                        description: 'Block that runs JavaScript code.'
                    }),
                    blockType: BlockType.COMMAND,
                    blockIconURI: warningIcon,
                    hideFromPalette: true,
                    arguments: {
                        JAVASCRIPT: {
                            type: ArgumentType.STRING,
                            defaultValue: "console.log('Hello!')"
                        }
                    }
                },
                {
                    opcode: 'evaluate2',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.evaluate2',
                        default: 'eval [JAVASCRIPT]',
                        description: 'Block that runs JavaScript code and returns the result of it.'
                    }),
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    blockIconURI: warningIcon,
                    hideFromPalette: true,
                    arguments: {
                        JAVASCRIPT: {
                            type: ArgumentType.STRING,
                            defaultValue: "Math.random()"
                        }
                    }
                },
                {
                    opcode: 'evaluate3',
                    text: formatMessage({
                        id: 'jgRuntime.blocks.evaluate3',
                        default: 'eval [JAVASCRIPT]',
                        description: 'Block that runs JavaScript code.'
                    }),
                    blockType: BlockType.HAT,
                    blockIconURI: warningIcon,
                    hideFromPalette: true,
                    arguments: {
                        JAVASCRIPT: {
                            type: ArgumentType.STRING,
                            defaultValue: "Math.round(Math.random()) == 1"
                        }
                    }
                }
            ]
        };
    }
    playAudioFromUrl(args) {
        if (!this.audioPlayer) this.audioPlayer = new Audio();
        this.audioPlayer.pause();
        this.audioPlayer.src = `${args.URL}`;
        this.audioPlayer.currentTime = 0;
        this.audioPlayer.play();
    }
    setAudioToLooping() {
        this.audioPlayer.loop = true;
    }
    setAudioToNotLooping() {
        this.audioPlayer.loop = false;
    }
    pauseAudio() {
        this.audioPlayer.pause();
    }
    playAudio() {
        this.audioPlayer.play();
    }
    setAudioPlaybackSpeed(args) {
        this.audioPlayer.playbackRate = (isNaN(Number(args.SPEED)) ? 100 : Number(args.SPEED)) / 100;
    }
    getAudioPlaybackSpeed() {
        return this.audioPlayer.playbackRate * 100;
    }
    setAudioPosition(args) {
        this.audioPlayer.currentTime = isNaN(Number(args.POSITION)) ? 0 : Number(args.POSITION);
    }
    getAudioPosition() {
        return this.audioPlayer.currentTime;
    }
    setAudioVolume(args) {
        this.audioPlayer.volume = (isNaN(Number(args.VOLUME)) ? 100 : Number(args.VOLUME)) / 100;
    }
    getAudioVolume() {
        return this.audioPlayer.volume * 100;
    }
    // eslint-disable-next-line no-unused-vars
    evaluate(args, util, realBlockInfo) {
        return new Promise((resolve, reject) => {
            // if (!(this.isJSPermissionGranted)) {
            //     this.isJSPermissionGranted = ProjectPermissionManager.RequestPermission("javascript");
            //     if (!this.isJSPermissionGranted) return;
            // }
            // // otherwise
            SandboxRunner.execute(String(args.JAVASCRIPT)).then(result => {
                if (!result.success) {
                    alert(result.value);
                    console.error(result.value);
                    return;
                }
                resolve(result.value)
            })
        })
    }
    // eslint-disable-next-line no-unused-vars
    evaluate2(args, util, realBlockInfo) {
        return new Promise((resolve, reject) => {
            // if (!(this.isJSPermissionGranted)) {
            //     this.isJSPermissionGranted = ProjectPermissionManager.RequestPermission("javascript");
            //     if (!this.isJSPermissionGranted) return "";
            // }
            // // otherwise
            SandboxRunner.execute(String(args.JAVASCRIPT)).then(result => {
                if (!result.success) {
                    console.error(result.value);
                }
                resolve(result.value)
            })
            // let result = "";
            // try {
            //     // eslint-disable-next-line no-eval
            //     result = eval(String(args.JAVASCRIPT));
            // } catch (e) {
            //     result = e;
            //     console.error(e);
            // }
            // return result;
            // return "";
        })
    }
    // eslint-disable-next-line no-unused-vars
    evaluate3(args, util, realBlockInfo) {
        return new Promise((resolve, reject) => {
            SandboxRunner.execute(String(args.JAVASCRIPT)).then(result => {
                if (!result.success) {
                    console.error(result.value);
                }
                resolve(result.value === true)
            })
        })
        // if (!(this.isJSPermissionGranted)) {
        //     this.isJSPermissionGranted = ProjectPermissionManager.RequestPermission("javascript");
        //     if (!this.isJSPermissionGranted) return false;
        // }
        // // otherwise
        // let result = true;
        // try {
        //     // eslint-disable-next-line no-eval
        //     result = eval(String(args.JAVASCRIPT));
        // } catch (e) {
        //     result = false;
        //     console.error(e);
        // }
        // // // otherwise
        // return result === true;
        // return false;
    }
    screenshotStage() {
        // should we look for an external canvas
        if (this.runtime.prism_screenshot_checkForExternalCanvas) {
            // if so, does one exist (this will check for more than 1 in the future)
            if (this.runtime.prism_screenshot_externalCanvas) {
                // we dont need to check camera permissions since external canvases
                // will never have the ability to get camera data
                return this.runtime.prism_screenshot_externalCanvas.toDataURL();
            }
        }
        // DO NOT REMOVE, USER HAS NOT GIVEN PERMISSION TO SAVE CAMERA IMAGES.
        if (this.runtime.ext_videoSensing || this.runtime.ioDevices.video.provider.enabled) {
            // user's camera is on, ask for permission to take a picture of them
            if (!(this.isCameraScreenshotEnabled)) {
                this.isCameraScreenshotEnabled = ProjectPermissionManager.RequestPermission("cameraPictures");
                if (!this.isCameraScreenshotEnabled) return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY/j//z8ABf4C/qc1gYQAAAAASUVORK5CYII="; // 1 pixel of white
            }
        }
        return new Promise(resolve => {
            vm.renderer.requestSnapshot(uri => {
                resolve(uri);
            });
        });
    }
    dataUriOfCostume(args, util) {
        const index = Number(args.INDEX);
        if (isNaN(index)) return "";
        if (index < 1) return "";

        const target = util.target;
        // eslint-disable-next-line no-undefined
        if (target.sprite.costumes[index - 1] === undefined || target.sprite.costumes[index - 1] === null) return "";
        const dataURI = target.sprite.costumes[index - 1].asset.encodeDataURI();
        return String(dataURI);
    }
    dataUriFromImageUrl(args) {
        return new Promise(resolve => {
            if (window && !window.FileReader) return resolve("");
            if (window && !window.fetch) return resolve("");
            fetch(`https://api.allorigins.win/raw?url=${encodeURIComponent(String(args.URL))}`).then(r => {
                r.blob().then(blob => {
                    const reader = new FileReader();
                    reader.onload = e => {
                        resolve(e.target.result);
                    };
                    reader.readAsDataURL(blob);
                })
                    .catch(() => {
                        resolve("");
                    });
            })
                .catch(() => {
                    resolve("");
                });
        });
    }

    dataUriFromArrayBuffer(args) {
        const array = validateArray(args.BUFFER);
        if (!array.isValid) return 'data:text/plain;base64,';
        const buffer = BufferParser.arrayToBuffer(array.array);
        let binary = '';
        let bytes = new Uint8Array(buffer);
        let len = bytes.byteLength;
        for (let i = 0; i < len; i++) {
            binary += String.fromCharCode(bytes[i]);
        }
        // use "application/octet-stream", we have no idea what the buffer actually contains
        return `data:application/octet-stream;base64,${btoa(binary)}`;
    }
    arrayBufferFromDataUri(args) {
        const dataUrl = Cast.toString(args.URL);
        return new Promise((resolve) => {
            fetch(dataUrl).then(res => {
                res.arrayBuffer().then(buffer => {
                    const array = BufferParser.bufferToArray(buffer);
                    resolve(JSON.stringify(array));
                }).catch(() => {
                    resolve('[]');
                });
            }).catch(() => {
                resolve('[]');
            });
        });
    }

    currentMouseScrollX() {
        return this.mouseScrollDelta.x;
    }
    currentMouseScroll() {
        return this.mouseScrollDelta.y;
    }
    currentMouseScrollZ() {
        return this.mouseScrollDelta.z;
    }
    base64Encode(args) {
        let result = "";
        try {
            result = btoa(String(args.TEXT));
        } catch {
            // what a shame
        }
        return result;
    }
    base64Decode(args) {
        let result = "";
        try {
            result = atob(String(args.TEXT));
        } catch {
            // what a shame
        }
        return result;
    }
    fromCharacterCodeString(args) {
        return String.fromCharCode(args.TEXT);
    }
    toCharacterCodeString(args) {
        return String(args.TEXT).charCodeAt(0);
    }
    lib_deflate_deflateArray(args) {
        const array = validateArray(args.ARRAY).array;

        return JSON.stringify(beatgammit.deflate(array));
    }
    lib_deflate_inflateArray(args) {
        const array = validateArray(args.ARRAY).array;

        return JSON.stringify(beatgammit.inflate(array));
    }

    NumericalEncode(args) {
        const toencode = String(args.DATA);
        let encoded = "";
        for (let i = 0; i < toencode.length; ++i) {
            // Get char code of character
            let encodedchar = String(toencode.charCodeAt(i));
            // Pad encodedchar with 0s to ensure all encodedchars are the same length
            encodedchar = "0".repeat(this.encodeCharacterLength - encodedchar.length) + encodedchar;
            encoded += encodedchar;
        }
        return encoded;
    }
    NumericalDecode(args) {
        const todecode = String(args.ENCODED);
        if (todecode == "") {
            return "";
        }
        let decoded = "";
        // Create regex to split by char length
        const regex = new RegExp('.{1,' + this.encodeCharacterLength + '}', 'g');
        // Split into array of characters
        let encodedchars = todecode.match(regex);
        for (let i = 0; i < encodedchars.length; i++) {
            // Get character from char code
            let decodedchar = String.fromCharCode(encodedchars[i]);
            decoded += decodedchar;
        }
        return decoded;
    }
}

module.exports = JgPrismBlocks;