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

View File

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