sceneLoader.ts 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. import { Tools } from "../Misc/tools";
  2. import { Observable } from "../Misc/observable";
  3. import { FilesInputStore } from "../Misc/filesInputStore";
  4. import { Nullable } from "../types";
  5. import { Scene } from "../scene";
  6. import { Engine } from "../Engines/engine";
  7. import { EngineStore } from "../Engines/engineStore";
  8. import { AbstractMesh } from "../Meshes/abstractMesh";
  9. import { AnimationGroup } from "../Animations/animationGroup";
  10. import { AssetContainer } from "../assetContainer";
  11. import { IParticleSystem } from "../Particles/IParticleSystem";
  12. import { Skeleton } from "../Bones/skeleton";
  13. import { Logger } from "../Misc/logger";
  14. import { Constants } from "../Engines/constants";
  15. import { SceneLoaderFlags } from "./sceneLoaderFlags";
  16. import { IFileRequest } from "../Misc/fileRequest";
  17. import { WebRequest } from "../Misc/webRequest";
  18. import { RequestFileError, ReadFileError } from '../Misc/fileTools';
  19. /**
  20. * Class used to represent data loading progression
  21. */
  22. export class SceneLoaderProgressEvent {
  23. /**
  24. * Create a new progress event
  25. * @param lengthComputable defines if data length to load can be evaluated
  26. * @param loaded defines the loaded data length
  27. * @param total defines the data length to load
  28. */
  29. constructor(
  30. /** defines if data length to load can be evaluated */
  31. public readonly lengthComputable: boolean,
  32. /** defines the loaded data length */
  33. public readonly loaded: number,
  34. /** defines the data length to load */
  35. public readonly total: number) {
  36. }
  37. /**
  38. * Creates a new SceneLoaderProgressEvent from a ProgressEvent
  39. * @param event defines the source event
  40. * @returns a new SceneLoaderProgressEvent
  41. */
  42. public static FromProgressEvent(event: ProgressEvent): SceneLoaderProgressEvent {
  43. return new SceneLoaderProgressEvent(event.lengthComputable, event.loaded, event.total);
  44. }
  45. }
  46. /**
  47. * Interface used by SceneLoader plugins to define supported file extensions
  48. */
  49. export interface ISceneLoaderPluginExtensions {
  50. /**
  51. * Defines the list of supported extensions
  52. */
  53. [extension: string]: {
  54. isBinary: boolean;
  55. };
  56. }
  57. /**
  58. * Interface used by SceneLoader plugin factory
  59. */
  60. export interface ISceneLoaderPluginFactory {
  61. /**
  62. * Defines the name of the factory
  63. */
  64. name: string;
  65. /**
  66. * Function called to create a new plugin
  67. * @return the new plugin
  68. */
  69. createPlugin(): ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  70. /**
  71. * The callback that returns true if the data can be directly loaded.
  72. * @param data string containing the file data
  73. * @returns if the data can be loaded directly
  74. */
  75. canDirectLoad?(data: string): boolean;
  76. }
  77. /**
  78. * Interface used to define the base of ISceneLoaderPlugin and ISceneLoaderPluginAsync
  79. */
  80. export interface ISceneLoaderPluginBase {
  81. /**
  82. * The friendly name of this plugin.
  83. */
  84. name: string;
  85. /**
  86. * The file extensions supported by this plugin.
  87. */
  88. extensions: string | ISceneLoaderPluginExtensions;
  89. /**
  90. * The callback called when loading from a url.
  91. * @param scene scene loading this url
  92. * @param url url to load
  93. * @param onSuccess callback called when the file successfully loads
  94. * @param onProgress callback called while file is loading (if the server supports this mode)
  95. * @param useArrayBuffer defines a boolean indicating that date must be returned as ArrayBuffer
  96. * @param onError callback called when the file fails to load
  97. * @returns a file request object
  98. */
  99. requestFile?(scene: Scene, url: string, onSuccess: (data: any, request?: WebRequest) => void, onProgress?: (ev: ProgressEvent) => void, useArrayBuffer?: boolean, onError?: (error: any) => void): IFileRequest;
  100. /**
  101. * The callback called when loading from a file object.
  102. * @param scene scene loading this file
  103. * @param file defines the file to load
  104. * @param onSuccess defines the callback to call when data is loaded
  105. * @param onProgress defines the callback to call during loading process
  106. * @param useArrayBuffer defines a boolean indicating that data must be returned as an ArrayBuffer
  107. * @param onError defines the callback to call when an error occurs
  108. * @returns a file request object
  109. */
  110. readFile?(scene: Scene, file: File, onSuccess: (data: any) => void, onProgress?: (ev: ProgressEvent) => any, useArrayBuffer?: boolean, onError?: (error: any) => void): IFileRequest;
  111. /**
  112. * The callback that returns true if the data can be directly loaded.
  113. * @param data string containing the file data
  114. * @returns if the data can be loaded directly
  115. */
  116. canDirectLoad?(data: string): boolean;
  117. /**
  118. * The callback that returns the data to pass to the plugin if the data can be directly loaded.
  119. * @param scene scene loading this data
  120. * @param data string containing the data
  121. * @returns data to pass to the plugin
  122. */
  123. directLoad?(scene: Scene, data: string): any;
  124. /**
  125. * The callback that allows custom handling of the root url based on the response url.
  126. * @param rootUrl the original root url
  127. * @param responseURL the response url if available
  128. * @returns the new root url
  129. */
  130. rewriteRootURL?(rootUrl: string, responseURL?: string): string;
  131. }
  132. /**
  133. * Interface used to define a SceneLoader plugin
  134. */
  135. export interface ISceneLoaderPlugin extends ISceneLoaderPluginBase {
  136. /**
  137. * Import meshes into a scene.
  138. * @param meshesNames An array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  139. * @param scene The scene to import into
  140. * @param data The data to import
  141. * @param rootUrl The root url for scene and resources
  142. * @param meshes The meshes array to import into
  143. * @param particleSystems The particle systems array to import into
  144. * @param skeletons The skeletons array to import into
  145. * @param onError The callback when import fails
  146. * @returns True if successful or false otherwise
  147. */
  148. importMesh(meshesNames: any, scene: Scene, data: any, rootUrl: string, meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], onError?: (message: string, exception?: any) => void): boolean;
  149. /**
  150. * Load into a scene.
  151. * @param scene The scene to load into
  152. * @param data The data to import
  153. * @param rootUrl The root url for scene and resources
  154. * @param onError The callback when import fails
  155. * @returns True if successful or false otherwise
  156. */
  157. load(scene: Scene, data: any, rootUrl: string, onError?: (message: string, exception?: any) => void): boolean;
  158. /**
  159. * Load into an asset container.
  160. * @param scene The scene to load into
  161. * @param data The data to import
  162. * @param rootUrl The root url for scene and resources
  163. * @param onError The callback when import fails
  164. * @returns The loaded asset container
  165. */
  166. loadAssetContainer(scene: Scene, data: any, rootUrl: string, onError?: (message: string, exception?: any) => void): AssetContainer;
  167. }
  168. /**
  169. * Interface used to define an async SceneLoader plugin
  170. */
  171. export interface ISceneLoaderPluginAsync extends ISceneLoaderPluginBase {
  172. /**
  173. * Import meshes into a scene.
  174. * @param meshesNames An array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  175. * @param scene The scene to import into
  176. * @param data The data to import
  177. * @param rootUrl The root url for scene and resources
  178. * @param onProgress The callback when the load progresses
  179. * @param fileName Defines the name of the file to load
  180. * @returns The loaded meshes, particle systems, skeletons, and animation groups
  181. */
  182. importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void, fileName?: string): Promise<{ meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[] }>;
  183. /**
  184. * Load into a scene.
  185. * @param scene The scene to load into
  186. * @param data The data to import
  187. * @param rootUrl The root url for scene and resources
  188. * @param onProgress The callback when the load progresses
  189. * @param fileName Defines the name of the file to load
  190. * @returns Nothing
  191. */
  192. loadAsync(scene: Scene, data: any, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void, fileName?: string): Promise<void>;
  193. /**
  194. * Load into an asset container.
  195. * @param scene The scene to load into
  196. * @param data The data to import
  197. * @param rootUrl The root url for scene and resources
  198. * @param onProgress The callback when the load progresses
  199. * @param fileName Defines the name of the file to load
  200. * @returns The loaded asset container
  201. */
  202. loadAssetContainerAsync(scene: Scene, data: any, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void, fileName?: string): Promise<AssetContainer>;
  203. }
  204. /**
  205. * Mode that determines how to handle old animation groups before loading new ones.
  206. */
  207. export enum SceneLoaderAnimationGroupLoadingMode {
  208. /**
  209. * Reset all old animations to initial state then dispose them.
  210. */
  211. Clean = 0,
  212. /**
  213. * Stop all old animations.
  214. */
  215. Stop = 1,
  216. /**
  217. * Restart old animations from first frame.
  218. */
  219. Sync = 2,
  220. /**
  221. * Old animations remains untouched.
  222. */
  223. NoSync = 3
  224. }
  225. /**
  226. * Defines a plugin registered by the SceneLoader
  227. */
  228. interface IRegisteredPlugin {
  229. /**
  230. * Defines the plugin to use
  231. */
  232. plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync | ISceneLoaderPluginFactory;
  233. /**
  234. * Defines if the plugin supports binary data
  235. */
  236. isBinary: boolean;
  237. }
  238. /**
  239. * Defines file information
  240. */
  241. interface IFileInfo {
  242. /**
  243. * Gets the file url
  244. */
  245. url: string;
  246. /**
  247. * Gets the root url
  248. */
  249. rootUrl: string;
  250. /**
  251. * Gets filename
  252. */
  253. name: string;
  254. /**
  255. * Gets the file
  256. */
  257. file: Nullable<File>;
  258. }
  259. /**
  260. * Class used to load scene from various file formats using registered plugins
  261. * @see http://doc.babylonjs.com/how_to/load_from_any_file_type
  262. */
  263. export class SceneLoader {
  264. /**
  265. * No logging while loading
  266. */
  267. public static readonly NO_LOGGING = Constants.SCENELOADER_NO_LOGGING;
  268. /**
  269. * Minimal logging while loading
  270. */
  271. public static readonly MINIMAL_LOGGING = Constants.SCENELOADER_MINIMAL_LOGGING;
  272. /**
  273. * Summary logging while loading
  274. */
  275. public static readonly SUMMARY_LOGGING = Constants.SCENELOADER_SUMMARY_LOGGING;
  276. /**
  277. * Detailled logging while loading
  278. */
  279. public static readonly DETAILED_LOGGING = Constants.SCENELOADER_DETAILED_LOGGING;
  280. /**
  281. * Gets or sets a boolean indicating if entire scene must be loaded even if scene contains incremental data
  282. */
  283. public static get ForceFullSceneLoadingForIncremental() {
  284. return SceneLoaderFlags.ForceFullSceneLoadingForIncremental;
  285. }
  286. public static set ForceFullSceneLoadingForIncremental(value: boolean) {
  287. SceneLoaderFlags.ForceFullSceneLoadingForIncremental = value;
  288. }
  289. /**
  290. * Gets or sets a boolean indicating if loading screen must be displayed while loading a scene
  291. */
  292. public static get ShowLoadingScreen(): boolean {
  293. return SceneLoaderFlags.ShowLoadingScreen;
  294. }
  295. public static set ShowLoadingScreen(value: boolean) {
  296. SceneLoaderFlags.ShowLoadingScreen = value;
  297. }
  298. /**
  299. * Defines the current logging level (while loading the scene)
  300. * @ignorenaming
  301. */
  302. public static get loggingLevel(): number {
  303. return SceneLoaderFlags.loggingLevel;
  304. }
  305. public static set loggingLevel(value: number) {
  306. SceneLoaderFlags.loggingLevel = value;
  307. }
  308. /**
  309. * Gets or set a boolean indicating if matrix weights must be cleaned upon loading
  310. */
  311. public static get CleanBoneMatrixWeights(): boolean {
  312. return SceneLoaderFlags.CleanBoneMatrixWeights;
  313. }
  314. public static set CleanBoneMatrixWeights(value: boolean) {
  315. SceneLoaderFlags.CleanBoneMatrixWeights = value;
  316. }
  317. // Members
  318. /**
  319. * Event raised when a plugin is used to load a scene
  320. */
  321. public static OnPluginActivatedObservable = new Observable<ISceneLoaderPlugin | ISceneLoaderPluginAsync>();
  322. private static _registeredPlugins: { [extension: string]: IRegisteredPlugin } = {};
  323. private static _getDefaultPlugin(): IRegisteredPlugin {
  324. return SceneLoader._registeredPlugins[".babylon"];
  325. }
  326. private static _getPluginForExtension(extension: string): IRegisteredPlugin {
  327. var registeredPlugin = SceneLoader._registeredPlugins[extension];
  328. if (registeredPlugin) {
  329. return registeredPlugin;
  330. }
  331. Logger.Warn("Unable to find a plugin to load " + extension + " files. Trying to use .babylon default plugin. To load from a specific filetype (eg. gltf) see: http://doc.babylonjs.com/how_to/load_from_any_file_type");
  332. return SceneLoader._getDefaultPlugin();
  333. }
  334. private static _getPluginForDirectLoad(data: string): IRegisteredPlugin {
  335. for (var extension in SceneLoader._registeredPlugins) {
  336. var plugin = SceneLoader._registeredPlugins[extension].plugin;
  337. if (plugin.canDirectLoad && plugin.canDirectLoad(data)) {
  338. return SceneLoader._registeredPlugins[extension];
  339. }
  340. }
  341. return SceneLoader._getDefaultPlugin();
  342. }
  343. private static _getPluginForFilename(sceneFilename: string): IRegisteredPlugin {
  344. var queryStringPosition = sceneFilename.indexOf("?");
  345. if (queryStringPosition !== -1) {
  346. sceneFilename = sceneFilename.substring(0, queryStringPosition);
  347. }
  348. var dotPosition = sceneFilename.lastIndexOf(".");
  349. var extension = sceneFilename.substring(dotPosition, sceneFilename.length).toLowerCase();
  350. return SceneLoader._getPluginForExtension(extension);
  351. }
  352. private static _getDirectLoad(sceneFilename: string): Nullable<string> {
  353. if (sceneFilename.substr(0, 5) === "data:") {
  354. return sceneFilename.substr(5);
  355. }
  356. return null;
  357. }
  358. private static _loadData(fileInfo: IFileInfo, scene: Scene, onSuccess: (plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync, data: any, responseURL?: string) => void, onProgress: ((event: SceneLoaderProgressEvent) => void) | undefined, onError: (message: string, exception?: any) => void, onDispose: () => void, pluginExtension: Nullable<string>): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  359. const directLoad = SceneLoader._getDirectLoad(fileInfo.name);
  360. const registeredPlugin = pluginExtension ? SceneLoader._getPluginForExtension(pluginExtension) : (directLoad ? SceneLoader._getPluginForDirectLoad(fileInfo.name) : SceneLoader._getPluginForFilename(fileInfo.name));
  361. let plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  362. if ((registeredPlugin.plugin as ISceneLoaderPluginFactory).createPlugin) {
  363. plugin = (registeredPlugin.plugin as ISceneLoaderPluginFactory).createPlugin();
  364. }
  365. else {
  366. plugin = <any>registeredPlugin.plugin;
  367. }
  368. if (!plugin) {
  369. throw "The loader plugin corresponding to the file type you are trying to load has not been found. If using es6, please import the plugin you wish to use before.";
  370. }
  371. SceneLoader.OnPluginActivatedObservable.notifyObservers(plugin);
  372. if (directLoad) {
  373. onSuccess(plugin, plugin.directLoad ? plugin.directLoad(scene, directLoad) : directLoad);
  374. return plugin;
  375. }
  376. const useArrayBuffer = registeredPlugin.isBinary;
  377. const dataCallback = (data: any, responseURL?: string) => {
  378. if (scene.isDisposed) {
  379. onError("Scene has been disposed");
  380. return;
  381. }
  382. onSuccess(plugin, data, responseURL);
  383. };
  384. let request: Nullable<IFileRequest> = null;
  385. let pluginDisposed = false;
  386. const onDisposeObservable = (plugin as any).onDisposeObservable as Observable<ISceneLoaderPlugin | ISceneLoaderPluginAsync>;
  387. if (onDisposeObservable) {
  388. onDisposeObservable.add(() => {
  389. pluginDisposed = true;
  390. if (request) {
  391. request.abort();
  392. request = null;
  393. }
  394. onDispose();
  395. });
  396. }
  397. const progressCallback = onProgress ? (event: ProgressEvent) => {
  398. onProgress(SceneLoaderProgressEvent.FromProgressEvent(event));
  399. } : undefined;
  400. const manifestChecked = () => {
  401. if (pluginDisposed) {
  402. return;
  403. }
  404. const successCallback = (data: string | ArrayBuffer, request?: WebRequest) => {
  405. dataCallback(data, request ? request.responseURL : undefined);
  406. };
  407. const errorCallback = (error: RequestFileError) => {
  408. onError(error.message, error);
  409. };
  410. request = plugin.requestFile
  411. ? plugin.requestFile(scene, fileInfo.url, successCallback, progressCallback, useArrayBuffer, errorCallback)
  412. : scene._requestFile(fileInfo.url, successCallback, progressCallback, true, useArrayBuffer, errorCallback);
  413. };
  414. const file = fileInfo.file || FilesInputStore.FilesToLoad[fileInfo.name.toLowerCase()];
  415. if (fileInfo.rootUrl.indexOf("file:") === -1 || (fileInfo.rootUrl.indexOf("file:") !== -1 && !file)) {
  416. const engine = scene.getEngine();
  417. let canUseOfflineSupport = engine.enableOfflineSupport;
  418. if (canUseOfflineSupport) {
  419. // Also check for exceptions
  420. let exceptionFound = false;
  421. for (var regex of scene.disableOfflineSupportExceptionRules) {
  422. if (regex.test(fileInfo.url)) {
  423. exceptionFound = true;
  424. break;
  425. }
  426. }
  427. canUseOfflineSupport = !exceptionFound;
  428. }
  429. if (canUseOfflineSupport && Engine.OfflineProviderFactory) {
  430. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  431. scene.offlineProvider = Engine.OfflineProviderFactory(fileInfo.url, manifestChecked, engine.disableManifestCheck);
  432. }
  433. else {
  434. manifestChecked();
  435. }
  436. }
  437. // Loading file from disk via input file or drag'n'drop
  438. else {
  439. if (file) {
  440. const errorCallback = (error: ReadFileError) => {
  441. onError(error.message, error);
  442. };
  443. request = plugin.readFile
  444. ? plugin.readFile(scene, file, dataCallback, progressCallback, useArrayBuffer, errorCallback)
  445. : scene._readFile(file, dataCallback, progressCallback, useArrayBuffer, errorCallback);
  446. } else {
  447. onError("Unable to find file named " + fileInfo.name);
  448. }
  449. }
  450. return plugin;
  451. }
  452. private static _getFileInfo(rootUrl: string, sceneFilename: string | File): Nullable<IFileInfo> {
  453. let url: string;
  454. let name: string;
  455. let file: Nullable<File> = null;
  456. if (!sceneFilename) {
  457. url = rootUrl;
  458. name = Tools.GetFilename(rootUrl);
  459. rootUrl = Tools.GetFolderPath(rootUrl);
  460. }
  461. else if ((sceneFilename as File).name) {
  462. const sceneFile = sceneFilename as File;
  463. url = rootUrl + sceneFile.name;
  464. name = sceneFile.name;
  465. file = sceneFile;
  466. }
  467. else {
  468. const filename = sceneFilename as string;
  469. if (filename.substr(0, 1) === "/") {
  470. Tools.Error("Wrong sceneFilename parameter");
  471. return null;
  472. }
  473. url = rootUrl + filename;
  474. name = filename;
  475. }
  476. return {
  477. url: url,
  478. rootUrl: rootUrl,
  479. name: name,
  480. file: file
  481. };
  482. }
  483. // Public functions
  484. /**
  485. * Gets a plugin that can load the given extension
  486. * @param extension defines the extension to load
  487. * @returns a plugin or null if none works
  488. */
  489. public static GetPluginForExtension(extension: string): ISceneLoaderPlugin | ISceneLoaderPluginAsync | ISceneLoaderPluginFactory {
  490. return SceneLoader._getPluginForExtension(extension).plugin;
  491. }
  492. /**
  493. * Gets a boolean indicating that the given extension can be loaded
  494. * @param extension defines the extension to load
  495. * @returns true if the extension is supported
  496. */
  497. public static IsPluginForExtensionAvailable(extension: string): boolean {
  498. return !!SceneLoader._registeredPlugins[extension];
  499. }
  500. /**
  501. * Adds a new plugin to the list of registered plugins
  502. * @param plugin defines the plugin to add
  503. */
  504. public static RegisterPlugin(plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync): void {
  505. if (typeof plugin.extensions === "string") {
  506. var extension = <string>plugin.extensions;
  507. SceneLoader._registeredPlugins[extension.toLowerCase()] = {
  508. plugin: plugin,
  509. isBinary: false
  510. };
  511. }
  512. else {
  513. var extensions = <ISceneLoaderPluginExtensions>plugin.extensions;
  514. Object.keys(extensions).forEach((extension) => {
  515. SceneLoader._registeredPlugins[extension.toLowerCase()] = {
  516. plugin: plugin,
  517. isBinary: extensions[extension].isBinary
  518. };
  519. });
  520. }
  521. }
  522. /**
  523. * Import meshes into a scene
  524. * @param meshNames an array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  525. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  526. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  527. * @param scene the instance of BABYLON.Scene to append to
  528. * @param onSuccess a callback with a list of imported meshes, particleSystems, and skeletons when import succeeds
  529. * @param onProgress a callback with a progress event for each file being loaded
  530. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  531. * @param pluginExtension the extension used to determine the plugin
  532. * @returns The loaded plugin
  533. */
  534. public static ImportMesh(meshNames: any, rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onSuccess: Nullable<(meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[]) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  535. if (!scene) {
  536. Logger.Error("No scene available to import mesh to");
  537. return null;
  538. }
  539. const fileInfo = SceneLoader._getFileInfo(rootUrl, sceneFilename);
  540. if (!fileInfo) {
  541. return null;
  542. }
  543. var loadingToken = {};
  544. scene._addPendingData(loadingToken);
  545. var disposeHandler = () => {
  546. scene._removePendingData(loadingToken);
  547. };
  548. var errorHandler = (message: string, exception?: any) => {
  549. let errorMessage = "Unable to import meshes from " + fileInfo.url + ": " + message;
  550. if (onError) {
  551. onError(scene, errorMessage, exception);
  552. } else {
  553. Logger.Error(errorMessage);
  554. // should the exception be thrown?
  555. }
  556. disposeHandler();
  557. };
  558. var progressHandler = onProgress ? (event: SceneLoaderProgressEvent) => {
  559. try {
  560. onProgress(event);
  561. }
  562. catch (e) {
  563. errorHandler("Error in onProgress callback: " + e, e);
  564. }
  565. } : undefined;
  566. var successHandler = (meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[]) => {
  567. scene.importedMeshesFiles.push(fileInfo.url);
  568. if (onSuccess) {
  569. try {
  570. onSuccess(meshes, particleSystems, skeletons, animationGroups);
  571. }
  572. catch (e) {
  573. errorHandler("Error in onSuccess callback: " + e, e);
  574. }
  575. }
  576. scene._removePendingData(loadingToken);
  577. };
  578. return SceneLoader._loadData(fileInfo, scene, (plugin, data, responseURL) => {
  579. if (plugin.rewriteRootURL) {
  580. fileInfo.rootUrl = plugin.rewriteRootURL(fileInfo.rootUrl, responseURL);
  581. }
  582. if ((<any>plugin).importMesh) {
  583. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  584. var meshes = new Array<AbstractMesh>();
  585. var particleSystems = new Array<IParticleSystem>();
  586. var skeletons = new Array<Skeleton>();
  587. if (!syncedPlugin.importMesh(meshNames, scene, data, fileInfo.rootUrl, meshes, particleSystems, skeletons, errorHandler)) {
  588. return;
  589. }
  590. scene.loadingPluginName = plugin.name;
  591. successHandler(meshes, particleSystems, skeletons, []);
  592. }
  593. else {
  594. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  595. asyncedPlugin.importMeshAsync(meshNames, scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then((result) => {
  596. scene.loadingPluginName = plugin.name;
  597. successHandler(result.meshes, result.particleSystems, result.skeletons, result.animationGroups);
  598. }).catch((error) => {
  599. errorHandler(error.message, error);
  600. });
  601. }
  602. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  603. }
  604. /**
  605. * Import meshes into a scene
  606. * @param meshNames an array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  607. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  608. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  609. * @param scene the instance of BABYLON.Scene to append to
  610. * @param onProgress a callback with a progress event for each file being loaded
  611. * @param pluginExtension the extension used to determine the plugin
  612. * @returns The loaded list of imported meshes, particle systems, skeletons, and animation groups
  613. */
  614. public static ImportMeshAsync(meshNames: any, rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<{ meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[] }> {
  615. return new Promise((resolve, reject) => {
  616. SceneLoader.ImportMesh(meshNames, rootUrl, sceneFilename, scene, (meshes, particleSystems, skeletons, animationGroups) => {
  617. resolve({
  618. meshes: meshes,
  619. particleSystems: particleSystems,
  620. skeletons: skeletons,
  621. animationGroups: animationGroups
  622. });
  623. }, onProgress, (scene, message, exception) => {
  624. reject(exception || new Error(message));
  625. },
  626. pluginExtension);
  627. });
  628. }
  629. /**
  630. * Load a scene
  631. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  632. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  633. * @param engine is the instance of BABYLON.Engine to use to create the scene
  634. * @param onSuccess a callback with the scene when import succeeds
  635. * @param onProgress a callback with a progress event for each file being loaded
  636. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  637. * @param pluginExtension the extension used to determine the plugin
  638. * @returns The loaded plugin
  639. */
  640. public static Load(rootUrl: string, sceneFilename: string | File = "", engine: Nullable<Engine> = EngineStore.LastCreatedEngine, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  641. if (!engine) {
  642. Tools.Error("No engine available");
  643. return null;
  644. }
  645. return SceneLoader.Append(rootUrl, sceneFilename, new Scene(engine), onSuccess, onProgress, onError, pluginExtension);
  646. }
  647. /**
  648. * Load a scene
  649. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  650. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  651. * @param engine is the instance of BABYLON.Engine to use to create the scene
  652. * @param onProgress a callback with a progress event for each file being loaded
  653. * @param pluginExtension the extension used to determine the plugin
  654. * @returns The loaded scene
  655. */
  656. public static LoadAsync(rootUrl: string, sceneFilename: string | File = "", engine: Nullable<Engine> = EngineStore.LastCreatedEngine, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<Scene> {
  657. return new Promise((resolve, reject) => {
  658. SceneLoader.Load(rootUrl, sceneFilename, engine, (scene) => {
  659. resolve(scene);
  660. }, onProgress, (scene, message, exception) => {
  661. reject(exception || new Error(message));
  662. }, pluginExtension);
  663. });
  664. }
  665. /**
  666. * Append a scene
  667. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  668. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  669. * @param scene is the instance of BABYLON.Scene to append to
  670. * @param onSuccess a callback with the scene when import succeeds
  671. * @param onProgress a callback with a progress event for each file being loaded
  672. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  673. * @param pluginExtension the extension used to determine the plugin
  674. * @returns The loaded plugin
  675. */
  676. public static Append(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  677. if (!scene) {
  678. Logger.Error("No scene available to append to");
  679. return null;
  680. }
  681. const fileInfo = SceneLoader._getFileInfo(rootUrl, sceneFilename);
  682. if (!fileInfo) {
  683. return null;
  684. }
  685. if (SceneLoader.ShowLoadingScreen) {
  686. scene.getEngine().displayLoadingUI();
  687. }
  688. var loadingToken = {};
  689. scene._addPendingData(loadingToken);
  690. var disposeHandler = () => {
  691. scene._removePendingData(loadingToken);
  692. scene.getEngine().hideLoadingUI();
  693. };
  694. var errorHandler = (message: Nullable<string>, exception?: any) => {
  695. let errorMessage = "Unable to load from " + fileInfo.url + (message ? ": " + message : "");
  696. if (onError) {
  697. onError(scene, errorMessage, exception);
  698. } else {
  699. Logger.Error(errorMessage);
  700. // should the exception be thrown?
  701. }
  702. disposeHandler();
  703. };
  704. var progressHandler = onProgress ? (event: SceneLoaderProgressEvent) => {
  705. try {
  706. onProgress(event);
  707. }
  708. catch (e) {
  709. errorHandler("Error in onProgress callback", e);
  710. }
  711. } : undefined;
  712. var successHandler = () => {
  713. if (onSuccess) {
  714. try {
  715. onSuccess(scene);
  716. }
  717. catch (e) {
  718. errorHandler("Error in onSuccess callback", e);
  719. }
  720. }
  721. scene._removePendingData(loadingToken);
  722. };
  723. return SceneLoader._loadData(fileInfo, scene, (plugin, data) => {
  724. if ((<any>plugin).load) {
  725. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  726. if (!syncedPlugin.load(scene, data, fileInfo.rootUrl, errorHandler)) {
  727. return;
  728. }
  729. scene.loadingPluginName = plugin.name;
  730. successHandler();
  731. } else {
  732. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  733. asyncedPlugin.loadAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then(() => {
  734. scene.loadingPluginName = plugin.name;
  735. successHandler();
  736. }).catch((error) => {
  737. errorHandler(error.message, error);
  738. });
  739. }
  740. if (SceneLoader.ShowLoadingScreen) {
  741. scene.executeWhenReady(() => {
  742. scene.getEngine().hideLoadingUI();
  743. });
  744. }
  745. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  746. }
  747. /**
  748. * Append a scene
  749. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  750. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  751. * @param scene is the instance of BABYLON.Scene to append to
  752. * @param onProgress a callback with a progress event for each file being loaded
  753. * @param pluginExtension the extension used to determine the plugin
  754. * @returns The given scene
  755. */
  756. public static AppendAsync(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<Scene> {
  757. return new Promise((resolve, reject) => {
  758. SceneLoader.Append(rootUrl, sceneFilename, scene, (scene) => {
  759. resolve(scene);
  760. }, onProgress, (scene, message, exception) => {
  761. reject(exception || new Error(message));
  762. }, pluginExtension);
  763. });
  764. }
  765. /**
  766. * Load a scene into an asset container
  767. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  768. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  769. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  770. * @param onSuccess a callback with the scene when import succeeds
  771. * @param onProgress a callback with a progress event for each file being loaded
  772. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  773. * @param pluginExtension the extension used to determine the plugin
  774. * @returns The loaded plugin
  775. */
  776. public static LoadAssetContainer(
  777. rootUrl: string,
  778. sceneFilename: string | File = "",
  779. scene: Nullable<Scene> = EngineStore.LastCreatedScene,
  780. onSuccess: Nullable<(assets: AssetContainer) => void> = null,
  781. onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null,
  782. onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null,
  783. pluginExtension: Nullable<string> = null
  784. ): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  785. if (!scene) {
  786. Logger.Error("No scene available to load asset container to");
  787. return null;
  788. }
  789. const fileInfo = SceneLoader._getFileInfo(rootUrl, sceneFilename);
  790. if (!fileInfo) {
  791. return null;
  792. }
  793. var loadingToken = {};
  794. scene._addPendingData(loadingToken);
  795. var disposeHandler = () => {
  796. scene._removePendingData(loadingToken);
  797. };
  798. var errorHandler = (message: Nullable<string>, exception?: any) => {
  799. let errorMessage = "Unable to load assets from " + fileInfo.url + (message ? ": " + message : "");
  800. if (exception && exception.message) {
  801. errorMessage += ` (${exception.message})`;
  802. }
  803. if (onError) {
  804. onError(scene, errorMessage, exception);
  805. } else {
  806. Logger.Error(errorMessage);
  807. // should the exception be thrown?
  808. }
  809. disposeHandler();
  810. };
  811. var progressHandler = onProgress ? (event: SceneLoaderProgressEvent) => {
  812. try {
  813. onProgress(event);
  814. }
  815. catch (e) {
  816. errorHandler("Error in onProgress callback", e);
  817. }
  818. } : undefined;
  819. var successHandler = (assets: AssetContainer) => {
  820. if (onSuccess) {
  821. try {
  822. onSuccess(assets);
  823. }
  824. catch (e) {
  825. errorHandler("Error in onSuccess callback", e);
  826. }
  827. }
  828. scene._removePendingData(loadingToken);
  829. };
  830. return SceneLoader._loadData(fileInfo, scene, (plugin, data) => {
  831. if ((<any>plugin).loadAssetContainer) {
  832. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  833. var assetContainer = syncedPlugin.loadAssetContainer(scene, data, fileInfo.rootUrl, errorHandler);
  834. if (!assetContainer) {
  835. return;
  836. }
  837. scene.loadingPluginName = plugin.name;
  838. successHandler(assetContainer);
  839. } else if ((<any>plugin).loadAssetContainerAsync) {
  840. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  841. asyncedPlugin.loadAssetContainerAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then((assetContainer) => {
  842. scene.loadingPluginName = plugin.name;
  843. successHandler(assetContainer);
  844. }).catch((error) => {
  845. errorHandler(error.message, error);
  846. });
  847. } else {
  848. errorHandler("LoadAssetContainer is not supported by this plugin. Plugin did not provide a loadAssetContainer or loadAssetContainerAsync method.");
  849. }
  850. if (SceneLoader.ShowLoadingScreen) {
  851. scene.executeWhenReady(() => {
  852. scene.getEngine().hideLoadingUI();
  853. });
  854. }
  855. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  856. }
  857. /**
  858. * Load a scene into an asset container
  859. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  860. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene (default: empty string)
  861. * @param scene is the instance of Scene to append to
  862. * @param onProgress a callback with a progress event for each file being loaded
  863. * @param pluginExtension the extension used to determine the plugin
  864. * @returns The loaded asset container
  865. */
  866. public static LoadAssetContainerAsync(rootUrl: string, sceneFilename: string = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<AssetContainer> {
  867. return new Promise((resolve, reject) => {
  868. SceneLoader.LoadAssetContainer(rootUrl, sceneFilename, scene, (assetContainer) => {
  869. resolve(assetContainer);
  870. }, onProgress, (scene, message, exception) => {
  871. reject(exception || new Error(message));
  872. }, pluginExtension);
  873. });
  874. }
  875. /**
  876. * Import animations from a file into a scene
  877. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  878. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  879. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  880. * @param overwriteAnimations when true, animations are cleaned before importing new ones. Animations are appended otherwise
  881. * @param animationGroupLoadingMode defines how to handle old animations groups before importing new ones
  882. * @param targetConverter defines a function used to convert animation targets from loaded scene to current scene (default: search node by name)
  883. * @param onSuccess a callback with the scene when import succeeds
  884. * @param onProgress a callback with a progress event for each file being loaded
  885. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  886. */
  887. public static ImportAnimations(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, overwriteAnimations = true, animationGroupLoadingMode = SceneLoaderAnimationGroupLoadingMode.Clean, targetConverter: Nullable<(target: any) => any> = null, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null): void {
  888. if (!scene) {
  889. Logger.Error("No scene available to load animations to");
  890. return;
  891. }
  892. if (overwriteAnimations) {
  893. // Reset, stop and dispose all animations before loading new ones
  894. for (let animatable of scene.animatables) {
  895. animatable.reset();
  896. }
  897. scene.stopAllAnimations();
  898. scene.animationGroups.slice().forEach((animationGroup) => {
  899. animationGroup.dispose();
  900. });
  901. let nodes = scene.getNodes();
  902. nodes.forEach((node) => {
  903. if (node.animations) {
  904. node.animations = [];
  905. }
  906. });
  907. }
  908. else {
  909. switch (animationGroupLoadingMode) {
  910. case SceneLoaderAnimationGroupLoadingMode.Clean:
  911. scene.animationGroups.slice().forEach((animationGroup) => {
  912. animationGroup.dispose();
  913. });
  914. break;
  915. case SceneLoaderAnimationGroupLoadingMode.Stop:
  916. scene.animationGroups.forEach((animationGroup) => {
  917. animationGroup.stop();
  918. });
  919. break;
  920. case SceneLoaderAnimationGroupLoadingMode.Sync:
  921. scene.animationGroups.forEach((animationGroup) => {
  922. animationGroup.reset();
  923. animationGroup.restart();
  924. });
  925. break;
  926. case SceneLoaderAnimationGroupLoadingMode.NoSync:
  927. // nothing to do
  928. break;
  929. default:
  930. Logger.Error("Unknown animation group loading mode value '" + animationGroupLoadingMode + "'");
  931. return;
  932. }
  933. }
  934. let startingIndexForNewAnimatables = scene.animatables.length;
  935. let onAssetContainerLoaded = (container: AssetContainer) => {
  936. container.mergeAnimationsTo(scene, scene.animatables.slice(startingIndexForNewAnimatables), targetConverter);
  937. container.dispose();
  938. scene.onAnimationFileImportedObservable.notifyObservers(scene);
  939. if (onSuccess) {
  940. onSuccess(scene);
  941. }
  942. };
  943. this.LoadAssetContainer(rootUrl, sceneFilename, scene, onAssetContainerLoaded, onProgress, onError);
  944. }
  945. /**
  946. * Import animations from a file into a scene
  947. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  948. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  949. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  950. * @param overwriteAnimations when true, animations are cleaned before importing new ones. Animations are appended otherwise
  951. * @param animationGroupLoadingMode defines how to handle old animations groups before importing new ones
  952. * @param targetConverter defines a function used to convert animation targets from loaded scene to current scene (default: search node by name)
  953. * @param onSuccess a callback with the scene when import succeeds
  954. * @param onProgress a callback with a progress event for each file being loaded
  955. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  956. * @returns the updated scene with imported animations
  957. */
  958. public static ImportAnimationsAsync(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, overwriteAnimations = true, animationGroupLoadingMode = SceneLoaderAnimationGroupLoadingMode.Clean, targetConverter: Nullable<(target: any) => any> = null, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null): Promise<Scene> {
  959. return new Promise((resolve, reject) => {
  960. SceneLoader.ImportAnimations(rootUrl, sceneFilename, scene, overwriteAnimations, animationGroupLoadingMode, targetConverter, (_scene: Scene) => {
  961. resolve(_scene);
  962. }, onProgress, (_scene: Scene, message: string, exception: any) => {
  963. reject(exception || new Error(message));
  964. });
  965. });
  966. }
  967. }