soiz1 commited on
Commit
15f51c7
·
verified ·
1 Parent(s): 77b3091

Update src/engine/runtime.js

Browse files
Files changed (1) hide show
  1. src/engine/runtime.js +61 -45
src/engine/runtime.js CHANGED
@@ -26,6 +26,8 @@ const Color = require('../util/color');
26
  const TabManager = require('../extension-support/pm-tab-manager');
27
  const ModalManager = require('../extension-support/pm-modal-manager');
28
  const MathUtil = require('../util/math-util');
 
 
29
 
30
  // Virtual I/O devices.
31
  const Clock = require('../io/clock');
@@ -626,6 +628,15 @@ class Runtime extends EventEmitter {
626
  * @type {Object}
627
  */
628
  this.variables = Object.create(null);
 
 
 
 
 
 
 
 
 
629
  }
630
 
631
  /**
@@ -1240,7 +1251,7 @@ class Runtime extends EventEmitter {
1240
  registerExtensionAudioContext(extensionId, audioContext, gainNode) {
1241
  if (typeof extensionId !== "string") throw new TypeError('Extension ID must be string');
1242
  if (!extensionId) throw new Error('No extension ID specified'); // empty string
1243
-
1244
  const obj = {};
1245
  if (audioContext) {
1246
  obj.audioContext = audioContext;
@@ -1311,7 +1322,7 @@ class Runtime extends EventEmitter {
1311
  categoryInfo.color2 = defaultExtensionColors[1];
1312
  categoryInfo.color3 = defaultExtensionColors[2];
1313
  }
1314
-
1315
  if (extensionInfo.isDynamic) {
1316
  categoryInfo.isDynamic = extensionInfo.isDynamic;
1317
  categoryInfo.orderBlocks = extensionInfo.orderBlocks;
@@ -1488,29 +1499,29 @@ class Runtime extends EventEmitter {
1488
  type: menuId,
1489
  inputsInline: true,
1490
  output: 'String',
1491
- colour: menuInfo.isTypeable
1492
- ? '#FFFFFF'
1493
  : categoryInfo.color1,
1494
- colourSecondary: menuInfo.isTypeable
1495
- ? '#FFFFFF'
1496
  : categoryInfo.color2,
1497
- colourTertiary: menuInfo.isTypeable
1498
- ? '#FFFFFF'
1499
  : categoryInfo.color3,
1500
  outputShape: menuInfo.acceptReporters || menuInfo.isTypeable ?
1501
  ScratchBlocksConstants.OUTPUT_SHAPE_ROUND : ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE,
1502
  args0: [
1503
- (typeof menuInfo.variableType !== 'undefined' ?
1504
  {
1505
  type: 'field_variable',
1506
  name: menuName,
1507
- variableTypes: [menuInfo.variableType === 'scalar'
1508
- ? Variable.SCALAR_TYPE
1509
  : menuInfo.variableType]
1510
- } : (menuInfo.isTypeable ?
1511
  {
1512
- type: menuInfo.isNumeric
1513
- ? 'field_numberdropdown'
1514
  : 'field_textdropdown',
1515
  name: menuName,
1516
  options: menuItems
@@ -1587,7 +1598,7 @@ class Runtime extends EventEmitter {
1587
  return this._convertSeparatorForScratchBlocks(blockInfo);
1588
  }
1589
 
1590
- if (blockInfo.blockType === BlockType.LABEL)
1591
  return this._convertLabelForScratchBlocks(blockInfo);
1592
 
1593
  if (blockInfo.blockType === BlockType.BUTTON) {
@@ -1769,11 +1780,12 @@ class Runtime extends EventEmitter {
1769
  blockJSON.output = blockInfo.forceOutputType;
1770
  }
1771
 
1772
- const mutation = blockInfo.isDynamic
1773
- ? `<mutation blockInfo="${xmlEscape.escapeAttribute(JSON.stringify(blockInfo))}"/>`
1774
  : '';
1775
  const inputs = context.inputList.join('');
1776
- const blockXML = `<block type="${xmlEscape.escapeAttribute(extendedOpcode)}">${mutation}${inputs}</block>`;
 
1777
 
1778
  return {
1779
  info: context.blockInfo,
@@ -1822,10 +1834,10 @@ class Runtime extends EventEmitter {
1822
  _convertButtonForScratchBlocks (buttonInfo) {
1823
  const extensionMessageContext = this.makeMessageContextForTarget();
1824
  const buttonText = xmlEscape.escapeAttribute(maybeFormatMessage(buttonInfo.text, extensionMessageContext));
1825
- const callback = xmlEscape.escapeAttribute(buttonInfo.opcode
1826
- ? buttonInfo.opcode
1827
  : buttonInfo.func);
1828
-
1829
  return {
1830
  info: buttonInfo,
1831
  xml: `<button text="${buttonText}" callbackKey="${callback}"></button>`
@@ -1863,7 +1875,7 @@ class Runtime extends EventEmitter {
1863
  }
1864
 
1865
  /**
1866
- * Helper for _convertPlaceholdes which handles variable dropdowns
1867
  * which are a specialized case of block "arguments".
1868
  * @param {object} argInfo Metadata about the variable dropdown
1869
  * @return {object} JSON blob for a scratch-blocks variable field.
@@ -1962,15 +1974,15 @@ class Runtime extends EventEmitter {
1962
  argJSON.type = isVariableGetter
1963
  ? 'field_variable_getter'
1964
  : 'field_variable';
1965
- argJSON.variableTypes = [menuInfo.variableType === 'scalar'
1966
- ? Variable.SCALAR_TYPE
1967
  : menuInfo.variableType];
1968
  argJSON.variableType = argJSON.variableTypes[0];
1969
  valueName = null;
1970
  shadowType = null;
1971
  fieldName = placeholder;
1972
- variableType = menuInfo.variableType === 'scalar'
1973
- ? Variable.SCALAR_TYPE
1974
  : menuInfo.variableType
1975
  const defaultVar = argInfo.defaultValue ?? [];
1976
  variableID = defaultVar[0];
@@ -1988,8 +2000,8 @@ class Runtime extends EventEmitter {
1988
  fieldName = (argTypeInfo.shadow && argTypeInfo.shadow.fieldName) || null;
1989
  }
1990
  // TODO: Allow fillIn to work with non-shadow.
1991
- if (argInfo.fillIn/* && argInfo.fillInShadow*/) {
1992
- shadowType = `${context.categoryInfo.id}_${argInfo.fillIn}`;
1993
  }/* else if (argInfo.fillIn) {
1994
  blockType = `${context.categoryInfo.id}_${argInfo.fillIn}`;
1995
  }*/
@@ -2004,7 +2016,7 @@ class Runtime extends EventEmitter {
2004
  if (shadowType) {
2005
  context.inputList.push(`<shadow type="${xmlEscape.escapeAttribute(shadowType)}">`);
2006
  }
2007
-
2008
  // TODO: This doesnt seem to work properly with fillIn. Default to shadow for now.
2009
  if (blockType) {
2010
  context.inputList.push(`<block type="${xmlEscape.escapeAttribute(blockType)}">`);
@@ -2027,7 +2039,7 @@ class Runtime extends EventEmitter {
2027
  // eslint-disable-next-line max-len
2028
  context.inputList.push(`<field name="${fieldName}" id="${variableID}" variableType="${variableType}">${variableName}</field>`);
2029
  }
2030
-
2031
  if (blockType) {
2032
  context.inputList.push('</block>');
2033
  }
@@ -2075,7 +2087,7 @@ class Runtime extends EventEmitter {
2075
  return blockFilterIncludesTarget && !block.info.hideFromPalette;
2076
  });
2077
 
2078
- orderBlocks = orderBlocks
2079
  ? orderBlocks
2080
  : blocks => blocks;
2081
 
@@ -2274,7 +2286,7 @@ class Runtime extends EventEmitter {
2274
  throw new TypeError('deserialize must be of type function');
2275
  }
2276
  this.serializers[id] = {
2277
- serialize,
2278
  deserialize
2279
  };
2280
  }
@@ -2572,7 +2584,7 @@ class Runtime extends EventEmitter {
2572
 
2573
  for (const opts in optMatchFields) {
2574
  if (!optMatchFields.hasOwnProperty(opts)) continue;
2575
- optMatchFields[opts] = optMatchFields[opts].toUpperCase();
2576
  }
2577
 
2578
  // tw: By assuming that all new threads will not interfere with eachother, we can optimize the loops
@@ -2702,7 +2714,7 @@ class Runtime extends EventEmitter {
2702
  const newVar = this.newVariableInstance(variable.type, ...variable.value);
2703
  // variable type doesnt exist, remove variable entirely
2704
  if (newVar.mustRecreate) {
2705
- delete target.variable[varId];
2706
  continue;
2707
  }
2708
  target.variables[varId] = newVar;
@@ -3002,11 +3014,11 @@ class Runtime extends EventEmitter {
3002
 
3003
  let forceUpd = false;
3004
  // if a custom type set _monitorUpToDate to false on an existing instance, we need to report that update to the gui
3005
- if (this._monitorState.some(item =>
3006
- typeof item.get('value') === 'object' &&
3007
- '_monitorUpToDate' in item.get('value') &&
3008
  !item.get('value')._monitorUpToDate
3009
- )) {
3010
  const old = this._monitorState;
3011
  // make a new instance so redux detects this as different later on
3012
  this._monitorState = this._monitorState.toOrderedMap();
@@ -3027,7 +3039,7 @@ class Runtime extends EventEmitter {
3027
  if (this.interpolationEnabled) {
3028
  this._lastStepTime = Date.now();
3029
  }
3030
-
3031
  // pm: RUNTIME_STEP_END runs after AFTER_EXECUTE
3032
  this.emit(Runtime.RUNTIME_STEP_END);
3033
  }
@@ -3662,7 +3674,7 @@ class Runtime extends EventEmitter {
3662
 
3663
  /**
3664
  * Report that the project has loaded in the Virtual Machine.
3665
- * and also handle the parsing of custom values to allow for
3666
  * minimal code when making cross-target refences
3667
  */
3668
  emitProjectLoaded () {
@@ -3761,12 +3773,12 @@ class Runtime extends EventEmitter {
3761
  scale: 1
3762
  };
3763
  }
3764
- this.cameraStates[screen] = state =
3765
  Object.assign(this.cameraStates[screen], state);
3766
  if (!silent ?? state.silent) this.emitCameraChanged(screen);
3767
  }
3768
  emitCameraChanged(screen) {
3769
- for (let i = 0; i < this.targets.length; i++)
3770
  if (this.targets[i].cameraBound === screen)
3771
  this.targets[i].cameraUpdateEvent();
3772
  this.emit(Runtime.CAMERA_CHANGED, screen);
@@ -3825,10 +3837,14 @@ class Runtime extends EventEmitter {
3825
  const block = categoryInfo.blocks.find(b => b.info.opcode === opcode);
3826
  if (!block) return;
3827
 
 
 
 
3828
  // TODO: we may want to format the label in a locale-specific way.
3829
  return {
3830
- category: 'extension', // This assumes that all extensions have the same monitor color.
3831
- label: `${categoryInfo.name}: ${block.info.text}`
 
3832
  };
3833
  }
3834
 
@@ -3972,4 +3988,4 @@ class Runtime extends EventEmitter {
3972
  * @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
3973
  */
3974
 
3975
- module.exports = Runtime;
 
26
  const TabManager = require('../extension-support/pm-tab-manager');
27
  const ModalManager = require('../extension-support/pm-modal-manager');
28
  const MathUtil = require('../util/math-util');
29
+ const Cast = require('../util/cast');
30
+ const ExtensionStorage = require('../util/deprecated-extension-storage.js');
31
 
32
  // Virtual I/O devices.
33
  const Clock = require('../io/clock');
 
628
  * @type {Object}
629
  */
630
  this.variables = Object.create(null);
631
+
632
+ /**
633
+ * Part of a recreation of the TurboWarp extensionStorage
634
+ * API. The only real reason this is here is to make sure
635
+ * that extensions that only implement TurboWarp's store
636
+ * method don't break in PenguinMod.
637
+ * @type {Object<string, Object>}
638
+ */
639
+ this.extensionStorage = ExtensionStorage();
640
  }
641
 
642
  /**
 
1251
  registerExtensionAudioContext(extensionId, audioContext, gainNode) {
1252
  if (typeof extensionId !== "string") throw new TypeError('Extension ID must be string');
1253
  if (!extensionId) throw new Error('No extension ID specified'); // empty string
1254
+
1255
  const obj = {};
1256
  if (audioContext) {
1257
  obj.audioContext = audioContext;
 
1322
  categoryInfo.color2 = defaultExtensionColors[1];
1323
  categoryInfo.color3 = defaultExtensionColors[2];
1324
  }
1325
+
1326
  if (extensionInfo.isDynamic) {
1327
  categoryInfo.isDynamic = extensionInfo.isDynamic;
1328
  categoryInfo.orderBlocks = extensionInfo.orderBlocks;
 
1499
  type: menuId,
1500
  inputsInline: true,
1501
  output: 'String',
1502
+ colour: menuInfo.isTypeable
1503
+ ? '#FFFFFF'
1504
  : categoryInfo.color1,
1505
+ colourSecondary: menuInfo.isTypeable
1506
+ ? '#FFFFFF'
1507
  : categoryInfo.color2,
1508
+ colourTertiary: menuInfo.isTypeable
1509
+ ? '#FFFFFF'
1510
  : categoryInfo.color3,
1511
  outputShape: menuInfo.acceptReporters || menuInfo.isTypeable ?
1512
  ScratchBlocksConstants.OUTPUT_SHAPE_ROUND : ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE,
1513
  args0: [
1514
+ (typeof menuInfo.variableType !== 'undefined' ?
1515
  {
1516
  type: 'field_variable',
1517
  name: menuName,
1518
+ variableTypes: [menuInfo.variableType === 'scalar'
1519
+ ? Variable.SCALAR_TYPE
1520
  : menuInfo.variableType]
1521
+ } : (menuInfo.isTypeable ?
1522
  {
1523
+ type: menuInfo.isNumeric
1524
+ ? 'field_numberdropdown'
1525
  : 'field_textdropdown',
1526
  name: menuName,
1527
  options: menuItems
 
1598
  return this._convertSeparatorForScratchBlocks(blockInfo);
1599
  }
1600
 
1601
+ if (blockInfo.blockType === BlockType.LABEL)
1602
  return this._convertLabelForScratchBlocks(blockInfo);
1603
 
1604
  if (blockInfo.blockType === BlockType.BUTTON) {
 
1780
  blockJSON.output = blockInfo.forceOutputType;
1781
  }
1782
 
1783
+ const mutation = blockInfo.isDynamic
1784
+ ? `<mutation blockInfo="${xmlEscape.escapeAttribute(JSON.stringify(blockInfo))}"/>`
1785
  : '';
1786
  const inputs = context.inputList.join('');
1787
+ const blockId = blockInfo.isSpriteSpecific ? `id="${xmlEscape.escapeAttribute(this.vm.editingTarget.id + '_' + extendedOpcode)}"` : '';
1788
+ const blockXML = `<block type="${xmlEscape.escapeAttribute(extendedOpcode)}" ${blockId}>${mutation}${inputs}</block>`;
1789
 
1790
  return {
1791
  info: context.blockInfo,
 
1834
  _convertButtonForScratchBlocks (buttonInfo) {
1835
  const extensionMessageContext = this.makeMessageContextForTarget();
1836
  const buttonText = xmlEscape.escapeAttribute(maybeFormatMessage(buttonInfo.text, extensionMessageContext));
1837
+ const callback = xmlEscape.escapeAttribute(buttonInfo.opcode
1838
+ ? buttonInfo.opcode
1839
  : buttonInfo.func);
1840
+
1841
  return {
1842
  info: buttonInfo,
1843
  xml: `<button text="${buttonText}" callbackKey="${callback}"></button>`
 
1875
  }
1876
 
1877
  /**
1878
+ * Helper for _convertPlaceholdes which handles variable dropdowns
1879
  * which are a specialized case of block "arguments".
1880
  * @param {object} argInfo Metadata about the variable dropdown
1881
  * @return {object} JSON blob for a scratch-blocks variable field.
 
1974
  argJSON.type = isVariableGetter
1975
  ? 'field_variable_getter'
1976
  : 'field_variable';
1977
+ argJSON.variableTypes = [menuInfo.variableType === 'scalar'
1978
+ ? Variable.SCALAR_TYPE
1979
  : menuInfo.variableType];
1980
  argJSON.variableType = argJSON.variableTypes[0];
1981
  valueName = null;
1982
  shadowType = null;
1983
  fieldName = placeholder;
1984
+ variableType = menuInfo.variableType === 'scalar'
1985
+ ? Variable.SCALAR_TYPE
1986
  : menuInfo.variableType
1987
  const defaultVar = argInfo.defaultValue ?? [];
1988
  variableID = defaultVar[0];
 
2000
  fieldName = (argTypeInfo.shadow && argTypeInfo.shadow.fieldName) || null;
2001
  }
2002
  // TODO: Allow fillIn to work with non-shadow.
2003
+ if (argInfo.fillIn || argInfo.fillInGlobal/* && argInfo.fillInShadow*/) {
2004
+ shadowType = argInfo.fillInGlobal || `${context.categoryInfo.id}_${argInfo.fillIn}`;
2005
  }/* else if (argInfo.fillIn) {
2006
  blockType = `${context.categoryInfo.id}_${argInfo.fillIn}`;
2007
  }*/
 
2016
  if (shadowType) {
2017
  context.inputList.push(`<shadow type="${xmlEscape.escapeAttribute(shadowType)}">`);
2018
  }
2019
+
2020
  // TODO: This doesnt seem to work properly with fillIn. Default to shadow for now.
2021
  if (blockType) {
2022
  context.inputList.push(`<block type="${xmlEscape.escapeAttribute(blockType)}">`);
 
2039
  // eslint-disable-next-line max-len
2040
  context.inputList.push(`<field name="${fieldName}" id="${variableID}" variableType="${variableType}">${variableName}</field>`);
2041
  }
2042
+
2043
  if (blockType) {
2044
  context.inputList.push('</block>');
2045
  }
 
2087
  return blockFilterIncludesTarget && !block.info.hideFromPalette;
2088
  });
2089
 
2090
+ orderBlocks = orderBlocks
2091
  ? orderBlocks
2092
  : blocks => blocks;
2093
 
 
2286
  throw new TypeError('deserialize must be of type function');
2287
  }
2288
  this.serializers[id] = {
2289
+ serialize,
2290
  deserialize
2291
  };
2292
  }
 
2584
 
2585
  for (const opts in optMatchFields) {
2586
  if (!optMatchFields.hasOwnProperty(opts)) continue;
2587
+ optMatchFields[opts] = Cast.toString(optMatchFields[opts]).toUpperCase();
2588
  }
2589
 
2590
  // tw: By assuming that all new threads will not interfere with eachother, we can optimize the loops
 
2714
  const newVar = this.newVariableInstance(variable.type, ...variable.value);
2715
  // variable type doesnt exist, remove variable entirely
2716
  if (newVar.mustRecreate) {
2717
+ delete target.variables[varId];
2718
  continue;
2719
  }
2720
  target.variables[varId] = newVar;
 
3014
 
3015
  let forceUpd = false;
3016
  // if a custom type set _monitorUpToDate to false on an existing instance, we need to report that update to the gui
3017
+ if (this._monitorState.some(item =>
3018
+ typeof item.get('value') === 'object' &&
3019
+ '_monitorUpToDate' in item.get('value') &&
3020
  !item.get('value')._monitorUpToDate
3021
+ )) {
3022
  const old = this._monitorState;
3023
  // make a new instance so redux detects this as different later on
3024
  this._monitorState = this._monitorState.toOrderedMap();
 
3039
  if (this.interpolationEnabled) {
3040
  this._lastStepTime = Date.now();
3041
  }
3042
+
3043
  // pm: RUNTIME_STEP_END runs after AFTER_EXECUTE
3044
  this.emit(Runtime.RUNTIME_STEP_END);
3045
  }
 
3674
 
3675
  /**
3676
  * Report that the project has loaded in the Virtual Machine.
3677
+ * and also handle the parsing of custom values to allow for
3678
  * minimal code when making cross-target refences
3679
  */
3680
  emitProjectLoaded () {
 
3773
  scale: 1
3774
  };
3775
  }
3776
+ this.cameraStates[screen] = state =
3777
  Object.assign(this.cameraStates[screen], state);
3778
  if (!silent ?? state.silent) this.emitCameraChanged(screen);
3779
  }
3780
  emitCameraChanged(screen) {
3781
+ for (let i = 0; i < this.targets.length; i++)
3782
  if (this.targets[i].cameraBound === screen)
3783
  this.targets[i].cameraUpdateEvent();
3784
  this.emit(Runtime.CAMERA_CHANGED, screen);
 
3837
  const block = categoryInfo.blocks.find(b => b.info.opcode === opcode);
3838
  if (!block) return;
3839
 
3840
+ const categoryInstance = this[`ext_${category}`];
3841
+ let labelFn = block.info.labelFn ? categoryInstance[block.info.labelFn] : undefined;
3842
+
3843
  // TODO: we may want to format the label in a locale-specific way.
3844
  return {
3845
+ category: block.info.color1 ?? categoryInfo.color1 ?? "extension",
3846
+ label: block.info.label ?? `${categoryInfo.name}: ${block.info.text}`,
3847
+ labelFn,
3848
  };
3849
  }
3850
 
 
3988
  * @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
3989
  */
3990
 
3991
+ module.exports = Runtime;