File size: 8,828 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
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const Cast = require('../../util/cast');

class JgScriptsBlocks {
  /**
   * Class for Script blocks
   * @constructor
   */
  constructor(runtime) {
    /**
     * The runtime instantiating this block package.
     * @type {Runtime}
     */
    this.runtime = runtime;
    this.scripts = {};

    this.runtime.on("PROJECT_START", () => { this.scripts = {} });
    this.runtime.on("PROJECT_STOP_ALL", () => { this.scripts = {} });
  }
  /**
   * @returns {object} metadata for this extension and its blocks.
  */
  getInfo() {
    return {
      id: "jgScripts",
      name: "Scripts",
      color1: "#8c8c8c",
      color2: "#7a7a7a",
      blocks: [
        {
          opcode: "createScript",
          blockType: BlockType.COMMAND,
          text: "create script named [NAME]",
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" }
          },
        },
        {
          opcode: "deleteScript",
          blockType: BlockType.COMMAND,
          text: "delete script named [NAME]",
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" }
          },
        },
        {
          opcode: "deleteAll",
          blockType: BlockType.COMMAND,
          text: "delete all scripts"
        },
        {
          opcode: "allScripts",
          blockType: BlockType.REPORTER,
          text: "all scripts"
        },
        {
          opcode: "scriptExists",
          blockType: BlockType.BOOLEAN,
          text: "script named [NAME] exists?",
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" }
          },
        },
        "---",
        {
          opcode: "addBlocksTo",
          blockType: BlockType.COMMAND,
          text: ["add blocks", "to script [NAME]"],
          branchCount: 1,
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" }
          },
        },
        {
          opcode: "JGreturn",
          text: "return [THING]",
          blockType: BlockType.COMMAND,
          isTerminal: true,
          arguments: {
            THING: { type: ArgumentType.STRING, defaultValue: "1" }
          },
        },
        "---",
        {
          opcode: "scriptData",
          text: "script data",
          blockType: BlockType.REPORTER,
          allowDropAnywhere: true,
          disableMonitor: true
        },
        "---",
        {
          opcode: "runBlocks",
          text: "run script [NAME] in [SPRITE]",
          blockType: BlockType.LOOP,
          branchCount: 0,
          branchIconURI: "",
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" },
            SPRITE: { type: ArgumentType.STRING, menu: "TARGETS" }
          },
        },
        {
          opcode: "runBlocksData",
          text: "run script [NAME] in [SPRITE] with data [DATA]",
          blockType: BlockType.LOOP,
          branchCount: 0,
          branchIconURI: "",
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" },
            SPRITE: { type: ArgumentType.STRING, menu: "TARGETS" },
            DATA: { type: ArgumentType.STRING, defaultValue: "data" }
          },
        },
        "---",
        {
          opcode: "reportBlocks",
          text: "run script [NAME] in [SPRITE]",
          blockType: BlockType.REPORTER,
          allowDropAnywhere: true,
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" },
            SPRITE: { type: ArgumentType.STRING, menu: "TARGETS" }
          },
        },
        {
          opcode: "reportBlocksData",
          text: "run script [NAME] in [SPRITE] with data [DATA]",
          blockType: BlockType.REPORTER,
          allowDropAnywhere: true,
          arguments: {
            NAME: { type: ArgumentType.STRING, defaultValue: "Script1" },
            SPRITE: { type: ArgumentType.STRING, menu: "TARGETS" },
            DATA: { type: ArgumentType.STRING, defaultValue: "data" }
          },
        }
      ],
      menus: {
        TARGETS: { acceptReporters: true, items: "getTargets" }
      },
    };
  }

  getTargets() {
    const spriteNames = [
      { text: "myself", value: "_myself_" },
      { text: "Stage", value: "_stage_" }
    ];
    const targets = this.runtime.targets;
    for (let index = 1; index < targets.length; index++) {
      const target = targets[index];
      if (target.isOriginal) spriteNames.push({
        text: target.getName(), value: target.getName()
      });
    }
    return spriteNames.length > 0 ? spriteNames : [""];
  }

  createScript(args) { this.scripts[Cast.toString(args.NAME)] = { blocks: [] } }

  deleteScript(args) { delete this.scripts[Cast.toString(args.NAME)] }

  deleteAll() { this.scripts = {} }

  allScripts() { return JSON.stringify(Object.keys(this.scripts)) }

  scriptExists(args) { return Cast.toBoolean(this.scripts[args.NAME]) }

  addBlocksTo(args, util) {
    const name = Cast.toString(args.NAME);
    const branch = util.thread.target.blocks.getBranch(util.thread.peekStack(), 1);
    if (branch && this.scripts[name] !== undefined) {
      this.scripts[name].blocks.push({ stack : branch, target : util.target });
    }
  }

  JGreturn(args, util) { util.thread.report = Cast.toString(args.THING) }

  scriptData(args, util) {
    const data = util.thread.scriptData;
    return data ? data : "";
  }

  runBlocksData(args, util) { this.runBlocks(args, util) }
  runBlocks(args, util) {
    const target = args.SPRITE === "_myself_" ? util.target :
      args.SPRITE === "_stage_" ? this.runtime.getTargetForStage() : this.runtime.getSpriteTargetByName(args.SPRITE);
    const name = Cast.toString(args.NAME);
    const data = args.DATA ? Cast.toString(args.DATA) : "";
    if (this.scripts[name] === undefined || !target) return;

    if (util.stackFrame.JGindex === undefined) util.stackFrame.JGindex = 0;
    if (util.stackFrame.JGthread === undefined) util.stackFrame.JGthread = "";
    const blocks = this.scripts[name].blocks;
    const index = util.stackFrame.JGindex;
    const thread = util.stackFrame.JGthread;
    if (!thread && index < blocks.length) {
      const thisStack = blocks[index];
      if (thisStack.target.blocks.getBlock(thisStack.stack) !== undefined) {
        util.stackFrame.JGthread = this.runtime._pushThread(thisStack.stack, thisStack.target, { stackClick: false });
        util.stackFrame.JGthread.scriptData = data;
        util.stackFrame.JGthread.target = target;
        util.stackFrame.JGthread.tryCompile(); // update thread
      }
      util.stackFrame.JGindex = util.stackFrame.JGindex + 1;
    }

    if (util.stackFrame.JGthread && this.runtime.isActiveThread(util.stackFrame.JGthread)) util.startBranch(1, true);
    else util.stackFrame.JGthread = "";
    if (util.stackFrame.JGindex < blocks.length) util.startBranch(1, true);
  }

  reportBlocksData(args, util) { return this.reportBlocks(args, util) || "" }
  reportBlocks(args, util) {
    const target = args.SPRITE === "_myself_" ? util.target :
      args.SPRITE === "_stage_" ? this.runtime.getTargetForStage() : this.runtime.getSpriteTargetByName(args.SPRITE);
    const name = Cast.toString(args.NAME);
    const data = args.DATA ? Cast.toString(args.DATA) : "";
    if (this.scripts[name] === undefined || !target) return;

    if (util.stackFrame.JGindex === undefined) util.stackFrame.JGindex = 0;
    if (util.stackFrame.JGthread === undefined) util.stackFrame.JGthread = "";
    const blocks = this.scripts[name].blocks;
    const index = util.stackFrame.JGindex;
    const thread = util.stackFrame.JGthread;
    if (!thread && index < blocks.length) {
      const thisStack = blocks[index];
      if (thisStack.target.blocks.getBlock(thisStack.stack) !== undefined) {
        util.stackFrame.JGthread = this.runtime._pushThread(thisStack.stack, thisStack.target, { stackClick: false });
        util.stackFrame.JGthread.scriptData = data;
        util.stackFrame.JGthread.target = target;
        util.stackFrame.JGthread.tryCompile(); // update thread
      }
      util.stackFrame.JGindex = util.stackFrame.JGindex + 1;
    }

    if (util.stackFrame.JGthread && this.runtime.isActiveThread(util.stackFrame.JGthread)) util.yield();
    else {
      if (util.stackFrame.JGthread.report !== undefined) {
        util.stackFrame.JGreport = util.stackFrame.JGthread.report;
        util.stackFrame.JGindex = blocks.length + 1;
      }
      util.stackFrame.JGthread = "";
    }
    if (util.stackFrame.JGindex < blocks.length) util.yield();
    return util.stackFrame.JGreport || "";
  }
}

module.exports = JgScriptsBlocks;