-
-
Save phoddie/bade2f7e49f2e4da26877c8f8d380c79 to your computer and use it in GitHub Desktop.
Executing mcrun in the Browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Test</title> | |
<body> | |
<input type="file" id="files" name="files[]" multiple webkitdirectory/> | |
<output id="list"></output> | |
<script type="module"> | |
import toolsEmscripten from './tools.js'; | |
var output = []; | |
function report(text) { | |
output.push('<li>', text, '</li>'); | |
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>'; | |
} | |
function handleFileSelect(evt) { | |
output.length = 0; | |
report("Loading tools..."); | |
toolsEmscripten({ | |
locateFile: () => { | |
return "./tools.wasm" | |
} | |
}) | |
.then(tools => { | |
let files = evt.target.files; // FileList object | |
let c = files.length; | |
let promises = new Array(c); | |
for (let i = 0; i < c; i++) { | |
let file = files[i]; | |
report("Loading " + file.name + "..."); | |
let reader = new FileReader(); | |
promises[i] = new Promise((resolve, reject) => { | |
reader.onerror = () => { | |
reader.abort(); | |
reject(new DOMException("Cannot read " + file.name)); | |
}; | |
reader.onload = () => { | |
resolve({ file:file, buffer:new Uint8Array(reader.result) }); | |
}; | |
reader.readAsArrayBuffer(file); | |
}); | |
} | |
Promise.all(promises).then(results => { | |
tools.FS.mkdir("/mc"); | |
for (let i = 0; i < c; i++) { | |
let result = results[i]; | |
tools.FS.writeFile("/mc/" + result.file.name, result.buffer); | |
} | |
report("mcrun -d /mc/manifest.json"); | |
tools.callMain([ "mcrun", "-d", "/mc/manifest.json" ]); | |
let make = JSON.parse(tools.FS.readFile("/moddable/build/tmp/wasm/debug/mc/make.json", { encoding: 'utf8' })); | |
for (let command of make) { | |
report(command.join(" ")); | |
tools.callMain(command); | |
} | |
report("Downloading /moddable/build/bin/wasm/debug/mc/mc.xsa"); | |
let archive = tools.FS.readFile("/moddable/build/bin/wasm/debug/mc/mc.xsa"); | |
let string = String.fromCharCode.apply(null, new Uint8Array(archive)); | |
var element = document.createElement('a'); | |
element.setAttribute('href', 'data:application/x-binary;base64,' + btoa(string)); | |
element.setAttribute('download', 'mc.xsa'); | |
element.style.display = 'none'; | |
document.body.appendChild(element); | |
element.click(); | |
document.body.removeChild(element); | |
}); | |
}) | |
} | |
document.getElementById('files').addEventListener('change', handleFileSelect, false); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment