-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcompiler.js
90 lines (85 loc) · 3.14 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 => {
delete node._deps;
node._deps = PRIMITIVES[node.type].getDependencies.apply(node, []);
});
state.nodes.sort((a, b) => {
var bDepends = b._deps.includes(a);
var aDepends = a._deps.includes(b);
if (bDepends && aDepends) {
alert(`Failed to compile:\nCircular dependency between ${a.name} and ${b.name}`);
}
if (bDepends) {
return -1;
}
if (aDepends) {
return 1;
}
return 0;
});
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) => {
if (!reader.result.includes("__eaglerforgeinjector_installation_flag__")) {
if (window.confirm("This file is not a EaglerForgeInjector build, or is too old.\nOpen the EaglerForgeInjector page?")) {
window.open("https://eaglerforge.github.io/EaglerForgeInjector");
}
rej();
}
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);