loader.js 888 B

123456789101112131415161718192021222324252627282930313233
  1. var Loader = /** @constructor */ function() {
  2. this.env = null;
  3. this.init = function(loadPromise, basePath, config) {
  4. var me = this;
  5. return new Promise(function(resolve, reject) {
  6. var cfg = config || {};
  7. cfg['locateFile'] = Utils.createLocateRewrite(basePath);
  8. cfg['instantiateWasm'] = Utils.createInstantiatePromise(loadPromise);
  9. loadPromise = null;
  10. Godot(cfg).then(function(module) {
  11. me.env = module;
  12. resolve();
  13. });
  14. });
  15. }
  16. this.start = function(preloadedFiles, args) {
  17. var me = this;
  18. return new Promise(function(resolve, reject) {
  19. if (!me.env) {
  20. reject(new Error('The engine must be initialized before it can be started'));
  21. }
  22. preloadedFiles.forEach(function(file) {
  23. Utils.copyToFS(me.env['FS'], file.path, file.buffer);
  24. });
  25. preloadedFiles.length = 0; // Clear memory
  26. me.env['callMain'](args);
  27. resolve();
  28. });
  29. }
  30. };