Plugin fixes

- Support returning a promise in the plugin load() function
- strReplace can now replace more than one occurence with a parameter, specify how many occurences are expected, with Infinity being as much as possible
- this.log() function now accepts multiple arguments
This commit is contained in:
KatieFrogs 2022-02-23 20:31:52 +03:00
parent 6ad78cc547
commit bedcc1e5ef

View File

@ -143,12 +143,21 @@ class Plugins{
var length = searchString.length var length = searchString.length
return input.slice(0, index + length) + insertedText + input.slice(index + length) return input.slice(0, index + length) + insertedText + input.slice(index + length)
} }
strReplace(input, searchString, insertedText){ strReplace(input, searchString, insertedText, repeat=1){
var index = input.indexOf(searchString) var position = 0
if(index === -1){ for(var i = 0; i < repeat; i++){
throw new Error("searchString not found: " + searchString) var index = input.indexOf(searchString, position)
if(index === -1){
if(repeat === Infinity){
break
}else{
throw new Error("searchString not found: " + searchString)
}
}
input = input.slice(0, index) + insertedText + input.slice(index + searchString.length)
position = index + insertedText.length
} }
return input.slice(0, index) + insertedText + input.slice(index + searchString.length) return input
} }
hasSettings(){ hasSettings(){
@ -288,17 +297,25 @@ class PluginLoader{
this.error() this.error()
return return
} }
var output
try{ try{
if(this.module.beforeLoad){ if(this.module.beforeLoad){
this.module.beforeLoad(this) this.module.beforeLoad(this)
} }
if(this.module.load){ if(this.module.load){
this.module.load(this) output = this.module.load(this)
} }
}catch(e){ }catch(e){
console.error(e) console.error(e)
this.error() this.error()
} }
if(typeof output === "object" && output.constructor === Promise){
return output.catch(e => {
console.error(e)
this.error()
return Promise.resolve()
})
}
}, e => { }, e => {
console.error(e) console.error(e)
this.error() this.error()
@ -494,12 +511,12 @@ class Patch{
beforeUnload(){ beforeUnload(){
this.edits.forEach(edit => edit.unload()) this.edits.forEach(edit => edit.unload())
} }
log(message){ log(...args){
var name = this.name || "Plugin" var name = this.name || "Plugin"
console.log( console.log(
"%c[" + name + "]%c " + message, "%c[" + name + "]",
"font-weight: bold;", "font-weight: bold;",
"" ...args
) )
} }
} }