feat: compatible blockly plugin

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-11 17:20:50 +08:00
parent 76fd6b8318
commit 53f20a9a8f
4 changed files with 65 additions and 51 deletions

View File

@ -21,6 +21,9 @@ export namespace event {
@injectable() @injectable()
export abstract class Event { export abstract class Event {
// export EventPriority to blockly
public EventPriority = EventPriority;
private mapEvent = []; private mapEvent = [];
private listenerMap = []; private listenerMap = [];

View File

@ -1,52 +1,57 @@
let containerStartTime = Date.now(); let containerStartTime = Date.now()
console.i18n("ms.core.ioc.initialize", { scope: global.scope }); console.i18n("ms.core.ioc.initialize", { scope: global.scope })
import { plugin, server, task, constants } from '@ccms/api' import { plugin, server, task, constants } from '@ccms/api'
import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule } from '@ccms/container' 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 }) console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 })
import http from '@ccms/common/dist/http'
@provideSingleton(MiaoScriptCore) @provideSingleton(MiaoScriptCore)
class MiaoScriptCore { class MiaoScriptCore {
@inject(server.Console) @inject(server.Console)
private Console: Console; private Console: Console
@inject(task.TaskManager) @inject(task.TaskManager)
private taskManager: task.TaskManager; private taskManager: task.TaskManager
@inject(plugin.PluginFolder) @inject(plugin.PluginFolder)
private pluginFolder: string; private pluginFolder: string
@inject(plugin.PluginManager) @inject(plugin.PluginManager)
private pluginManager: plugin.PluginManager; private pluginManager: plugin.PluginManager
enable() { enable() {
this.loadServerConsole(); this.loadServerConsole()
this.loadTaskFunction(); this.loadTaskFunction()
global.level = "TRACE" global.level = "TRACE"
this.loadPlugins(); this.loadPlugins()
return () => this.disable(); return () => this.disable()
} }
loadServerConsole() { loadServerConsole() {
//@ts-ignore //@ts-ignore
global.setGlobal('console', new this.Console()) global.setGlobal('console', new this.Console(), { writable: false, configurable: false })
} }
loadTaskFunction() { loadTaskFunction() {
global.setGlobal('setTimeout', (func: Function, tick: number, async: boolean = false) => this.taskManager.create(func).later(tick).async(async).submit()) global.setGlobal('setTimeout', (func: Function, tick: number, async: boolean = false) => {
global.setGlobal('setInterval', (func: Function, tick: number, async: boolean = false) => this.taskManager.create(func).timer(tick).async(async).submit()) 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() { loadPlugins() {
let loadPluginStartTime = new Date().getTime() let loadPluginStartTime = new Date().getTime()
console.i18n("ms.core.plugin.initialize") console.i18n("ms.core.plugin.initialize")
this.pluginManager.scan(this.pluginFolder); this.pluginManager.scan(this.pluginFolder)
this.pluginManager.build(); this.pluginManager.build()
this.pluginManager.load(); this.pluginManager.load()
this.pluginManager.enable(); this.pluginManager.enable()
console.i18n("ms.core.plugin.completed", { time: (new Date().getTime() - loadPluginStartTime) / 1000 }) console.i18n("ms.core.plugin.completed", { time: (new Date().getTime() - loadPluginStartTime) / 1000 })
} }
disable() { disable() {
console.i18n("ms.core.engine.disable") console.i18n("ms.core.engine.disable")
this.pluginManager.disable(); this.pluginManager.disable()
this.taskManager.disable(); this.taskManager.disable()
//@ts-ignore //@ts-ignore
require.disable() require.disable()
} }
@ -54,22 +59,22 @@ class MiaoScriptCore {
function detectServer(): constants.ServerType { function detectServer(): constants.ServerType {
try { try {
Java.type("org.bukkit.Bukkit"); Java.type("org.bukkit.Bukkit")
return constants.ServerType.Bukkit return constants.ServerType.Bukkit
} catch (ex) { } catch (ex) {
} }
try { try {
Java.type("org.spongepowered.api.Sponge"); Java.type("org.spongepowered.api.Sponge")
return constants.ServerType.Sponge return constants.ServerType.Sponge
} catch (ex) { } catch (ex) {
} }
try { try {
Java.type("cn.nukkit.Nukkit"); Java.type("cn.nukkit.Nukkit")
return constants.ServerType.Nukkit return constants.ServerType.Nukkit
} catch (ex) { } catch (ex) {
} }
try { try {
Java.type("net.md_5.bungee.api.ProxyServer"); Java.type("net.md_5.bungee.api.ProxyServer")
return constants.ServerType.Bungee return constants.ServerType.Bungee
} catch (ex) { } catch (ex) {
} }
@ -77,27 +82,29 @@ function detectServer(): constants.ServerType {
} }
function initialize() { 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 { try {
let corePackageStartTime = new Date().getTime() let corePackageStartTime = new Date().getTime()
container.bind(ContainerInstance).toConstantValue(container); container.bind(ContainerInstance).toConstantValue(container)
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()
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)
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 })
let disable = container.get<MiaoScriptCore>(MiaoScriptCore).enable() let disable = container.get<MiaoScriptCore>(MiaoScriptCore).enable()
console.i18n("ms.core.engine.completed", { time: (Date.now() - global.NashornEngineStartTime) / 1000 }); console.i18n("ms.core.engine.completed", { time: (Date.now() - global.NashornEngineStartTime) / 1000 })
return disable; return disable
} catch (error) { } catch (error) {
console.i18n("ms.core.initialize.error", { error }); console.i18n("ms.core.initialize.error", { error })
console.ex(error) console.ex(error)
return () => console.i18n('ms.core.engine.disable.abnormal') return () => console.i18n('ms.core.engine.disable.abnormal')
} }
} }
export default initialize(); export default initialize()

View File

@ -46,7 +46,7 @@ declare global {
debug: boolean; debug: boolean;
level: string; level: string;
NashornEngineStartTime: number; NashornEngineStartTime: number;
setGlobal: (key: string, value: any) => void; setGlobal: (key: string, value: any, config?: PropertyDescriptor & ThisType<any>) => void;
noop: () => void; noop: () => void;
console: Console; console: Console;
} }

View File

@ -122,10 +122,11 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
} }
private checkAndGet(name: string | interfaces.Plugin | undefined): Map<string, interfaces.Plugin> | interfaces.Plugin[] { private checkAndGet(name: string | interfaces.Plugin | undefined | any): Map<string, interfaces.Plugin> | interfaces.Plugin[] {
if (name == undefined) { return this.pluginInstanceMap } if (name == undefined) { return this.pluginInstanceMap }
if (typeof name == 'string' && this.pluginInstanceMap.has(name)) { return [this.pluginInstanceMap.get(name)] } 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 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!`) throw new Error(`Plugin ${JSON.stringify(name)} not exist!`)
} }
@ -157,17 +158,19 @@ export class PluginManagerImpl implements plugin.PluginManager {
* JS类型插件预加载 * JS类型插件预加载
*/ */
private loadJsPlugins(files: any[]) { 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 => {
}
private loadPlugin(file: any) {
try { try {
this.updatePlugin(file) this.loadPlugin(file)
return this.createPlugin(file.toString())
} catch (ex) { } catch (ex) {
console.i18n("ms.plugin.manager.initialize.error", { name: file.name, ex }) console.i18n("ms.plugin.manager.initialize.error", { name: file.name, ex })
console.ex(ex) console.ex(ex)
} }
})
}
private loadPlugin(file: any) {
this.updatePlugin(file)
return this.createPlugin(file.toString())
} }
private updatePlugin(file: any) { private updatePlugin(file: any) {
@ -210,28 +213,29 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
private buildPlugin(metadata: interfaces.PluginMetadata) { private buildPlugin(metadata: interfaces.PluginMetadata) {
let pluginInstance: interfaces.Plugin;
switch (metadata.type) { switch (metadata.type) {
case "ioc": case "ioc":
try { try {
this.bindPlugin(metadata) this.bindPlugin(metadata)
let pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name) pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
if (!(pluginInstance instanceof interfaces.Plugin)) { if (!(pluginInstance instanceof interfaces.Plugin)) {
console.i18n('ms.plugin.manager.build.not.extends', { source: metadata.source }) console.i18n('ms.plugin.manager.build.not.extends', { source: metadata.source })
return return
} }
this.pluginInstanceMap.set(metadata.name, pluginInstance)
return pluginInstance;
} catch (ex) { } catch (ex) {
console.i18n("ms.plugin.manager.initialize.error", { name: metadata.name, ex }) console.i18n("ms.plugin.manager.initialize.error", { name: metadata.name, ex })
console.ex(ex) console.ex(ex)
} }
break; break;
case "basic": case "basic":
this.pluginInstanceMap.set(metadata.name, this.pluginRequireMap.get(metadata.source.toString())) pluginInstance = this.pluginRequireMap.get(metadata.source.toString())
break; break;
default: default:
throw new Error('§4不支持的插件类型 请检查加载器是否正常启用!') throw new Error('§4不支持的插件类型 请检查加载器是否正常启用!')
} }
this.pluginInstanceMap.set(metadata.name, pluginInstance)
return pluginInstance;
} }
private bindPlugin(metadata: interfaces.PluginMetadata) { private bindPlugin(metadata: interfaces.PluginMetadata) {