Skip to content

Commit abd325e

Browse files
committed
feat(kether): 优化跨插件 kether 执行效率
1 parent 86eb3d2 commit abd325e

File tree

6 files changed

+160
-30
lines changed

6 files changed

+160
-30
lines changed

module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/KetherHelper.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ fun Throwable.printKetherErrorMessage(detailError: Boolean = false) {
117117
""".t()
118118
)
119119
}
120+
warning("$javaClass:")
120121
localizedMessage.split('\n').forEach { warning(it) }
121122
}
122123

module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteQuest.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package taboolib.module.kether
22

3+
import org.tabooproject.reflex.ClassMethod
34
import org.tabooproject.reflex.Reflex.Companion.getProperty
45
import org.tabooproject.reflex.Reflex.Companion.invokeMethod
6+
import org.tabooproject.reflex.ReflexClass
7+
import org.tabooproject.reflex.Unknown
58
import taboolib.common.OpenContainer
69
import taboolib.common.platform.function.pluginId
10+
import taboolib.common.util.supplierLazy
711
import taboolib.library.kether.ParsedAction
812
import taboolib.library.kether.Quest
913
import java.util.*
@@ -15,14 +19,15 @@ import java.util.*
1519
* @author sky
1620
* @since 2021/8/11 11:33 上午
1721
*/
22+
@Suppress("UNCHECKED_CAST")
1823
class RemoteQuest(val remote: OpenContainer, val source: Any) : Quest {
1924

2025
override fun getId(): String {
2126
return source.invokeMethod("getId", remap = false)!!
2227
}
2328

2429
override fun getBlock(label: String): Optional<Quest.Block> {
25-
val getBlock = source.invokeMethod<Optional<Any>>("getBlock", label, remap = false)!!
30+
val getBlock = getBlockMethod[source].invoke(source, label) as Optional<Any>
2631
return if (getBlock.isPresent) {
2732
Optional.of(RemoteBlock(remote, getBlock.get()))
2833
} else {
@@ -38,7 +43,7 @@ class RemoteQuest(val remote: OpenContainer, val source: Any) : Quest {
3843
// 远程创建 ParsedAction
3944
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, action.action, action.properties))
4045
// 远程执行方法
41-
val blockOf = source.invokeMethod<Optional<Any>>("blockOf", remoteAction.value, remap = false)!!
46+
val blockOf = blockOfMethod[source].invoke(source, remoteAction.value) as Optional<Any>
4247
return if (blockOf.isPresent) {
4348
Optional.of(RemoteBlock(remote, blockOf.get()))
4449
} else {
@@ -62,11 +67,11 @@ class RemoteQuest(val remote: OpenContainer, val source: Any) : Quest {
6267
// 远程创建 ParsedAction
6368
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, action.action, action.properties))
6469
// 远程执行方法
65-
return source.invokeMethod("indexOf", remoteAction.value, remap = false)!!
70+
return indexOfMethod[source].invoke(source, remoteAction.value) as Int
6671
}
6772

6873
override fun get(i: Int): Optional<ParsedAction<*>> {
69-
val action = source.invokeMethod<Optional<Any>>("get", i, remap = false)!!
74+
val action = getMethod[source].invoke(source, i) as Optional<Any>
7075
return if (action.isPresent) {
7176
val remoteAction = RemoteQuestAction<Any>(remote, action.get().getProperty<Any>("action", remap = false)!!)
7277
Optional.of(ParsedAction(remoteAction, action.get().getProperty<Map<String, Any>>("properties", remap = false)!!))
@@ -75,4 +80,23 @@ class RemoteQuest(val remote: OpenContainer, val source: Any) : Quest {
7580
}
7681
}
7782
}
83+
84+
companion object {
85+
86+
val getBlockMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
87+
ReflexClass.of(it.javaClass).getMethodByTypes("getBlock", remap = false, parameter = arrayOf(String::class.java))
88+
}
89+
90+
val blockOfMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
91+
ReflexClass.of(it.javaClass).getMethodByTypes("blockOf", remap = false, parameter = arrayOf(Unknown::class.java))
92+
}
93+
94+
val indexOfMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
95+
ReflexClass.of(it.javaClass).getMethodByTypes("indexOf", remap = false, parameter = arrayOf(Unknown::class.java))
96+
}
97+
98+
val getMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
99+
ReflexClass.of(it.javaClass).getMethodByTypes("get", remap = false, parameter = arrayOf(Int::class.java))
100+
}
101+
}
78102
}

module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteQuestAction.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package taboolib.module.kether
22

3-
import org.tabooproject.reflex.Reflex.Companion.invokeMethod
3+
import org.tabooproject.reflex.ClassMethod
4+
import org.tabooproject.reflex.ReflexClass
5+
import org.tabooproject.reflex.Unknown
46
import taboolib.common.OpenContainer
57
import taboolib.common.platform.function.pluginId
8+
import taboolib.common.util.supplierLazy
69
import taboolib.library.kether.QuestAction
710
import taboolib.library.kether.QuestContext
811
import java.util.concurrent.CompletableFuture
@@ -14,9 +17,18 @@ import java.util.concurrent.CompletableFuture
1417
* @author sky
1518
* @since 2021/8/10 3:51 下午
1619
*/
20+
@Suppress("UNCHECKED_CAST")
1721
class RemoteQuestAction<T>(val remote: OpenContainer, val source: Any) : QuestAction<T>() {
1822

1923
override fun process(frame: QuestContext.Frame): CompletableFuture<T> {
20-
return source.invokeMethod("process", remote.call(StandardChannel.REMOTE_CREATE_FLAME, arrayOf(pluginId, frame)).value, remap = false)!!
24+
val remoteFrame = remote.call(StandardChannel.REMOTE_CREATE_FLAME, arrayOf(pluginId, frame))
25+
return processMethod[source].invoke(source, remoteFrame.value) as CompletableFuture<T>
26+
}
27+
28+
companion object {
29+
30+
val processMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
31+
ReflexClass.of(it.javaClass).getMethodByTypes("process", remap = false, parameter = arrayOf(Unknown::class.java))
32+
}
2133
}
2234
}

module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteQuestContext.kt

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package taboolib.module.kether
22

3+
import org.tabooproject.reflex.ClassMethod
34
import org.tabooproject.reflex.Reflex.Companion.getProperty
45
import org.tabooproject.reflex.Reflex.Companion.invokeMethod
6+
import org.tabooproject.reflex.ReflexClass
7+
import org.tabooproject.reflex.Unknown
58
import taboolib.common.OpenContainer
69
import taboolib.common.platform.function.pluginId
10+
import taboolib.common.util.WrappedContext
711
import taboolib.common.util.orNull
12+
import taboolib.common.util.supplierLazy
813
import taboolib.library.kether.*
914
import java.util.*
1015
import java.util.concurrent.CompletableFuture
@@ -16,15 +21,16 @@ import java.util.concurrent.CompletableFuture
1621
* @author sky
1722
* @since 2021/8/11 12:04 上午
1823
*/
19-
class RemoteQuestContext(val remote: OpenContainer, val source: Any) : ScriptContext(ScriptService, RemoteQuest(remote, source.invokeMethod("getQuest")!!)) {
24+
@Suppress("UNCHECKED_CAST")
25+
class RemoteQuestContext(val remote: OpenContainer, val source: Any) : ScriptContext(ScriptService, RemoteQuest(remote, source.invokeMethod("getQuest", remap = false)!!)) {
2026

2127
override fun getService(): QuestService<ScriptContext> {
2228
error("remote context")
2329
}
2430

2531
override fun setExitStatus(exitStatus: ExitStatus) {
2632
val status = remote.call(StandardChannel.REMOTE_CREATE_EXIT_STATUS, arrayOf(exitStatus.isRunning, exitStatus.isWaiting, exitStatus.startTime))
27-
source.invokeMethod<Void>("setExitStatus", status, remap = false)
33+
setExitStatusMethod[source].invoke(source, status)
2834
}
2935

3036
override fun getExitStatus(): Optional<ExitStatus> {
@@ -89,29 +95,29 @@ class RemoteQuestContext(val remote: OpenContainer, val source: Any) : ScriptCon
8995
}
9096

9197
override fun setNext(action: ParsedAction<*>) {
92-
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, action.action, action.properties))
93-
source.invokeMethod<Void>("setNext", remoteAction.value, remap = false)
98+
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, action.action, action.properties)).value!!
99+
setNextActionMethod[WrappedContext(source, remoteAction)].invoke(source, remoteAction)
94100
}
95101

96102
override fun setNext(block: Quest.Block) {
97-
context().quest.getBlock(block.label).ifPresent { source.invokeMethod<Void>("setNext", it, remap = false) }
103+
context().quest.getBlock(block.label).ifPresent { setNextBlockMethod[WrappedContext(source, it)].invoke(source, it) }
98104
}
99105

100106
override fun newFrame(name: String): QuestContext.Frame {
101-
return RemoteFrame(remote, source.invokeMethod<Any>("newFrame", name, remap = false)!!)
107+
return RemoteFrame(remote, newFrameStringMethod[WrappedContext(source, name)].invoke(source, name)!!)
102108
}
103109

104110
override fun newFrame(action: ParsedAction<*>): QuestContext.Frame {
105-
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, action.action, action.properties))
106-
return RemoteFrame(remote, source.invokeMethod("newFrame", remoteAction.value, remap = false)!!)
111+
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, action.action, action.properties)).value!!
112+
return RemoteFrame(remote, newFrameActionMethod[WrappedContext(source, remoteAction)].invoke(source, remoteAction)!!)
107113
}
108114

109115
override fun variables(): QuestContext.VarTable {
110116
return RemoteVarTable(remote, source.invokeMethod<Any>("variables", remap = false)!!)
111117
}
112118

113119
override fun <T : AutoCloseable?> addClosable(closeable: T): T {
114-
return source.invokeMethod("addClosable", closeable, remap = false)!!
120+
return addClosableMethod[source].invoke(source, closeable) as T
115121
}
116122

117123
override fun <T : Any?> run(): CompletableFuture<T> {
@@ -126,24 +132,24 @@ class RemoteQuestContext(val remote: OpenContainer, val source: Any) : ScriptCon
126132
class RemoteVarTable(val remote: OpenContainer, val source: Any) : QuestContext.VarTable {
127133

128134
override fun <T> get(name: String): Optional<T>? {
129-
return source.invokeMethod("get", name, remap = false)
135+
return getMethod[source].invoke(source, name) as? Optional<T>
130136
}
131137

132138
override fun <T> getFuture(name: String): Optional<QuestFuture<T>>? {
133-
return source.invokeMethod("getFuture", name, remap = false)
139+
return getFutureMethod[source].invoke(source, name) as? Optional<QuestFuture<T>>
134140
}
135141

136142
override fun set(name: String, value: Any?) {
137-
source.invokeMethod<Void>("set", name, value, remap = false)
143+
setValueMethod[source].invoke(source, name, value)
138144
}
139145

140146
override fun <T> set(name: String, owner: ParsedAction<T>, future: CompletableFuture<T>) {
141-
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, owner.action, owner.properties))
142-
source.invokeMethod<Void>("set", name, remoteAction.value, future, remap = false)
147+
val remoteAction = remote.call(StandardChannel.REMOTE_CREATE_PARSED_ACTION, arrayOf(pluginId, owner.action, owner.properties)).value
148+
setFutureMethod[source].invoke(source, name, remoteAction, future)
143149
}
144150

145151
override fun remove(name: String) {
146-
source.invokeMethod<Void>("remote", name, remap = false)
152+
removeMethod[source].invoke(source, name)
147153
}
148154

149155
override fun clear() {
@@ -159,7 +165,7 @@ class RemoteQuestContext(val remote: OpenContainer, val source: Any) : ScriptCon
159165
}
160166

161167
override fun initialize(frame: QuestContext.Frame) {
162-
source.invokeMethod<Void>("initialize", remote.call(StandardChannel.REMOTE_CREATE_FLAME, arrayOf(pluginId, frame)).value, remap = false)
168+
initializeMethod[source].invoke(source, remote.call(StandardChannel.REMOTE_CREATE_FLAME, arrayOf(pluginId, frame)).value)
163169
}
164170

165171
override fun close() {
@@ -170,4 +176,58 @@ class RemoteQuestContext(val remote: OpenContainer, val source: Any) : ScriptCon
170176
return RemoteVarTable(remote, source.invokeMethod("parent", remap = false)!!)
171177
}
172178
}
179+
180+
companion object {
181+
182+
// RemoteQuestContext methods
183+
val setExitStatusMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
184+
ReflexClass.of(it.javaClass).getMethodByTypes("setExitStatus", remap = false, parameter = arrayOf(Unknown::class.java))
185+
}
186+
187+
// RemoteFrame methods
188+
val setNextActionMethod = supplierLazy<WrappedContext<Any, Any>, ClassMethod>(typeIsolation = true) { (source, action) ->
189+
ReflexClass.of(source.javaClass).getMethod("setNext", remap = false, parameter = arrayOf(action))
190+
}
191+
192+
val setNextBlockMethod = supplierLazy<WrappedContext<Any, Any>, ClassMethod>(typeIsolation = true) { (source, block) ->
193+
ReflexClass.of(source.javaClass).getMethod("setNext", remap = false, parameter = arrayOf(block))
194+
}
195+
196+
val newFrameStringMethod = supplierLazy<WrappedContext<Any, String>, ClassMethod>(typeIsolation = true) { (source, string) ->
197+
ReflexClass.of(source.javaClass).getMethod("newFrame", remap = false, parameter = arrayOf(string))
198+
}
199+
200+
val newFrameActionMethod = supplierLazy<WrappedContext<Any, Any>, ClassMethod>(typeIsolation = true) { (source, action) ->
201+
ReflexClass.of(source.javaClass).getMethod("newFrame", remap = false, parameter = arrayOf(action))
202+
}
203+
204+
val addClosableMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
205+
ReflexClass.of(it.javaClass).getMethodByTypes("addClosable", remap = false, parameter = arrayOf(AutoCloseable::class.java))
206+
}
207+
208+
// RemoteVarTable methods
209+
val getMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
210+
ReflexClass.of(it.javaClass).getMethodByTypes("get", remap = false, parameter = arrayOf(String::class.java))
211+
}
212+
213+
val getFutureMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
214+
ReflexClass.of(it.javaClass).getMethodByTypes("getFuture", remap = false, parameter = arrayOf(String::class.java))
215+
}
216+
217+
val setValueMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
218+
ReflexClass.of(it.javaClass).getMethodByTypes("set", remap = false, parameter = arrayOf(String::class.java, Any::class.java))
219+
}
220+
221+
val setFutureMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
222+
ReflexClass.of(it.javaClass).getMethodByTypes("set", remap = false, parameter = arrayOf(String::class.java, Any::class.java, CompletableFuture::class.java))
223+
}
224+
225+
val removeMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
226+
ReflexClass.of(it.javaClass).getMethodByTypes("remove", remap = false, parameter = arrayOf(String::class.java))
227+
}
228+
229+
val initializeMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
230+
ReflexClass.of(it.javaClass).getMethodByTypes("initialize", remap = false, parameter = arrayOf(Unknown::class.java))
231+
}
232+
}
173233
}

module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteQuestReader.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package taboolib.module.kether
22

3+
import org.tabooproject.reflex.ClassMethod
34
import org.tabooproject.reflex.Reflex.Companion.getProperty
45
import org.tabooproject.reflex.Reflex.Companion.invokeMethod
6+
import org.tabooproject.reflex.ReflexClass
57
import taboolib.common.OpenContainer
8+
import taboolib.common.util.supplierLazy
69
import taboolib.library.kether.ParsedAction
710
import taboolib.library.kether.QuestReader
811

12+
@Suppress("UNCHECKED_CAST")
913
class RemoteQuestReader(val remote: OpenContainer, val source: Any) : QuestReader {
1014

1115
override fun peek(): Char {
1216
return source.invokeMethod("peek", remap = false)!!
1317
}
1418

1519
override fun peek(n: Int): Char {
16-
return source.invokeMethod("peek", n, remap = false)!!
20+
return peekIntMethod[source].invoke(source, n) as Char
1721
}
1822

1923
override fun getIndex(): Int {
@@ -43,20 +47,35 @@ class RemoteQuestReader(val remote: OpenContainer, val source: Any) : QuestReade
4347
override fun <T> nextAction(): ParsedAction<T> {
4448
val action = source.invokeMethod<T>("nextAction", remap = false)!!
4549
val questAction = RemoteQuestAction<T>(remote, action.getProperty<Any>("action", remap = false)!!)
46-
return ParsedAction(questAction, action.getProperty<Map<String, Any>>("properties")!!)
50+
return ParsedAction(questAction, action.getProperty<Map<String, Any>>("properties", remap = false)!!)
4751
}
4852

4953
override fun <T : Any?> nextAction(namespace: String?): ParsedAction<T> {
5054
return try {
51-
val action = source.invokeMethod<T>("nextAction", namespace, remap = false)!!
55+
val action = nextActionStringMethod[source].invoke(source, namespace)!!
5256
val questAction = RemoteQuestAction<T>(remote, action.getProperty<Any>("action", remap = false)!!)
53-
ParsedAction(questAction, action.getProperty<Map<String, Any>>("properties")!!)
57+
ParsedAction(questAction, action.getProperty<Map<String, Any>>("properties", remap = false)!!)
5458
} catch (_: NoSuchMethodException) {
5559
nextAction()
5660
}
5761
}
5862

5963
override fun expect(value: String) {
60-
source.invokeMethod<Void>("expect", value, remap = false)
64+
expectMethod[source].invoke(source, value)
65+
}
66+
67+
companion object {
68+
69+
val peekIntMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
70+
ReflexClass.of(it.javaClass).getMethodByTypes("peek", remap = false, parameter = arrayOf(Int::class.java))
71+
}
72+
73+
val nextActionStringMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
74+
ReflexClass.of(it.javaClass).getMethodByTypes("nextAction", remap = false, parameter = arrayOf(String::class.java))
75+
}
76+
77+
val expectMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
78+
ReflexClass.of(it.javaClass).getMethodByTypes("expect", remap = false, parameter = arrayOf(String::class.java))
79+
}
6180
}
6281
}
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package taboolib.module.kether
22

3-
import org.tabooproject.reflex.Reflex.Companion.invokeMethod
3+
import org.tabooproject.reflex.ClassMethod
4+
import org.tabooproject.reflex.ReflexClass
45
import taboolib.common.OpenContainer
56
import taboolib.common.OpenResult
7+
import taboolib.common.util.supplierLazy
68

79
/**
810
* TabooLib
@@ -11,13 +13,25 @@ import taboolib.common.OpenResult
1113
* @author sky
1214
* @since 2021/8/12 8:37 下午
1315
*/
16+
@Suppress("UNCHECKED_CAST")
1417
class RemoteScriptProperty(val remote: OpenContainer, val source: Any, id: String) : ScriptProperty<Any>(id) {
1518

1619
override fun read(instance: Any, key: String): OpenResult {
17-
return OpenResult.cast(source.invokeMethod("read", instance, key, remap = false))
20+
return OpenResult.cast(readMethod[source].invoke(source, instance, key))
1821
}
1922

2023
override fun write(instance: Any, key: String, value: Any?): OpenResult {
21-
return OpenResult.cast(source.invokeMethod("write", instance, key, value, remap = false))
24+
return OpenResult.cast(writeMethod[source].invoke(source, instance, key, value))
25+
}
26+
27+
companion object {
28+
29+
val readMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
30+
ReflexClass.of(it.javaClass).getMethodByTypes("read", remap = false, parameter = arrayOf(Any::class.java, String::class.java))
31+
}
32+
33+
val writeMethod = supplierLazy<Any, ClassMethod>(typeIsolation = true) {
34+
ReflexClass.of(it.javaClass).getMethodByTypes("write", remap = false, parameter = arrayOf(Any::class.java, String::class.java, Any::class.java))
35+
}
2236
}
2337
}

0 commit comments

Comments
 (0)