From 53f20a9a8f5eed17c7a3a8877556a165206b3ec8 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 11 May 2020 17:20:50 +0800 Subject: [PATCH] feat: compatible blockly plugin Signed-off-by: MiaoWoo --- packages/api/src/event.ts | 3 ++ packages/core/src/index.ts | 81 ++++++++++++++++++---------------- packages/nashorn/src/index.ts | 2 +- packages/plugin/src/manager.ts | 30 +++++++------ 4 files changed, 65 insertions(+), 51 deletions(-) diff --git a/packages/api/src/event.ts b/packages/api/src/event.ts index 3750a0b2..54af5f06 100644 --- a/packages/api/src/event.ts +++ b/packages/api/src/event.ts @@ -21,6 +21,9 @@ export namespace event { @injectable() export abstract class Event { + // export EventPriority to blockly + public EventPriority = EventPriority; + private mapEvent = []; private listenerMap = []; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 622a2406..8ba00bfb 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,52 +1,57 @@ -let containerStartTime = Date.now(); -console.i18n("ms.core.ioc.initialize", { scope: global.scope }); +let containerStartTime = Date.now() +console.i18n("ms.core.ioc.initialize", { scope: global.scope }) import { plugin, server, task, constants } from '@ccms/api' import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule } from '@ccms/container' console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 }) +import http from '@ccms/common/dist/http' @provideSingleton(MiaoScriptCore) class MiaoScriptCore { @inject(server.Console) - private Console: Console; + private Console: Console @inject(task.TaskManager) - private taskManager: task.TaskManager; + private taskManager: task.TaskManager @inject(plugin.PluginFolder) - private pluginFolder: string; + private pluginFolder: string @inject(plugin.PluginManager) - private pluginManager: plugin.PluginManager; + private pluginManager: plugin.PluginManager enable() { - this.loadServerConsole(); - this.loadTaskFunction(); + this.loadServerConsole() + this.loadTaskFunction() global.level = "TRACE" - this.loadPlugins(); - return () => this.disable(); + this.loadPlugins() + return () => this.disable() } loadServerConsole() { //@ts-ignore - global.setGlobal('console', new this.Console()) + global.setGlobal('console', new this.Console(), { writable: false, configurable: false }) } loadTaskFunction() { - global.setGlobal('setTimeout', (func: Function, tick: number, async: boolean = false) => this.taskManager.create(func).later(tick).async(async).submit()) - global.setGlobal('setInterval', (func: Function, tick: number, async: boolean = false) => this.taskManager.create(func).timer(tick).async(async).submit()) + global.setGlobal('setTimeout', (func: Function, tick: number, async: boolean = false) => { + this.taskManager.create(func).later(tick).async(async).submit() + }, { writable: false, configurable: false }) + global.setGlobal('setInterval', (func: Function, tick: number, async: boolean = false) => { + this.taskManager.create(func).timer(tick).async(async).submit() + }, { writable: false, configurable: false }) } loadPlugins() { let loadPluginStartTime = new Date().getTime() console.i18n("ms.core.plugin.initialize") - this.pluginManager.scan(this.pluginFolder); - this.pluginManager.build(); - this.pluginManager.load(); - this.pluginManager.enable(); + this.pluginManager.scan(this.pluginFolder) + this.pluginManager.build() + this.pluginManager.load() + this.pluginManager.enable() console.i18n("ms.core.plugin.completed", { time: (new Date().getTime() - loadPluginStartTime) / 1000 }) } disable() { console.i18n("ms.core.engine.disable") - this.pluginManager.disable(); - this.taskManager.disable(); + this.pluginManager.disable() + this.taskManager.disable() //@ts-ignore require.disable() } @@ -54,22 +59,22 @@ class MiaoScriptCore { function detectServer(): constants.ServerType { try { - Java.type("org.bukkit.Bukkit"); + Java.type("org.bukkit.Bukkit") return constants.ServerType.Bukkit } catch (ex) { } try { - Java.type("org.spongepowered.api.Sponge"); + Java.type("org.spongepowered.api.Sponge") return constants.ServerType.Sponge } catch (ex) { } try { - Java.type("cn.nukkit.Nukkit"); + Java.type("cn.nukkit.Nukkit") return constants.ServerType.Nukkit } catch (ex) { } try { - Java.type("net.md_5.bungee.api.ProxyServer"); + Java.type("net.md_5.bungee.api.ProxyServer") return constants.ServerType.Bungee } catch (ex) { } @@ -77,27 +82,29 @@ function detectServer(): constants.ServerType { } function initialize() { + // @ts-ignore + try { engineLoad({ script: http.get("http://ms.yumc.pw/api/plugin/download/name/initialize"), name: 'core/initialize.js' }) } catch (error) { console.debug(error) } try { let corePackageStartTime = new Date().getTime() - container.bind(ContainerInstance).toConstantValue(container); - container.bind(plugin.PluginInstance).toConstantValue(base.getInstance()); - container.bind(plugin.PluginFolder).toConstantValue('plugins'); - let type = detectServer(); - console.i18n("ms.core.initialize.detect", { scope: global.scope, type }); - container.bind(server.ServerType).toConstantValue(type); - console.i18n("ms.core.package.initialize", { scope: global.scope, type }); - require(`${global.scope}/${type}`).default(container); + container.bind(ContainerInstance).toConstantValue(container) + container.bind(plugin.PluginInstance).toConstantValue(base.getInstance()) + container.bind(plugin.PluginFolder).toConstantValue('plugins') + let type = detectServer() + console.i18n("ms.core.initialize.detect", { scope: global.scope, type }) + container.bind(server.ServerType).toConstantValue(type) + 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 }); + container.load(buildProviderModule()) + console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 }) let disable = container.get(MiaoScriptCore).enable() - console.i18n("ms.core.engine.completed", { time: (Date.now() - global.NashornEngineStartTime) / 1000 }); - return disable; + console.i18n("ms.core.engine.completed", { time: (Date.now() - global.NashornEngineStartTime) / 1000 }) + return disable } catch (error) { - console.i18n("ms.core.initialize.error", { error }); + console.i18n("ms.core.initialize.error", { error }) console.ex(error) return () => console.i18n('ms.core.engine.disable.abnormal') } } -export default initialize(); +export default initialize() diff --git a/packages/nashorn/src/index.ts b/packages/nashorn/src/index.ts index 4483298e..7d296e89 100644 --- a/packages/nashorn/src/index.ts +++ b/packages/nashorn/src/index.ts @@ -46,7 +46,7 @@ declare global { debug: boolean; level: string; NashornEngineStartTime: number; - setGlobal: (key: string, value: any) => void; + setGlobal: (key: string, value: any, config?: PropertyDescriptor & ThisType) => void; noop: () => void; console: Console; } diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index ef528448..faa86e28 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -122,10 +122,11 @@ export class PluginManagerImpl implements plugin.PluginManager { } } - private checkAndGet(name: string | interfaces.Plugin | undefined): Map | interfaces.Plugin[] { + private checkAndGet(name: string | interfaces.Plugin | undefined | any): Map | interfaces.Plugin[] { if (name == undefined) { return this.pluginInstanceMap } if (typeof name == 'string' && this.pluginInstanceMap.has(name)) { return [this.pluginInstanceMap.get(name)] } if (name instanceof interfaces.Plugin) { return [name as interfaces.Plugin] } + if (name.description || name.description.name) { return [name as interfaces.Plugin] } throw new Error(`Plugin ${JSON.stringify(name)} not exist!`) } @@ -157,17 +158,19 @@ export class PluginManagerImpl implements plugin.PluginManager { * JS类型插件预加载 */ private loadJsPlugins(files: any[]) { - files.filter(file => file.name.endsWith(".js")).forEach(file => this.loadPlugin(file)) + files.filter(file => file.name.endsWith(".js")).forEach(file => { + try { + this.loadPlugin(file) + } catch (ex) { + console.i18n("ms.plugin.manager.initialize.error", { name: file.name, ex }) + console.ex(ex) + } + }) } private loadPlugin(file: any) { - try { - this.updatePlugin(file) - return this.createPlugin(file.toString()) - } catch (ex) { - console.i18n("ms.plugin.manager.initialize.error", { name: file.name, ex }) - console.ex(ex) - } + this.updatePlugin(file) + return this.createPlugin(file.toString()) } private updatePlugin(file: any) { @@ -210,28 +213,29 @@ export class PluginManagerImpl implements plugin.PluginManager { } private buildPlugin(metadata: interfaces.PluginMetadata) { + let pluginInstance: interfaces.Plugin; switch (metadata.type) { case "ioc": try { this.bindPlugin(metadata) - let pluginInstance = this.container.getNamed(plugin.Plugin, metadata.name) + pluginInstance = this.container.getNamed(plugin.Plugin, metadata.name) if (!(pluginInstance instanceof interfaces.Plugin)) { console.i18n('ms.plugin.manager.build.not.extends', { source: metadata.source }) return } - this.pluginInstanceMap.set(metadata.name, pluginInstance) - return pluginInstance; } catch (ex) { console.i18n("ms.plugin.manager.initialize.error", { name: metadata.name, ex }) console.ex(ex) } break; case "basic": - this.pluginInstanceMap.set(metadata.name, this.pluginRequireMap.get(metadata.source.toString())) + pluginInstance = this.pluginRequireMap.get(metadata.source.toString()) break; default: throw new Error('§4不支持的插件类型 请检查加载器是否正常启用!') } + this.pluginInstanceMap.set(metadata.name, pluginInstance) + return pluginInstance; } private bindPlugin(metadata: interfaces.PluginMetadata) {