feat: merge stage exec & catch error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-04-26 10:19:44 +08:00
parent f40eddd5e3
commit 860946a44b
3 changed files with 27 additions and 19 deletions

View File

@ -34,6 +34,7 @@ ms.plugin.manager.scan: "Scanning Plugins in {folder} ..."
ms.plugin.manager.initialize.error: "§6Plugin §b{name} §6initialize error §4{ex}" ms.plugin.manager.initialize.error: "§6Plugin §b{name} §6initialize error §4{ex}"
ms.plugin.manager.stage: "{stage} {plugin} version {version} by {author}" ms.plugin.manager.stage: "{stage} {plugin} version {version} by {author}"
ms.plugin.manager.stage.exec: "[{plugin}] Exec {name} Stage {stage} When servers is {servers}..." ms.plugin.manager.stage.exec: "[{plugin}] Exec {name} Stage {stage} When servers is {servers}..."
ms.plugin.manager.stage.exec.error: "§6Plugin §b{plugin} §6exec §d{executor} §6function error §4{error}"
ms.plugin.manager.stage.load: "Loading" ms.plugin.manager.stage.load: "Loading"
ms.plugin.manager.stage.enable: "Enabling" ms.plugin.manager.stage.enable: "Enabling"
ms.plugin.manager.stage.disable: "Disabling" ms.plugin.manager.stage.disable: "Disabling"

View File

@ -34,6 +34,7 @@ ms.plugin.manager.scan: "扫描 {folder} 文件夹中插件..."
ms.plugin.manager.initialize.error: "§6插件 §b{name} §6初始化错误 §4{ex}" ms.plugin.manager.initialize.error: "§6插件 §b{name} §6初始化错误 §4{ex}"
ms.plugin.manager.stage: "{stage} {plugin} 版本 {version} 作者 {author}" ms.plugin.manager.stage: "{stage} {plugin} 版本 {version} 作者 {author}"
ms.plugin.manager.stage.exec: "[{plugin}] 执行 {stage} 阶段函数 {name} 匹配类型 {servers}..." ms.plugin.manager.stage.exec: "[{plugin}] 执行 {stage} 阶段函数 {name} 匹配类型 {servers}..."
ms.plugin.manager.stage.exec.error: "§6插件 §b{plugin} §6执行 §d{executor} §6函数发生异常 错误: §4{error}"
ms.plugin.manager.stage.load: "加载" ms.plugin.manager.stage.load: "加载"
ms.plugin.manager.stage.enable: "启用" ms.plugin.manager.stage.enable: "启用"
ms.plugin.manager.stage.disable: "关闭" ms.plugin.manager.stage.disable: "关闭"

View File

@ -52,35 +52,36 @@ export class PluginManagerImpl implements plugin.PluginManager {
console.i18n("ms.plugin.manager.stage", { stage, plugin: plugin.description.name, version: plugin.description.version, author: plugin.description.author }) console.i18n("ms.plugin.manager.stage", { stage, plugin: plugin.description.name, version: plugin.description.version, author: plugin.description.author })
} }
private runPluginStage(plugin: interfaces.Plugin, stage: string, ext: Function) {
this.logStage(plugin, i18n.translate(`ms.plugin.manager.stage.${stage}`))
this.runCatch(plugin, stage)
this.runCatch(plugin, `${this.serverType}${stage}`)
this.execPluginStage(plugin, stage)
}
load(...args: any[]): void { load(...args: any[]): void {
this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => {
this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.load")) this.runPluginStage(plugin, 'load', () => {
this.loadConfig(plugin) this.loadConfig(plugin)
this.runCatch(plugin, 'load') })
this.runCatch(plugin, `${this.serverType}load`)
this.execPluginStage(plugin, 'load')
}) })
} }
enable(...args: any[]): void { enable(...args: any[]): void {
this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => {
this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.enable")) this.runPluginStage(plugin, 'enable', () => {
this.registryCommand(plugin) this.registryCommand(plugin)
this.registryListener(plugin) this.registryListener(plugin)
this.runCatch(plugin, 'enable') })
this.runCatch(plugin, `${this.serverType}enable`)
this.execPluginStage(plugin, 'enable')
}) })
} }
disable(...args: any[]): void { disable(...args: any[]): void {
this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => {
this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.disable")) this.runPluginStage(plugin, 'disable', () => {
this.unregistryCommand(plugin) this.unregistryCommand(plugin)
this.unregistryListener(plugin) this.unregistryListener(plugin)
this.runCatch(plugin, 'disable') })
this.runCatch(plugin, `${this.serverType}disable`)
this.execPluginStage(plugin, 'disable')
}) })
} }
@ -102,7 +103,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
try { try {
if (pl[func]) pl[func].call(pl) if (pl[func]) pl[func].call(pl)
} catch (ex) { } catch (ex) {
console.console(`§6Plugin §b${pl.description.name} §6exec §d${func} §6function error §4${ex}`) console.i18n("ms.plugin.manager.stage.exec.error", { plugin: pl.description.name, executor: func, error: ex })
console.ex(ex) console.ex(ex)
} }
} }
@ -269,7 +270,12 @@ export class PluginManagerImpl implements plugin.PluginManager {
for (const stage of stages) { for (const stage of stages) {
if (!this.allowProcess(stage.servers)) { continue } if (!this.allowProcess(stage.servers)) { continue }
console.i18n("ms.plugin.manager.stage.exec", { plugin: pluginInstance.description.name, name: stage.executor, stage: stageName, servers: stage.servers }) console.i18n("ms.plugin.manager.stage.exec", { plugin: pluginInstance.description.name, name: stage.executor, stage: stageName, servers: stage.servers })
try {
pluginInstance[stage.executor].apply(pluginInstance) pluginInstance[stage.executor].apply(pluginInstance)
} catch (error) {
console.i18n("ms.plugin.manager.stage.exec.error", { plugin: pluginInstance.description.name, executor: stage.executor, error })
console.ex(error)
}
} }
} }
} }