2018-01-30 14:20:24 +01:00
|
|
|
/**
|
|
|
|
* BetterDiscord Plugin Manager Module
|
|
|
|
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
|
|
|
|
* All rights reserved.
|
|
|
|
* https://betterdiscord.net
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2018-01-30 16:59:27 +01:00
|
|
|
import ContentManager from './contentmanager';
|
|
|
|
import Plugin from './plugin';
|
2018-02-11 23:07:23 +01:00
|
|
|
import PluginApi from './pluginapi';
|
|
|
|
import Vendor from './vendor';
|
2018-01-31 13:17:40 +01:00
|
|
|
import { ClientLogger as Logger } from 'common';
|
2018-02-07 13:15:46 +01:00
|
|
|
import { Events } from 'modules';
|
2018-01-30 14:20:24 +01:00
|
|
|
|
2018-02-13 17:18:01 +01:00
|
|
|
|
2018-01-30 16:59:27 +01:00
|
|
|
export default class extends ContentManager {
|
2018-01-30 14:20:24 +01:00
|
|
|
|
|
|
|
static get localPlugins() {
|
2018-01-30 16:59:27 +01:00
|
|
|
return this.localContent;
|
|
|
|
}
|
|
|
|
|
2018-02-07 17:02:27 +01:00
|
|
|
static get contentType() {
|
|
|
|
return 'plugin';
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:59:27 +01:00
|
|
|
static get moduleName() {
|
2018-02-13 17:57:05 +01:00
|
|
|
return 'Plugin Manager';
|
2018-01-30 14:20:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 16:59:27 +01:00
|
|
|
static get pathId() {
|
|
|
|
return 'plugins';
|
2018-01-30 14:25:35 +01:00
|
|
|
}
|
|
|
|
|
2018-02-13 17:57:05 +01:00
|
|
|
static async loadAllPlugins(supressErrors) {
|
|
|
|
const loadAll = await this.loadAllContent(supressErrors);
|
2018-02-13 17:22:48 +01:00
|
|
|
this.localPlugins.forEach(plugin => {
|
|
|
|
if (plugin.enabled) plugin.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
return loadAll;
|
|
|
|
}
|
2018-01-30 23:21:06 +01:00
|
|
|
static get refreshPlugins() { return this.refreshContent }
|
2018-01-30 16:59:27 +01:00
|
|
|
|
|
|
|
static get loadContent() { return this.loadPlugin }
|
2018-02-13 17:06:36 +01:00
|
|
|
static async loadPlugin(paths, configs, info, main, type) {
|
2018-02-11 23:07:23 +01:00
|
|
|
const plugin = window.require(paths.mainPath)(Plugin, new PluginApi(info), Vendor);
|
2018-02-13 17:18:01 +01:00
|
|
|
const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName, mainPath: paths.mainPath } });
|
2018-01-30 16:59:27 +01:00
|
|
|
return instance;
|
2018-01-30 14:38:34 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 23:21:06 +01:00
|
|
|
static get unloadContent() { return this.unloadPlugin }
|
|
|
|
static async unloadPlugin(plugin) {
|
|
|
|
try {
|
|
|
|
if (plugin.enabled) plugin.stop();
|
|
|
|
const { pluginPath } = plugin;
|
|
|
|
const index = this.getPluginIndex(plugin);
|
|
|
|
|
|
|
|
delete window.require.cache[window.require.resolve(pluginPath)];
|
|
|
|
this.localPlugins.splice(index, 1);
|
|
|
|
} catch (err) {
|
|
|
|
//This might fail but we don't have any other option at this point
|
|
|
|
Logger.err('PluginManager', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:17:15 +01:00
|
|
|
static async reloadPlugin(plugin) {
|
|
|
|
const _plugin = plugin instanceof Plugin ? plugin : this.findPlugin(plugin);
|
|
|
|
if (!_plugin) throw { 'message': 'Attempted to reload a plugin that is not loaded?' };
|
|
|
|
if (!_plugin.stop()) throw { 'message': 'Plugin failed to stop!' };
|
|
|
|
const index = this.getPluginIndex(_plugin);
|
|
|
|
const { pluginPath, dirName } = _plugin;
|
|
|
|
|
|
|
|
delete window.require.cache[window.require.resolve(pluginPath)];
|
|
|
|
|
|
|
|
return this.preloadContent(dirName, true, index);
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:32:20 +01:00
|
|
|
static stopPlugin(name) {
|
|
|
|
const plugin = name instanceof Plugin ? name : this.getPluginByName(name);
|
|
|
|
try {
|
|
|
|
if (plugin) return plugin.stop();
|
|
|
|
} catch (err) {
|
|
|
|
// Logger.err('PluginManager', err);
|
|
|
|
}
|
|
|
|
return true; //Return true anyways since plugin doesn't exist
|
|
|
|
}
|
|
|
|
|
|
|
|
static startPlugin(name) {
|
|
|
|
const plugin = name instanceof Plugin ? name : this.getPluginByName(name);
|
|
|
|
try {
|
|
|
|
if (plugin) return plugin.start();
|
|
|
|
} catch (err) {
|
|
|
|
// Logger.err('PluginManager', err);
|
|
|
|
}
|
|
|
|
return true; //Return true anyways since plugin doesn't exist
|
|
|
|
}
|
|
|
|
|
2018-01-30 23:21:06 +01:00
|
|
|
static get findPlugin() { return this.findContent }
|
|
|
|
static get getPluginIndex() { return this.getContentIndex }
|
|
|
|
static get getPluginByName() { return this.getContentByName }
|
|
|
|
static get getPluginById() { return this.getContentById }
|
|
|
|
static get getPluginByPath() { return this.getContentByPath }
|
|
|
|
static get getPluginByDirName() { return this.getContentByDirName }
|
|
|
|
|
2018-02-12 23:49:44 +01:00
|
|
|
static get waitForPlugin() { return this.waitForContent }
|
|
|
|
|
2018-01-30 14:20:24 +01:00
|
|
|
}
|