feat: add more event emit

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-04-02 16:51:08 +08:00
parent db526e9444
commit b55e662f81
2 changed files with 23 additions and 3 deletions

View File

@ -17,14 +17,18 @@ class MiaoScriptCore {
private pluginManager: plugin.PluginManager
enable() {
process.emit('core.before.enable')
this.loadServerConsole()
this.loadPlugins()
process.emit('core.after.enable')
return () => this.disable()
}
loadServerConsole() {
process.emit('core.before.load.console')
//@ts-ignore
global.setGlobal('console', new this.Console(), { writable: false, configurable: false })
process.emit('core.after.load.console')
}
loadPlugins() {
@ -81,6 +85,7 @@ function detectServer(): constants.ServerType {
}
function initialize() {
process.emit('core.before.initialize')
global.ScriptSlowExecuteTime = 30
global.ScriptEngineVersion = require('../package.json').version
try {
@ -97,21 +102,29 @@ function initialize() {
container.bind(plugin.PluginInstance).toConstantValue(base.getInstance())
container.bind(plugin.PluginFolder).toConstantValue('plugins')
let type = detectServer()
process.emit('core.before.initialize.detect')
console.i18n("ms.core.initialize.detect", { scope: global.scope, type })
container.bind(server.ServerType).toConstantValue(type)
container.bind(server.ServerChecker).toSelf().inSingletonScope()
container.bind(server.NativePluginManager).toSelf().inSingletonScope()
process.emit('core.after.initialize.detect')
process.emit('core.before.package.initialize')
console.i18n("ms.core.package.initialize", { scope: global.scope, type })
require(`${global.scope}/${type}`).default(container)
require(`${global.scope}/plugin`)
container.load(buildProviderModule())
console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 })
process.emit('core.after.package.initialize')
let disable = container.get<MiaoScriptCore>(MiaoScriptCore).enable()
console.i18n("ms.core.engine.completed", {
loader: base.version,
version: 'v' + global.ScriptEngineVersion,
time: (Date.now() - global.ScriptEngineStartTime) / 1000
})
process.emit('core.after.initialize')
return disable
} catch (error: any) {
console.i18n("ms.core.initialize.error", { error })

View File

@ -64,6 +64,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
initialize() {
if (this.pluginInstance === undefined) { throw new Error("Can't found Plugin Instance!") }
if (this.initialized !== true) {
process.emit('plugin.manager.before.initialize')
console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance, loader: Thread.currentThread().contextClassLoader })
console.i18n('ms.plugin.event.map', { count: this.eventManager.mapEventName(), type: this.serverType })
let pluginScanner = this.container.getAll<plugin.PluginScanner>(plugin.PluginScanner)
@ -77,13 +78,14 @@ export class PluginManagerImpl implements plugin.PluginManager {
this.loaderMap.set(loader.type, loader)
})
this.initialized = true
process.emit('plugin.initialize')
process.emit('plugin.manager.after.initialize')
}
}
scan(folder: string): void {
if (!folder) { throw new Error('plugin scan folder can\'t be empty!') }
this.initialize()
process.emit('plugin.manager.before.scan', folder)
for (const [, scanner] of this.sacnnerMap) {
try {
console.i18n('ms.plugin.manager.scan', { scanner: scanner.type, folder })
@ -102,12 +104,13 @@ export class PluginManagerImpl implements plugin.PluginManager {
console.ex(error)
}
}
process.emit('plugin.scan', folder)
process.emit('plugin.manager.after.scan', folder)
}
build(): void {
process.emit('plugin.manager.before.build')
this.buildPlugins()
process.emit('plugin.build')
process.emit('plugin.manager.after.build')
}
private logStage(plugin: plugin.Plugin, stage: string) {
@ -132,6 +135,7 @@ 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}!`) }
process.emit(`plugin.before.require`, loadMetadata)
try {
for (const [, loader] of this.loaderMap) {
if (this.loaderRequirePlugin(loadMetadata, loader)?.loaded) return loadMetadata.metadata
@ -154,6 +158,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
}
this.metadataMap.set(metadata.name, metadata)
metadata.loadMetadata = loadMetadata
process.emit(`plugin.after.require`, loadMetadata)
}
return loadMetadata
} catch (error: any) {
@ -269,6 +274,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
return loseDepends
}
private buildPlugin(metadata: plugin.PluginMetadata) {
process.emit(`plugin.before.build`, metadata)
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加载器是否正常启用!`) }
@ -280,6 +286,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
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)
process.emit(`plugin.after.build`, metadata, pluginInstance)
return pluginInstance
} catch (error: any) {
console.console(`§4无法加载插件 §b${metadata.name} §4构建插件失败!`)