Add plugin exports

This commit is contained in:
Samuel Elliott 2018-02-12 22:49:44 +00:00
parent 663af3ca83
commit 7664266dfe
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
8 changed files with 98 additions and 10 deletions

View File

@ -112,6 +112,8 @@ export default class {
const readConfig = await this.readConfig(contentPath);
const mainPath = path.join(contentPath, readConfig.main);
readConfig.defaultConfig = readConfig.defaultConfig || [];
const userConfig = {
enabled: false,
config: readConfig.defaultConfig
@ -188,4 +190,16 @@ export default class {
static getContentByPath(path) { return this.localContent.find(c => c.contentPath === path) }
static getContentByDirName(dirName) { return this.localContent.find(c => c.dirName === dirName) }
static waitForContent(content_id) {
return new Promise((resolve, reject) => {
const check = () => {
const content = this.getContentById(content_id);
if (content) return resolve(content);
setTimeout(check, 100);
};
check();
});
}
}

View File

@ -27,13 +27,15 @@ export default class {
get main() { return this.__pluginInternals.main }
get defaultConfig() { return this.configs.defaultConfig }
get userConfig() { return this.configs.userConfig }
get id() { return this.info.id || this.info.name.replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-') }
get name() { return this.info.name }
get authors() { return this.info.authors }
get version() { return this.info.version }
get pluginPath() { return this.paths.contentPath }
get dirName() { return this.paths.dirName }
get enabled() { return this.userConfig.enabled }
get pluginConfig() { return this.userConfig.config }
get pluginConfig() { return this.userConfig.config || [] }
get exports() { return this._exports ? this._exports : (this._exports = this.getExports()) }
getSetting(setting_id, category_id) {
for (let category of this.pluginConfig) {
@ -85,10 +87,8 @@ export default class {
}
start() {
if (this.onStart) {
const started = this.onStart();
if (!started) return false;
}
if (this.onstart && !this.onstart()) return false;
if (this.onStart && !this.onStart()) return false;
if (!this.enabled) {
this.userConfig.enabled = true;
@ -99,10 +99,8 @@ export default class {
}
stop() {
if (this.onStop) {
const stopped = this.onStop();
if (!stopped) return false;
}
if (this.onstop && !this.onstop()) return false;
if (this.onStop && !this.onStop()) return false;
this.userConfig.enabled = false;
this.saveConfiguration();

View File

@ -9,6 +9,8 @@
*/
import { ClientLogger as Logger } from 'common';
import PluginManager from './pluginmanager';
import ThemeManager from './thememanager';
import Events from './events';
export default class PluginApi {
@ -64,4 +66,37 @@ export default class PluginApi {
}
}
async getPlugin(plugin_id) {
// This should require extra permissions
return await PluginManager.waitForPlugin(plugin_id);
}
getPlugins(plugin_id) {
return PluginManager.localContent.map(plugin => plugin.id);
}
get Plugins() {
return {
getPlugin: this.getPlugin.bind(this),
getPlugins: this.getPlugins.bind(this)
};
}
async getTheme(theme_id) {
// This should require extra permissions
return await ThemeManager.waitForContent(theme_id);
}
getThemes(plugin_id) {
return ThemeManager.localContent.map(theme => theme.id);
}
get Themes() {
return {
getTheme: this.getTheme.bind(this),
getThemes: this.getThemes.bind(this)
};
}
async require(plugin_id) {
const plugin = await PluginManager.waitForPlugin(plugin_id);
return plugin.exports;
}
}

View File

@ -99,4 +99,6 @@ export default class extends ContentManager {
static get getPluginByPath() { return this.getContentByPath }
static get getPluginByDirName() { return this.getContentByDirName }
static get waitForPlugin() { return this.waitForContent }
}

View File

@ -0,0 +1,10 @@
{
"info": {
"id": "example-plugin-3",
"name": "Example Plugin 3",
"authors": [ "Samuel Elliott" ],
"version": 1.0,
"description": "A plugin for testing BetterDiscord plugin exports"
},
"main": "index.js"
}

View File

@ -0,0 +1,18 @@
module.exports = (Plugin, Api, Vendor) => {
const { $, moment } = Vendor;
const { Events, Logger } = Api;
return class extends Plugin {
async onstart() {
const example_plugin = await Api.require('example-plugin');
console.log('Example plugin exports:', example_plugin.test1());
}
async onstop() {
const example_plugin = await Api.require('example-plugin');
console.log('Example plugin exports:', example_plugin.test2());
}
}
};

View File

@ -1,5 +1,6 @@
{
"info": {
"id": "example-plugin",
"name": "Example Plugin",
"authors": [ "Jiiks" ],
"version": 1.0,

View File

@ -21,6 +21,16 @@ module.exports = (Plugin, Api, Vendor) => {
Logger.log(e);
}
getExports() {
return {
test1: this.test1.bind(this),
test2: this.test2.bind(this)
};
}
test1() { return 'It works!'; }
test2() { return 'This works too!'; }
settingChanged(category, setting_id, value) {
if (!this.enabled) return;
Logger.log(`${category}/${setting_id} changed to ${value}`);