From 1ca7269b34477675d6e363407100a91c311c2b24 Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Tue, 6 Oct 2020 17:44:10 -0400 Subject: [PATCH] Fixes some watcher issues Also fixes BdApi~monkeyPatch not returning the cancel patch. --- src/modules/addonmanager.js | 14 ++++++++++---- src/modules/pluginapi.js | 7 ++++--- src/modules/pluginmanager.js | 7 ++++--- src/modules/thememanager.js | 11 ++++------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/modules/addonmanager.js b/src/modules/addonmanager.js index 24f6516c..57f8e567 100644 --- a/src/modules/addonmanager.js +++ b/src/modules/addonmanager.js @@ -199,10 +199,13 @@ export default class AddonManager { if (typeof(filename) === "undefined") return; try {__non_webpack_require__(path.resolve(this.addonFolder, filename));} catch (error) {return new AddonError(filename, filename, Strings.Addons.compileError, {message: error.message, stack: error.stack});} + const addon = __non_webpack_require__(path.resolve(this.addonFolder, filename)); if (this.addonList.find(c => c.id == addon.id)) return new AddonError(addon.name, filename, Strings.Addons.alreadyExists.format({type: this.prefix, name: addon.name})); + const error = this.initializeAddon(addon); if (error) return error; + this.addonList.push(addon); if (shouldToast) Toasts.success(`${addon.name} v${addon.version} was loaded.`); this.emit("loaded", addon.id); @@ -225,8 +228,8 @@ export default class AddonManager { reloadAddon(idOrFileOrAddon, shouldToast = true) { const addon = typeof(idOrFileOrAddon) == "string" ? this.addonList.find(c => c.id == idOrFileOrAddon || c.filename == idOrFileOrAddon) : idOrFileOrAddon; const didUnload = this.unloadAddon(addon, shouldToast, true); - if (!didUnload) return didUnload; - return this.loadAddon(addon.filename, shouldToast); + if (addon && !didUnload) return didUnload; + return this.loadAddon(addon ? addon.filename : idOrFileOrAddon, shouldToast); } isLoaded(idOrFile) { @@ -288,7 +291,10 @@ export default class AddonManager { for (const filename of files) { const absolutePath = path.resolve(this.addonFolder, filename); - if (!fs.statSync(absolutePath).isFile()) continue; + const stats = fs.statSync(absolutePath); + if (!stats || !stats.isFile()) continue; + this.timeCache[filename] = stats.mtime.getTime(); + if (!filename.endsWith(this.extension)) { // Lets check to see if this filename has the duplicated file pattern `something(1).ext` const match = filename.match(this.duplicatePattern); @@ -311,7 +317,7 @@ export default class AddonManager { } this.saveState(); - // if (Settings.get(this.collection, this.category, this.id)) this.watchAddons(); + if (Settings.get(this.collection, this.category, this.id)) this.watchAddons(); return errors; } diff --git a/src/modules/pluginapi.js b/src/modules/pluginapi.js index b847983b..03e66125 100644 --- a/src/modules/pluginapi.js +++ b/src/modules/pluginapi.js @@ -194,7 +194,7 @@ BdApi.deleteData = function(pluginName, key) { // return cancel; // }; BdApi.monkeyPatch = function(what, methodName, options) { - const {before, after, instead, once = false} = options; + const {before, after, instead, once = false, callerId = "BdApi"} = options; const patchType = before ? "before" : after ? "after" : instead ? "instead" : ""; if (!patchType) return Logger.err("BdApi", "Must provide one of: after, before, instead"); const originalMethod = what[methodName]; @@ -202,7 +202,7 @@ BdApi.monkeyPatch = function(what, methodName, options) { originalMethod: originalMethod, callOriginalMethod: () => data.originalMethod.apply(data.thisObject, data.methodArguments) }; - data.cancelPatch = Patcher[patchType]("BdApi", what, methodName, (thisObject, args, returnValue) => { + data.cancelPatch = Patcher[patchType](callerId, what, methodName, (thisObject, args, returnValue) => { data.thisObject = thisObject; data.methodArguments = args; data.returnValue = returnValue; @@ -211,9 +211,10 @@ BdApi.monkeyPatch = function(what, methodName, options) { if (once) data.cancelPatch(); } catch (err) { - // Logger.err("monkeyPatch", `Error in the ${patchType} of ${methodName}`); + Logger.err(`${callerId}:monkeyPatch`, `Error in the ${patchType} of ${methodName}`); } }); + return data.cancelPatch; }; // Event when element is removed BdApi.onRemoved = function(node, callback) { diff --git a/src/modules/pluginmanager.js b/src/modules/pluginmanager.js index f797121c..220c15d0 100644 --- a/src/modules/pluginmanager.js +++ b/src/modules/pluginmanager.js @@ -56,10 +56,11 @@ export default new class PluginManager extends AddonManager { togglePlugin(id) {return this.toggleAddon(id);} unloadPlugin(idOrFileOrAddon) {return this.unloadAddon(idOrFileOrAddon);} + loadPlugin(filename) {return this.loadAddon(filename);} - loadPlugin(filename) { - const error = this.loadAddon(filename); - if (error) Modals.showAddonErrors({themes: [error]}); + loadAddon(filename) { + const error = super.loadAddon(filename); + if (error) Modals.showAddonErrors({plugins: [error]}); } reloadPlugin(idOrFileOrAddon) { diff --git a/src/modules/thememanager.js b/src/modules/thememanager.js index 70ff75e9..4c393e7f 100644 --- a/src/modules/thememanager.js +++ b/src/modules/thememanager.js @@ -43,14 +43,11 @@ export default new class ThemeManager extends AddonManager { toggleTheme(id) {return this.toggleAddon(id);} unloadTheme(idOrFileOrAddon) {return this.unloadAddon(idOrFileOrAddon);} + loadTheme(filename) {return this.loadAddon(filename);} + reloadTheme(idOrFileOrAddon) {return this.reloadAddon(idOrFileOrAddon);} - loadTheme(filename) { - const error = this.loadAddon(filename); - if (error) Modals.showAddonErrors({themes: [error]}); - } - - reloadTheme(idOrFileOrAddon) { - const error = this.reloadAddon(idOrFileOrAddon); + loadAddon(filename) { + const error = super.loadAddon(filename); if (error) Modals.showAddonErrors({themes: [error]}); }