@@ -9,10 +9,9 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
export function plugin(metadata: interfaces.PluginMetadata) {
 | 
					export function plugin(metadata: interfaces.PluginMetadata) {
 | 
				
			||||||
    return function(target: any) {
 | 
					    return function(target: any) {
 | 
				
			||||||
        target.description = metadata;
 | 
					 | 
				
			||||||
        metadata.target = target;
 | 
					        metadata.target = target;
 | 
				
			||||||
        decorate(injectable(), target);
 | 
					        decorate(injectable(), target);
 | 
				
			||||||
        Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target.constructor);
 | 
					        Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target);
 | 
				
			||||||
        const previousMetadata: Map<string, interfaces.PluginMetadata> = getPluginMetadatas();
 | 
					        const previousMetadata: Map<string, interfaces.PluginMetadata> = getPluginMetadatas();
 | 
				
			||||||
        previousMetadata.set(metadata.name, metadata);
 | 
					        previousMetadata.set(metadata.name, metadata);
 | 
				
			||||||
        Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect);
 | 
					        Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,7 @@
 | 
				
			|||||||
import { injectable } from "@ms/container";
 | 
					import { server, MiaoScriptConsole } from "@ms/api";
 | 
				
			||||||
 | 
					import { METADATA_KEY } from './constants'
 | 
				
			||||||
 | 
					import { injectable, inject, postConstruct } from "@ms/container";
 | 
				
			||||||
 | 
					import { getPluginMetadata } from "./utils";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export namespace interfaces {
 | 
					export namespace interfaces {
 | 
				
			||||||
    @injectable()
 | 
					    @injectable()
 | 
				
			||||||
@@ -6,6 +9,12 @@ export namespace interfaces {
 | 
				
			|||||||
        public description: PluginMetadata;
 | 
					        public description: PluginMetadata;
 | 
				
			||||||
        public logger: Console;
 | 
					        public logger: Console;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        constructor(@inject(server.Console) Console: MiaoScriptConsole) {
 | 
				
			||||||
 | 
					            this.description = getPluginMetadata(this)
 | 
				
			||||||
 | 
					            // @ts-ignore
 | 
				
			||||||
 | 
					            this.logger = new Console(this.description.prefix || this.description.name)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public load() { }
 | 
					        public load() { }
 | 
				
			||||||
        public enable() { }
 | 
					        public enable() { }
 | 
				
			||||||
        public disable() { }
 | 
					        public disable() { }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,8 +13,6 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
				
			|||||||
    private pluginInstance: any
 | 
					    private pluginInstance: any
 | 
				
			||||||
    @inject(server.ServerType)
 | 
					    @inject(server.ServerType)
 | 
				
			||||||
    private serverType: string
 | 
					    private serverType: string
 | 
				
			||||||
    @inject(server.Console)
 | 
					 | 
				
			||||||
    private Console: MiaoScriptConsole
 | 
					 | 
				
			||||||
    @inject(command.Command)
 | 
					    @inject(command.Command)
 | 
				
			||||||
    private CommandManager: command.Command
 | 
					    private CommandManager: command.Command
 | 
				
			||||||
    @inject(event.Event)
 | 
					    @inject(event.Event)
 | 
				
			||||||
@@ -149,19 +147,22 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private checkServers(servers: string[]) {
 | 
				
			||||||
 | 
					        if (!servers) { return true }
 | 
				
			||||||
 | 
					        return servers?.indexOf(this.serverType) != -1 && servers?.indexOf(`!${this.serverType}`) == -1
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private createPlugin(file: string) {
 | 
					    private createPlugin(file: string) {
 | 
				
			||||||
        //@ts-ignore
 | 
					        //@ts-ignore
 | 
				
			||||||
        require(file, {
 | 
					        require(file, { cache: false })
 | 
				
			||||||
            cache: false
 | 
					 | 
				
			||||||
        })
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private buildPlugins() {
 | 
					    private buildPlugins() {
 | 
				
			||||||
        let pluginMetadatas = getPluginMetadatas()
 | 
					        let pluginMetadatas = getPluginMetadatas()
 | 
				
			||||||
        pluginMetadatas.forEach(metadata => {
 | 
					        for (const [_, metadata] of pluginMetadatas) {
 | 
				
			||||||
            if (metadata.servers?.indexOf(this.serverType) == -1) { return }
 | 
					            if (!this.checkServers(metadata.servers)) { continue }
 | 
				
			||||||
            this.buildPlugin(metadata)
 | 
					            this.buildPlugin(metadata)
 | 
				
			||||||
        })
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private buildPlugin(metadata: interfaces.PluginMetadata) {
 | 
					    private buildPlugin(metadata: interfaces.PluginMetadata) {
 | 
				
			||||||
@@ -172,9 +173,6 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
				
			|||||||
            return
 | 
					            return
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        this.pluginMap.set(metadata.name, pluginInstance)
 | 
					        this.pluginMap.set(metadata.name, pluginInstance)
 | 
				
			||||||
        pluginInstance.description = metadata
 | 
					 | 
				
			||||||
        // @ts-ignore
 | 
					 | 
				
			||||||
        pluginInstance.logger = new this.Console(metadata.prefix || metadata.name)
 | 
					 | 
				
			||||||
        return pluginInstance
 | 
					        return pluginInstance
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -195,7 +193,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
				
			|||||||
        let tabs = getPluginTabCompleterMetadata(pluginInstance)
 | 
					        let tabs = getPluginTabCompleterMetadata(pluginInstance)
 | 
				
			||||||
        for (const [_, cmd] of cmds) {
 | 
					        for (const [_, cmd] of cmds) {
 | 
				
			||||||
            let tab = tabs.get(cmd.name)
 | 
					            let tab = tabs.get(cmd.name)
 | 
				
			||||||
            if (cmd.servers?.indexOf(this.serverType) == -1) { continue }
 | 
					            if (!this.checkServers(cmd.servers)) { continue }
 | 
				
			||||||
            this.CommandManager.on(pluginInstance, cmd.name, {
 | 
					            this.CommandManager.on(pluginInstance, cmd.name, {
 | 
				
			||||||
                cmd: pluginInstance[cmd.executor].bind(pluginInstance),
 | 
					                cmd: pluginInstance[cmd.executor].bind(pluginInstance),
 | 
				
			||||||
                tab: tab ? pluginInstance[tab.executor].bind(pluginInstance) : undefined
 | 
					                tab: tab ? pluginInstance[tab.executor].bind(pluginInstance) : undefined
 | 
				
			||||||
@@ -207,7 +205,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
				
			|||||||
        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 (event.servers?.indexOf(this.serverType) == -1) { continue }
 | 
					            if (!this.checkServers(event.servers)) { continue }
 | 
				
			||||||
            // here must bind this to pluginInstance
 | 
					            // here must bind this to pluginInstance
 | 
				
			||||||
            this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance))
 | 
					            this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance))
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user