feat: add illegal plugin check

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-11-05 17:03:49 +08:00
parent c0106e941b
commit 5a29133561

View File

@ -3,112 +3,108 @@ import { injectable, inject, postConstruct, Container, DefaultContainer as conta
import * as fs from '@ms/common/dist/fs' import * as fs from '@ms/common/dist/fs'
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata } from './utils' import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata } from './utils'
import { interfaces } from './interfaces'; import { interfaces } from './interfaces'
@injectable() @injectable()
export class PluginManagerImpl implements plugin.PluginManager { export class PluginManagerImpl implements plugin.PluginManager {
@inject(plugin.PluginInstance) @inject(plugin.PluginInstance)
private pluginInstance: any; private pluginInstance: any
@inject(server.ServerType) @inject(server.ServerType)
private serverType: string; private serverType: string
@inject(server.Console) @inject(server.Console)
private Console: MiaoScriptConsole; private Console: MiaoScriptConsole
@inject(command.Command) @inject(command.Command)
private CommandManager: command.Command; private CommandManager: command.Command
@inject(event.Event) @inject(event.Event)
private EventManager: event.Event; private EventManager: event.Event
private pluginMap: Map<string, interfaces.Plugin>; private pluginMap: Map<string, interfaces.Plugin>
@postConstruct() @postConstruct()
init() { init() {
if (this.pluginInstance !== null) { if (this.pluginInstance !== null) {
// 如果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...`); console.info(`${this.EventManager.mapEventName().toFixed(0)} ${this.serverType} Event Mapping Complate...`)
} }
} }
scan(folder: string): void { scan(folder: string): void {
var plugin = fs.file(root, folder); var plugin = fs.file(root, folder)
var files = [] var files = []
// load common plugin // load common plugin
.concat(this.scanFloder(plugin)) .concat(this.scanFloder(plugin))
// load space plugin // load space plugin
.concat(this.scanFloder(fs.file(plugin, this.serverType))) .concat(this.scanFloder(fs.file(plugin, this.serverType)))
this.loadPlugins(files); this.loadPlugins(files)
} }
build(container: Container): void { build(container: Container): void {
this.buildPlugins(container); this.buildPlugins(container)
} }
load(...args: any[]): void { load(...args: any[]): void {
this.checkAndGet(args[0]).forEach(pl => { this.checkAndGet(args[0]).forEach(pl => {
this.runCatch(pl, 'load'); this.runCatch(pl, 'load')
this.runCatch(pl, `${this.serverType}load`); this.runCatch(pl, `${this.serverType}load`)
}); })
} }
enable(...args: any[]): void { enable(...args: any[]): void {
this.checkAndGet(args[0]).forEach(plugin => { this.checkAndGet(args[0]).forEach(plugin => {
this.runCatch(plugin, 'enable') this.runCatch(plugin, 'enable')
this.runCatch(plugin, `${this.serverType}enable`); this.runCatch(plugin, `${this.serverType}enable`)
this.registryCommand(plugin); this.registryCommand(plugin)
this.registryListener(plugin); this.registryListener(plugin)
}); })
} }
disable(...args: any[]): void { disable(...args: any[]): void {
this.checkAndGet(args[0]).forEach(pl => { this.checkAndGet(args[0]).forEach(pl => {
this.runCatch(pl, 'disable'); this.runCatch(pl, 'disable')
this.runCatch(pl, `${this.serverType}disable`); this.runCatch(pl, `${this.serverType}disable`)
this.unregistryCommand(pl); this.unregistryCommand(pl)
this.unregistryListener(pl); this.unregistryListener(pl)
}); })
} }
reload(...args: any[]): void { reload(...args: any[]): void {
this.checkAndGet(args[0]).forEach((pl: interfaces.Plugin) => { this.checkAndGet(args[0]).forEach((pl: interfaces.Plugin) => {
this.disable(pl); this.disable(pl)
this.loadPlugin(pl.description.source); this.loadPlugin(pl.description.source)
pl = this.buildPlugin(getPlugin(pl.description.name)); pl = this.buildPlugin(getPlugin(pl.description.name))
this.load(pl); this.load(pl)
this.enable(pl); this.enable(pl)
}) })
} }
getPlugins() { getPlugins() {
return this.pluginMap; return this.pluginMap
} }
private runCatch(pl: any, func: string) { private runCatch(pl: any, func: string) {
try { try {
if (pl[func]) pl[func].call(pl); if (pl[func]) pl[func].call(pl)
} catch (ex) { } catch (ex) {
console.console(`§6插件 §b${pl.description.name} §6执行 §d${func} §6方法时发生错误 §4${ex}`); console.console(`§6插件 §b${pl.description.name} §6执行 §d${func} §6方法时发生错误 §4${ex}`)
console.ex(ex); console.ex(ex)
} }
} }
private checkAndGet(name: string | interfaces.Plugin | undefined): Map<string, interfaces.Plugin> | interfaces.Plugin[] { private checkAndGet(name: string | interfaces.Plugin | undefined): Map<string, interfaces.Plugin> | interfaces.Plugin[] {
if (name == undefined) { if (name == undefined) { return this.pluginMap }
return this.pluginMap; if (typeof name == 'string' && this.pluginMap.has(name)) { return [this.pluginMap.get(name)] }
} if (name instanceof interfaces.Plugin) { return [name as interfaces.Plugin] }
if (!(name instanceof interfaces.Plugin) && !this.pluginMap.has(name)) { throw new Error(`Plugin ${JSON.stringify(name)} not exist!`)
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(`Scanning Plugins in ${plugin} ...`); console.info(`Scanning Plugins in ${plugin} ...`)
this.checkUpdateFolder(plugin); this.checkUpdateFolder(plugin)
fs.list(plugin).forEach((file: any) => files.push(file.toFile())); fs.list(plugin).forEach((file: any) => files.push(file.toFile()))
return files; return files
} }
/** /**
@ -116,14 +112,14 @@ export class PluginManagerImpl implements plugin.PluginManager {
* @param path * @param path
*/ */
private checkUpdateFolder(path) { private checkUpdateFolder(path) {
var update = fs.file(path, "update"); var update = fs.file(path, "update")
if (!update.exists()) { if (!update.exists()) {
update.mkdirs(); update.mkdirs()
} }
} }
private loadPlugins(files: any[]): void { private loadPlugins(files: any[]): void {
this.loadJsPlugins(files); this.loadJsPlugins(files)
} }
/** /**
@ -135,19 +131,19 @@ export class PluginManagerImpl implements plugin.PluginManager {
private loadPlugin(file: any) { private loadPlugin(file: any) {
try { try {
this.updatePlugin(file); this.updatePlugin(file)
this.createPlugin(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 updatePlugin(file: any) { private updatePlugin(file: any) {
var update = fs.file(fs.file(file.parentFile, 'update'), file.name); var update = fs.file(fs.file(file.parentFile, 'update'), file.name)
if (update.exists()) { if (update.exists()) {
console.info(`Auto Update Plugin ${file.name} ...`); console.info(`Auto Update Plugin ${file.name} ...`)
fs.move(update, file, true); fs.move(update, file, true)
} }
} }
@ -155,43 +151,51 @@ export class PluginManagerImpl implements plugin.PluginManager {
//@ts-ignore //@ts-ignore
require(file, { require(file, {
cache: false cache: false
}); })
} }
private buildPlugins(container: Container) { private buildPlugins(container: Container) {
let pluginMetadatas = getPluginMetadatas(); let pluginMetadatas = getPluginMetadatas()
pluginMetadatas.forEach(metadata => { pluginMetadatas.forEach(metadata => {
this.buildPlugin(metadata); this.buildPlugin(metadata)
}); })
} }
private buildPlugin(metadata: interfaces.PluginMetadata) { private buildPlugin(metadata: interfaces.PluginMetadata) {
this.bindPlugin(metadata)
let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
if (!(pluginInstance instanceof interfaces.Plugin)) {
console.console(`§4found error plugin §b${metadata.source} §4it's not extends interfaces.Plugin, the plugin will be ignore!`)
return
}
this.pluginMap.set(metadata.name, pluginInstance)
pluginInstance.description = metadata
// @ts-ignore
pluginInstance.logger = new this.Console(metadata.prefix || metadata.name)
return pluginInstance
}
private bindPlugin(metadata: interfaces.PluginMetadata) {
try { try {
let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name) let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
if (pluginInstance.description.source + '' !== metadata.source + '') { if (pluginInstance.description.source + '' !== metadata.source + '') {
console.warn(`find duplicate plugin ${pluginInstance.description.source} and ${metadata.source}. the first plugin will be ignore!`) console.console(`§4found duplicate plugin §b${pluginInstance.description.source} §4and §b${metadata.source}§4. the first plugin will be ignore!`)
} }
container.rebind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name); container.rebind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
} catch{ } catch{
container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name); container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
} }
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.prefix || metadata.name);
return pluginInstance;
} }
private registryCommand(pluginInstance: interfaces.Plugin) { private registryCommand(pluginInstance: interfaces.Plugin) {
let cmds = getPluginCommandMetadata(pluginInstance); let cmds = getPluginCommandMetadata(pluginInstance)
let tabs = getPluginTabCompleterMetadata(pluginInstance); let tabs = getPluginTabCompleterMetadata(pluginInstance)
cmds.forEach(cmd => { cmds.forEach(cmd => {
let tab = tabs.get(cmd.name); let tab = tabs.get(cmd.name)
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
}); })
}) })
} }
@ -199,20 +203,20 @@ 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.servertype && event.servertype != this.serverType) { continue; } if (event.servertype && event.servertype != this.serverType) { 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))
} }
} }
private unregistryCommand(pluginInstance: interfaces.Plugin) { private unregistryCommand(pluginInstance: interfaces.Plugin) {
let cmds = getPluginCommandMetadata(pluginInstance); let cmds = getPluginCommandMetadata(pluginInstance)
cmds.forEach(cmd => { cmds.forEach(cmd => {
this.CommandManager.off(pluginInstance, cmd.name); this.CommandManager.off(pluginInstance, cmd.name)
}) })
} }
private unregistryListener(pluginInstance: interfaces.Plugin) { private unregistryListener(pluginInstance: interfaces.Plugin) {
this.EventManager.disable(pluginInstance); this.EventManager.disable(pluginInstance)
} }
} }