2022-08-29 18:27:47 +00:00
|
|
|
import Plugins from "plugins";
|
2022-08-31 02:07:16 +00:00
|
|
|
import { Settings } from "../api/settings";
|
2022-08-29 18:27:47 +00:00
|
|
|
import Logger from "../utils/logger";
|
2022-08-31 20:08:05 +00:00
|
|
|
import { Patch, Plugin } from "../utils/types";
|
2022-08-29 16:11:44 +00:00
|
|
|
|
2022-08-29 18:27:47 +00:00
|
|
|
const logger = new Logger("PluginManager", "#a6d189");
|
|
|
|
|
|
|
|
export const plugins = Plugins;
|
|
|
|
export const patches = [] as Patch[];
|
|
|
|
|
2022-08-31 21:04:18 +00:00
|
|
|
for (const plugin of Object.values(Plugins)) if (plugin.patches && Settings.plugins[plugin.name].enabled) {
|
2022-08-29 18:27:47 +00:00
|
|
|
for (const patch of plugin.patches) {
|
|
|
|
patch.plugin = plugin.name;
|
|
|
|
if (!Array.isArray(patch.replacement)) patch.replacement = [patch.replacement];
|
|
|
|
patches.push(patch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function startAll() {
|
2022-08-31 21:04:18 +00:00
|
|
|
for (const plugin in Plugins) if (Settings.plugins[plugin].enabled) {
|
|
|
|
startPlugin(Plugins[plugin]);
|
2022-08-31 20:08:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function startPlugin(p: Plugin) {
|
|
|
|
if (p.start) {
|
|
|
|
logger.info("Starting plugin", p.name);
|
|
|
|
if (p.started) {
|
|
|
|
logger.warn(`${p.name} already started`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
p.start();
|
|
|
|
p.started = true;
|
|
|
|
return true;
|
|
|
|
} catch (err: any) {
|
|
|
|
logger.error(`Failed to start ${p.name}\n`, err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stopPlugin(p: Plugin) {
|
|
|
|
if (p.stop) {
|
|
|
|
logger.info("Stopping plugin", p.name);
|
|
|
|
if (!p.started) {
|
|
|
|
logger.warn(`${p.name} already stopped / never started`);
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-29 18:27:47 +00:00
|
|
|
try {
|
2022-08-31 20:08:05 +00:00
|
|
|
p.stop();
|
|
|
|
p.started = false;
|
|
|
|
return true;
|
|
|
|
} catch (err: any) {
|
|
|
|
logger.error(`Failed to stop ${p.name}\n`, err);
|
|
|
|
return false;
|
2022-08-29 18:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-16 20:59:34 +00:00
|
|
|
}
|