2023-01-03 00:18:19 +00:00
|
|
|
import { Indexable, PluginManifest, Plugin } from "@types";
|
2023-01-10 22:05:40 +00:00
|
|
|
import { navigation } from "@metro/common";
|
2023-01-29 21:05:47 +00:00
|
|
|
import { createStorage, wrapSync } from "@lib/storage";
|
2023-01-07 23:05:14 +00:00
|
|
|
import logger from "@lib/logger";
|
2023-01-29 21:05:47 +00:00
|
|
|
import Subpage from "@ui/settings/components/Subpage";
|
2023-01-03 08:05:16 +00:00
|
|
|
|
|
|
|
type EvaledPlugin = {
|
|
|
|
onLoad?(): void;
|
|
|
|
onUnload(): void;
|
2023-01-05 23:07:58 +00:00
|
|
|
settings: JSX.Element;
|
2023-01-03 08:05:16 +00:00
|
|
|
};
|
2023-01-03 00:18:19 +00:00
|
|
|
|
2023-01-29 21:05:47 +00:00
|
|
|
export const plugins = wrapSync(createStorage<Indexable<Plugin>>("VENDETTA_PLUGINS").then(async function (store) {
|
|
|
|
for (let p of Object.keys(store)) {
|
|
|
|
const plugin: Plugin = store[p];
|
2023-01-03 00:18:19 +00:00
|
|
|
|
2023-01-29 21:05:47 +00:00
|
|
|
if (store[p].update) await fetchPlugin(plugin.id);
|
|
|
|
if (store[p].enabled && plugins[p]) await startPlugin(p);
|
2023-01-10 08:05:03 +00:00
|
|
|
}
|
2023-01-29 21:05:47 +00:00
|
|
|
|
|
|
|
return store;
|
|
|
|
}));
|
2023-01-07 23:05:14 +00:00
|
|
|
const loadedPlugins: Indexable<EvaledPlugin> = {};
|
|
|
|
|
|
|
|
export async function fetchPlugin(id: string) {
|
|
|
|
if (!id.endsWith("/")) id += "/";
|
|
|
|
if (typeof id !== "string" || id in plugins) throw new Error("Plugin ID invalid or taken");
|
2023-01-03 00:18:19 +00:00
|
|
|
|
|
|
|
let pluginManifest: PluginManifest;
|
|
|
|
|
|
|
|
try {
|
2023-01-07 23:05:14 +00:00
|
|
|
pluginManifest = await (await fetch(new URL("manifest.json", id), { cache: "no-store" })).json();
|
2023-01-03 00:18:19 +00:00
|
|
|
} catch {
|
2023-01-07 23:05:14 +00:00
|
|
|
throw new Error(`Failed to fetch manifest for ${id}`);
|
2023-01-03 00:18:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 08:05:16 +00:00
|
|
|
let pluginJs: string;
|
|
|
|
|
2023-01-04 08:01:56 +00:00
|
|
|
// TODO: Remove duplicate error if possible
|
2023-01-03 08:05:16 +00:00
|
|
|
try {
|
2023-01-04 22:39:28 +00:00
|
|
|
// by polymanifest spec, plugins should always specify their main file, but just in case
|
2023-01-07 23:05:14 +00:00
|
|
|
pluginJs = await (await fetch(new URL(pluginManifest.main || "index.js", id), { cache: "no-store" })).text();
|
2023-01-03 08:05:16 +00:00
|
|
|
} catch {
|
2023-01-07 23:05:14 +00:00
|
|
|
throw new Error(`Failed to fetch JS for ${id}`);
|
2023-01-03 08:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 23:05:14 +00:00
|
|
|
if (pluginJs.length === 0) throw new Error(`Failed to fetch JS for ${id}`);
|
2023-01-04 08:01:56 +00:00
|
|
|
|
2023-01-03 08:05:16 +00:00
|
|
|
plugins[id] = {
|
|
|
|
id: id,
|
2023-01-03 00:18:19 +00:00
|
|
|
manifest: pluginManifest,
|
2023-01-03 08:05:16 +00:00
|
|
|
enabled: false,
|
2023-01-07 23:05:14 +00:00
|
|
|
update: true,
|
2023-01-03 08:05:16 +00:00
|
|
|
js: pluginJs,
|
2023-01-03 00:18:19 +00:00
|
|
|
};
|
2023-01-03 08:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 21:05:47 +00:00
|
|
|
export async function evalPlugin(plugin: Plugin) {
|
2023-01-03 08:05:16 +00:00
|
|
|
// TODO: Refactor to not depend on own window object
|
2023-01-10 22:05:40 +00:00
|
|
|
const vendettaForPlugins = {
|
|
|
|
...window.vendetta,
|
|
|
|
plugin: {
|
|
|
|
manifest: plugin.manifest,
|
2023-01-29 21:05:47 +00:00
|
|
|
// Wrapping this with wrapSync is NOT an option.
|
|
|
|
storage: await createStorage<Indexable<any>>(plugin.id),
|
2023-01-10 22:05:40 +00:00
|
|
|
showSettings: () => showSettings(plugin),
|
|
|
|
}
|
|
|
|
};
|
2023-01-04 08:01:56 +00:00
|
|
|
const pluginString = `vendetta=>{return ${plugin.js}}\n//# sourceURL=${plugin.id}`;
|
2023-01-03 08:05:16 +00:00
|
|
|
|
2023-01-04 08:01:56 +00:00
|
|
|
const raw = (0, eval)(pluginString)(vendettaForPlugins);
|
|
|
|
const ret = typeof raw == "function" ? raw() : raw;
|
|
|
|
return ret.default || ret;
|
2023-01-03 08:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 21:05:47 +00:00
|
|
|
export async function startPlugin(id: string) {
|
2023-01-03 08:05:16 +00:00
|
|
|
const plugin = plugins[id];
|
|
|
|
if (!plugin) throw new Error("Attempted to start non-existent plugin");
|
|
|
|
|
|
|
|
try {
|
2023-01-29 21:05:47 +00:00
|
|
|
const pluginRet: EvaledPlugin = await evalPlugin(plugin);
|
2023-01-03 08:05:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-07 23:05:14 +00:00
|
|
|
export function removePlugin(id: string) {
|
2023-01-07 23:21:29 +00:00
|
|
|
const plugin = plugins[id];
|
|
|
|
if (plugin.enabled) stopPlugin(id);
|
2023-01-07 23:05:14 +00:00
|
|
|
delete plugins[id];
|
|
|
|
}
|
|
|
|
|
2023-01-05 23:07:58 +00:00
|
|
|
export const getSettings = (id: string) => loadedPlugins[id]?.settings;
|
2023-01-10 22:05:40 +00:00
|
|
|
|
|
|
|
export function showSettings(plugin: Plugin) {
|
|
|
|
const settings = getSettings(plugin.id);
|
|
|
|
if (!settings) return logger.error(`Plugin ${plugin.id} is not loaded or has no settings`);
|
|
|
|
|
2023-01-14 02:39:10 +00:00
|
|
|
navigation.push(Subpage, {
|
|
|
|
name: plugin.manifest.name,
|
2023-01-10 22:05:40 +00:00
|
|
|
children: settings,
|
|
|
|
});
|
|
|
|
}
|