From 1c579c978934544c8ac44f967928f57945a6eae8 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 14 Oct 2022 10:07:36 +0800 Subject: [PATCH] feat: framework optimization Signed-off-by: MiaoWoo --- packages/api/src/database.ts | 4 ++++ packages/bukkit/src/internal/chat.ts | 7 +++++-- packages/common/src/http.ts | 10 ++++++++- packages/core/src/index.ts | 5 +++++ packages/plugin/src/manager.ts | 13 +++++------- packages/polyfill/src/xml-http-request.ts | 25 +++++++++++++++++------ 6 files changed, 47 insertions(+), 17 deletions(-) diff --git a/packages/api/src/database.ts b/packages/api/src/database.ts index 86b850e6..85b052a5 100644 --- a/packages/api/src/database.ts +++ b/packages/api/src/database.ts @@ -30,6 +30,10 @@ export namespace database { * 链接属性 */ properties?: { [key: string]: any } + /** + * 调试模式 + */ + debug?: boolean } @injectable() diff --git a/packages/bukkit/src/internal/chat.ts b/packages/bukkit/src/internal/chat.ts index 3bfb6e40..37e8a3a8 100644 --- a/packages/bukkit/src/internal/chat.ts +++ b/packages/bukkit/src/internal/chat.ts @@ -161,11 +161,12 @@ class BukkitChatInvoke_1_17_1 extends BukkitChatInvoke_1_16_5 { return base.getClass('net.minecraft.network.protocol.Packet') } } - -class BukkitChatInvoke_1_19 extends BukkitChatInvoke_1_17_1 { +class BukkitChatInvoke_1_18_2 extends BukkitChatInvoke_1_17_1 { getSendPacketMethodName(playerConnectionClass: any) { return playerConnectionClass.getMethod('a', this.getPacketClass()).getName() } +} +class BukkitChatInvoke_1_19 extends BukkitChatInvoke_1_18_2 { getPacketPlayOutChatClass() { return base.getClass('net.minecraft.network.protocol.game.ClientboundSystemChatPacket') } @@ -181,6 +182,8 @@ try { let nmsSubVersion = nmsVersion.split("_")[1] if (nmsSubVersion >= 19) { bukkitChatInvoke = new BukkitChatInvoke_1_19(nmsVersion) + } else if (nmsSubVersion >= 18) { + bukkitChatInvoke = new BukkitChatInvoke_1_18_2(nmsVersion) } else if (nmsSubVersion >= 17) { bukkitChatInvoke = new BukkitChatInvoke_1_17_1(nmsVersion) } else if (nmsSubVersion >= 16) { diff --git a/packages/common/src/http.ts b/packages/common/src/http.ts index f415859f..04406950 100644 --- a/packages/common/src/http.ts +++ b/packages/common/src/http.ts @@ -19,12 +19,20 @@ interface RequestConfig { method?: Method headers?: { [key: string]: string } params?: { [key: string]: string } - data?: any + data?: any, + connectTimeout?: number, + readTimeout?: number, } function request(config: RequestConfig) { // @ts-ignore XMLHttpRequest class only exist nashorn polyfill let xhr = new XMLHttpRequest() + if (config.connectTimeout) { + xhr.connectTimeout = config.connectTimeout + } + if (config.readTimeout) { + xhr.readTimeout = config.readTimeout + } xhr.open(config.method, config.url, false) for (const header in config.headers) { xhr.setRequestHeader(header, config.headers[header]) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 1500ef4a..9352dea6 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -9,6 +9,7 @@ import * as fs from '@ccms/common/dist/fs' import { VersionUtils } from '@ccms/common/dist/version' const UUID = Java.type('java.util.UUID') +const MiaoScriptAPI = Java.type('pw.yumc.MiaoScript.api.MiaoScriptAPI') @provideSingleton(MiaoScriptCore) class MiaoScriptCore { @@ -24,6 +25,10 @@ class MiaoScriptCore { enable() { process.emit('core.before.enable') this.loadServerConsole() + try { + MiaoScriptAPI.setPluginManager(this.pluginManager) + } catch (error) { + } this.loadPlugins() process.emit('core.after.enable') console.i18n("ms.core.engine.completed", { diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index 5acf5619..1b952982 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -2,12 +2,11 @@ import i18n from '@ccms/i18n' import { plugin, server } from '@ccms/api' import { provideSingleton, Container, ContainerInstance, Autowired } from '@ccms/container' -import './config' import { interfaces } from './interfaces' import { PluginTaskManager } from './task' import { PluginEventManager } from './event' -import { PluginCommandManager } from './command' import { PluginConfigManager } from './config' +import { PluginCommandManager } from './command' const Thread = Java.type('java.lang.Thread') @@ -93,18 +92,16 @@ export class PluginManagerImpl implements plugin.PluginManager { for (const [, scanner] of this.sacnnerMap) { try { console.i18n('ms.plugin.manager.scan', { scanner: scanner.type, folder }) - let plugins = scanner.scan(folder) - console.i18n('ms.plugin.manager.scan.finish', { scanner: scanner.type, folder, size: plugins.length }) - plugins.forEach(loadMetadata => { + let loadMetadatas = scanner.scan(folder) + console.i18n('ms.plugin.manager.scan.finish', { scanner: scanner.type, folder, size: loadMetadatas.length }) + for (const loadMetadata of loadMetadatas) { try { this.loadAndRequirePlugin(loadMetadata) } catch (error: any) { - console.error(`plugin scanner ${scanner.type} load ${loadMetadata.file} occurred error ${error}`) console.ex(error) } - }) + } } catch (error: any) { - console.error(`plugin scanner ${scanner.type} occurred error ${error}`) console.ex(error) } } diff --git a/packages/polyfill/src/xml-http-request.ts b/packages/polyfill/src/xml-http-request.ts index 83e5bb26..9189c423 100644 --- a/packages/polyfill/src/xml-http-request.ts +++ b/packages/polyfill/src/xml-http-request.ts @@ -74,7 +74,8 @@ type HttpHeader = { [key: string]: string } const executor = Executors.newCachedThreadPool() export class XMLHttpRequest { - private _timeout: number = 120000; + private _connectTimeout: number = 5000; + private _readTimeout: number = 120000; private _responseType: ResponseType = 'text'; private _withCredentials: boolean @@ -96,10 +97,22 @@ export class XMLHttpRequest { private _connection = null; get timeout() { - return this._timeout + return this._readTimeout } set timeout(timeout: number) { - this._timeout = timeout + this._readTimeout = timeout + } + get connectTimeout() { + return this._connectTimeout + } + set connectTimeout(timeout: number) { + this._connectTimeout = timeout + } + get readTimeout() { + return this._readTimeout + } + set readTimeout(timeout: number) { + this._readTimeout = timeout } get readyState() { return this._readyState @@ -169,8 +182,8 @@ export class XMLHttpRequest { this._connection.setRequestMethod(this._method) this._connection.setDoOutput(true) this._connection.setDoInput(true) - this._connection.setConnectTimeout(this._timeout) - this._connection.setReadTimeout(this._timeout) + this._connection.setConnectTimeout(this._connectTimeout) + this._connection.setReadTimeout(this._readTimeout) this.setRequestHeader('X-Requested-With', 'XMLHttpRequest') this.setReadyState(ReadyState.OPENED) @@ -181,7 +194,7 @@ export class XMLHttpRequest { } if (this._readyState !== ReadyState.OPENED) { throw new Error(`Error Status ${this._readyState}!`) } let future = executor.submit(new Callable({ call: () => this._send(body) })) - if (!this._async) { future.get(this._timeout, TimeUnit.MILLISECONDS) } + if (!this._async) { future.get(this._connectTimeout + this._readTimeout + 100, TimeUnit.MILLISECONDS) } return future } get() {