BetterDiscordApp-rauenzi/src/modules/pluginmanager.js

214 lines
8.9 KiB
JavaScript
Raw Normal View History

2019-06-04 21:17:23 +02:00
import {PluginCookie, Plugins} from "data";
2019-05-29 05:48:41 +02:00
import ContentManager from "./contentmanager";
import Utilities from "./utilities";
import Emitter from "./emitter";
import DataStore from "./datastore";
2019-05-31 07:53:11 +02:00
import {Toasts, Modals} from "ui";
2019-05-28 20:19:48 +02:00
function PluginModule() {
}
PluginModule.prototype.loadPlugins = function () {
this.loadPluginData();
2019-05-28 23:27:25 +02:00
const errors = ContentManager.loadPlugins();
const plugins = Object.keys(Plugins);
for (let i = 0; i < plugins.length; i++) {
let plugin, name;
2019-05-28 20:19:48 +02:00
try {
2019-05-29 05:48:41 +02:00
plugin = Plugins[plugins[i]].plugin;
2019-05-28 20:19:48 +02:00
name = plugin.getName();
if (plugin.load && typeof(plugin.load) == "function") plugin.load();
}
catch (err) {
2019-05-29 05:48:41 +02:00
PluginCookie[name] = false;
Utilities.err("Plugins", name + " could not be loaded.", err);
errors.push({name: name, file: Plugins[plugins[i]].filename, message: "load() could not be fired.", error: {message: err.message, stack: err.stack}});
2019-05-28 20:19:48 +02:00
continue;
}
2019-05-29 05:48:41 +02:00
if (!PluginCookie[name]) PluginCookie[name] = false;
2019-05-28 20:19:48 +02:00
2019-05-29 05:48:41 +02:00
if (PluginCookie[name]) {
2019-05-28 20:19:48 +02:00
try {
plugin.start();
2019-06-03 22:25:08 +02:00
Toasts.show(`${plugin.getName()} v${plugin.getVersion()} has started.`);
2019-05-28 20:19:48 +02:00
}
catch (err) {
2019-05-29 05:48:41 +02:00
PluginCookie[name] = false;
Utilities.err("Plugins", name + " could not be started.", err);
errors.push({name: name, file: Plugins[plugins[i]].filename, message: "start() could not be fired.", error: {message: err.message, stack: err.stack}});
2019-05-28 20:19:48 +02:00
}
}
}
this.savePluginData();
require("electron").remote.getCurrentWebContents().on("did-navigate-in-page", this.channelSwitch.bind(this));
2019-05-29 05:48:41 +02:00
// if (SettingsCookie["fork-ps-5"]) ContentManager.watchContent("plugin");
2019-05-28 23:27:25 +02:00
return errors;
2019-05-28 20:19:48 +02:00
};
PluginModule.prototype.startPlugin = function(plugin, reload = false) {
try {
2019-05-29 05:48:41 +02:00
Plugins[plugin].plugin.start();
2019-06-03 22:25:08 +02:00
if (!reload) Toasts.show(`${Plugins[plugin].plugin.getName()} v${Plugins[plugin].plugin.getVersion()} has started.`);
2019-05-28 20:19:48 +02:00
}
catch (err) {
2019-06-03 22:25:08 +02:00
if (!reload) Toasts.show(`${Plugins[plugin].plugin.getName()} v${Plugins[plugin].plugin.getVersion()} could not be started.`, {type: "error"});
2019-05-29 05:48:41 +02:00
PluginCookie[plugin] = false;
2019-05-28 20:19:48 +02:00
this.savePluginData();
2019-05-30 07:06:17 +02:00
Utilities.err("Plugins", plugin + " could not be started.", err);
2019-05-28 20:19:48 +02:00
}
};
PluginModule.prototype.stopPlugin = function(plugin, reload = false) {
try {
2019-05-29 05:48:41 +02:00
Plugins[plugin].plugin.stop();
2019-06-03 22:25:08 +02:00
if (!reload) Toasts.show(`${Plugins[plugin].plugin.getName()} v${Plugins[plugin].plugin.getVersion()} has stopped.`);
2019-05-28 20:19:48 +02:00
}
catch (err) {
2019-06-03 22:25:08 +02:00
if (!reload) Toasts.show(`${Plugins[plugin].plugin.getName()} v${Plugins[plugin].plugin.getVersion()} could not be stopped.`, {type: "error"});
2019-05-29 05:48:41 +02:00
Utilities.err("Plugins", Plugins[plugin].plugin.getName() + " could not be stopped.", err);
2019-05-28 20:19:48 +02:00
}
};
PluginModule.prototype.enablePlugin = function (plugin, reload = false) {
2019-05-29 05:48:41 +02:00
if (PluginCookie[plugin]) return;
PluginCookie[plugin] = true;
2019-05-28 20:19:48 +02:00
this.savePluginData();
this.startPlugin(plugin, reload);
};
PluginModule.prototype.disablePlugin = function (plugin, reload = false) {
2019-05-29 05:48:41 +02:00
if (!PluginCookie[plugin]) return;
PluginCookie[plugin] = false;
2019-05-28 20:19:48 +02:00
this.savePluginData();
this.stopPlugin(plugin, reload);
};
PluginModule.prototype.togglePlugin = function (plugin) {
2019-05-29 05:48:41 +02:00
if (PluginCookie[plugin]) this.disablePlugin(plugin);
2019-05-28 20:19:48 +02:00
else this.enablePlugin(plugin);
};
PluginModule.prototype.loadPlugin = function(filename) {
const error = ContentManager.loadContent(filename, "plugin");
if (error) {
2019-06-03 22:25:08 +02:00
Modals.showContentErrors({plugins: [error]});
Toasts.show(`${filename} could not be loaded.`, {type: "error"});
2019-05-29 05:48:41 +02:00
return Utilities.err("ContentManager", `${filename} could not be loaded.`, error);
2019-05-28 20:19:48 +02:00
}
2019-05-29 05:48:41 +02:00
const plugin = Object.values(Plugins).find(p => p.filename == filename).plugin;
2019-05-28 20:19:48 +02:00
try { if (plugin.load && typeof(plugin.load) == "function") plugin.load();}
2019-06-03 22:25:08 +02:00
catch (err) {Modals.showContentErrors({plugins: [err]});}
2019-05-29 05:48:41 +02:00
Utilities.log("ContentManager", `${plugin.getName()} v${plugin.getVersion()} was loaded.`);
2019-06-03 22:25:08 +02:00
Toasts.show(`${plugin.getName()} v${plugin.getVersion()} was loaded.`, {type: "success"});
2019-05-29 05:48:41 +02:00
Emitter.dispatch("plugin-loaded", plugin.getName());
2019-05-28 20:19:48 +02:00
};
PluginModule.prototype.unloadPlugin = function(filenameOrName) {
2019-05-29 05:48:41 +02:00
const bdplugin = Object.values(Plugins).find(p => p.filename == filenameOrName) || Plugins[filenameOrName];
2019-05-28 20:19:48 +02:00
if (!bdplugin) return;
const plugin = bdplugin.plugin.getName();
2019-05-29 05:48:41 +02:00
if (PluginCookie[plugin]) this.disablePlugin(plugin, true);
const error = ContentManager.unloadContent(Plugins[plugin].filename, "plugin");
delete Plugins[plugin];
2019-05-28 20:19:48 +02:00
if (error) {
2019-06-03 22:25:08 +02:00
Modals.showContentErrors({plugins: [error]});
Toasts.show(`${plugin} could not be unloaded. It may have not been loaded yet.`, {type: "error"});
2019-05-29 05:48:41 +02:00
return Utilities.err("ContentManager", `${plugin} could not be unloaded. It may have not been loaded yet.`, error);
2019-05-28 20:19:48 +02:00
}
2019-05-29 05:48:41 +02:00
Utilities.log("ContentManager", `${plugin} was unloaded.`);
2019-06-03 22:25:08 +02:00
Toasts.show(`${plugin} was unloaded.`, {type: "success"});
2019-05-29 05:48:41 +02:00
Emitter.dispatch("plugin-unloaded", plugin);
2019-05-28 20:19:48 +02:00
};
PluginModule.prototype.reloadPlugin = function(filenameOrName) {
2019-05-29 05:48:41 +02:00
const bdplugin = Object.values(Plugins).find(p => p.filename == filenameOrName) || Plugins[filenameOrName];
2019-05-28 20:19:48 +02:00
if (!bdplugin) return this.loadPlugin(filenameOrName);
const plugin = bdplugin.plugin.getName();
2019-05-29 05:48:41 +02:00
const enabled = PluginCookie[plugin];
2019-05-28 20:19:48 +02:00
if (enabled) this.stopPlugin(plugin, true);
2019-05-29 05:48:41 +02:00
const error = ContentManager.reloadContent(Plugins[plugin].filename, "plugin");
2019-05-28 20:19:48 +02:00
if (error) {
2019-06-03 22:25:08 +02:00
Modals.showContentErrors({plugins: [error]});
Toasts.show(`${plugin} could not be reloaded.`, {type: "error"});
2019-05-29 05:48:41 +02:00
return Utilities.err("ContentManager", `${plugin} could not be reloaded.`, error);
2019-05-28 20:19:48 +02:00
}
2019-05-29 05:48:41 +02:00
if (Plugins[plugin].plugin.load && typeof(Plugins[plugin].plugin.load) == "function") Plugins[plugin].plugin.load();
2019-05-28 20:19:48 +02:00
if (enabled) this.startPlugin(plugin, true);
2019-05-29 05:48:41 +02:00
Utilities.log("ContentManager", `${plugin} v${Plugins[plugin].plugin.getVersion()} was reloaded.`);
2019-06-03 22:25:08 +02:00
Toasts.show(`${plugin} v${Plugins[plugin].plugin.getVersion()} was reloaded.`, {type: "success"});
2019-05-29 05:48:41 +02:00
Emitter.dispatch("plugin-reloaded", plugin);
2019-05-28 20:19:48 +02:00
};
PluginModule.prototype.updatePluginList = function() {
const results = ContentManager.loadNewContent("plugin");
for (const filename of results.added) this.loadPlugin(filename);
for (const name of results.removed) this.unloadPlugin(name);
};
PluginModule.prototype.loadPluginData = function () {
2019-06-06 21:57:25 +02:00
const saved = DataStore.getData("plugins");
2019-05-29 05:48:41 +02:00
if (!saved) return;
Object.assign(PluginCookie, saved);
2019-05-28 20:19:48 +02:00
};
PluginModule.prototype.savePluginData = function () {
2019-06-06 21:57:25 +02:00
DataStore.setData("plugins", PluginCookie);
2019-05-28 20:19:48 +02:00
};
PluginModule.prototype.newMessage = function () {
const plugins = Object.keys(Plugins);
for (let i = 0; i < plugins.length; i++) {
const plugin = Plugins[plugins[i]].plugin;
2019-05-29 05:48:41 +02:00
if (!PluginCookie[plugin.getName()]) continue;
2019-05-28 20:19:48 +02:00
if (typeof plugin.onMessage === "function") {
try { plugin.onMessage(); }
2019-05-29 05:48:41 +02:00
catch (err) { Utilities.err("Plugins", "Unable to fire onMessage for " + plugin.getName() + ".", err); }
2019-05-28 20:19:48 +02:00
}
}
};
PluginModule.prototype.channelSwitch = function () {
const plugins = Object.keys(Plugins);
for (let i = 0; i < plugins.length; i++) {
const plugin = Plugins[plugins[i]].plugin;
2019-05-29 05:48:41 +02:00
if (!PluginCookie[plugin.getName()]) continue;
2019-05-28 20:19:48 +02:00
if (typeof plugin.onSwitch === "function") {
try { plugin.onSwitch(); }
2019-05-29 05:48:41 +02:00
catch (err) { Utilities.err("Plugins", "Unable to fire onSwitch for " + plugin.getName() + ".", err); }
2019-05-28 20:19:48 +02:00
}
}
};
PluginModule.prototype.rawObserver = function(e) {
const plugins = Object.keys(Plugins);
for (let i = 0; i < plugins.length; i++) {
const plugin = Plugins[plugins[i]].plugin;
2019-05-29 05:48:41 +02:00
if (!PluginCookie[plugin.getName()]) continue;
2019-05-28 20:19:48 +02:00
if (typeof plugin.observer === "function") {
try { plugin.observer(e); }
2019-05-29 05:48:41 +02:00
catch (err) { Utilities.err("Plugins", "Unable to fire observer for " + plugin.getName() + ".", err); }
2019-05-28 20:19:48 +02:00
}
}
2019-05-28 23:27:25 +02:00
};
2019-06-07 05:52:08 +02:00
export default new PluginModule();
// makePlaceholderPlugin(data) {
// return {plugin: {
// start: () => {},
// getName: () => {return data.name || data.filename;},
// getAuthor: () => {return "???";},
// getDescription: () => {return data.message ? data.message : "This plugin was unable to be loaded. Check the author's page for updates.";},
// getVersion: () => {return "???";}
// },
// name: data.name || data.filename,
// filename: data.filename,
// source: data.source ? data.source : "",
// website: data.website ? data.website : ""
// };
// }