2019-06-19 21:24:05 +02:00
|
|
|
import Logger from "../modules/logger";
|
2019-05-30 17:44:05 +02:00
|
|
|
import Events from "../modules/emitter";
|
2019-06-06 21:57:25 +02:00
|
|
|
import Settings from "../modules/settingsmanager";
|
2019-06-20 04:19:34 +02:00
|
|
|
import Patcher from "../modules/patcher";
|
2019-05-30 23:18:52 +02:00
|
|
|
|
2019-05-30 17:44:05 +02:00
|
|
|
export default class BuiltinModule {
|
|
|
|
|
|
|
|
get name() {return "Unnamed Builtin";}
|
2019-06-06 06:28:43 +02:00
|
|
|
get collection() {return "settings";}
|
|
|
|
get category() {return "general";}
|
2019-05-30 17:44:05 +02:00
|
|
|
get id() {return "None";}
|
|
|
|
|
2019-05-30 23:18:52 +02:00
|
|
|
async initialize() {
|
2019-06-09 22:30:33 +02:00
|
|
|
if (Settings.get(this.collection, this.category, this.id)) await this.enable();
|
2019-06-06 21:57:25 +02:00
|
|
|
Events.on("setting-updated", (collection, category, id, enabled) => {
|
|
|
|
if (collection != this.collection || category !== this.category || id !== this.id) return;
|
2019-05-30 23:18:52 +02:00
|
|
|
if (enabled) this.enable();
|
|
|
|
else this.disable();
|
2019-05-30 17:44:05 +02:00
|
|
|
});
|
2020-07-16 23:17:02 +02:00
|
|
|
this.initialized = true;
|
2019-05-30 17:44:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-06 21:57:25 +02:00
|
|
|
registerSetting(collection, category, id, onEnable, onDisable) {
|
|
|
|
if (arguments.length == 4) {
|
|
|
|
collection = this.collection;
|
|
|
|
category = arguments[0];
|
|
|
|
id = arguments[1];
|
|
|
|
onEnable = arguments[2];
|
|
|
|
onDisable = arguments[3];
|
|
|
|
}
|
|
|
|
else if (arguments.length == 3) {
|
|
|
|
collection = this.collection;
|
|
|
|
category = this.category;
|
|
|
|
id = arguments[0];
|
|
|
|
onEnable = arguments[1];
|
|
|
|
onDisable = arguments[2];
|
|
|
|
}
|
|
|
|
return Settings.on(collection, category, id, (value) => {
|
|
|
|
if (value) onEnable();
|
|
|
|
else onDisable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get(collection, category, id) {
|
|
|
|
if (arguments.length == 2) {
|
|
|
|
collection = this.collection;
|
|
|
|
category = arguments[0];
|
|
|
|
id = arguments[1];
|
|
|
|
}
|
|
|
|
else if (arguments.length == 1) {
|
|
|
|
collection = this.collection;
|
|
|
|
category = this.category;
|
|
|
|
id = arguments[0];
|
|
|
|
}
|
|
|
|
return Settings.get(collection, category, id);
|
|
|
|
}
|
|
|
|
|
2019-05-30 17:44:05 +02:00
|
|
|
async enable() {
|
|
|
|
this.log("Enabled");
|
2020-07-19 01:01:49 +02:00
|
|
|
try {await this.enabled();}
|
|
|
|
catch (e) {this.stacktrace("Could not be enabled", e);}
|
2019-05-30 17:44:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async disable() {
|
|
|
|
this.log("Disabled");
|
2020-07-19 01:01:49 +02:00
|
|
|
try {await this.disabled();}
|
|
|
|
catch (e) {this.stacktrace("Could not be disabled", e);}
|
2019-05-30 17:44:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async enabled() {}
|
|
|
|
async disabled() {}
|
|
|
|
|
2019-05-30 23:18:52 +02:00
|
|
|
log(...message) {
|
2019-06-19 21:24:05 +02:00
|
|
|
Logger.log(this.name, ...message);
|
2019-05-30 17:44:05 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 23:18:52 +02:00
|
|
|
warn(...message) {
|
2019-06-19 21:24:05 +02:00
|
|
|
Logger.warn(this.name, ...message);
|
2019-05-30 17:44:05 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 23:18:52 +02:00
|
|
|
error(...message) {
|
2019-06-19 21:24:05 +02:00
|
|
|
Logger.err(this.name, ...message);
|
|
|
|
}
|
|
|
|
|
|
|
|
stacktrace(message, error) {
|
|
|
|
Logger.stacktrace(this.name, message, error);
|
2019-05-30 17:44:05 +02:00
|
|
|
}
|
2019-06-20 04:19:34 +02:00
|
|
|
|
2020-07-18 04:24:20 +02:00
|
|
|
before(object, func, callback) {
|
|
|
|
return Patcher.before(this.name, object, func, callback);
|
|
|
|
}
|
|
|
|
|
2019-06-20 04:19:34 +02:00
|
|
|
after(object, func, callback) {
|
|
|
|
return Patcher.after(this.name, object, func, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
unpatchAll() {
|
|
|
|
return Patcher.unpatchAll(this.name);
|
|
|
|
}
|
2019-05-30 17:44:05 +02:00
|
|
|
}
|