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";
|
|
|
|
|
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 21:59:26 +00:00
|
|
|
async function fetch_ruffle(): Promise<{ new (...args: any[]): Ruffle }> {
|
2019-09-12 00:28:58 +00:00
|
|
|
try {
|
|
|
|
//If runtime_path is defined then we are executing inside the extension
|
2020-05-12 22:24:41 +00:00
|
|
|
//closure. In that case, we configure our local Webpack instance
|
2020-01-11 03:10:43 +00:00
|
|
|
__webpack_public_path__ = runtime_path + "dist/";
|
2019-09-12 00:28:58 +00:00
|
|
|
} catch (e) {
|
|
|
|
//Checking an undefined closure variable usually throws ReferencError,
|
|
|
|
//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-01-11 03:10:43 +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 21:59:26 +00:00
|
|
|
export function load_ruffle(): Promise<{ new (...args: any[]): Ruffle }> {
|
2020-11-17 22:16:35 +00:00
|
|
|
if (lastLoaded == null) {
|
|
|
|
lastLoaded = fetch_ruffle();
|
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
|
|
|
}
|