File size: 4,834 Bytes
76cc654 |
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 |
const log = require("../util/log");
const switches = {};
const parser = new DOMParser();
const define_error_noop = (msg) => {
log.error(msg);
return {
msg,
isNoop: true,
isError: true,
};
};
function get_extension_switches(id, blocks) {
let _switches = {};
for (let block of blocks) {
var blockswitches = block.info.switches;
if (!blockswitches) continue;
let opcode = block.info.opcode;
_switches[opcode] = blockswitches.map(current => {
switch (typeof current) {
case "object":
break;
case "string":
current = { opcode: current };
break;
default:
return define_error_noop((typeof current) + " disallowed");
}
if (current.isNoop) {
return {
isNoop: true,
msg: current.overwriteText ?? block.info.switchText ?? block.info.text
};
}
if (!current.opcode) {
return define_error_noop("No defined opcode");
}
let get_block = blocks.find(e => e.info.opcode === current.opcode);
if (!get_block) {
return define_error_noop(`Block ${current.opcode} doesn't exist`);
}
let createInputs = {};
let currargs = current.createArguments ?? {};
parser.parseFromString(get_block.xml, "text/xml")
.querySelectorAll(`[type="${get_block.json.type}"] > value`)
.forEach(el => {
let name = el.getAttribute("name");
if (
!!block.info.arguments[name]
&& !(current.remapArguments ?? {})[name]
) return;
if (Object.values(current.remapArguments ?? {}).includes(name)) return;
let shadowType = el.getElementsByTagName("shadow")[0].getAttribute("type");
let value = (currargs[name] ?? get_block.info.arguments[name].defaultValue ?? "").toString();
createInputs[name] = {
shadowType,
value
};
});
const splitInputs = Object.keys(block.info.arguments)
.filter(arg =>
!!get_block.info.arguments[arg]
&& !!(current.remapArguments ?? {})[arg]
&& !Object.values(current.remapArguments ?? {}).includes(arg)
);
const remapShadowType = {};
parser.parseFromString(block.xml, "text/xml")
.querySelectorAll(`[type="${block.json.type}"] > value`)
.forEach(el => {
let name = el.getAttribute("name");
if ((current.remapArguments ?? {})[name]) name = current.remapArguments[name];
if (!get_block.info.arguments[name]) return;
let shadowType = el.querySelector("shadow")
if (!shadowType) return;
shadowType = shadowType.getAttribute("type");
remapShadowType[name] = shadowType;
});
parser.parseFromString(get_block.xml, "text/xml")
.querySelectorAll(`[type="${get_block.json.type}"] > value`)
.forEach(el => {
let name = el.getAttribute("name");
if (!remapShadowType[name]) return;
let shadowType = el.querySelector("shadow");
if (!shadowType) return;
shadowType = shadowType.getAttribute("type");
if (remapShadowType[name] == shadowType) {
delete remapShadowType[name];
return;
}
remapShadowType[name] = shadowType;
});
return {
opcode: `${id}_${current.opcode}`,
msg: current.overwriteText ?? get_block.info.switchText ?? get_block.info.text,
mapFieldValues: current.remapMenus ?? {},
remapInputName: current.remapArguments ?? {},
createInputs,
splitInputs,
remapShadowType,
};
});
}
return _switches;
}
function getSwitches({runtime}) {
for (let ext of runtime._blockInfo) {
if (ext.id in switches) continue;
switches[ext.id] = get_extension_switches(ext.id, ext.blocks);
}
return switches;
}
module.exports = getSwitches;
module.exports.get_extension_switches = get_extension_switches;
module.exports.noopSwitch = { isNoop: true };
|