refactor: optimize config & plugin load

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2021-06-22 15:51:13 +08:00
parent a4368cc9ba
commit 02dbf535bc
3 changed files with 40 additions and 28 deletions

View File

@ -47,8 +47,8 @@ export class PluginCommandManager {
let subcommand = args[0] || 'help'
let cmdKey = 'cmd' + subcommand
if (!pluginInstance[cmdKey]) {
console.sender(sender, '§4未知的子命令: §c' + subcommand)
pluginInstance['cmdhelp'] && console.sender(sender, `§6请执行 §b/${command} §ahelp §6查看帮助!`)
pluginInstance.logger.sender(sender, '§4未知的子命令: §c' + subcommand)
pluginInstance['cmdhelp'] && pluginInstance.logger.sender(sender, `§6请执行 §b/${command} §ahelp §6查看帮助!`)
return
}
args.shift()

View File

@ -85,25 +85,27 @@ export class PluginConfigManager {
try {
metadata.file = fs.concat(fs.file(plugin.description.loadMetadata.file).parent, plugin.description.name, metadata.filename)
let configLoader = this.getConfigLoader(metadata.format)
let value = plugin[metadata.variable]
let defaultValue = metadata.default ?? plugin[metadata.variable]
let configValue = defaultValue
if (!fs.exists(metadata.file)) {
base.save(metadata.file, configLoader.dump(value))
base.save(metadata.file, configLoader.dump(defaultValue))
console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: metadata.name, format: metadata.format })
} else {
value = configLoader.load(base.read(metadata.file))
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file} =>\n${JSON.stringify(value, undefined, 4)}`)
if (metadata.default) {
configValue = configLoader.load(base.read(metadata.file)) || {}
if (defaultValue) {
let needSave = false
for (const key of Object.keys(metadata.default)) {
if (value[key] == undefined) {
value[key] = metadata.default[key]
for (const key of Object.keys(defaultValue)) {
// 当配置文件不存在当前属性时才进行赋值
if (!Object.prototype.hasOwnProperty.call(configValue, key)) {
configValue[key] = defaultValue[key]
needSave = true
}
}
needSave && base.save(metadata.file, configLoader.dump(value))
needSave && base.save(metadata.file, configLoader.dump(configValue))
}
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file} =>\n${JSON.stringify(configValue, undefined, 4)}`)
}
this.defienConfigProp(plugin, metadata, value)
this.defienConfigProp(plugin, metadata, configValue)
} catch (error) {
console.i18n("ms.plugin.manager.config.load.error", { plugin: plugin.description.name, name: metadata.name, format: metadata.format, error })
console.ex(error)

View File

@ -41,6 +41,10 @@ export class PluginManagerImpl implements plugin.PluginManager {
private instanceMap: Map<string, plugin.Plugin>
private metadataMap: Map<string, plugin.PluginMetadata>
/**
*
*/
private lazyMetadataMap: Map<string, plugin.PluginMetadata>
constructor() {
this.sacnnerMap = new Map()
@ -48,6 +52,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
this.instanceMap = new Map()
this.metadataMap = new Map()
this.lazyMetadataMap = new Map()
// ignore unused
this.taskManager
@ -239,14 +244,14 @@ export class PluginManagerImpl implements plugin.PluginManager {
}
private buildPlugins() {
this.metadataMap.forEach((metadata) => {
try {
this.metadataMap.forEach((metadata, key) => {
if (metadata?.depends?.length) {
this.lazyMetadataMap.set(key, metadata)
} else {
this.buildPlugin(metadata)
} catch (error) {
console.console(`§4无法加载插件 §b${metadata.name} §4构建插件失败!`)
console.ex(error)
}
})
this.lazyMetadataMap.forEach((metadata, key) => this.buildPlugin(metadata))
}
private checkDepends(depends: string | string[]) {
@ -262,16 +267,21 @@ export class PluginManagerImpl implements plugin.PluginManager {
return loseDepends
}
private buildPlugin(metadata: plugin.PluginMetadata) {
try {
if (this.instanceMap.has(metadata.name)) { throw new Error(`Plugin ${metadata.name} is already load from ${metadata.source}...`) }
if (!this.loaderMap.has(metadata.type)) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查 §c${metadata.type} §4加载器是否正常启用!`) }
if (!this.serverChecker.check(metadata.servers)) { throw new Error(`§6插件 §b${metadata.name} §c服务器类型不兼容(${metadata.servers.join(',')}) §6忽略加载...`) }
let loseDepends = this.checkDepends(metadata.depends) || []
if (loseDepends.length) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查依赖 §3[${loseDepends.join(',')}] §4是否安装完整!`) }
if (loseDepends.length) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查脚本依赖 §3[${loseDepends.join(',')}] §4是否安装完整!`) }
let loseNativeDepends = this.checkNativeDepends(metadata.nativeDepends) || []
if (loseNativeDepends.length) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查插件依赖 §3[${loseNativeDepends.join(',')}] §4是否安装完整!`) }
let pluginInstance = this.loaderMap.get(metadata.type).build(metadata)
if (!pluginInstance) { throw new Error(`§4加载器 §c${metadata.type} §4加载插件 §c${metadata.name} §4失败!`) }
this.instanceMap.set(metadata.name, pluginInstance)
return pluginInstance
} catch (error) {
console.console(`§4无法加载插件 §b${metadata.name} §4构建插件失败!`)
console.ex(error)
}
}
}