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 origin: any
[key: string]: any [key: string]: any
} }
@injectable() @injectable()
export abstract class NativePluginManager { export abstract class NativePluginManager {
list(): NativePlugin[] { list(): NativePlugin[] {
@ -53,6 +54,7 @@ export namespace server {
throw new Error("Method not implemented.") throw new Error("Method not implemented.")
} }
} }
/** /**
* MiaoScript Server * MiaoScript Server
*/ */
@ -98,6 +100,7 @@ export namespace server {
throw new Error("Method not implemented.") throw new Error("Method not implemented.")
} }
} }
@injectable() @injectable()
export class ServerChecker { export class ServerChecker {
@Autowired(ServerType) @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() @injectable()
export abstract class ReflectServer extends server.Server { export abstract class ReflectServer extends server.Server {
@Autowired(ContainerInstance) @Autowired(ContainerInstance)

View File

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

View File

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

View File

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