forked from Karlatemp/JvmHookFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,73 @@ | ||
package me.earzuchan.sakiko.api.reflecting | ||
|
||
import me.earzuchan.sakiko.api.HookConfig | ||
import me.earzuchan.sakiko.api.UnhookConfig | ||
import me.earzuchan.sakiko.api.hook | ||
import java.lang.reflect.Method | ||
|
||
fun Class<*>.method(configure: MethodConfig.() -> Unit): Method { | ||
val config = MethodConfig().apply(configure) | ||
return this.getDeclaredMethod(config.name, *config.params.toTypedArray()) | ||
fun Class<*>.method(configure: MethodMatchConfig.() -> Unit): MethodMatches { | ||
val config = MethodMatchConfig().apply(configure) | ||
val methods = this.declaredMethods.filter { it.name == config.name } | ||
|
||
return if (config.paramsHasSet) { | ||
// 方法类型匹配 | ||
MethodMatches(methods.filter { it.parameterTypes.contentEquals(config.params.toTypedArray()) }) | ||
} else { | ||
MethodMatches(methods) | ||
} | ||
} | ||
|
||
// 方法匹配器 | ||
class MethodMatches(private val methods: List<Method>) { | ||
// 选择仅一个方法 | ||
fun hook(configure: HookConfig.() -> Unit): UnhookConfig { | ||
if (methods.size == 1) { | ||
return methods[0].hook(configure) | ||
} else { | ||
throw IllegalStateException("Multiple methods found. Use all() to select specific methods.") | ||
} | ||
} | ||
|
||
// 选择所有方法 | ||
fun all(): List<Method> { | ||
return methods | ||
} | ||
} | ||
|
||
class MethodConfig { | ||
// 批量 hook | ||
fun List<Method>.hook(configure: HookConfig.() -> Unit): List<UnhookConfig> = map { it.hook(configure) } | ||
|
||
// 方法匹配配置 | ||
class MethodMatchConfig { | ||
lateinit var name: String | ||
val params = mutableListOf<Class<*>>() | ||
var paramsHasSet = false | ||
|
||
fun param(vararg givenParams: Class<*>) { | ||
if (paramsHasSet) { | ||
throw IllegalStateException("Params has been set") | ||
} | ||
params.addAll(givenParams) | ||
paramsHasSet = true | ||
} | ||
|
||
fun param(param: Class<*>) { | ||
params.add(param) | ||
fun emptyParam() { | ||
param() | ||
} | ||
} | ||
|
||
// 简便化的类名匹配 | ||
fun String.toClass(): Class<*> { | ||
return Class.forName(this) | ||
} | ||
} | ||
|
||
val StringClass = String::class.java | ||
val IntType = Int::class.javaPrimitiveType | ||
val LongType = Long::class.javaPrimitiveType | ||
val FloatType = Float::class.javaPrimitiveType | ||
val DoubleType = Double::class.javaPrimitiveType | ||
val BooleanType = Boolean::class.javaPrimitiveType | ||
val ByteType = Byte::class.javaPrimitiveType | ||
val ShortType = Short::class.javaPrimitiveType | ||
val CharType = Char::class.javaPrimitiveType | ||
val UnitType = Unit::class.javaPrimitiveType |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.