11package taboolib.module.kether
22
3+ import org.tabooproject.reflex.ClassMethod
34import org.tabooproject.reflex.Reflex.Companion.getProperty
45import org.tabooproject.reflex.Reflex.Companion.invokeMethod
6+ import org.tabooproject.reflex.ReflexClass
7+ import org.tabooproject.reflex.Unknown
58import taboolib.common.OpenContainer
69import taboolib.common.platform.function.pluginId
10+ import taboolib.common.util.WrappedContext
711import taboolib.common.util.orNull
12+ import taboolib.common.util.supplierLazy
813import taboolib.library.kether.*
914import java.util.*
1015import 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}
0 commit comments