2020-11-17 21:59:26 +00:00
|
|
|
/* eslint @typescript-eslint/no-explicit-any: "off" */
|
2020-05-11 19:37:57 +00:00
|
|
|
|
2019-08-24 23:56:46 +00:00
|
|
|
/**
|
|
|
|
* Conditional ruffle loader
|
|
|
|
*/
|
|
|
|
|
2020-11-17 21:59:26 +00:00
|
|
|
import { Ruffle } from "../pkg/ruffle_web";
|
|
|
|
|
2020-12-30 19:41:34 +00:00
|
|
|
import { setPolyfillsOnLoad } from "./js-polyfills";
|
2020-12-20 15:06:50 +00:00
|
|
|
|
2019-08-24 23:56:46 +00:00
|
|
|
/**
|
|
|
|
* Load ruffle from an automatically-detected location.
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2019-08-24 23:56:46 +00:00
|
|
|
* This function returns a new instance of Ruffle and downloads it every time.
|
|
|
|
* You should not use it directly; this module will memoize the resource
|
|
|
|
* download.
|
2020-11-17 22:53:17 +00:00
|
|
|
*
|
|
|
|
* @returns A ruffle constructor that may be used to create new Ruffle
|
|
|
|
* instances.
|
2019-08-24 23:56:46 +00:00
|
|
|
*/
|
2020-11-17 22:17:21 +00:00
|
|
|
async function fetchRuffle(): Promise<{ new (...args: any[]): Ruffle }> {
|
2020-12-30 19:41:34 +00:00
|
|
|
// Apply some pure JavaScript polyfills to prevent conflicts with external
|
|
|
|
// libraries, if needed.
|
|
|
|
setPolyfillsOnLoad();
|
2020-12-20 15:06:50 +00:00
|
|
|
|
2019-09-12 00:28:58 +00:00
|
|
|
try {
|
2020-12-13 15:45:14 +00:00
|
|
|
// If ruffleRuntimePath is defined then we are executing inside the extension
|
|
|
|
// closure. In that case, we configure our local Webpack instance.
|
|
|
|
__webpack_public_path__ = ruffleRuntimePath + "dist/";
|
2019-09-12 00:28:58 +00:00
|
|
|
} catch (e) {
|
2020-12-20 15:06:50 +00:00
|
|
|
// Checking an undefined closure variable usually throws ReferenceError,
|
2020-12-13 15:45:14 +00:00
|
|
|
// so we need to catch it here and continue onward.
|
2020-05-12 21:36:18 +00:00
|
|
|
if (!(e instanceof ReferenceError)) {
|
2019-09-12 00:28:58 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 22:24:41 +00:00
|
|
|
|
2020-12-13 15:45:14 +00:00
|
|
|
// We currently assume that if we are not executing inside the extension,
|
|
|
|
// then we can use webpack to get Ruffle.
|
2020-11-17 22:16:35 +00:00
|
|
|
const module = await import("../pkg/ruffle_web");
|
|
|
|
return module.Ruffle;
|
2019-08-24 23:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 22:16:35 +00:00
|
|
|
let lastLoaded: Promise<{ new (...args: any[]): Ruffle }> | null = null;
|
2019-08-24 23:56:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain an instance of `Ruffle`.
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2019-08-24 23:56:46 +00:00
|
|
|
* This function returns a promise which yields `Ruffle` asynchronously.
|
2020-11-17 22:53:17 +00:00
|
|
|
*
|
|
|
|
* @returns A ruffle constructor that may be used to create new Ruffle
|
|
|
|
* instances.
|
2019-08-24 23:56:46 +00:00
|
|
|
*/
|
2020-11-17 22:17:38 +00:00
|
|
|
export function loadRuffle(): Promise<{ new (...args: any[]): Ruffle }> {
|
2020-11-17 22:16:35 +00:00
|
|
|
if (lastLoaded == null) {
|
2020-11-17 22:17:21 +00:00
|
|
|
lastLoaded = fetchRuffle();
|
2019-08-24 23:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 22:16:35 +00:00
|
|
|
return lastLoaded;
|
2020-11-12 22:32:53 +00:00
|
|
|
}
|