2023-04-15 02:04:48 +00:00
|
|
|
import { PluginManifest, Plugin } from "@types";
|
2023-02-06 07:48:55 +00:00
|
|
|
import { awaitSyncWrapper, createMMKVBackend, createStorage, wrapSync } from "@lib/storage";
|
2023-04-05 19:40:17 +00:00
|
|
|
import { MMKVManager } from "@lib/native";
|
2023-04-13 18:42:14 +00:00
|
|
|
import settings from "@lib/settings";
|
2023-02-19 22:29:25 +00:00
|
|
|
import logger, { logModule } from "@lib/logger";
|
2023-02-04 16:54:03 +00:00
|
|
|
import safeFetch from "@utils/safeFetch";
|
2023-02-17 06:21:56 +00:00
|
|
|
|
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-04-15 02:04:48 +00:00
|
|
|
export const plugins = wrapSync(createStorage<Record<string, Plugin>>(createMMKVBackend("VENDETTA_PLUGINS")));
|
|
|
|
const loadedPlugins: Record<string, EvaledPlugin> = {};
|
2023-01-07 23:05:14 +00:00
|
|
|
|
2023-02-05 23:30:51 +00:00
|
|
|
export async function fetchPlugin(id: string) {
|
2023-01-07 23:05:14 +00:00
|
|
|
if (!id.endsWith("/")) id += "/";
|
2023-02-05 23:30:51 +00:00
|
|
|
const existingPlugin = plugins[id];
|
2023-01-03 00:18:19 +00:00
|
|
|
|
|
|
|
let pluginManifest: PluginManifest;
|
|
|
|
|
|
|
|
try {
|
2023-02-04 16:54:03 +00:00
|
|
|
pluginManifest = await (await safeFetch(id + "manifest.json", { 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-02-05 23:30:51 +00:00
|
|
|
let pluginJs: string | undefined;
|
2023-01-03 08:05:16 +00:00
|
|
|
|
2023-02-05 23:30:51 +00:00
|
|
|
if (existingPlugin?.manifest.hash !== pluginManifest.hash) {
|
|
|
|
try {
|
|
|
|
// by polymanifest spec, plugins should always specify their main file, but just in case
|
|
|
|
pluginJs = await (await safeFetch(id + (pluginManifest.main || "index.js"), { cache: "no-store" })).text();
|
2023-02-19 22:35:53 +00:00
|
|
|
} catch {} // Empty catch, checked below
|
2023-02-05 23:30:51 +00:00
|
|
|
}
|
2023-01-04 08:01:56 +00:00
|
|
|
|
2023-02-19 22:35:53 +00:00
|
|
|
if (!pluginJs && !existingPlugin) throw new Error(`Failed to fetch JS for ${id}`);
|
|
|
|
|
2023-01-03 08:05:16 +00:00
|
|
|
plugins[id] = {
|
|
|
|
id: id,
|
2023-01-03 00:18:19 +00:00
|
|
|
manifest: pluginManifest,
|
2023-02-05 23:30:51 +00:00
|
|
|
enabled: existingPlugin?.enabled ?? false,
|
|
|
|
update: existingPlugin?.update ?? true,
|
|
|
|
js: pluginJs ?? existingPlugin.js,
|
2023-01-03 00:18:19 +00:00
|
|
|
};
|
2023-02-05 23:30:51 +00:00
|
|
|
}
|
2023-01-30 00:40:56 +00:00
|
|
|
|
2023-02-05 23:30:51 +00:00
|
|
|
export async function installPlugin(id: string, enabled = true) {
|
2023-02-05 23:57:58 +00:00
|
|
|
if (!id.endsWith("/")) id += "/";
|
2023-02-05 23:30:51 +00:00
|
|
|
if (typeof id !== "string" || id in plugins) throw new Error("Plugin already installed");
|
|
|
|
await fetchPlugin(id);
|
2023-01-30 00:40:56 +00:00
|
|
|
if (enabled) await startPlugin(id);
|
2023-01-03 08:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 21:05:47 +00:00
|
|
|
export async function evalPlugin(plugin: Plugin) {
|
2023-01-10 22:05:40 +00:00
|
|
|
const vendettaForPlugins = {
|
|
|
|
...window.vendetta,
|
|
|
|
plugin: {
|
2023-02-20 18:47:30 +00:00
|
|
|
id: plugin.id,
|
2023-01-10 22:05:40 +00:00
|
|
|
manifest: plugin.manifest,
|
2023-01-29 21:05:47 +00:00
|
|
|
// Wrapping this with wrapSync is NOT an option.
|
2023-04-15 02:04:48 +00:00
|
|
|
storage: await createStorage<Record<string, any>>(createMMKVBackend(plugin.id)),
|
2023-02-19 22:29:25 +00:00
|
|
|
},
|
|
|
|
logger: new logModule(`Vendetta » ${plugin.manifest.name}`),
|
2023-01-10 22:05:40 +00:00
|
|
|
};
|
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-02-05 23:57:58 +00:00
|
|
|
if (!id.endsWith("/")) id += "/";
|
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-04-13 18:42:14 +00:00
|
|
|
if (!settings.safeMode?.enabled) {
|
|
|
|
const pluginRet: EvaledPlugin = await evalPlugin(plugin);
|
|
|
|
loadedPlugins[id] = pluginRet;
|
|
|
|
pluginRet.onLoad?.();
|
|
|
|
}
|
2023-01-03 08:05:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 02:30:46 +00:00
|
|
|
export function stopPlugin(id: string, disable = true) {
|
2023-02-05 23:57:58 +00:00
|
|
|
if (!id.endsWith("/")) id += "/";
|
2023-01-03 08:05:16 +00:00
|
|
|
const plugin = plugins[id];
|
|
|
|
const pluginRet = loadedPlugins[id];
|
|
|
|
if (!plugin) throw new Error("Attempted to stop non-existent plugin");
|
|
|
|
|
2023-04-13 18:42:14 +00:00
|
|
|
if (!settings.safeMode?.enabled) {
|
|
|
|
try {
|
|
|
|
pluginRet?.onUnload?.();
|
|
|
|
} catch(e) {
|
|
|
|
logger.error(`Plugin ${plugin.id} errored whilst unloading`, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete loadedPlugins[id];
|
2023-01-03 08:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 02:30:46 +00:00
|
|
|
disable && (plugin.enabled = false);
|
2023-01-03 08:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 23:05:14 +00:00
|
|
|
export function removePlugin(id: string) {
|
2023-02-05 23:57:58 +00:00
|
|
|
if (!id.endsWith("/")) id += "/";
|
2023-01-07 23:21:29 +00:00
|
|
|
const plugin = plugins[id];
|
|
|
|
if (plugin.enabled) stopPlugin(id);
|
2023-04-04 18:56:34 +00:00
|
|
|
MMKVManager.removeItem(id);
|
2023-01-07 23:05:14 +00:00
|
|
|
delete plugins[id];
|
|
|
|
}
|
|
|
|
|
2023-02-04 02:30:46 +00:00
|
|
|
export async function initPlugins() {
|
2023-04-13 18:42:14 +00:00
|
|
|
await awaitSyncWrapper(settings);
|
2023-01-30 13:59:47 +00:00
|
|
|
await awaitSyncWrapper(plugins);
|
|
|
|
const allIds = Object.keys(plugins);
|
2023-02-20 18:58:07 +00:00
|
|
|
|
2023-04-13 18:42:14 +00:00
|
|
|
if (!settings.safeMode?.enabled) {
|
|
|
|
// Loop over any plugin that is enabled, update it if allowed, then start it.
|
|
|
|
await Promise.allSettled(allIds.filter(pl => plugins[pl].enabled).map(async (pl) => (plugins[pl].update && await fetchPlugin(pl), await startPlugin(pl))));
|
|
|
|
// Wait for the above to finish, then update all disabled plugins that are allowed to.
|
|
|
|
allIds.filter(pl => !plugins[pl].enabled && plugins[pl].update).forEach(pl => fetchPlugin(pl));
|
|
|
|
};
|
2023-02-04 02:30:46 +00:00
|
|
|
|
|
|
|
return stopAllPlugins;
|
2023-01-30 13:59:47 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 00:10:34 +00:00
|
|
|
const stopAllPlugins = () => Object.keys(loadedPlugins).forEach(p => stopPlugin(p, false));
|
2023-02-04 02:30:46 +00:00
|
|
|
|
2023-02-05 23:57:58 +00:00
|
|
|
export const getSettings = (id: string) => loadedPlugins[id]?.settings;
|