-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (166 loc) · 6.12 KB
/
index.js
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
var workspace = globalThis.workspace = Blockly.inject('blockly', {
collapse: true,
comments: true,
css: true,
disable: true,
renderer: 'zelos',
scrollbars: true,
sounds: true,
theme: "dark",
trashcan: true,
readOnly: false,
toolbox: document.querySelector("#toolbox"),
zoom: {
controls: true,
wheel: true,
startScale: 1.0,
maxScale: 3,
minScale: 0.3,
scaleSpeed: 1.2,
pinch: true
},
});
blocklyDeveloperTools();
const oldScrub = javascript.javascriptGenerator.scrub_;
var rippedCode = null; //Used to decompile function definitions later.
javascript.javascriptGenerator.scrub_ = function (...args) {
rippedCode = args[1];
return oldScrub.apply(this, args);
}
var handlers = {};
var handlerMapDict = {};
function getHandlers(type) {
return handlers["handle_" + type] || [];
}
function getHandler(type, name) {
return handlerMapDict["handle_" + type]?.[name] || null;
}
function depsgraph_search(blck, enounteredFunctions, usedVariableSet, procMap) {
const FN_PROCCODES = ["procedures_callreturn", "procedures_callnoreturn"];
const FN_DEFCODES = ["procedures_defreturn", "procedures_defnoreturn"];
enounteredFunctions ||= new Set();
usedVariableSet ||= new Set();
procMap ||= Object.fromEntries(workspace.getAllBlocks().filter(x=>FN_DEFCODES.includes(x.type)).map(x=>[x.getProcedureDef()[0], x]));
var descendants = blck.getDescendants(true);
for (let i = 0; i < descendants.length; i++) {
const descendant = descendants[i];
if (FN_PROCCODES.includes(descendant.type) && !enounteredFunctions.has(descendant.getProcedureCall())) {
var proccode = descendant.getProcedureCall();
enounteredFunctions.add(proccode);
depsgraph_search(procMap[proccode], enounteredFunctions, usedVariableSet, procMap);
continue;
}
descendant.getVars().forEach(varId => {
usedVariableSet.add(varId);
});
}
return {
usedVariableSet,
enounteredFunctions,
procMap
}
}
function getProcedureCode(name, procMap) {
javascript.javascriptGenerator.blockToCode(procMap[name]); //for some *stupid* reason, is hardcoded to always return null.
return rippedCode;
}
function getHandlerCode(type, tag, defaultArgs) {
var handler = getHandler(type, tag);
if (!handler) { return { code: "", args: defaultArgs } };
var { usedVariableSet, enounteredFunctions, procMap } = depsgraph_search(handler);
var generatedCode = javascript.javascriptGenerator.forBlock[handler.type].apply(handler, []);
var variableCode = [...usedVariableSet].map(varId => { return javascript.javascriptGenerator.getVariableName(varId) }).join(",");
variableCode = variableCode ? ("var " + variableCode + ";") : "";
var procedureCode = [...enounteredFunctions].map(x => getProcedureCode(x, procMap)).join(";");
generatedCode.code = variableCode + procedureCode + generatedCode.code;
return generatedCode;
}
const supportedEvents = new Set([
Blockly.Events.BLOCK_CHANGE,
Blockly.Events.BLOCK_CREATE,
Blockly.Events.BLOCK_DELETE
]);
function updateHandlers() {
if (workspace.isDragging()) return;
handlers = {};
handlerMapDict = {};
workspace.getAllBlocks().forEach(block => {
var id = block.getFieldValue("ID");
if (id === "None") {
return;
}
if (!handlers[block.type]) {
handlers[block.type] = [id];
handlerMapDict[block.type] = Object.fromEntries([[id, block]]);
} else {
if (handlers[block.type].includes(id)) {
return;
}
handlers[block.type].push(id);
handlerMapDict[block.type][id] = block;
}
});
document.querySelectorAll(".handler_select").forEach(x => {
x.updateHandlerList();
});
}
workspace.addChangeListener(updateHandlers);
var state = globalThis.state = {
nodes: [
getPrimitive("metadata"),
getPrimitive("icon"),
]
};
function updatePropsUI() {
}
function reloadUI(sel) {
if (!state.nodes.includes(sel)) {
sel = null;
}
document.querySelector("#propnav").innerHTML = "";
document.querySelectorAll(".datablock").forEach(elem => {
elem.remove()
});
document.querySelector("#search").value = "";
var datablockContainer = document.querySelector("#datablock_container");
state.nodes.forEach((node, index) => {
var datablock = document.createElement("span");
datablock.datablock = node;
datablock.classList.add("datablock");
if (node === sel) {
datablock.classList.add("selected");
}
datablock.addEventListener("click", (e) => {
document.querySelectorAll(".datablock.selected").forEach(x => x.classList.remove("selected"));
datablock.classList.add("selected");
editObject(node, datablock);
});
var h4 = document.createElement("h4");
h4.innerText = node.name;
datablock.appendChild(h4);
datablock.appendChild(document.createElement("br"));
var type = document.createElement("i");
type.innerText = "Type: " + node.type;
datablock.appendChild(type);
datablock.appendChild(document.createElement("br"));
datablock.appendChild(document.createElement("br"));
var controls = document.createElement("div");
controls.classList.add("controls");
var deleteButton = document.createElement("button");
deleteButton.innerText = "Delete";
deleteButton.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
state.nodes.splice(index, 1);
reloadUI();
});
controls.appendChild(deleteButton);
datablock.appendChild(controls);
datablockContainer.appendChild(datablock);
});
}
document.querySelector("#newdatablock").addEventListener("click", (e) => {
state.nodes.push(getPrimitive(document.querySelector("#addtype").value));
reloadUI(document.querySelector(".datablock.selected")?.datablock);
});
reloadUI();