feat: support basic plugin...

This commit is contained in:
MiaoWoo 2020-05-08 18:41:56 +08:00
parent 4b3176e98e
commit b9ef3f2e33
3 changed files with 45 additions and 24 deletions

View File

@ -10,6 +10,7 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata
export function plugin(metadata: interfaces.PluginMetadata) { export function plugin(metadata: interfaces.PluginMetadata) {
return function (target: any) { return function (target: any) {
metadata.target = target; metadata.target = target;
metadata.type = "ioc";
decorate(injectable(), target); decorate(injectable(), target);
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target); Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target);
const previousMetadata: Map<string, interfaces.PluginMetadata> = getPluginMetadatas(); const previousMetadata: Map<string, interfaces.PluginMetadata> = getPluginMetadatas();

View File

@ -55,6 +55,10 @@ export namespace interfaces {
* __filename * __filename
*/ */
source: string; source: string;
/**
* ioc MiaoScript
*/
type?: string;
/** /**
* *
*/ */

View File

@ -25,17 +25,19 @@ export class PluginManagerImpl implements plugin.PluginManager {
private EventManager: event.Event private EventManager: event.Event
private initialized: boolean = false private initialized: boolean = false
private pluginMap: Map<string, interfaces.Plugin> private pluginRequireMap: Map<string, any>
private plugnMappings: Map<string, interfaces.PluginMetadata> private pluginInstanceMap: Map<string, interfaces.Plugin>
private pluginMetadataMap: Map<string, interfaces.PluginMetadata>
initialize() { initialize() {
if (this.pluginInstance !== null && this.initialized !== true) { if (this.pluginInstance !== null && this.initialized !== true) {
// 如果plugin不等于null 则代表是正式环境 // 如果plugin不等于null 则代表是正式环境
console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance, loader: Thread.currentThread().contextClassLoader }) console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance, loader: Thread.currentThread().contextClassLoader })
this.pluginMap = new Map()
console.i18n('ms.plugin.event.map', { count: this.EventManager.mapEventName().toFixed(0), type: this.serverType }); console.i18n('ms.plugin.event.map', { count: this.EventManager.mapEventName().toFixed(0), type: this.serverType });
this.pluginRequireMap = new Map()
this.pluginInstanceMap = new Map()
this.pluginMetadataMap = getPluginSources();
this.initialized = true; this.initialized = true;
this.plugnMappings = getPluginSources();
} }
} }
@ -67,8 +69,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
* @param file java.io.File * @param file java.io.File
*/ */
loadFromFile(file: string): interfaces.Plugin { loadFromFile(file: string): interfaces.Plugin {
this.loadPlugin(file) let metadata = this.loadPlugin(file);
let plugin = this.buildPlugin(this.plugnMappings.get(file.toString())) let plugin = this.buildPlugin(metadata && metadata.description ? metadata.description : this.pluginMetadataMap.get(file.toString()))
this.load(plugin) this.load(plugin)
this.enable(plugin) this.enable(plugin)
return plugin; return plugin;
@ -108,7 +110,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
getPlugins() { getPlugins() {
return this.pluginMap return this.pluginInstanceMap
} }
private runCatch(pl: any, func: string) { private runCatch(pl: any, func: string) {
@ -121,8 +123,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
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) { return this.pluginMap } if (name == undefined) { return this.pluginInstanceMap }
if (typeof name == 'string' && this.pluginMap.has(name)) { return [this.pluginMap.get(name)] } if (typeof name == 'string' && this.pluginInstanceMap.has(name)) { return [this.pluginInstanceMap.get(name)] }
if (name instanceof interfaces.Plugin) { return [name as interfaces.Plugin] } if (name instanceof interfaces.Plugin) { return [name as interfaces.Plugin] }
throw new Error(`Plugin ${JSON.stringify(name)} not exist!`) throw new Error(`Plugin ${JSON.stringify(name)} not exist!`)
} }
@ -161,7 +163,7 @@ 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) return this.createPlugin(file.toString())
} catch (ex) { } catch (ex) {
console.i18n("ms.plugin.manager.initialize.error", { name: file.name, ex }) console.i18n("ms.plugin.manager.initialize.error", { name: file.name, ex })
console.ex(ex) console.ex(ex)
@ -191,18 +193,25 @@ export class PluginManagerImpl implements plugin.PluginManager {
private createPlugin(file: string) { private createPlugin(file: string) {
//@ts-ignore //@ts-ignore
require(file + '', { cache: false }) let instance = require(file, { cache: false })
this.pluginRequireMap.set(file, instance);
return instance;
} }
private buildPlugins() { private buildPlugins() {
let metadatas = [];
let pluginMetadatas = getPluginMetadatas() let pluginMetadatas = getPluginMetadatas()
for (const [_, metadata] of pluginMetadatas) { for (const [_, metadata] of pluginMetadatas) { metadatas.push(metadata); }
for (const [_, instance] of this.pluginRequireMap) { if (instance.description) { this.buildPlugin(instance.description) } }
for (const metadata of metadatas) {
if (!this.allowProcess(metadata.servers)) { continue } if (!this.allowProcess(metadata.servers)) { continue }
this.buildPlugin(metadata) this.buildPlugin(metadata)
} }
} }
private buildPlugin(metadata: interfaces.PluginMetadata) { private buildPlugin(metadata: interfaces.PluginMetadata) {
switch (metadata.type) {
case "ioc":
try { try {
this.bindPlugin(metadata) this.bindPlugin(metadata)
let pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name) let pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
@ -210,12 +219,19 @@ export class PluginManagerImpl implements plugin.PluginManager {
console.i18n('ms.plugin.manager.build.not.extends', { source: metadata.source }) console.i18n('ms.plugin.manager.build.not.extends', { source: metadata.source })
return return
} }
this.pluginMap.set(metadata.name, pluginInstance) this.pluginInstanceMap.set(metadata.name, pluginInstance)
return pluginInstance; return pluginInstance;
} catch (ex) { } catch (ex) {
console.i18n("ms.plugin.manager.initialize.error", { name: metadata.name, ex }) console.i18n("ms.plugin.manager.initialize.error", { name: metadata.name, ex })
console.ex(ex) console.ex(ex)
} }
break;
case "basic":
this.pluginInstanceMap.set(metadata.name, this.pluginRequireMap.get(metadata.source.toString()))
break;
default:
throw new Error('§4不支持的插件类型 请检查加载器是否正常启用!')
}
} }
private bindPlugin(metadata: interfaces.PluginMetadata) { private bindPlugin(metadata: interfaces.PluginMetadata) {