-
Notifications
You must be signed in to change notification settings - Fork 2
/
compiler.js
67 lines (62 loc) · 2.33 KB
/
compiler.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
function toFunctionName(str) {
return (/^[a-zA-Z_]/.test(str.replace(/[^a-zA-Z0-9_]/g, '')) ? '' : '_') + str.replace(/[^a-zA-Z0-9_]/g, '');
}
function getCompiledCode() {
javascript.javascriptGenerator.init(workspace);
let datablock_contents = "";
var prereq_contents = "";
let functionPrereqs = [];
state.nodes.forEach(node => {
functionPrereqs = functionPrereqs.concat(PRIMITIVES[node.type].uses);
datablock_contents += PRIMITIVES[node.type].asJavaScript.apply(node, []);
});
workspace.getAllBlocks().forEach(block => {
functionPrereqs = functionPrereqs.concat(getBlockLibs(block));
});
functionPrereqs = [...new Set(functionPrereqs)]; //dedupe the list
functionPrereqs.forEach(fn => {
prereq_contents += getFunctionCode(FUNCTIONS[fn]);
});
//let modCode = javascript.javascriptGenerator.workspaceToCode(workspace);
return `(function EFB2Mod() {
${prereq_contents}
${datablock_contents}
})();
`;
}
function exportMod() {
let output = getCompiledCode()
fileSave(output, "mod.js");
}
var efiBuild = null;
function getEfiBuild() {
return new Promise((res,rej)=>{
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = ".html";
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e) => {
//efiBuild = URL.createObjectURL(new Blob([reader.result], { type: "text/html" }));
efiBuild = reader.result;
res();
};
reader.readAsText(file);
});
fileInput.click();
});
}
async function runMod() {
var url = "data:text/javascript," + encodeURIComponent(getCompiledCode());
if (!efiBuild) {
await getEfiBuild();
}
var insp = document.querySelector("#inspector");
insp.srcdoc = efiBuild + `<script>eaglercraftXOpts.noInitialModGui = true; eaglercraftXOpts.Mods = ["${url}"];</script>`;
if (document.querySelector(".datablock[data-dtype=inspector]")) {
document.querySelector(".datablock[data-dtype=inspector]").click();
}
}
document.querySelector("#export").addEventListener("click", exportMod);
document.querySelector("#run").addEventListener("click", runMod);