From d5c2a825fc9e8bb99cf2106b1bce996862405f9f Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 3 Aug 2021 17:30:53 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MiaoWoo --- packages/api/src/server.ts | 17 +++++++++-- packages/bukkit/src/plugin.ts | 34 ++++++++++++++++++--- packages/bukkit/src/task.ts | 6 ++-- packages/plugin/src/decorators.ts | 3 +- packages/plugin/src/scanner/file-scanner.ts | 2 +- packages/polyfill/src/node-shim.ts | 22 ++++++++++--- 6 files changed, 66 insertions(+), 18 deletions(-) diff --git a/packages/api/src/server.ts b/packages/api/src/server.ts index fb302b8e..37c48924 100644 --- a/packages/api/src/server.ts +++ b/packages/api/src/server.ts @@ -16,15 +16,28 @@ export namespace server { * Runtime Server Instance */ export const ServerInstance = Symbol("ServerInstance") + export interface NativePlugin { + name: string + version: string + authors?: string | string[] + enable: boolean + depends?: string[] + softDepends?: string[] + /** + * 插件本体 + */ + origin: any + [key: string]: any + } @injectable() export abstract class NativePluginManager { - list(): any[] { + list(): NativePlugin[] { throw new Error("Method not implemented.") } has(name: string): boolean { return true } - get(name: string): any { + get(name: string): NativePlugin { throw new Error("Method not implemented.") } load(name: string): boolean { diff --git a/packages/bukkit/src/plugin.ts b/packages/bukkit/src/plugin.ts index 2a638928..8220c3db 100644 --- a/packages/bukkit/src/plugin.ts +++ b/packages/bukkit/src/plugin.ts @@ -1,12 +1,36 @@ import { server } from '@ccms/api' -const Bukkit = org.bukkit.Bukkit +const Bukkit: typeof org.bukkit.Bukkit = Java.type('org.bukkit.Bukkit') export class BukkitNativePluginManager extends server.NativePluginManager { - has(name: string) { - return !!this.get(name) + private bukkitPluginManager: org.bukkit.plugin.PluginManager + + constructor() { + super() + this.bukkitPluginManager = Bukkit.getPluginManager() } - get(name: string) { - return Bukkit.getPluginManager().getPlugin(name) + + list(): server.NativePlugin[] { + return Java.from(this.bukkitPluginManager.getPlugins()).map(plugin => this.convert(plugin)) + } + has(name: string): boolean { + return !!Bukkit.getPluginManager().getPlugin(name) + } + get(name: string): server.NativePlugin { + return this.convert(Bukkit.getPluginManager().getPlugin(name)) + } + + private convert(plugin: org.bukkit.plugin.Plugin): server.NativePlugin { + if (!plugin) return plugin as any + let desc = plugin.getDescription() + return { + name: plugin.getName(), + version: desc.getVersion(), + authors: Java.from(desc.getAuthors()), + depends: Java.from(desc.getDepend()), + softDepends: Java.from(desc.getSoftDepend()), + enable: plugin.isEnabled(), + origin: plugin + } } } diff --git a/packages/bukkit/src/task.ts b/packages/bukkit/src/task.ts index 81b3c2c7..0673fd2b 100644 --- a/packages/bukkit/src/task.ts +++ b/packages/bukkit/src/task.ts @@ -21,11 +21,11 @@ export class BukkitTaskManager extends task.TaskManager { export class BukkitTask extends task.Task { submit0(...args: any[]): task.Cancelable { let run = new BukkitRunnable({ run: () => this.run(...args) }) - let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}` + let suffix = this.isAsync ? 'Asynchronously' : '' if (this.interval) { - return run[funcName](base.getInstance(), this.laterTime, this.interval) + return run[`runTaskTimer${suffix}`](base.getInstance(), this.laterTime, this.interval) } else { - return run[funcName](base.getInstance(), this.laterTime) + return run[`runTaskLater${suffix}`](base.getInstance(), this.laterTime) } } } diff --git a/packages/plugin/src/decorators.ts b/packages/plugin/src/decorators.ts index ece6191f..dd7e6907 100644 --- a/packages/plugin/src/decorators.ts +++ b/packages/plugin/src/decorators.ts @@ -8,9 +8,8 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata * MiaoScript plugin * @param metadata PluginMetadata */ -export function plugin(metadata: pluginApi.PluginMetadata | any) { +export function plugin(metadata: pluginApi.PluginMetadata) { return function (target: any) { - if (!metadata.source) metadata = { souece: metadata } metadata = { name: target.name, version: '1.0.0', author: 'Unknow', target, type: 'ioc', ...metadata } decorate(injectable(), target) Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target) diff --git a/packages/plugin/src/scanner/file-scanner.ts b/packages/plugin/src/scanner/file-scanner.ts index c6785c9e..97630a4e 100644 --- a/packages/plugin/src/scanner/file-scanner.ts +++ b/packages/plugin/src/scanner/file-scanner.ts @@ -19,7 +19,7 @@ export class JSFileScanner implements plugin.PluginScanner { load(metadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata { if (metadata.type !== this.type) { return } this.updatePlugin(metadata.file) - //@ts-ignore + //@ts-ignore load plugin not use cache metadata.instance = require(metadata.file.toString(), { cache: false }) return metadata } diff --git a/packages/polyfill/src/node-shim.ts b/packages/polyfill/src/node-shim.ts index b2b312ea..62bcbd03 100644 --- a/packages/polyfill/src/node-shim.ts +++ b/packages/polyfill/src/node-shim.ts @@ -39,17 +39,29 @@ class Process extends EventEmitter { return super.on(event, (...args) => { try { listener(...args) - } catch (error) { + } catch (origin) { try { - super.emit('error', error) + super.emit('error', origin) } catch (error) { + console.ex(origin) console.ex(error) } } }) } - nextTick(func: Function) { - microTaskPool.execute(func) + nextTick(func: Function, ...args: any[]) { + microTaskPool.execute(() => { + try { + func(args) + } catch (origin) { + try { + super.emit('error', origin) + } catch (error) { + console.ex(origin) + console.ex(error) + } + } + }) } exit(code: number) { console.log(`process exit by code ${code}!`) @@ -198,7 +210,7 @@ Object.defineProperty(process, require('core-js/es/symbol/to-string-tag'), { val const eventLoop = new EventLoop() Object.defineProperty(process, 'eventLoop', { value: eventLoop }) eventLoop.startEventLoop() -global.setGlobal('queueMicrotask', (func: any) => microTaskPool.execute(func), {}) +global.setGlobal('queueMicrotask', (func: any, ...args: any[]) => process.nextTick(func, args), {}) global.setGlobal('setTimeout', eventLoop.setTimeout.bind(eventLoop), {}) global.setGlobal('clearTimeout', eventLoop.clearTimeout.bind(eventLoop), {}) global.setGlobal('setInterval', eventLoop.setInterval.bind(eventLoop), {})