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

View File

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

View File

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