From 1896a0d558b41ed246c68c3ee2cb5ed4a679bb05 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 31 Jan 2018 00:21:06 +0200 Subject: [PATCH] Content refresh --- client/src/index.js | 1 + client/src/modules/contentmanager.js | 51 ++++++++++++++++++++- client/src/modules/plugin.js | 4 +- client/src/modules/pluginmanager.js | 29 ++++++++++-- client/src/ui/components/bd/PluginsView.vue | 8 +++- 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/client/src/index.js b/client/src/index.js index 320fcf6b..9f859f39 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -14,6 +14,7 @@ import { Events, CssEditor, Globals, PluginManager, ThemeManager } from 'modules class BetterDiscord { constructor() { + window.pom = PluginManager; DOM.injectStyle(BdCss, 'bdmain'); Events.on('global-ready', this.globalReady.bind(this)); } diff --git a/client/src/modules/contentmanager.js b/client/src/modules/contentmanager.js index de25e9d0..b5576d4d 100644 --- a/client/src/modules/contentmanager.js +++ b/client/src/modules/contentmanager.js @@ -24,7 +24,6 @@ export default class { static async loadAllContent() { try { - await FileUtils.ensureDirectory(this.contentPath); const directories = await FileUtils.listDirectory(this.contentPath); @@ -43,6 +42,39 @@ export default class { } } + static async refreshContent() { + if (!this.localContent.length) return this.loadAllContent(); + + try { + await FileUtils.ensureDirectory(this.contentPath); + const directories = await FileUtils.listDirectory(this.contentPath); + + for (let dir of directories) { + // If content is already loaded this should resolve. + if (this.getContentByDirName(dir)) continue; + + try { + // Load if not + await this.preloadContent(dir); + } catch (err) { + //We don't want every plugin/theme to fail loading when one does + Logger.err(this.moduleName, err); + } + } + + for (let content of this.localContent) { + if (directories.includes(content.dirName)) continue; + //Plugin/theme was deleted manually, stop it and remove any reference + this.unloadContent(content); + } + + return this.localContent; + + } catch (err) { + throw err; + } + } + static async preloadContent(dirName, reload = false, index) { try { const contentPath = path.join(this.contentPath, dirName); @@ -102,4 +134,21 @@ export default class { return FileUtils.readJsonFromFile(configPath); } + //TODO make this nicer + static findContent(wild) { + let content = this.getContentByName(wild); + if (content) return content; + content = this.getContentById(wild); + if (content) return content; + content = this.getContentByPath(wild); + if (content) return content; + return this.getContentByDirName(wild); + } + + static getContentIndex(content) { return this.localContent.findIndex(c => c === content) } + static getContentByName(name) { return this.localContent.find(c => c.name === name) } + static getContentById(id) { return this.localContent.find(c => c.id === id) } + static getContentByPath(path) { return this.localContent.find(c => c.contentPath === path) } + static getContentByDirName(dirName) { return this.localContent.find(c => c.dirName === dirName) } + } \ No newline at end of file diff --git a/client/src/modules/plugin.js b/client/src/modules/plugin.js index bb514e9b..dbbe7de3 100644 --- a/client/src/modules/plugin.js +++ b/client/src/modules/plugin.js @@ -26,10 +26,10 @@ export default class { get name() { return this.info.name } get authors() { return this.info.authors } get version() { return this.info.version } - get pluginPath() { return this.paths.pluginPath } + get pluginPath() { return this.paths.contentPath } get dirName() { return this.paths.dirName } get enabled() { return this.userConfig.enabled } - get pluginConfig() { return this.userConfig.pluginConfig } + get pluginConfig() { return this.userConfig.config } start() { if (this.onStart) { diff --git a/client/src/modules/pluginmanager.js b/client/src/modules/pluginmanager.js index 9136f10b..6182153f 100644 --- a/client/src/modules/pluginmanager.js +++ b/client/src/modules/pluginmanager.js @@ -25,17 +25,38 @@ export default class extends ContentManager { return 'plugins'; } - static get loadAllPlugins() { - return this.loadAllContent; - } + static get loadAllPlugins() { return this.loadAllContent } + static get refreshPlugins() { return this.refreshContent } static get loadContent() { return this.loadPlugin } static async loadPlugin(paths, configs, info, main) { const plugin = window.require(paths.mainPath)(Plugin, {}, {}); - const instance = new plugin({ configs, info, main, paths: { pluginPath: paths.contentPath, dirName: paths.dirName } }); + const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName } }); if (instance.enabled) instance.start(); return instance; } + 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); + } + } + + 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 } + } diff --git a/client/src/ui/components/bd/PluginsView.vue b/client/src/ui/components/bd/PluginsView.vue index b78ebc7c..a3f19cea 100644 --- a/client/src/ui/components/bd/PluginsView.vue +++ b/client/src/ui/components/bd/PluginsView.vue @@ -63,8 +63,12 @@ } }, methods: { - showLocal() { }, - showOnline() { }, + showLocal() { + this.local = true; + }, + showOnline() { + this.local = false; + }, refreshLocal() { }, togglePlugin() { }, reloadPlugin() { },