feat: add plugin checker

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2023-02-18 16:01:15 +08:00
parent dd76e563c8
commit 07a5d0c8de
4 changed files with 38 additions and 7 deletions

View File

@ -29,6 +29,7 @@ export namespace server {
origin: any
[key: string]: any
}
@injectable()
export abstract class NativePluginManager {
list(): NativePlugin[] {
@ -53,6 +54,7 @@ export namespace server {
throw new Error("Method not implemented.")
}
}
/**
* MiaoScript Server
*/
@ -98,6 +100,7 @@ export namespace server {
throw new Error("Method not implemented.")
}
}
@injectable()
export class ServerChecker {
@Autowired(ServerType)
@ -116,6 +119,22 @@ export namespace server {
}
}
}
@injectable()
export class NativePluginChecker {
@Autowired(NativePluginManager)
private nativePluginManager: NativePluginManager
check(plugins: string[]) {
// Not set plugins -> allow
if (!plugins || !plugins.length) return true
for (const plugin of plugins) {
if (!this.nativePluginManager.has(plugin)) { return false }
}
return true
}
}
@injectable()
export abstract class ReflectServer extends server.Server {
@Autowired(ContainerInstance)

View File

@ -146,6 +146,7 @@ function createCore() {
container.bind(server.ServerType).toConstantValue(type)
container.bind(server.ServerChecker).toSelf().inSingletonScope()
container.bind(server.NativePluginManager).toSelf().inSingletonScope()
container.bind(server.NativePluginChecker).toSelf().inSingletonScope()
process.emit('core.after.initialize.detect')
process.emit('core.before.package.initialize')

View File

@ -5,9 +5,11 @@ import { getPluginListenerMetadata } from './utils'
@provideSingleton(PluginEventManager)
export class PluginEventManager {
@Autowired()
private EventManager: event.Event
private eventManager: event.Event
@Autowired()
private ServerChecker: server.ServerChecker
private serverChecker: server.ServerChecker
@Autowired()
private nativePluginChecker: server.NativePluginChecker
constructor() {
process.on('plugin.before.enable', this.registryListener.bind(this))
@ -15,26 +17,31 @@ export class PluginEventManager {
}
mapEventName() {
return this.EventManager.mapEventName().toFixed(0)
return this.eventManager.mapEventName().toFixed(0)
}
private registryListener(pluginInstance: plugin.Plugin) {
let events = getPluginListenerMetadata(pluginInstance)
for (const event of events) {
// ignore space listener
if (!this.ServerChecker.check(event.servers)) {
console.debug(`[${pluginInstance.description.name}] ${event.target.constructor.name} incompatible event ${event.name} server(${event.servers}) ignore.`)
if (!this.serverChecker.check(event.servers)) {
console.debug(`[${pluginInstance.description.name}] ${event.target.constructor.name} incompatible server(${event.servers}) ignore event ${event.name}.`)
continue
}
// ignore space listener
if (!this.nativePluginChecker.check(event.plugins)) {
console.debug(`[${pluginInstance.description.name}] ${event.target.constructor.name} require native plugins(${event.plugins}) ignore event ${event.name}.`)
continue
}
// here must bind this to pluginInstance
let exec = event.target[event.executor]
let execBinded = exec.bind(pluginInstance)
execBinded.executor = event.executor
exec.off = this.EventManager.listen(pluginInstance, event.name, execBinded, event.priority, event.ignoreCancel)
exec.off = this.eventManager.listen(pluginInstance, event.name, execBinded, event.priority, event.ignoreCancel)
}
}
private unregistryListener(pluginInstance: plugin.Plugin) {
this.EventManager.disable(pluginInstance)
this.eventManager.disable(pluginInstance)
}
}

View File

@ -73,6 +73,10 @@ export namespace interfaces {
*
*/
ignoreCancel?: boolean
/**
*
*/
plugins?: string[]
}
export interface ConfigMetadata extends plugin.BaseMetadata {
/**