Small improvements

This commit is contained in:
DJj123dj
2024-09-01 20:55:50 +02:00
parent 4e030bf95b
commit c1c80fc04d
15 changed files with 117 additions and 138 deletions
+1 -1
View File
@@ -367,7 +367,7 @@ export class ODChecker extends ODManagerData {
type,message,
path:this.locationTraceToString(locationTrace),
filepath:this.config.file,
filepath:this.config.path,
translationParams,
messageDocs:docs,
+6 -4
View File
@@ -67,8 +67,10 @@ export class ODConfigManager extends ODManager<ODConfig> {
* You will only use this class if you want to create your own config implementation (e.g. `yml`, `xml`,...)!
*/
export class ODConfig extends ODManagerData {
/**The full path to this config with extension */
/**The name of the file with extension. */
file: string = ""
/**The path to the file relative to the main directory. */
path: string = ""
/**An object/array of the entire config file! Variables inside it can be edited while the bot is running! */
data: any = undefined
}
@@ -87,9 +89,9 @@ export class ODJsonConfig extends ODConfig {
constructor(id:ODValidId, file:string, customPath?:string){
super(id)
try {
const filename = (file.endsWith(".json")) ? file : file+".json"
this.file = customPath ? nodepath.join("./",customPath,filename) : nodepath.join("./config/",filename)
this.data = JSON.parse(fs.readFileSync(this.file).toString())
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./config/",this.file)
this.data = JSON.parse(fs.readFileSync(this.path).toString())
}catch(err){
process.emit("uncaughtException",err)
throw new ODSystemError("Config \""+nodepath.join("./",customPath ?? "./config/",file)+"\" doesn't exist!")
-3
View File
@@ -20,8 +20,6 @@ export interface ODDefaults {
/**Load the default open ticket plugin classes. */
pluginClassLoading:boolean,
/**Load the default open ticket plugin events. */
pluginEventLoading:boolean,
/**Load the default open ticket flags. */
flagLoading:boolean,
@@ -225,7 +223,6 @@ export class ODDefaultsManager {
softPluginLoading:false,
pluginClassLoading:true,
pluginEventLoading:true,
flagLoading:true,
flagInitiating:true,
+6 -3
View File
@@ -7,7 +7,10 @@ import { ODDebugger } from "./console"
import fs from "fs"
export class ODLanguage extends ODManagerData {
/**The name of the file with `.json` extension. */
file: string
/**The path to the file relative to the main directory. */
path: string
data: any
metadata: {
otversion:string,
@@ -19,9 +22,9 @@ export class ODLanguage extends ODManagerData {
constructor(id:ODValidId, file:string, customPath?:string){
super(id)
try{
const filename = (file.endsWith(".json")) ? file : file+".json"
this.file = customPath ? nodepath.join("./",customPath,filename) : nodepath.join("./languages/",filename)
this.data = JSON.parse(fs.readFileSync(this.file).toString())
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./languages/",this.file)
this.data = JSON.parse(fs.readFileSync(this.path).toString())
}catch(err){
process.emit("uncaughtException",err)
throw new ODSystemError("Language \""+nodepath.join("./",customPath ?? "./languages/",file)+"\" doesn't exist!")
-87
View File
@@ -11,13 +11,11 @@ export interface ODUnknownCrashedPlugin {
}
export class ODPluginManager extends ODManager<ODPlugin> {
events: ODPluginEventManager
classes: ODPluginClassManager
unknownCrashedPlugins: ODUnknownCrashedPlugin[] = []
constructor(debug:ODDebugger){
super(debug,"plugin")
this.events = new ODPluginEventManager(debug)
this.classes = new ODPluginClassManager(debug)
}
@@ -147,91 +145,6 @@ export class ODPlugin extends ODManagerData {
}
}
export class ODPluginEvent extends ODManagerData {
listeners: Function[] = []
oncelisteners: Function[] = []
getCurrentListeners(){
const final: Function[] = []
this.oncelisteners.forEach((l) => final.push(l))
this.listeners.forEach((l) => final.push(l))
this.oncelisteners = []
return final
}
}
export class ODPluginEventManager extends ODManager<ODPluginEvent> {
#debug: ODDebugger
listenerLimit: number = 25
constructor(debug:ODDebugger){
super(debug,"plugin event")
this.#debug = debug
}
setListenerLimit(limit:number){
this.listenerLimit = limit
}
on(event:string, callback:Function){
const eventdata = this.get(event)
if (eventdata){
eventdata.listeners.push(callback)
if (eventdata.listeners.length > this.listenerLimit){
this.#debug.console.log(new ODConsoleWarningMessage("Possible plugin event memory leak detected!",[
{key:"event",value:event},
{key:"listeners",value:eventdata.listeners.length.toString()}
]))
}
}else{
throw new ODSystemError("unknown plugin event \""+event+"\"")
}
}
once(event:string, callback:Function){
const eventdata = this.get(event)
if (eventdata){
eventdata.oncelisteners.push(callback)
}else{
throw new ODSystemError("unknown plugin event \""+event+"\"")
}
}
wait(event:string): Promise<any[]> {
return new Promise((resolve,reject) => {
const eventdata = this.get(event)
if (eventdata){
eventdata.oncelisteners.push((...args:any) => {resolve(args)})
}else{
reject("unknown plugin event \""+event+"\"")
}
})
}
emit(event:string, params:any[]): Promise<void> {
return new Promise(async (resolve,reject) => {
const eventdata = this.get(event)
if (eventdata){
const listeners = eventdata.getCurrentListeners()
for (const listener of listeners){
try {
await listener(...params)
}catch(err){
process.emit("uncaughtException",err)
}
}
resolve()
}else{
reject("unknown plugin event \""+event+"\"")
}
})
}
}
export class ODPluginClassManager extends ODManager<ODManagerData> {
constructor(debug:ODDebugger){
super(debug,"plugin class")