feat: 优化插件加载逻辑
This commit is contained in:
@@ -13,6 +13,8 @@ import './config/loader'
|
||||
export class PluginConfigManager {
|
||||
@Autowired(ContainerInstance)
|
||||
private container: Container
|
||||
@Autowired(plugin.PluginFolder)
|
||||
private pluginFolder: string
|
||||
|
||||
private configLoaderMap = new Map<string, PluginConfigLoader>()
|
||||
|
||||
@@ -31,7 +33,7 @@ export class PluginConfigManager {
|
||||
}
|
||||
|
||||
getConfigLoader(format: string) {
|
||||
if (!this.configLoaderMap.has(format)) { throw new Error(`Unsupport config format ${format} !`) }
|
||||
if (!this.configLoaderMap.has(format)) { throw new Error(`unsupport config format ${format} !`) }
|
||||
return this.configLoaderMap.get(format)
|
||||
}
|
||||
|
||||
@@ -68,7 +70,8 @@ export class PluginConfigManager {
|
||||
try {
|
||||
let defaultValue = metadata.default ?? plugin[metadata.variable]
|
||||
metadata.file = fs.concat(
|
||||
fs.file(plugin.description.loadMetadata.file).parent,
|
||||
root,
|
||||
this.pluginFolder,
|
||||
plugin.description.name,
|
||||
metadata.filename
|
||||
)
|
||||
@@ -85,7 +88,7 @@ export class PluginConfigManager {
|
||||
if (metadata.migrate && defaultValue && this.setDefaultValue(configValue, defaultValue, !!metadata.default)) {
|
||||
base.save(metadata.file, configLoader.dump(configValue))
|
||||
}
|
||||
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file}`)
|
||||
console.debug(`[${plugin.description.name}] load config ${metadata.variable} from file ${metadata.file}`)
|
||||
this.defienConfigProp(plugin, metadata, configValue)
|
||||
} catch (error: any) {
|
||||
console.i18n("ms.plugin.manager.config.load.error", {
|
||||
@@ -118,7 +121,7 @@ export class PluginConfigManager {
|
||||
metadata.file = fs.concat(fs.file(plugin.description.loadMetadata.file).parent, plugin.description.name, metadata.filename)
|
||||
let result = this.getConfigLoader(metadata.format).dump(plugin[metadata.variable])
|
||||
base.save(metadata.file, result)
|
||||
console.debug(`[${plugin.description.name}] Save Config ${metadata.variable} to file ${metadata.file} =>
|
||||
console.debug(`[${plugin.description.name}] save config ${metadata.variable} to file ${metadata.file} =>
|
||||
${result.substring(0, 500)}`)
|
||||
return true
|
||||
} catch (error: any) {
|
||||
|
||||
@@ -17,11 +17,11 @@ export class BasicLoader implements plugin.PluginLoader {
|
||||
if (metadata?.type == this.type) {
|
||||
loadMetadata.metadata = metadata
|
||||
loadMetadata.loaded = true
|
||||
this.pluginRequireMap.set(metadata.source.toString(), loadMetadata.instance)
|
||||
this.pluginRequireMap.set(loadMetadata.path, loadMetadata.instance)
|
||||
}
|
||||
return loadMetadata
|
||||
}
|
||||
build(metadata: plugin.PluginMetadata) {
|
||||
return this.pluginRequireMap.get(metadata.source.toString())
|
||||
return this.pluginRequireMap.get(metadata.loadMetadata.path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ export class IocLoader implements plugin.PluginLoader {
|
||||
}
|
||||
|
||||
require(loadMetadata: plugin.PluginLoadMetadata) {
|
||||
let metadata = this.pluginMetadataMap.get(loadMetadata.file.toString())
|
||||
if (metadata && metadata.type == this.type) {
|
||||
let metadata = this.pluginMetadataMap.get(loadMetadata.path)
|
||||
if (metadata?.type == this.type) {
|
||||
loadMetadata.metadata = metadata
|
||||
loadMetadata.loaded = true
|
||||
}
|
||||
@@ -54,13 +54,6 @@ export class IocLoader implements plugin.PluginLoader {
|
||||
disable(plugin: plugin.Plugin): void {
|
||||
this.stage(plugin, 'disable')
|
||||
}
|
||||
reload(plugin: plugin.Plugin): void {
|
||||
this.disable(plugin)
|
||||
//@ts-ignore
|
||||
require(plugin.description.source, { cache: false })
|
||||
this.load(plugin)
|
||||
this.enable(plugin)
|
||||
}
|
||||
|
||||
private bindPlugin(metadata: plugin.PluginMetadata) {
|
||||
if (this.container.isBoundNamed(plugin.Plugin, metadata.name)) {
|
||||
|
||||
@@ -18,6 +18,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
private pluginInstance: any
|
||||
@Autowired(server.ServerType)
|
||||
private serverType: string
|
||||
@Autowired(plugin.PluginFolder)
|
||||
private pluginFolder: string
|
||||
|
||||
@Autowired()
|
||||
private serverChecker: server.ServerChecker
|
||||
@@ -58,6 +60,21 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
this.eventManager
|
||||
this.configManager
|
||||
this.commandManager
|
||||
process.on('core.enable', (core) => {
|
||||
process.emit('core.before.load.plugins')
|
||||
let loadPluginStartTime = new Date().getTime()
|
||||
console.i18n("ms.core.plugin.initialize")
|
||||
process.emit('core.load.plugins', this.pluginFolder)
|
||||
console.i18n("ms.core.plugin.completed", { time: (new Date().getTime() - loadPluginStartTime) / 1000 })
|
||||
process.emit('core.after.load.plugins')
|
||||
})
|
||||
process.on('core.load.plugins', (pluginFolder) => {
|
||||
this.scan(pluginFolder)
|
||||
this.buildPlugins()
|
||||
this.loadPlugins()
|
||||
this.enablePlugins()
|
||||
})
|
||||
process.on('core.disable', () => { this.disablePlugins() })
|
||||
}
|
||||
|
||||
initialize() {
|
||||
@@ -86,7 +103,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
}
|
||||
|
||||
scan(folder: string): void {
|
||||
if (!folder) { throw new Error('plugin scan folder can\'t be empty!') }
|
||||
if (!folder) { throw new Error("plugin scan folder can't be undefiend.") }
|
||||
this.initialize()
|
||||
process.emit('plugin.manager.before.scan', folder, this)
|
||||
for (const [, scanner] of this.sacnnerMap) {
|
||||
@@ -96,7 +113,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
console.i18n('ms.plugin.manager.scan.finish', { scanner: scanner.type, folder, size: loadMetadatas.length })
|
||||
for (const loadMetadata of loadMetadatas) {
|
||||
try {
|
||||
this.loadAndRequirePlugin(loadMetadata)
|
||||
this.require(loadMetadata)
|
||||
} catch (error: any) {
|
||||
console.console(`§c扫描器 §4${scanner.type} §c文件 §4${loadMetadata.file.toString().replace(root, '')} §c编译失败 请提供下列错误给开发者`)
|
||||
console.ex(error)
|
||||
@@ -109,12 +126,6 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
process.emit('plugin.manager.after.scan', folder, this)
|
||||
}
|
||||
|
||||
build(): void {
|
||||
process.emit('plugin.manager.before.build', this)
|
||||
this.buildPlugins()
|
||||
process.emit('plugin.manager.after.build', this)
|
||||
}
|
||||
|
||||
private logStage(plugin: plugin.Plugin, stage: string) {
|
||||
console.i18n("ms.plugin.manager.stage", { stage, plugin: plugin.description.name, version: plugin.description.version, author: plugin.description.author })
|
||||
}
|
||||
@@ -134,9 +145,9 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
private loadPlugin(loadMetadata: plugin.PluginLoadMetadata) {
|
||||
if (!loadMetadata) { throw new Error('loadMetadata can\'t be undefiend when loadPlugin!') }
|
||||
if (loadMetadata.loaded) { throw new Error(`Plugin file ${loadMetadata.file} is already loaded by ${loadMetadata.loader?.type}!`) }
|
||||
private loadPlugin(loadMetadata: plugin.PluginLoadMetadata): plugin.PluginMetadata {
|
||||
if (!loadMetadata) { throw new Error("loadMetadata can't be undefiend when loadPlugin.") }
|
||||
if (loadMetadata.loaded) { throw new Error(`plugin file ${loadMetadata.file} is already loaded by ${loadMetadata.loader?.type}.`) }
|
||||
process.emit(`plugin.before.require`, loadMetadata)
|
||||
try {
|
||||
for (const [, loader] of this.loaderMap) {
|
||||
@@ -145,6 +156,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
} catch (error: any) {
|
||||
console.i18n("ms.plugin.manager.initialize.error", { name: loadMetadata.file, ex: error })
|
||||
console.ex(error)
|
||||
} finally {
|
||||
process.emit(`plugin.after.require`, loadMetadata)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,19 +168,18 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
let metadata = loadMetadata.metadata
|
||||
if (this.metadataMap.has(metadata.name) && this.instanceMap.has(metadata.name)) {
|
||||
let oldMetadata = this.metadataMap.get(metadata.name)
|
||||
throw new Error(`Plugin ${oldMetadata.name} is already load from ${oldMetadata.source}...`)
|
||||
throw new Error(`plugin ${oldMetadata.name} is already load from ${oldMetadata.source}.`)
|
||||
}
|
||||
this.metadataMap.set(metadata.name, metadata)
|
||||
metadata.loadMetadata = loadMetadata
|
||||
process.emit(`plugin.after.require`, loadMetadata)
|
||||
}
|
||||
return loadMetadata
|
||||
} catch (error: any) {
|
||||
if (global.debug) {
|
||||
console.console(`§6Loader §b${loader.type} §6load §a${loadMetadata.file} §cerror. §4Err: §c${error}`)
|
||||
console.console(`§6loader §b${loader.type} §6load §a${loadMetadata.file} §cerror. §4Err: §c${error}`)
|
||||
console.ex(error)
|
||||
} else {
|
||||
console.warn(`Loader ${loader.type} load ${loadMetadata.file} error. Err: ${error}`)
|
||||
console.warn(`loader ${loader.type} load ${loadMetadata.file} error. Err: ${error}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,10 +187,10 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
private loadAndRequirePlugin(loadMetadata: plugin.PluginLoadMetadata) {
|
||||
let startTime = Date.now()
|
||||
let metadata = this.loadPlugin(loadMetadata.scanner.load(loadMetadata))
|
||||
if (!metadata) { throw new Error('load plugin metadata failed.') }
|
||||
if (!metadata) { throw new Error(`can't load plugin from metadata: ${JSON.stringify(loadMetadata)}`) }
|
||||
console.i18n('ms.plugin.manager.build', {
|
||||
name: loadMetadata.metadata.name,
|
||||
version: loadMetadata.metadata.version,
|
||||
name: metadata.name,
|
||||
version: metadata.version,
|
||||
file: loadMetadata.file.toString().replace(root, ''),
|
||||
scanner: loadMetadata.scanner.type,
|
||||
loader: loadMetadata.loader.type,
|
||||
@@ -192,39 +204,54 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
* @param file java.io.File
|
||||
*/
|
||||
loadFromFile(file: string, ext: any = 'js'): plugin.Plugin {
|
||||
if (!file) { throw new Error('plugin file can\'t be undefiend.') }
|
||||
if (!file) { throw new Error("plugin file can't be undefiend.") }
|
||||
let scanner = this.sacnnerMap.get(ext)
|
||||
if (!scanner) { throw new Error(`plugin scanner ${ext} can't found in sacnnerMap.`) }
|
||||
if (!scanner) { throw new Error(`scanner ${ext} can't found in sacnnerMap.`) }
|
||||
let metadata = this.loadAndRequirePlugin(scanner.read(file))
|
||||
this.buildPlugin(metadata)
|
||||
let plugin = metadata.target
|
||||
if (!plugin) { throw new Error(`plugin scanner ${ext} can't found in sacnnerMap.`) }
|
||||
if (!plugin) { throw new Error(`loader ${metadata.loadMetadata.loader.type} build plugin ${metadata.name} failed.`) }
|
||||
this.load(plugin)
|
||||
this.enable(plugin)
|
||||
return plugin
|
||||
}
|
||||
|
||||
load(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => this.runPluginStage(plugin, 'load'))
|
||||
load(plugin: plugin.Plugin): void {
|
||||
this.runPluginStage(plugin, 'load')
|
||||
}
|
||||
|
||||
enable(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => this.runPluginStage(plugin, 'enable'))
|
||||
enable(plugin: plugin.Plugin): void {
|
||||
this.runPluginStage(plugin, 'enable')
|
||||
}
|
||||
|
||||
disable(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => {
|
||||
this.runPluginStage(plugin, 'disable')
|
||||
this.metadataMap.delete(plugin.description.name)
|
||||
this.instanceMap.delete(plugin.description.name)
|
||||
})
|
||||
disable(plugin: plugin.Plugin): void {
|
||||
this.runPluginStage(plugin, 'disable')
|
||||
this.metadataMap.delete(plugin.description.name)
|
||||
this.instanceMap.delete(plugin.description.name)
|
||||
}
|
||||
|
||||
reload(...args: any[]): void {
|
||||
this.checkAndGet(args[0]).forEach((pl: plugin.Plugin) => {
|
||||
this.disable(pl)
|
||||
this.loadFromFile(pl.description.loadMetadata.file, pl.description.loadMetadata.scanner.type)
|
||||
})
|
||||
reload(plugin: plugin.Plugin): void {
|
||||
this.rebuild(plugin)
|
||||
}
|
||||
|
||||
require(loadMetadata: plugin.PluginLoadMetadata): plugin.PluginMetadata {
|
||||
return this.loadAndRequirePlugin(loadMetadata)
|
||||
}
|
||||
|
||||
build(metadata: plugin.PluginMetadata): plugin.Plugin {
|
||||
try {
|
||||
return this.buildPlugin(metadata)
|
||||
} catch (error: any) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
|
||||
rebuild(plugin: plugin.Plugin): void {
|
||||
if (!plugin) { throw new Error(`can't run reload plugin because plugin is ${plugin}`) }
|
||||
this.disable(plugin)
|
||||
let loadMetadata = plugin.description.loadMetadata
|
||||
delete require.cache[loadMetadata.path]
|
||||
this.loadFromFile(loadMetadata.file)
|
||||
}
|
||||
|
||||
has(name: string) {
|
||||
@@ -232,44 +259,63 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
}
|
||||
|
||||
get(name: string) {
|
||||
return this.instanceMap.get(name) || null
|
||||
return this.instanceMap.get(name)
|
||||
}
|
||||
|
||||
getPlugin(name: string) {
|
||||
return this.instanceMap.get(name) || null
|
||||
return this.instanceMap.get(name)
|
||||
}
|
||||
|
||||
getPlugins() {
|
||||
return this.instanceMap
|
||||
}
|
||||
|
||||
private runCatch(pl: any, func: string) {
|
||||
try {
|
||||
if (pl[func]) pl[func].call(pl)
|
||||
} catch (ex: any) {
|
||||
console.i18n("ms.plugin.manager.stage.exec.error", { plugin: pl.description.name, executor: func, error: ex })
|
||||
console.ex(ex)
|
||||
buildPlugins() {
|
||||
process.emit('plugin.manager.before.build', this)
|
||||
for (const [key, metadata] of this.metadataMap) {
|
||||
if (metadata.depends?.length) {
|
||||
this.lazyMetadataMap.set(key, metadata)
|
||||
} else {
|
||||
this.build(metadata)
|
||||
}
|
||||
}
|
||||
for (const metadata of this.lazyMetadataMap.values()) {
|
||||
this.build(metadata)
|
||||
}
|
||||
process.emit('plugin.manager.after.build', this)
|
||||
}
|
||||
|
||||
rebuildPlugins() {
|
||||
for (const plugin of this.instanceMap.values()) {
|
||||
this.rebuild(plugin)
|
||||
}
|
||||
}
|
||||
|
||||
private checkAndGet(name: string | plugin.Plugin | undefined | any): Map<string, plugin.Plugin> | plugin.Plugin[] {
|
||||
if (name === undefined) throw new Error(`checkAndGet Plugin can't be undefiend.`)
|
||||
if (name == this.instanceMap) { return this.instanceMap }
|
||||
if (typeof name == 'string' && this.instanceMap.has(name)) { return [this.instanceMap.get(name)] }
|
||||
if (name instanceof interfaces.Plugin) { return [name as plugin.Plugin] }
|
||||
if (name.description?.name) { return [name as plugin.Plugin] }
|
||||
throw new Error(`Plugin ${JSON.stringify(name)} not exist.`)
|
||||
loadPlugins() {
|
||||
for (const plugin of this.instanceMap.values()) {
|
||||
this.load(plugin)
|
||||
}
|
||||
}
|
||||
|
||||
private buildPlugins() {
|
||||
this.metadataMap.forEach((metadata, key) => {
|
||||
if (metadata?.depends?.length) {
|
||||
this.lazyMetadataMap.set(key, metadata)
|
||||
} else {
|
||||
this.tryBuildPlugin(metadata)
|
||||
}
|
||||
})
|
||||
this.lazyMetadataMap.forEach((metadata, key) => this.tryBuildPlugin(metadata))
|
||||
enablePlugins() {
|
||||
for (const plugin of this.instanceMap.values()) {
|
||||
this.enable(plugin)
|
||||
}
|
||||
}
|
||||
|
||||
disablePlugins() {
|
||||
for (const plugin of this.instanceMap.values()) {
|
||||
this.disable(plugin)
|
||||
}
|
||||
}
|
||||
|
||||
private runCatch(plugin: any, fn: string) {
|
||||
try {
|
||||
if (plugin[fn]) plugin[fn].call(plugin)
|
||||
} catch (ex: any) {
|
||||
console.i18n("ms.plugin.manager.stage.exec.error", { plugin: plugin.description.name, executor: fn, error: ex })
|
||||
console.ex(ex)
|
||||
}
|
||||
}
|
||||
|
||||
private checkDepends(depends: string | string[]) {
|
||||
@@ -280,18 +326,11 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
}
|
||||
private checkNativeDepends(depends: string | string[]) {
|
||||
if (!depends) return []
|
||||
let loseDepends = []
|
||||
for (const depend of depends) { if (!this.nativePluginManager.has(depend)) loseDepends.push(depend) }
|
||||
return loseDepends
|
||||
let loseNativeDepends = []
|
||||
for (const depend of depends) { if (!this.nativePluginManager.has(depend)) loseNativeDepends.push(depend) }
|
||||
return loseNativeDepends
|
||||
}
|
||||
|
||||
private tryBuildPlugin(metadata: plugin.PluginMetadata) {
|
||||
try {
|
||||
return this.buildPlugin(metadata)
|
||||
} catch (error: any) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
|
||||
private buildPlugin(metadata: plugin.PluginMetadata) {
|
||||
process.emit(`plugin.before.build`, metadata)
|
||||
@@ -304,6 +343,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
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失败!`) }
|
||||
process.emit(`plugin.build`, pluginInstance)
|
||||
metadata.target = pluginInstance
|
||||
this.instanceMap.set(metadata.name, pluginInstance)
|
||||
process.emit(`plugin.after.build`, metadata, pluginInstance)
|
||||
|
||||
@@ -13,15 +13,21 @@ export class JSFileScanner implements plugin.PluginScanner {
|
||||
}
|
||||
|
||||
read(file: any): plugin.PluginLoadMetadata {
|
||||
return { file, type: this.type, scanner: this, loaded: false }
|
||||
return {
|
||||
file,
|
||||
path: file.canonicalPath || file.toString(),
|
||||
type: this.type,
|
||||
scanner: this,
|
||||
loaded: false
|
||||
}
|
||||
}
|
||||
|
||||
load(metadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata {
|
||||
if (metadata.type !== this.type) { return }
|
||||
this.updatePlugin(metadata.file)
|
||||
//@ts-ignore load plugin not use cache
|
||||
metadata.instance = require(metadata.file.toString(), { cache: false })
|
||||
return metadata
|
||||
load(loadMetadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata {
|
||||
if (loadMetadata.type !== this.type) { return }
|
||||
this.updatePlugin(loadMetadata.file)
|
||||
delete require.cache[loadMetadata.path]
|
||||
loadMetadata.instance = require(loadMetadata.path)
|
||||
return loadMetadata
|
||||
}
|
||||
|
||||
private scanFolder(folder: any): string[] {
|
||||
@@ -45,7 +51,7 @@ export class JSFileScanner implements plugin.PluginScanner {
|
||||
}
|
||||
|
||||
private updatePlugin(file: any) {
|
||||
var update = fs.file(fs.file(fs.file(file).parentFile, 'update'), file.name)
|
||||
var update = fs.file(fs.concat(file.parentFile, 'update', file.name))
|
||||
if (update.exists()) {
|
||||
console.i18n("ms.plugin.manager.build.update", { name: file.name })
|
||||
fs.move(update, file, true)
|
||||
|
||||
Reference in New Issue
Block a user