2023-01-03 00:18:19 +00:00
|
|
|
import { Indexable, PluginManifest, Plugin } from "@types";
|
2023-01-03 08:05:16 +00:00
|
|
|
import logger from "./logger";
|
|
|
|
|
|
|
|
type EvaledPlugin = {
|
|
|
|
onLoad?(): void;
|
|
|
|
onUnload(): void;
|
|
|
|
};
|
2023-01-03 00:18:19 +00:00
|
|
|
|
|
|
|
export const plugins: Indexable<Plugin> = {};
|
2023-01-03 08:05:16 +00:00
|
|
|
const loadedPlugins: Indexable<EvaledPlugin> = {};
|
2023-01-03 00:18:19 +00:00
|
|
|
|
|
|
|
export async function fetchPlugin(url: string) {
|
|
|
|
if (!url.endsWith("/")) url += "/";
|
|
|
|
|
2023-01-03 08:05:16 +00:00
|
|
|
const id = url.split("://")[1];
|
|
|
|
if (typeof url !== "string" || url in plugins) throw new Error("Plugin ID invalid or taken");
|
2023-01-03 00:18:19 +00:00
|
|
|
|
|
|
|
let pluginManifest: PluginManifest;
|
|
|
|
|
|
|
|
try {
|
|
|
|
pluginManifest = await (await fetch(new URL("manifest.json", url), { cache: "no-store" })).json();
|
|
|
|
} catch {
|
|
|
|
throw new Error(`Failed to fetch manifest for ${url}`);
|
|
|
|
}
|
|
|
|
|
2023-01-03 08:05:16 +00:00
|
|
|
let pluginJs: string;
|
|
|
|
|
|
|
|
try {
|
|
|
|
pluginJs = await (await fetch(new URL("plugin.js", url), { cache: "no-store" })).text()
|
|
|
|
} catch {
|
|
|
|
throw new Error(`Failed to fetch JS for ${url}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins[id] = {
|
|
|
|
id: id,
|
2023-01-03 00:18:19 +00:00
|
|
|
manifest: pluginManifest,
|
2023-01-03 08:05:16 +00:00
|
|
|
enabled: false,
|
|
|
|
js: pluginJs,
|
2023-01-03 00:18:19 +00:00
|
|
|
};
|
2023-01-03 08:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bundlers don't like eval
|
|
|
|
const theSystemHasBeenDestroyed = eval;
|
|
|
|
|
|
|
|
export function evalPlugin(plugin: Plugin) {
|
|
|
|
// TODO: Refactor to not depend on own window object
|
|
|
|
const vendettaForPlugins = Object.assign({}, window.vendetta);
|
|
|
|
const pluginString = `(vendetta)=>{return ${plugin.js}}\n//# sourceURL=${plugin.id}`;
|
|
|
|
|
|
|
|
const ret = theSystemHasBeenDestroyed(pluginString)(vendettaForPlugins);
|
|
|
|
return typeof ret == "function" ? ret() : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function startPlugin(id: string) {
|
|
|
|
const plugin = plugins[id];
|
|
|
|
if (!plugin) throw new Error("Attempted to start non-existent plugin");
|
|
|
|
|
|
|
|
try {
|
|
|
|
const pluginRet: EvaledPlugin = evalPlugin(plugin);
|
|
|
|
loadedPlugins[id] = pluginRet;
|
|
|
|
pluginRet.onLoad?.();
|
|
|
|
plugin.enabled = true;
|
|
|
|
} catch(e) {
|
|
|
|
logger.error(`Plugin ${plugin.id} errored whilst loading, and will be unloaded`, e);
|
|
|
|
|
|
|
|
try {
|
|
|
|
loadedPlugins[plugin.id]?.onUnload?.();
|
|
|
|
} catch(e2) {
|
|
|
|
logger.error(`Plugin ${plugin.id} errored whilst unloading`, e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete loadedPlugins[id];
|
|
|
|
plugin.enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stopPlugin(id: string) {
|
|
|
|
const plugin = plugins[id];
|
|
|
|
const pluginRet = loadedPlugins[id];
|
|
|
|
if (!plugin) throw new Error("Attempted to stop non-existent plugin");
|
|
|
|
if (!pluginRet) throw new Error("Attempted to stop a non-started plugin");
|
|
|
|
|
|
|
|
try {
|
|
|
|
loadedPlugins[plugin.id]?.onUnload?.();
|
|
|
|
} catch(e) {
|
|
|
|
logger.error(`Plugin ${plugin.id} errored whilst unloading`, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete loadedPlugins[id];
|
|
|
|
plugin.enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: When startAllPlugins exists, return this so cleanup in index.ts is easier
|
|
|
|
const stopAllPlugins = () => Object.keys(loadedPlugins).forEach(stopPlugin);
|