From 8e5588ca23cdb028209b1e4d2ec0a249ae8d083b Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 24 Apr 2020 13:05:46 +0800 Subject: [PATCH] fix: netty inject error Signed-off-by: MiaoWoo --- packages/api/src/event.ts | 4 ++-- packages/bukkit/src/event.ts | 2 +- packages/bukkit/src/server.ts | 29 ++++++++++++++++------------- packages/sponge/src/server.ts | 28 ++++++++++++++++------------ 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/packages/api/src/event.ts b/packages/api/src/event.ts index 0b6f9fa6..0d58f8a5 100644 --- a/packages/api/src/event.ts +++ b/packages/api/src/event.ts @@ -62,8 +62,8 @@ export namespace event { return count; } - getJarFile(resource: string) { - let dirs = Thread.currentThread().getContextClassLoader().getResources(resource); + getJarFile(resource: string, loader?: any) { + let dirs = (loader || Thread.currentThread().getContextClassLoader()).getResources(resource); if (dirs.hasMoreElements()) { let url = dirs.nextElement(); if (url.protocol === "jar") { return url.openConnection().jarFile; } diff --git a/packages/bukkit/src/event.ts b/packages/bukkit/src/event.ts index 2ed585fb..8f9a8f31 100644 --- a/packages/bukkit/src/event.ts +++ b/packages/bukkit/src/event.ts @@ -19,7 +19,7 @@ export class BukkitEvent extends event.Event { } getJarFile(resource: string) { - return super.getJarFile('org/bukkit/Bukkit.class') + return super.getJarFile('org/bukkit/Bukkit.class', Bukkit.class.classLoader) } isValidEvent(clazz: any): boolean { // 继承于 org.bukkit.event.Event diff --git a/packages/bukkit/src/server.ts b/packages/bukkit/src/server.ts index abe502e4..5d077d44 100644 --- a/packages/bukkit/src/server.ts +++ b/packages/bukkit/src/server.ts @@ -1,16 +1,9 @@ -import { server } from '@ms/api' +import { server, constants } from '@ms/api' import { provideSingleton } from '@ms/container'; import * as reflect from '@ms/common/dist/reflect' import chat from './enhance/chat' -const refList: Array<{ server: string, future: string }> = [ - { server: 'aq', future: 'g' },//spigot 1.8.8 - { server: 'an', future: 'g' },//spigot 1.12.2 - { server: 'getServerConnection', future: 'f' },//after spigot 1.14.4 - { server: 'func_147137_ag', future: 'field_151274_e' }//catserver 1.12.2 -] - let Bukkit = org.bukkit.Bukkit; @provideSingleton(server.Server) @@ -68,14 +61,24 @@ export class BukkitServer implements server.Server { private reflectPipeline() { let consoleServer = reflect.on(Bukkit.getServer()).get('console').get() + let connection: any; let promise: any; - for (const ref of refList) { + for (const method of constants.Reflect.Method.getServerConnection) { try { - promise = reflect.on(consoleServer).call(ref.server).get(ref.future).get().get(0); break; - } catch (error) { - } + connection = reflect.on(consoleServer).call(method).get() + if (connection.class.name.indexOf('ServerConnection') !== -1) { break; } + connection = undefined; + } catch (error) { } } - if (!promise) { console.error("Can't found ServerConnection or ChannelFuture !"); return } + if (!connection) { console.error("Can't found ServerConnection!"); return } + for (const field of constants.Reflect.Field.listeningChannels) { + try { + promise = reflect.on(connection).get(field).get().get(0); + if (promise.class.name.indexOf('Promise') !== -1) { break; } + promise = undefined; + } catch (error) { } + } + if (!promise) { console.error("Can't found listeningChannels!"); return } this.pipeline = reflect.on(promise).get('channel').get().pipeline() } } diff --git a/packages/sponge/src/server.ts b/packages/sponge/src/server.ts index 923033f9..04f64c3c 100644 --- a/packages/sponge/src/server.ts +++ b/packages/sponge/src/server.ts @@ -1,14 +1,8 @@ -import { server } from '@ms/api' +import { server, constants } from '@ms/api' import { provideSingleton } from '@ms/container'; import * as reflect from '@ms/common/dist/reflect' -const refList: Array<{ server: string, future: string }> = [ - { server: 'an', future: 'g' },//spigot 1.12.2 - { server: 'getServerConnection', future: 'f' },//after spigot 1.14.4 - { server: 'func_147137_ag', future: 'field_151274_e' }//catserver 1.12.2 -] - const Sponge = org.spongepowered.api.Sponge; const TextSerializers = org.spongepowered.api.text.serializer.TextSerializers; const File = Java.type("java.io.File"); @@ -64,14 +58,24 @@ export class SpongeServer implements server.Server { } private reflectPipeline() { let consoleServer = reflect.on(Sponge.getServer()).get() + let connection: any; let promise: any; - for (const ref of refList) { + for (const method of constants.Reflect.Method.getServerConnection) { try { - promise = reflect.on(consoleServer).call(ref.server).get(ref.future).get().get(0); break; - } catch (error) { - } + connection = reflect.on(consoleServer).call(method).get() + if (connection.class.name.indexOf('ServerConnection') !== -1) { break; } + connection = undefined; + } catch (error) { } } - if (!promise) { throw Error(`Can't found ServerConnection or ChannelFuture !`) } + if (!connection) { console.error("Can't found ServerConnection!"); return } + for (const field of constants.Reflect.Field.listeningChannels) { + try { + promise = reflect.on(connection).get(field).get().get(0); + if (promise.class.name.indexOf('Promise') !== -1) { break; } + promise = undefined; + } catch (error) { } + } + if (!promise) { console.error("Can't found listeningChannels!"); return } this.pipeline = reflect.on(promise).get('channel').get().pipeline() } }