feat: complate event and command module

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-19 18:59:32 +08:00
parent 15cea1dfc8
commit 533859808f
5 changed files with 198 additions and 39 deletions

View File

@ -1,3 +1,6 @@
export const METADATA_KEY = { export const METADATA_KEY = {
plugin: "@ms/plugin:plugin", plugin: "@ms/plugin:plugin",
cmd: "@ms/plugin:cmd",
tab: "@ms/plugin:tab",
listener: "@ms/plugin:listener",
}; };

View File

@ -1,7 +1,7 @@
import { interfaces } from './interfaces' import { interfaces } from './interfaces'
import { METADATA_KEY } from './constants' import { METADATA_KEY } from './constants'
import { injectable, decorate } from "inversify"; import { injectable, decorate } from "inversify";
import { getPluginMetadatas } from './utils' import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata } from './utils'
/** /**
* MiaoScript plugin * MiaoScript plugin
@ -12,8 +12,52 @@ export function plugin(metadata: interfaces.PluginMetadata) {
target.description = metadata; target.description = metadata;
metadata.target = target; metadata.target = target;
decorate(injectable(), target); decorate(injectable(), target);
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target); Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target.constructor);
const previousMetadata: interfaces.PluginMetadata[] = getPluginMetadatas(); const previousMetadata: Map<string, interfaces.PluginMetadata> = getPluginMetadatas();
Reflect.defineMetadata(METADATA_KEY.plugin, [metadata, ...previousMetadata], Reflect); previousMetadata.set(metadata.name, metadata);
Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect);
};
}
/**
* MiaoScript Command
* @param metadata CommandMetadata
*/
export function cmd(metadata: interfaces.CommandMetadata = {}) {
return function(target: any, key: string, value: any) {
metadata.name = metadata.name || key;
metadata.executor = key;
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
const previousMetadata: Map<string, interfaces.CommandMetadata> = getPluginCommandMetadata(target)
previousMetadata.set(metadata.name, metadata);
Reflect.defineMetadata(METADATA_KEY.cmd, previousMetadata, target.constructor);
};
}
/**
* MiaoScript Command
* @param metadata CommandMetadata
*/
export function tab(metadata: interfaces.TabCompleterMetadata = {}) {
return function(target: any, key: string, value: any) {
metadata.name = metadata.name || key;
metadata.executor = key;
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
const previousMetadata: Map<string, interfaces.TabCompleterMetadata> = getPluginCommandMetadata(target)
previousMetadata.set(metadata.name, metadata)
Reflect.defineMetadata(METADATA_KEY.tab, previousMetadata, target.constructor);
};
}
/**
* MiaoScript Listener
* @param metadata ListenerMetadata
*/
export function listener(metadata: interfaces.ListenerMetadata = {}) {
return function(target: any, key: string, value: any) {
metadata.name = metadata.name || key;
metadata.executor = key;
const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target)
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor);
}; };
} }

View File

@ -1,5 +1,4 @@
import { injectable, postConstruct } from "inversify"; import { injectable, postConstruct } from "inversify";
import { getPluginMetadata } from './utils'
export namespace interfaces { export namespace interfaces {
@injectable() @injectable()
@ -15,6 +14,22 @@ export namespace interfaces {
name: string; name: string;
version: string; version: string;
author: string | string[]; author: string | string[];
source: string;
target?: any; target?: any;
} }
export interface CommandMetadata {
name?: string;
executor?: string;
paramtypes?: string[];
}
export interface TabCompleterMetadata {
name?: string;
executor?: string;
paramtypes?: string[];
}
export interface ListenerMetadata {
name?: string;
executor?: string;
}
export type PluginLike = Plugin | string;
} }

View File

@ -1,8 +1,8 @@
import { plugin, server, MiaoScriptConsole } from '@ms/api' import { plugin, server, command, event, MiaoScriptConsole } from '@ms/api'
import { injectable, inject, postConstruct, Container } from '@ms/container' import { injectable, inject, postConstruct, Container, DefaultContainer as container } from '@ms/container'
import * as fs from '@ms/common/dist/fs' import * as fs from '@ms/common/dist/fs'
import { getPluginMetadatas } from './utils' import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata } from './utils'
import { interfaces } from './interfaces'; import { interfaces } from './interfaces';
@injectable() @injectable()
@ -13,6 +13,10 @@ export class PluginManagerImpl implements plugin.PluginManager {
private serverType: string; private serverType: string;
@inject(server.Console) @inject(server.Console)
private Console: MiaoScriptConsole; private Console: MiaoScriptConsole;
@inject(command.Command)
private CommandManager: command.Command;
@inject(event.Event)
private EventManager: event.Event;
private pluginMap: Map<string, interfaces.Plugin>; private pluginMap: Map<string, interfaces.Plugin>;
@ -22,6 +26,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
// 如果plugin不等于null 则代表是正式环境 // 如果plugin不等于null 则代表是正式环境
console.info(`Initialization MiaoScript Plugin System: ${this.pluginInstance} ...`); console.info(`Initialization MiaoScript Plugin System: ${this.pluginInstance} ...`);
this.pluginMap = new Map(); this.pluginMap = new Map();
console.info(`${this.EventManager.mapEventName().toFixed(0)} ${this.serverType} Event Mapping Complate...`);
} }
} }
@ -35,17 +40,35 @@ export class PluginManagerImpl implements plugin.PluginManager {
this.loadPlugins(files); this.loadPlugins(files);
} }
load(container: Container): void { build(container: Container): void {
this.buildPlugins(container); this.buildPlugins(container);
} }
enable(): void { load(...args: any[]): void {
this.pluginMap.forEach(pl => this.runCatch(pl, 'load')); this.checkAndGet(args[0]).forEach(pl => this.runCatch(pl, 'load'));
this.pluginMap.forEach(pl => this.runCatch(pl, 'enable'));
} }
disable(): void { enable(...args: any[]): void {
throw new Error("Method not implemented."); this.checkAndGet(args[0]).forEach(pl => this.runCatch(pl, 'enable'));
}
disable(...args: any[]): void {
this.checkAndGet(args[0]).forEach(pl => {
this.runCatch(pl, 'disable');
this.EventManager.disable(pl);
});
}
reload(...args: any[]): void {
this.checkAndGet(arguments[0]).forEach((pl: interfaces.Plugin) => {
this.disable(pl);
this.EventManager.disable(pl);
this.loadPlugin(pl.description.source);
pl = this.buildPlugin(getPlugin(pl.description.name));
this.load(pl);
this.enable(pl);
})
} }
private runCatch(pl: any, func: string) { private runCatch(pl: any, func: string) {
@ -57,13 +80,22 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
} }
private checkAndGet(name: string | interfaces.Plugin | undefined): Map<string, interfaces.Plugin> | interfaces.Plugin[] {
if (name == undefined) {
return this.pluginMap;
}
if (!(name instanceof interfaces.Plugin) && !this.pluginMap.has(name)) {
throw new Error(`Plugin ${name} not exist!`);
}
// 如果是插件 则直接返回
return [name as interfaces.Plugin];
}
private scanFloder(plugin: any): string[] { private scanFloder(plugin: any): string[] {
var files = []; var files = [];
console.info(`Start Scan Plugins in ${plugin} ...`); console.info(`Scanning Plugins in ${plugin} ...`);
this.checkUpdateFolder(plugin); this.checkUpdateFolder(plugin);
fs.list(plugin).forEach(function searchPlugin(file) { fs.list(plugin).forEach((file: any) => files.push(file.toFile()));
files.push(file.toFile());
});
return files; return files;
} }
@ -86,19 +118,17 @@ export class PluginManagerImpl implements plugin.PluginManager {
* JS类型插件预加载 * JS类型插件预加载
*/ */
private loadJsPlugins(files: any[]) { private loadJsPlugins(files: any[]) {
files.filter(file => file.name.endsWith(".js")).forEach(file => { files.filter(file => file.name.endsWith(".js")).forEach(file => this.loadPlugin(file))
}
private loadPlugin(file: any) {
try { try {
this.loadPlugin(file) this.updatePlugin(file);
this.createPlugin(file);
} catch (ex) { } catch (ex) {
console.console(`§6插件 §b${file.name} §6初始化时发生错误 §4${ex.message}`); console.console(`§6插件 §b${file.name} §6初始化时发生错误 §4${ex.message}`);
console.ex(ex); console.ex(ex);
} }
})
}
private loadPlugin(file: any) {
this.updatePlugin(file);
this.createPlugin(file);
} }
private updatePlugin(file: any) { private updatePlugin(file: any) {
@ -127,13 +157,48 @@ export class PluginManagerImpl implements plugin.PluginManager {
private buildPlugins(container: Container) { private buildPlugins(container: Container) {
let pluginMetadatas = getPluginMetadatas(); let pluginMetadatas = getPluginMetadatas();
for (const metadata of pluginMetadatas) { pluginMetadatas.forEach(metadata => {
this.buildPlugin(metadata);
});
}
private buildPlugin(metadata: interfaces.PluginMetadata) {
try {
let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
if (pluginInstance.description.source + '' !== metadata.source + '') {
console.warn(`find duplicate plugin ${pluginInstance.description.source} and ${metadata.source}. the first plugin will be ignore!`)
}
container.rebind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name);
} catch{
container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name); container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name);
this.pluginMap.set(metadata.name, container.getNamed(plugin.Plugin, metadata.name)); }
let pluginInstance = this.pluginMap.get(metadata.name) let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
this.pluginMap.set(metadata.name, pluginInstance);
pluginInstance.description = metadata; pluginInstance.description = metadata;
// @ts-ignore // @ts-ignore
pluginInstance.logger = new this.Console(metadata.name); pluginInstance.logger = new this.Console(metadata.name);
this.registryCommand(pluginInstance);
this.registryListener(pluginInstance);
return pluginInstance;
}
private registryCommand(pluginInstance: interfaces.Plugin) {
let cmds = getPluginCommandMetadata(pluginInstance);
let tabs = getPluginTabCompleterMetadata(pluginInstance);
cmds.forEach(cmd => {
let tab = tabs.get(cmd.name);
this.CommandManager.on(pluginInstance, cmd.name, {
cmd: pluginInstance[cmd.executor].bind(pluginInstance),
tab: tab ? pluginInstance[tab.executor].bind(pluginInstance) : undefined
});
})
}
private registryListener(pluginInstance: interfaces.Plugin) {
let events = getPluginListenerMetadata(pluginInstance)
for (const event of events) {
// here must bind this to pluginInstance
this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance));
} }
} }
} }

View File

@ -2,27 +2,59 @@ import { interfaces } from './interfaces'
import { METADATA_KEY } from './constants' import { METADATA_KEY } from './constants'
function getPlugins() { function getPlugins() {
return getPluginMetadatas().map((target) => target.target); return [...getPluginMetadatas().values()].map((target) => target.target);
}
function getPlugin(name: string) {
return getPluginMetadatas().get(name);
} }
function getPluginMetadatas() { function getPluginMetadatas() {
let pluginMetadatas: interfaces.PluginMetadata[] = Reflect.getMetadata( let pluginMetadatas: Map<string, interfaces.PluginMetadata> = Reflect.getMetadata(
METADATA_KEY.plugin, METADATA_KEY.plugin,
Reflect Reflect
) || []; ) || new Map<string, interfaces.PluginMetadata>();
return pluginMetadatas; return pluginMetadatas;
} }
function getPluginMetadata(target: any) { function getPluginMetadata(target: any) {
let pluginMetadata: interfaces.PluginMetadata = Reflect.getMetadata( let pluginMetadata: interfaces.PluginMetadata = Reflect.getMetadata(
METADATA_KEY.plugin, METADATA_KEY.plugin,
target target.constructor
) || {}; ) || {};
return pluginMetadata; return pluginMetadata;
} }
function getPluginCommandMetadata(target: any) {
let commandMetadata: Map<string, interfaces.CommandMetadata> = Reflect.getMetadata(
METADATA_KEY.cmd,
target.constructor
) || new Map<string, interfaces.CommandMetadata>();
return commandMetadata;
}
function getPluginTabCompleterMetadata(target: any) {
let tabcompleterMetadata: Map<string, interfaces.CommandMetadata> = Reflect.getMetadata(
METADATA_KEY.tab,
target.constructor
) || new Map<string, interfaces.TabCompleterMetadata>();
return tabcompleterMetadata;
}
function getPluginListenerMetadata(target: any) {
let listnerMetadata: interfaces.ListenerMetadata[] = Reflect.getMetadata(
METADATA_KEY.listener,
target.constructor
) || [];
return listnerMetadata;
}
export { export {
getPlugin,
getPlugins, getPlugins,
getPluginMetadatas, getPluginMetadatas,
getPluginMetadata getPluginMetadata,
getPluginCommandMetadata,
getPluginTabCompleterMetadata,
getPluginListenerMetadata,
} }