feat: update Autowired & support command alias

Signed-off-by: MiaoWoo <admin@yumc.pw>
backup
MiaoWoo 2020-09-22 17:33:59 +08:00
parent af85703bd9
commit 05e9feee9b
9 changed files with 96 additions and 71 deletions

View File

@ -138,6 +138,8 @@ export namespace event {
// add to cache Be used for close plugin to close event
if (!listenerMap[name]) listenerMap[name] = []
var off = () => {
if (off['offed']) return
off['offed'] = true
this.unregister(eventCls, listener)
console.debug(i18n.translate("ms.api.event.unregister", { name, event: this.class2Name(eventCls), exec: executor }))
}

View File

@ -1,3 +1,5 @@
import { injectable } from '@ccms/container'
export namespace plugin {
/**
* MiaoScript Plugin
@ -11,23 +13,20 @@ export namespace plugin {
* Runtime Plugin Instance
*/
export const PluginInstance = Symbol("PluginInstance")
/**
* MiaoScript Plugin Manager Symbol
*/
export const PluginManager = Symbol("PluginManager")
/**
* MiaoScript Plugin Manager Interface
*/
export interface PluginManager {
scan(folder: string): void
build(): void
loadFromFile(file: string, scanner?: plugin.PluginScanner): Plugin
load(...args: any[]): void
enable(...args: any[]): void
disable(...args: any[]): void
reload(...args: any[]): void
getPlugin(name: string): plugin.Plugin
getPlugins(): Map<string, plugin.Plugin>
@injectable()
export abstract class PluginManager {
abstract scan(folder: string): void
abstract build(): void
abstract loadFromFile(file: string, scanner?: plugin.PluginScanner): Plugin
abstract load(...args: any[]): void
abstract enable(...args: any[]): void
abstract disable(...args: any[]): void
abstract reload(...args: any[]): void
abstract getPlugin(name: string): plugin.Plugin
abstract getPlugins(): Map<string, plugin.Plugin>
}
export const PluginScanner = Symbol("PluginScanner")
/**

View File

@ -22,19 +22,44 @@ export namespace server {
/**
* MiaoScript Server
*/
@injectable()
export abstract class Server {
abstract getVersion(): string
abstract getPlayer(name: string): any
abstract getOnlinePlayers(): any[]
abstract getConsoleSender(): any
abstract getService(service: string): any
abstract dispatchCommand(sender: string | any, command: string): boolean
abstract dispatchConsoleCommand(command: string): boolean
abstract getPluginsFolder(): string
abstract getNativePluginManager(): NativePluginManager
abstract getDedicatedServer?(): any
abstract getNettyPipeline(): any
abstract getRootLogger(): any
getVersion(): string {
throw new Error("Method not implemented.")
}
getPlayer(name: string): any {
throw new Error("Method not implemented.")
}
getOnlinePlayers(): any[] {
throw new Error("Method not implemented.")
}
getConsoleSender(): any {
throw new Error("Method not implemented.")
}
getService(service: string): any {
throw new Error("Method not implemented.")
}
dispatchCommand(sender: string | any, command: string): boolean {
throw new Error("Method not implemented.")
}
dispatchConsoleCommand(command: string): boolean {
throw new Error("Method not implemented.")
}
getPluginsFolder(): string {
throw new Error("Method not implemented.")
}
getNativePluginManager(): NativePluginManager {
throw new Error("Method not implemented.")
}
getDedicatedServer?(): any {
throw new Error("Method not implemented.")
}
getNettyPipeline(): any {
throw new Error("Method not implemented.")
}
getRootLogger(): any {
throw new Error("Method not implemented.")
}
}
@injectable()
export class ServerChecker {
@ -54,50 +79,15 @@ export namespace server {
}
}
@injectable()
export abstract class ReflectServer implements server.Server {
export abstract class ReflectServer extends server.Server {
protected pipeline: any
protected rootLogger: any
constructor() {
super()
this.reflect()
}
getVersion(): string {
throw new Error("Method not implemented.")
}
getPlayer(name: string) {
throw new Error("Method not implemented.")
}
getOnlinePlayers(): any[] {
throw new Error("Method not implemented.")
}
getConsoleSender() {
throw new Error("Method not implemented.")
}
getService(service: string) {
throw new Error("Method not implemented.")
}
dispatchCommand(sender: any, command: string): boolean {
throw new Error("Method not implemented.")
}
dispatchConsoleCommand(command: string): boolean {
throw new Error("Method not implemented.")
}
getPluginsFolder(): string {
throw new Error("Method not implemented.")
}
getNativePluginManager(): NativePluginManager {
throw new Error("Method not implemented.")
}
getDedicatedServer() {
throw new Error("Method not implemented.")
}
getNettyPipeline() {
throw new Error("Method not implemented.")
}
getRootLogger() {
throw new Error("Method not implemented.")
}
protected reflect() {
try {
let consoleServer = this.getDedicatedServer()

View File

@ -60,8 +60,22 @@ export const JSClass = (className: string) => {
* @param className
*/
export const Autowired = (className?: string | any) => {
return function (target: any, propertyKey: string) {
target[propertyKey] = getContainer().getNamed(ioc.Autowired, className || propertyKey)
return function (target: any, propertyKey: string, index?: number) {
let container = getContainer()
let type = Reflect.getMetadata('design:type', target, propertyKey)
if (type && type !== Object) {
try {
return target[propertyKey] = container.getNamed(type, className || propertyKey)
} catch (error) {
try {
return target[propertyKey] = container.get(type)
} catch (error) {
}
}
}
if (container.isBound(ioc.Autowired)) {
target[propertyKey] = container.getNamed(ioc.Autowired, className || propertyKey)
}
}
}
@ -70,7 +84,7 @@ export const Autowired = (className?: string | any) => {
* @param className
*/
export const Resource = (resourceName?: string | any) => {
return function (target: any, propertyKey: string) {
return function (target: any, propertyKey: string, index?: number) {
target[propertyKey] = getContainer().getNamed(ioc.Resource, resourceName || propertyKey)
}
}
@ -82,8 +96,18 @@ export const reduceMetadata = (ctx: interfaces.Context): any => {
}, {})
}
function initAutowired(container: Container) {
container.bind(ioc.Autowired).toDynamicValue((ctx) => {
var metadata: any = reduceMetadata(ctx)
let key = Object.toString.call(metadata.named)
if (key === "[object Function]" || key === "[object Symbol]") { return container.get(metadata.named) }
return undefined
})
}
export const DefaultContainer = new Container()
initContainer(DefaultContainer)
initAutowired(DefaultContainer)
export * from 'inversify'
export * from './constants'

View File

@ -49,10 +49,12 @@ class MiaoScriptCore {
}
disable() {
let disableStartTime = Date.now()
console.i18n("ms.core.engine.disable")
this.pluginManager.disable(this.pluginManager.getPlugins())
this.taskManager.disable()
process.exit(0)
console.i18n("ms.core.engine.disable.finish", { version: 'v' + global.ScriptEngineVersion, time: (new Date().getTime() - disableStartTime) / 1000 })
}
}
@ -86,7 +88,7 @@ function detectServer(): constants.ServerType {
}
function initialize() {
// @ts-ignore
global.ScriptEngineVersion = require('../package.json').version
try { engineLoad({ script: http.get("http://ms.yumc.pw/api/plugin/download/name/initialize"), name: 'core/initialize.js' }) } catch (error) { console.debug(error) }
try {
let corePackageStartTime = new Date().getTime()
@ -103,7 +105,7 @@ function initialize() {
container.load(buildProviderModule())
console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 })
let disable = container.get<MiaoScriptCore>(MiaoScriptCore).enable()
console.i18n("ms.core.engine.completed", { time: (Date.now() - global.NashornEngineStartTime) / 1000 })
console.i18n("ms.core.engine.completed", { version: 'v' + global.ScriptEngineVersion, time: (Date.now() - global.ScriptEngineStartTime) / 1000 })
return disable
} catch (error) {
console.i18n("ms.core.initialize.error", { error })

View File

@ -10,8 +10,9 @@ ms.core.package.initialize: "初始化 MiaoScript 扩展 {scope}/core {scope}/{t
ms.core.package.completed: "MiaoScript 扩展 {scope}/core {scope}/{type} {scope}/plugin 加载完成 耗时({time}s)"
ms.core.plugin.initialize: "MiaoScript 开始引导插件系统. 请稍候..."
ms.core.plugin.completed: "MiaoScript 插件加载完毕 耗时({time}s)!"
ms.core.engine.completed: "MiaoScript 脚本引擎 加载完毕... 耗时({time}s)!"
ms.core.engine.disable: "关闭 MiaoScript 引擎..."
ms.core.engine.completed: "MiaoScript 脚本引擎 {version} 加载完毕... 耗时({time}s)!"
ms.core.engine.disable: "MiaoScript 关闭脚本引擎 请稍候..."
ms.core.engine.disable.finish: "MiaoScript 脚本引擎 {version} 关闭完成... 耗时({time}s)!"
ms.core.engine.disable.abnormal: "引擎异常启动或初始化未完成 跳过关闭流程..."
ms.api.event.resource.not.found: "无法映射事件 未找到资源文件 {resource}!"

View File

@ -48,7 +48,8 @@ declare global {
logger: any
debug: boolean
level: string
NashornEngineStartTime: number
ScriptEngineVersion: string
ScriptEngineStartTime: number
setGlobal: (key: string, value: any, config?: PropertyDescriptor & ThisType<any>) => void
noop: () => void
console: Console

View File

@ -35,6 +35,7 @@ export function cmd(metadata: interfaces.CommandMetadata = {}) {
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
const previousMetadata: Map<string, interfaces.CommandMetadata> = getPluginCommandMetadata(target)
previousMetadata.set(metadata.name, metadata)
metadata.alias?.forEach((name) => previousMetadata.set(name, metadata))
Reflect.defineMetadata(METADATA_KEY.cmd, previousMetadata, target.constructor)
}
}
@ -52,6 +53,7 @@ export function tab(metadata: interfaces.CommandMetadata = {}) {
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
const previousMetadata: Map<string, interfaces.CommandMetadata> = getPluginTabCompleterMetadata(target)
previousMetadata.set(metadata.name, metadata)
metadata.alias?.forEach((name) => previousMetadata.set(name, metadata))
Reflect.defineMetadata(METADATA_KEY.tab, previousMetadata, target.constructor)
}
}

View File

@ -35,6 +35,10 @@ export namespace interfaces {
executor?: string
}
export interface CommandMetadata extends ExecMetadata {
/**
*
*/
alias?: string[]
/**
*
*/