Fixes some watcher issues

Also fixes BdApi~monkeyPatch not returning the cancel patch.
This commit is contained in:
Zack Rauen 2020-10-06 17:44:10 -04:00
parent 4405f0df23
commit 1ca7269b34
4 changed files with 22 additions and 17 deletions

View File

@ -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;
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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]});
}