| 1 |
//Module.arguments = ["--debug", "--window"]; |
| 2 |
Module['noInitialRun'] = true; |
| 3 |
|
| 4 |
function loadChildScript(name, then) { |
| 5 |
var js = document.createElement('script'); |
| 6 |
js.onerror = function(err) { |
| 7 |
console.log(err); |
| 8 |
Module.print("Could not download " + name); |
| 9 |
Module.setStatus("Missing data file!"); |
| 10 |
}; |
| 11 |
if (then) js.onload = then; |
| 12 |
js.src = name; |
| 13 |
document.body.appendChild(js); |
| 14 |
} |
| 15 |
|
| 16 |
Module['preInit'] = function() { |
| 17 |
// populate savegames |
| 18 |
Module['addRunDependency']('fs_dotdink'); |
| 19 |
try { |
| 20 |
FS.mkdir('/home/web_user/.dink'); |
| 21 |
FS.mount(IDBFS, {}, '/home/web_user/.dink'); |
| 22 |
} catch(e) { |
| 23 |
Module.print("Could not create ~/.dink/"); |
| 24 |
} |
| 25 |
FS.syncfs(true, function(err) { |
| 26 |
Module['removeRunDependency']('fs_dotdink'); |
| 27 |
if (err) { console.trace(); console.log(err); } |
| 28 |
}); |
| 29 |
} |
| 30 |
|
| 31 |
Module['onRuntimeInitialized'] = function() { |
| 32 |
document.getElementById('ID_ScriptInstallDecoy').disabled = 0; |
| 33 |
document.getElementById('ID_SavegamesImportDecoy').disabled = 0; |
| 34 |
document.getElementById('ID_SavegamesExport').disabled = 0; |
| 35 |
|
| 36 |
// music can be added after the game starts |
| 37 |
loadChildScript('/freedink-data-bgm-ogg-data.js', |
| 38 |
function() { Module.setStatus('Downloading music...'); }); |
| 39 |
|
| 40 |
_GET = {}; |
| 41 |
if (location.search.length > 0) { |
| 42 |
location.search.substr(1).split('&').forEach(function(item) { |
| 43 |
_GET[item.split("=")[0]] = item.split("=")[1] |
| 44 |
}); |
| 45 |
} |
| 46 |
//var query_string = '?'; |
| 47 |
//for (i in _GET) { query_string += i+'='+_GET[i]+'&'; } |
| 48 |
//query_string = query_string.slice(0, -1); |
| 49 |
|
| 50 |
if (_GET['dmod']) { |
| 51 |
if (!_GET['dmod'].startsWith('http://') && !_GET['dmod'].startsWith('http://')) |
| 52 |
_GET['dmod'] = 'https://' + _GET['dmod'] |
| 53 |
var xhr = new XMLHttpRequest(); |
| 54 |
xhr.open('GET', _GET['dmod'], true); |
| 55 |
xhr.responseType = 'arraybuffer'; |
| 56 |
xhr.onprogress = function(event) { |
| 57 |
var url = _GET['dmod']; |
| 58 |
var size = -1; |
| 59 |
if (event.total) size = event.total; |
| 60 |
if (event.loaded) { |
| 61 |
if (!xhr.addedTotal) { |
| 62 |
xhr.addedTotal = true; |
| 63 |
if (!Module.dataFileDownloads) Module.dataFileDownloads = {}; |
| 64 |
Module.dataFileDownloads[url] = { |
| 65 |
loaded: event.loaded, |
| 66 |
total: size |
| 67 |
}; |
| 68 |
} else { |
| 69 |
Module.dataFileDownloads[url].loaded = event.loaded; |
| 70 |
} |
| 71 |
var total = 0; |
| 72 |
var loaded = 0; |
| 73 |
var num = 0; |
| 74 |
for (var download in Module.dataFileDownloads) { |
| 75 |
var data = Module.dataFileDownloads[download]; |
| 76 |
total += data.total; |
| 77 |
loaded += data.loaded; |
| 78 |
num++; |
| 79 |
} |
| 80 |
total = Math.ceil(total * Module.expectedDataFileDownloads/num); |
| 81 |
if (Module['setStatus']) Module['setStatus']('Downloading D-Mod... (' + loaded + '/' + total + ')'); |
| 82 |
} else if (!Module.dataFileDownloads) { |
| 83 |
if (Module['setStatus']) Module['setStatus']('Downloading D-Mod...'); |
| 84 |
} |
| 85 |
}; |
| 86 |
xhr.onerror = function(event) { |
| 87 |
console.log(event); |
| 88 |
Module.print("Cannot download D-Mod. Maybe the download was blocked, see the JavaScript console for more information."); |
| 89 |
UISetState(true, false); |
| 90 |
} |
| 91 |
xhr.onload = function(event) { |
| 92 |
if (xhr.status == 200 || xhr.status == 304 || xhr.status == 206 || (xhr.status == 0 && xhr.response)) { |
| 93 |
FS.writeFile('mine.dmod', new Uint8Array(xhr.response)); |
| 94 |
DmodExtract(); |
| 95 |
if (Module['setStatus']) Module['setStatus'](''); |
| 96 |
} else { |
| 97 |
console.log(event); |
| 98 |
Module.print("Error while downloading " + xhr.responseURL |
| 99 |
+ " : " + xhr.statusText + " (status code " + xhr.status + ")"); |
| 100 |
} |
| 101 |
UISetState(true, false); |
| 102 |
}; |
| 103 |
xhr.send(null); |
| 104 |
} else { /* no D-Mod querystring */ |
| 105 |
try { |
| 106 |
dmoddiz = new TextDecoder().decode(FS.readFile('/usr/local/share/dink/dink/dmod.diz'), {encoding:'ISO-8859-1'}); |
| 107 |
Module.print(dmoddiz); |
| 108 |
} catch(e) { |
| 109 |
Module.print("Could not read dmod.diz"); |
| 110 |
} |
| 111 |
Module.print(""); |
| 112 |
Module.print("Welcome! Click Play, or load one of your D-Mods!"); |
| 113 |
UISetState(true, false); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
Module['onExit'] = function() { |
| 118 |
UISetState(true, false); |
| 119 |
} |