feat: plugin & cmd add server check
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
e68005fd6f
commit
1ff33702d8
@ -10,7 +10,11 @@ export namespace interfaces {
|
|||||||
public enable() { }
|
public enable() { }
|
||||||
public disable() { }
|
public disable() { }
|
||||||
}
|
}
|
||||||
export interface PluginMetadata {
|
interface BaseMetadata {
|
||||||
|
name?: string;
|
||||||
|
servers?: string[];
|
||||||
|
}
|
||||||
|
export interface PluginMetadata extends BaseMetadata {
|
||||||
/**
|
/**
|
||||||
* 插件名称
|
* 插件名称
|
||||||
*/
|
*/
|
||||||
@ -36,20 +40,16 @@ export namespace interfaces {
|
|||||||
*/
|
*/
|
||||||
target?: any;
|
target?: any;
|
||||||
}
|
}
|
||||||
export interface CommandMetadata {
|
interface ExecMetadata extends BaseMetadata {
|
||||||
name?: string;
|
|
||||||
executor?: string;
|
executor?: string;
|
||||||
|
}
|
||||||
|
export interface CommandMetadata extends ExecMetadata {
|
||||||
paramtypes?: string[];
|
paramtypes?: string[];
|
||||||
}
|
}
|
||||||
export interface TabCompleterMetadata {
|
export interface TabCompleterMetadata extends ExecMetadata {
|
||||||
name?: string;
|
|
||||||
executor?: string;
|
|
||||||
paramtypes?: string[];
|
paramtypes?: string[];
|
||||||
}
|
}
|
||||||
export interface ListenerMetadata {
|
export interface ListenerMetadata extends ExecMetadata {
|
||||||
name?: string;
|
|
||||||
executor?: string;
|
|
||||||
servertype?: string;
|
|
||||||
}
|
}
|
||||||
export type PluginLike = Plugin | string;
|
export type PluginLike = Plugin | string;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { plugin, server, command, event, MiaoScriptConsole } from '@ms/api'
|
import { plugin, server, command, event, MiaoScriptConsole } from '@ms/api'
|
||||||
import { injectable, inject, postConstruct, Container, DefaultContainer as container } from '@ms/container'
|
import { injectable, inject, postConstruct, Container, ContainerInstance } from '@ms/container'
|
||||||
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'
|
||||||
@ -7,6 +7,8 @@ import { interfaces } from './interfaces'
|
|||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export class PluginManagerImpl implements plugin.PluginManager {
|
export class PluginManagerImpl implements plugin.PluginManager {
|
||||||
|
@inject(ContainerInstance)
|
||||||
|
private container: Container
|
||||||
@inject(plugin.PluginInstance)
|
@inject(plugin.PluginInstance)
|
||||||
private pluginInstance: any
|
private pluginInstance: any
|
||||||
@inject(server.ServerType)
|
@inject(server.ServerType)
|
||||||
@ -40,8 +42,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
this.loadPlugins(files)
|
this.loadPlugins(files)
|
||||||
}
|
}
|
||||||
|
|
||||||
build(container: Container): void {
|
build(): void {
|
||||||
this.buildPlugins(container)
|
this.buildPlugins()
|
||||||
}
|
}
|
||||||
|
|
||||||
load(...args: any[]): void {
|
load(...args: any[]): void {
|
||||||
@ -147,23 +149,24 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private createPlugin(file) {
|
private createPlugin(file: string) {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
require(file, {
|
require(file, {
|
||||||
cache: false
|
cache: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildPlugins(container: Container) {
|
private buildPlugins() {
|
||||||
let pluginMetadatas = getPluginMetadatas()
|
let pluginMetadatas = getPluginMetadatas()
|
||||||
pluginMetadatas.forEach(metadata => {
|
pluginMetadatas.forEach(metadata => {
|
||||||
|
if (metadata.servers?.indexOf(this.serverType) == -1) { return }
|
||||||
this.buildPlugin(metadata)
|
this.buildPlugin(metadata)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildPlugin(metadata: interfaces.PluginMetadata) {
|
private buildPlugin(metadata: interfaces.PluginMetadata) {
|
||||||
this.bindPlugin(metadata)
|
this.bindPlugin(metadata)
|
||||||
let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
|
let pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
|
||||||
if (!(pluginInstance instanceof interfaces.Plugin)) {
|
if (!(pluginInstance instanceof interfaces.Plugin)) {
|
||||||
console.console(`§4found error plugin §b${metadata.source} §4it's not extends interfaces.Plugin, the plugin will be ignore!`)
|
console.console(`§4found error plugin §b${metadata.source} §4it's not extends interfaces.Plugin, the plugin will be ignore!`)
|
||||||
return
|
return
|
||||||
@ -177,33 +180,34 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
|
|
||||||
private bindPlugin(metadata: interfaces.PluginMetadata) {
|
private bindPlugin(metadata: interfaces.PluginMetadata) {
|
||||||
try {
|
try {
|
||||||
let pluginInstance = container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
|
let pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
|
||||||
if (pluginInstance.description.source + '' !== metadata.source + '') {
|
if (pluginInstance.description.source + '' !== metadata.source + '') {
|
||||||
console.console(`§4found duplicate plugin §b${pluginInstance.description.source} §4and §b${metadata.source}§4. 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)
|
this.container.rebind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
|
||||||
} catch{
|
} catch{
|
||||||
container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
|
this.container.bind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 => {
|
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 }
|
||||||
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
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private registryListener(pluginInstance: interfaces.Plugin) {
|
private registryListener(pluginInstance: interfaces.Plugin) {
|
||||||
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.servers?.indexOf(this.serverType) == -1) { 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))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user