2019-09-19 10:59:32 +00:00
|
|
|
import { plugin, server, command, event, MiaoScriptConsole } from '@ms/api'
|
|
|
|
import { injectable, inject, postConstruct, Container, DefaultContainer as container } from '@ms/container'
|
2019-09-07 04:23:15 +00:00
|
|
|
import * as fs from '@ms/common/dist/fs'
|
|
|
|
|
2019-09-19 10:59:32 +00:00
|
|
|
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata } from './utils'
|
2019-09-07 04:23:15 +00:00
|
|
|
import { interfaces } from './interfaces';
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class PluginManagerImpl implements plugin.PluginManager {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any;
|
|
|
|
@inject(server.ServerType)
|
|
|
|
private serverType: string;
|
2019-09-10 09:22:00 +00:00
|
|
|
@inject(server.Console)
|
|
|
|
private Console: MiaoScriptConsole;
|
2019-09-19 10:59:32 +00:00
|
|
|
@inject(command.Command)
|
|
|
|
private CommandManager: command.Command;
|
|
|
|
@inject(event.Event)
|
|
|
|
private EventManager: event.Event;
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
private pluginMap: Map<string, interfaces.Plugin>;
|
|
|
|
|
|
|
|
@postConstruct()
|
|
|
|
init() {
|
|
|
|
if (this.pluginInstance !== null) {
|
|
|
|
// 如果plugin不等于null 则代表是正式环境
|
|
|
|
console.info(`Initialization MiaoScript Plugin System: ${this.pluginInstance} ...`);
|
|
|
|
this.pluginMap = new Map();
|
2019-09-19 10:59:32 +00:00
|
|
|
console.info(`${this.EventManager.mapEventName().toFixed(0)} ${this.serverType} Event Mapping Complate...`);
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scan(folder: string): void {
|
|
|
|
var plugin = fs.file(root, folder);
|
|
|
|
var files = []
|
|
|
|
// load common plugin
|
|
|
|
.concat(this.scanFloder(plugin))
|
|
|
|
// load space plugin
|
|
|
|
.concat(this.scanFloder(fs.file(plugin, this.serverType)))
|
|
|
|
this.loadPlugins(files);
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:59:32 +00:00
|
|
|
build(container: Container): void {
|
2019-09-07 04:23:15 +00:00
|
|
|
this.buildPlugins(container);
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:59:32 +00:00
|
|
|
load(...args: any[]): void {
|
2019-09-21 07:05:37 +00:00
|
|
|
this.checkAndGet(args[0]).forEach(pl => {
|
|
|
|
this.runCatch(pl, 'load');
|
|
|
|
this.runCatch(pl, `${this.serverType}load`);
|
|
|
|
});
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 10:59:32 +00:00
|
|
|
enable(...args: any[]): void {
|
2019-09-21 07:05:37 +00:00
|
|
|
this.checkAndGet(args[0]).forEach(pl => {
|
|
|
|
this.runCatch(pl, 'enable')
|
|
|
|
this.runCatch(pl, `${this.serverType}enable`);
|
|
|
|
});
|
2019-09-19 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disable(...args: any[]): void {
|
|
|
|
this.checkAndGet(args[0]).forEach(pl => {
|
|
|
|
this.runCatch(pl, 'disable');
|
2019-09-21 07:05:37 +00:00
|
|
|
this.runCatch(pl, `${this.serverType}disable`);
|
2019-09-19 10:59:32 +00:00
|
|
|
this.EventManager.disable(pl);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
reload(...args: any[]): void {
|
2019-09-21 07:05:37 +00:00
|
|
|
this.checkAndGet(args[0]).forEach((pl: interfaces.Plugin) => {
|
2019-09-19 10:59:32 +00:00
|
|
|
this.disable(pl);
|
|
|
|
this.loadPlugin(pl.description.source);
|
|
|
|
pl = this.buildPlugin(getPlugin(pl.description.name));
|
|
|
|
this.load(pl);
|
|
|
|
this.enable(pl);
|
|
|
|
})
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private runCatch(pl: any, func: string) {
|
|
|
|
try {
|
2019-09-21 07:05:37 +00:00
|
|
|
if (pl[func]) pl[func].call(pl);
|
2019-09-07 04:23:15 +00:00
|
|
|
} catch (ex) {
|
|
|
|
console.console(`§6插件 §b${pl.description.name} §6执行 §d${func} §6方法时发生错误 §4${ex}`);
|
|
|
|
console.ex(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:59:32 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2019-09-07 04:23:15 +00:00
|
|
|
private scanFloder(plugin: any): string[] {
|
|
|
|
var files = [];
|
2019-09-19 10:59:32 +00:00
|
|
|
console.info(`Scanning Plugins in ${plugin} ...`);
|
2019-09-07 04:23:15 +00:00
|
|
|
this.checkUpdateFolder(plugin);
|
2019-09-19 10:59:32 +00:00
|
|
|
fs.list(plugin).forEach((file: any) => files.push(file.toFile()));
|
2019-09-07 04:23:15 +00:00
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新插件
|
|
|
|
* @param path
|
|
|
|
*/
|
|
|
|
private checkUpdateFolder(path) {
|
|
|
|
var update = fs.file(path, "update");
|
|
|
|
if (!update.exists()) {
|
|
|
|
update.mkdirs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadPlugins(files: any[]): void {
|
|
|
|
this.loadJsPlugins(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JS类型插件预加载
|
|
|
|
*/
|
|
|
|
private loadJsPlugins(files: any[]) {
|
2019-09-19 10:59:32 +00:00
|
|
|
files.filter(file => file.name.endsWith(".js")).forEach(file => this.loadPlugin(file))
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private loadPlugin(file: any) {
|
2019-09-19 10:59:32 +00:00
|
|
|
try {
|
|
|
|
this.updatePlugin(file);
|
|
|
|
this.createPlugin(file);
|
|
|
|
} catch (ex) {
|
|
|
|
console.console(`§6插件 §b${file.name} §6初始化时发生错误 §4${ex.message}`);
|
|
|
|
console.ex(ex);
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private updatePlugin(file: any) {
|
|
|
|
var update = fs.file(fs.file(file.parentFile, 'update'), file.name);
|
|
|
|
if (update.exists()) {
|
|
|
|
console.info(`Auto Update Plugin ${file.name} ...`);
|
|
|
|
fs.move(update, file, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private createPlugin(file) {
|
|
|
|
//@ts-ignore
|
|
|
|
require(file, {
|
|
|
|
cache: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildPlugins(container: Container) {
|
|
|
|
let pluginMetadatas = getPluginMetadatas();
|
2019-09-19 10:59:32 +00:00
|
|
|
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{
|
2019-09-07 04:23:15 +00:00
|
|
|
container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name);
|
2019-09-19 10:59:32 +00:00
|
|
|
}
|
|
|
|
let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
|
|
|
|
this.pluginMap.set(metadata.name, pluginInstance);
|
|
|
|
pluginInstance.description = metadata;
|
|
|
|
// @ts-ignore
|
|
|
|
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) {
|
2019-09-21 07:05:37 +00:00
|
|
|
// ignore space listener
|
|
|
|
if (event.servertype && event.servertype != this.serverType) { continue; }
|
2019-09-19 10:59:32 +00:00
|
|
|
// here must bind this to pluginInstance
|
|
|
|
this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance));
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|