feat: update Autowired & support command alias

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-09-22 17:33:59 +08:00
parent e9a4f2f351
commit bc19021c63
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 // add to cache Be used for close plugin to close event
if (!listenerMap[name]) listenerMap[name] = [] if (!listenerMap[name]) listenerMap[name] = []
var off = () => { var off = () => {
if (off['offed']) return
off['offed'] = true
this.unregister(eventCls, listener) this.unregister(eventCls, listener)
console.debug(i18n.translate("ms.api.event.unregister", { name, event: this.class2Name(eventCls), exec: executor })) 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 { export namespace plugin {
/** /**
* MiaoScript Plugin * MiaoScript Plugin
@ -11,23 +13,20 @@ export namespace plugin {
* Runtime Plugin Instance * Runtime Plugin Instance
*/ */
export const PluginInstance = Symbol("PluginInstance") export const PluginInstance = Symbol("PluginInstance")
/**
* MiaoScript Plugin Manager Symbol
*/
export const PluginManager = Symbol("PluginManager")
/** /**
* MiaoScript Plugin Manager Interface * MiaoScript Plugin Manager Interface
*/ */
export interface PluginManager { @injectable()
scan(folder: string): void export abstract class PluginManager {
build(): void abstract scan(folder: string): void
loadFromFile(file: string, scanner?: plugin.PluginScanner): Plugin abstract build(): void
load(...args: any[]): void abstract loadFromFile(file: string, scanner?: plugin.PluginScanner): Plugin
enable(...args: any[]): void abstract load(...args: any[]): void
disable(...args: any[]): void abstract enable(...args: any[]): void
reload(...args: any[]): void abstract disable(...args: any[]): void
getPlugin(name: string): plugin.Plugin abstract reload(...args: any[]): void
getPlugins(): Map<string, plugin.Plugin> abstract getPlugin(name: string): plugin.Plugin
abstract getPlugins(): Map<string, plugin.Plugin>
} }
export const PluginScanner = Symbol("PluginScanner") export const PluginScanner = Symbol("PluginScanner")
/** /**

View File

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

View File

@ -60,8 +60,22 @@ export const JSClass = (className: string) => {
* @param className * @param className
*/ */
export const Autowired = (className?: string | any) => { export const Autowired = (className?: string | any) => {
return function (target: any, propertyKey: string) { return function (target: any, propertyKey: string, index?: number) {
target[propertyKey] = getContainer().getNamed(ioc.Autowired, className || propertyKey) 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 * @param className
*/ */
export const Resource = (resourceName?: string | any) => { 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) 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() export const DefaultContainer = new Container()
initContainer(DefaultContainer) initContainer(DefaultContainer)
initAutowired(DefaultContainer)
export * from 'inversify' export * from 'inversify'
export * from './constants' export * from './constants'

View File

@ -49,10 +49,12 @@ class MiaoScriptCore {
} }
disable() { disable() {
let disableStartTime = Date.now()
console.i18n("ms.core.engine.disable") console.i18n("ms.core.engine.disable")
this.pluginManager.disable(this.pluginManager.getPlugins()) this.pluginManager.disable(this.pluginManager.getPlugins())
this.taskManager.disable() this.taskManager.disable()
process.exit(0) 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() { 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 { engineLoad({ script: http.get("http://ms.yumc.pw/api/plugin/download/name/initialize"), name: 'core/initialize.js' }) } catch (error) { console.debug(error) }
try { try {
let corePackageStartTime = new Date().getTime() let corePackageStartTime = new Date().getTime()
@ -103,7 +105,7 @@ function initialize() {
container.load(buildProviderModule()) container.load(buildProviderModule())
console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 }) console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 })
let disable = container.get<MiaoScriptCore>(MiaoScriptCore).enable() 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 return disable
} catch (error) { } catch (error) {
console.i18n("ms.core.initialize.error", { 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.package.completed: "MiaoScript 扩展 {scope}/core {scope}/{type} {scope}/plugin 加载完成 耗时({time}s)"
ms.core.plugin.initialize: "MiaoScript 开始引导插件系统. 请稍候..." ms.core.plugin.initialize: "MiaoScript 开始引导插件系统. 请稍候..."
ms.core.plugin.completed: "MiaoScript 插件加载完毕 耗时({time}s)!" ms.core.plugin.completed: "MiaoScript 插件加载完毕 耗时({time}s)!"
ms.core.engine.completed: "MiaoScript 脚本引擎 加载完毕... 耗时({time}s)!" ms.core.engine.completed: "MiaoScript 脚本引擎 {version} 加载完毕... 耗时({time}s)!"
ms.core.engine.disable: "关闭 MiaoScript 引擎..." ms.core.engine.disable: "MiaoScript 关闭脚本引擎 请稍候..."
ms.core.engine.disable.finish: "MiaoScript 脚本引擎 {version} 关闭完成... 耗时({time}s)!"
ms.core.engine.disable.abnormal: "引擎异常启动或初始化未完成 跳过关闭流程..." ms.core.engine.disable.abnormal: "引擎异常启动或初始化未完成 跳过关闭流程..."
ms.api.event.resource.not.found: "无法映射事件 未找到资源文件 {resource}!" ms.api.event.resource.not.found: "无法映射事件 未找到资源文件 {resource}!"

View File

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

View File

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

View File

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