filesystem.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var Module;
  2. if (typeof Module === 'undefined') Module = eval('(function() { try { return Module || {} } catch(e) { return {} } })()');
  3. if (!Module.expectedDataFileDownloads) {
  4. Module.expectedDataFileDownloads = 0;
  5. Module.finishedDataFileDownloads = 0;
  6. }
  7. Module.expectedDataFileDownloads++;
  8. (function() {
  9. function fetchRemotePackage(packageName, callback, errback) {
  10. var xhr = new XMLHttpRequest();
  11. xhr.open('GET', packageName, true);
  12. xhr.responseType = 'arraybuffer';
  13. xhr.onprogress = function(event) {
  14. var url = packageName;
  15. if (event.loaded && event.total) {
  16. if (!xhr.addedTotal) {
  17. xhr.addedTotal = true;
  18. if (!Module.dataFileDownloads) Module.dataFileDownloads = {};
  19. Module.dataFileDownloads[url] = {
  20. loaded: event.loaded,
  21. total: event.total
  22. };
  23. } else {
  24. Module.dataFileDownloads[url].loaded = event.loaded;
  25. }
  26. var total = 0;
  27. var loaded = 0;
  28. var num = 0;
  29. for (var download in Module.dataFileDownloads) {
  30. var data = Module.dataFileDownloads[download];
  31. total += data.total;
  32. loaded += data.loaded;
  33. num++;
  34. }
  35. total = Math.ceil(total * Module.expectedDataFileDownloads/num);
  36. if (Module['setStatus']) Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')');
  37. } else if (!Module.dataFileDownloads) {
  38. if (Module['setStatus']) Module['setStatus']('Downloading data...');
  39. }
  40. };
  41. xhr.onload = function(event) {
  42. var packageData = xhr.response;
  43. callback(packageData);
  44. };
  45. xhr.send(null);
  46. };
  47. function handleError(error) {
  48. console.error('package error:', error);
  49. };
  50. var fetched = null, fetchedCallback = null;
  51. fetchRemotePackage('data.pck', function(data) {
  52. if (fetchedCallback) {
  53. fetchedCallback(data);
  54. fetchedCallback = null;
  55. } else {
  56. fetched = data;
  57. }
  58. }, handleError);
  59. function runWithFS() {
  60. function assert(check, msg) {
  61. if (!check) throw msg + new Error().stack;
  62. }
  63. function DataRequest(start, end, crunched, audio) {
  64. this.start = start;
  65. this.end = end;
  66. this.crunched = crunched;
  67. this.audio = audio;
  68. }
  69. DataRequest.prototype = {
  70. requests: {},
  71. open: function(mode, name) {
  72. this.name = name;
  73. this.requests[name] = this;
  74. Module['addRunDependency']('fp ' + this.name);
  75. },
  76. send: function() {},
  77. onload: function() {
  78. var byteArray = this.byteArray.subarray(this.start, this.end);
  79. this.finish(byteArray);
  80. },
  81. finish: function(byteArray) {
  82. var that = this;
  83. Module['FS_createPreloadedFile'](this.name, null, byteArray, true, true, function() {
  84. Module['removeRunDependency']('fp ' + that.name);
  85. }, function() {
  86. if (that.audio) {
  87. Module['removeRunDependency']('fp ' + that.name); // workaround for chromium bug 124926 (still no audio with this, but at least we don't hang)
  88. } else {
  89. Module.printErr('Preloading file ' + that.name + ' failed');
  90. }
  91. }, false, true); // canOwn this data in the filesystem, it is a slide into the heap that will never change
  92. this.requests[this.name] = null;
  93. },
  94. };
  95. new DataRequest(0, $DPLEN, 0, 0).open('GET', '/data.pck');
  96. var PACKAGE_PATH;
  97. if (typeof window === 'object') {
  98. PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
  99. } else {
  100. // worker
  101. PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/');
  102. }
  103. var PACKAGE_NAME = 'data.pck';
  104. var REMOTE_PACKAGE_NAME = 'data.pck';
  105. var PACKAGE_UUID = 'b39761ce-0348-4959-9b16-302ed8e1592e';
  106. function processPackageData(arrayBuffer) {
  107. Module.finishedDataFileDownloads++;
  108. assert(arrayBuffer, 'Loading data file failed.');
  109. var byteArray = new Uint8Array(arrayBuffer);
  110. var curr;
  111. // Reuse the bytearray from the XHR as the source for file reads.
  112. DataRequest.prototype.byteArray = byteArray;
  113. DataRequest.prototype.requests["/data.pck"].onload();
  114. Module['removeRunDependency']('datafile_datapack');
  115. };
  116. Module['addRunDependency']('datafile_datapack');
  117. if (!Module.preloadResults) Module.preloadResults = {};
  118. Module.preloadResults[PACKAGE_NAME] = {fromCache: false};
  119. if (fetched) {
  120. processPackageData(fetched);
  121. fetched = null;
  122. } else {
  123. fetchedCallback = processPackageData;
  124. }
  125. }
  126. if (Module['calledRun']) {
  127. runWithFS();
  128. } else {
  129. if (!Module['preRun']) Module['preRun'] = [];
  130. Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
  131. }
  132. })();