This repository has been archived by the owner on May 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
luna-utils.js
90 lines (81 loc) · 3.15 KB
/
luna-utils.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
var Module;
// From emscripten: in order to reference the preexisting Module var or create it if needed.
if (!Module) Module = (typeof Module !== 'undefined' ? Module : null) || {};
Module['preRun'] = function() {
callLuna = function(inputFileName, tnsFileName)
{
return (function MyCallMain(args) {
args = args || [];
ensureInitRuntime();
var argc = args.length + 1;
function pad() {
for (var i = 0; i < 4 - 1; i++) {
argv.push(0);
}
}
var argv = [allocate(intArrayFromString(Module['thisProgram']), 'i8', ALLOC_NORMAL)];
pad();
for (var i = 0; i < argc - 1; i = i + 1) {
argv.push(allocate(intArrayFromString(args[i]), 'i8', ALLOC_NORMAL));
pad();
}
argv.push(0);
argv = allocate(argv, 'i32', ALLOC_NORMAL);
return Module['_main'](argc, argv, 0);
})([inputFileName, tnsFileName]);
};
makeAndDownloadTNS = function(luaScript)
{
const luaFileName = 'myScript.lua';
const tnsFileName = 'myScript.tns';
FS.writeFile(luaFileName, luaScript);
try { FS.unlink(tnsFileName); } catch (e){}
const retVal = callLuna(luaFileName, tnsFileName);
if (retVal !== 0) {
alert('Oops, something went wrong generating the .tns file (see console)');
return;
}
const file = FS.readFile(tnsFileName, {encoding: 'binary'});
if (file) {
const blob = new Blob([file], {type: 'application/octet-stream'});
window['saveAs'](blob, tnsFileName);
} else {
alert('Oops, something went wrong retrieving the generated .tns file :(');
}
};
fileLoaded = function(event, filename)
{
if (event.target.readyState == FileReader.DONE)
{
var tnsName = filename.replace(/\.(lua|xml)$/, '.tns');
FS.writeFile(filename, new Uint8Array(event.target.result), {encoding: 'binary'});
try { FS.unlink(tnsName); } catch (e){}
var retVal = callLuna(filename, tnsName);
if (retVal !== 0) {
alert('Oops, something went wrong generating the .tns file (see console)');
return;
}
var file = FS.readFile(tnsName, {encoding: 'binary'});
if (file) {
var blob = new Blob([file], {type: 'application/octet-stream'});
window['saveAs'](blob, tnsName);
} else {
alert('Oops, something went wrong retrieving the generated .tns file :(');
}
}
};
fileLoad = function(event)
{
var file = event.target.files[0];
if (!file || (file.name.substr(-4) !== '.lua' && file.name.substr(-4) !== '.xml'))
{
alert('Error: a .lua script or .xml file is needed');
return;
}
var reader = new FileReader();
reader.onloadend = function(event) {
fileLoaded(event, file.name);
};
reader.readAsArrayBuffer(file);
};
}; // preRun function