AtomicResources_js.template 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. var PACKAGE_PATH;
  10. if (typeof window === 'object') {
  11. PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
  12. } else {
  13. // worker
  14. PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/');
  15. }
  16. var PACKAGE_NAME = 'AtomicResources.data';
  17. var REMOTE_PACKAGE_BASE = 'AtomicResources.data';
  18. if (typeof Module['locateFilePackage'] === 'function' && !Module['locateFile']) {
  19. Module['locateFile'] = Module['locateFilePackage'];
  20. Module.printErr('warning: you defined Module.locateFilePackage, that has been renamed to Module.locateFile (using your locateFilePackage for now)');
  21. }
  22. var REMOTE_PACKAGE_NAME = typeof Module['locateFile'] === 'function' ?
  23. Module['locateFile'](REMOTE_PACKAGE_BASE) :
  24. ((Module['filePackagePrefixURL'] || '') + REMOTE_PACKAGE_BASE);
  25. var REMOTE_PACKAGE_SIZE = $$REMOTE_PACKAGE_SIZE$$;
  26. var PACKAGE_UUID = 'd832f5b3-9296-453e-be32-e9a18cf5c8c7';
  27. function fetchRemotePackage(packageName, packageSize, callback, errback) {
  28. var xhr = new XMLHttpRequest();
  29. xhr.open('GET', packageName, true);
  30. xhr.responseType = 'arraybuffer';
  31. xhr.onprogress = function(event) {
  32. var url = packageName;
  33. var size = packageSize;
  34. if (event.total) size = event.total;
  35. if (event.loaded) {
  36. if (!xhr.addedTotal) {
  37. xhr.addedTotal = true;
  38. if (!Module.dataFileDownloads) Module.dataFileDownloads = {};
  39. Module.dataFileDownloads[url] = {
  40. loaded: event.loaded,
  41. total: size
  42. };
  43. } else {
  44. Module.dataFileDownloads[url].loaded = event.loaded;
  45. }
  46. var total = 0;
  47. var loaded = 0;
  48. var num = 0;
  49. for (var download in Module.dataFileDownloads) {
  50. var data = Module.dataFileDownloads[download];
  51. total += data.total;
  52. loaded += data.loaded;
  53. num++;
  54. }
  55. total = Math.ceil(total * Module.expectedDataFileDownloads/num);
  56. if (Module['setStatus']) Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')');
  57. } else if (!Module.dataFileDownloads) {
  58. if (Module['setStatus']) Module['setStatus']('Downloading data...');
  59. }
  60. };
  61. xhr.onload = function(event) {
  62. var packageData = xhr.response;
  63. callback(packageData);
  64. };
  65. xhr.send(null);
  66. };
  67. function handleError(error) {
  68. console.error('package error:', error);
  69. };
  70. var fetched = null, fetchedCallback = null;
  71. fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE, function(data) {
  72. if (fetchedCallback) {
  73. fetchedCallback(data);
  74. fetchedCallback = null;
  75. } else {
  76. fetched = data;
  77. }
  78. }, handleError);
  79. function runWithFS() {
  80. function assert(check, msg) {
  81. if (!check) throw msg + new Error().stack;
  82. }
  83. function DataRequest(start, end, crunched, audio) {
  84. this.start = start;
  85. this.end = end;
  86. this.crunched = crunched;
  87. this.audio = audio;
  88. }
  89. DataRequest.prototype = {
  90. requests: {},
  91. open: function(mode, name) {
  92. this.name = name;
  93. this.requests[name] = this;
  94. Module['addRunDependency']('fp ' + this.name);
  95. },
  96. send: function() {},
  97. onload: function() {
  98. var byteArray = this.byteArray.subarray(this.start, this.end);
  99. this.finish(byteArray);
  100. },
  101. finish: function(byteArray) {
  102. var that = this;
  103. Module['FS_createPreloadedFile'](this.name, null, byteArray, true, true, function() {
  104. Module['removeRunDependency']('fp ' + that.name);
  105. }, function() {
  106. if (that.audio) {
  107. Module['removeRunDependency']('fp ' + that.name); // workaround for chromium bug 124926 (still no audio with this, but at least we don't hang)
  108. } else {
  109. Module.printErr('Preloading file ' + that.name + ' failed');
  110. }
  111. }, false, true); // canOwn this data in the filesystem, it is a slide into the heap that will never change
  112. this.requests[this.name] = null;
  113. },
  114. };
  115. $$ATOMIC_RESOURCES_DATA_REQUEST$$
  116. function processPackageData(arrayBuffer) {
  117. Module.finishedDataFileDownloads++;
  118. assert(arrayBuffer, 'Loading data file failed.');
  119. var byteArray = new Uint8Array(arrayBuffer);
  120. var curr;
  121. // copy the entire loaded file into a spot in the heap. Files will refer to slices in that. They cannot be freed though.
  122. var ptr = Module['_malloc'](byteArray.length);
  123. Module['HEAPU8'].set(byteArray, ptr);
  124. DataRequest.prototype.byteArray = Module['HEAPU8'].subarray(ptr, ptr+byteArray.length);
  125. DataRequest.prototype.requests["/AtomicResources.pak"].onload();
  126. Module['removeRunDependency']('datafile_AtomicResources.data');
  127. };
  128. Module['addRunDependency']('datafile_AtomicResources.data');
  129. if (!Module.preloadResults) Module.preloadResults = {};
  130. Module.preloadResults[PACKAGE_NAME] = {fromCache: false};
  131. if (fetched) {
  132. processPackageData(fetched);
  133. fetched = null;
  134. } else {
  135. fetchedCallback = processPackageData;
  136. }
  137. }
  138. if (Module['calledRun']) {
  139. runWithFS();
  140. } else {
  141. if (!Module['preRun']) Module['preRun'] = [];
  142. Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
  143. }
  144. })();