BetterDiscordApp-v2/client/src/builtin/BuiltinModule.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-08-07 11:48:50 +02:00
/**
* BetterDiscord Builtin Module Base
* 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.
*/
import { Settings } from 'modules';
2018-08-23 21:50:12 +02:00
import { Patcher, MonkeyPatch as Patch, Cache } from 'modules';
import { ClientLogger as Logger } from 'common';
2018-08-07 11:48:50 +02:00
export default class BuiltinModule {
constructor() {
this._settingUpdated = this._settingUpdated.bind(this);
if (this.enabled) this.enabled = this.enabled.bind(this);
if (this.disabled) this.disabled = this.disabled.bind(this);
2018-08-23 21:50:12 +02:00
if (this.applyPatches) this.applyPatches = this.applyPatches.bind(this);
this.patch = this.patch.bind(this);
2018-08-07 11:48:50 +02:00
}
2019-03-10 18:45:20 +01:00
async init() {
2018-08-07 11:48:50 +02:00
this.setting.on('setting-updated', this._settingUpdated);
2018-08-23 21:50:12 +02:00
if (this.setting.value) {
2019-03-10 18:45:20 +01:00
if (this.enabled) await this.enabled();
2018-08-23 21:50:12 +02:00
if (this.applyPatches) this.applyPatches();
}
2018-08-07 11:48:50 +02:00
}
get setting() {
return Settings.getSetting(...this.settingPath);
}
2018-08-23 21:50:12 +02:00
get patches() {
return Patcher.getPatchesByCaller(`BD:${this.moduleName}`);
}
2019-03-10 18:45:20 +01:00
async _settingUpdated(e) {
if (e.value) {
2019-03-10 18:45:20 +01:00
if (this.enabled) await this.enabled(e);
if (this.applyPatches) await this.applyPatches();
if (this.rerenderPatchedComponents) this.rerenderPatchedComponents();
} else {
if (this.disabled) await this.disabled(e);
2018-08-23 21:50:12 +02:00
this.unpatch();
if (this.rerenderPatchedComponents) this.rerenderPatchedComponents();
2018-08-23 21:50:12 +02:00
}
}
get cache() {
return {
push: data => Cache.push(this.moduleName, data),
find: filter => Cache.find(this.moduleName, filter)
}
}
/**
* By default unpatch everything.
* Override to do something else.
*/
unpatch() {
Patcher.unpatchAll(`BD:${this.moduleName}`);
}
/**
* Patch a function in a module
* @param {any} module Module to patch
2018-08-23 21:51:35 +02:00
* @param {String} fnName Name of the function to patch
2018-08-23 21:50:12 +02:00
* @param {Function} cb Callback
* @param {String} [when=after] before|after|instead
*/
2018-08-23 21:51:35 +02:00
patch(module, fnName, cb, when = 'after') {
2018-08-23 21:50:12 +02:00
if (!['before', 'after', 'instead'].includes(when)) when = 'after';
2019-03-11 18:56:29 +01:00
return Patch(`BD:${this.moduleName}`, module)[when](fnName, cb.bind(this));
2018-08-07 11:48:50 +02:00
}
2018-11-26 06:22:15 +01:00
childPatch(module, fnName, child, cb, when = 'after') {
2019-03-11 18:56:29 +01:00
const last = child.pop();
2018-11-26 06:22:15 +01:00
this.patch(module, fnName, (component, args, retVal) => {
2019-03-12 17:35:12 +01:00
const unpatch = this.patch(child.reduce((obj, key) => obj[key], retVal), last, function(...args) {unpatch(); return cb.call(this, component, ...args);}, when);
2018-11-26 06:22:15 +01:00
});
}
2018-08-23 21:50:12 +02:00
/**
* Logger wraps
*/
log(message) {
Logger.log(this.moduleName, message);
}
warn(message) {
Logger.warn(this.moduleName, message);
}
info(message) {
Logger.warn(this.moduleName, message);
}
debug(message) {
Logger.dbg(this.moduleName, message);
}
2018-08-07 11:48:50 +02:00
}