diff --git a/demo-module/src/main/java/com/highcapable/yukihookapi/demo_module/hook/HookEntry.kt b/demo-module/src/main/java/com/highcapable/yukihookapi/demo_module/hook/HookEntry.kt index d199db15..2b45a921 100644 --- a/demo-module/src/main/java/com/highcapable/yukihookapi/demo_module/hook/HookEntry.kt +++ b/demo-module/src/main/java/com/highcapable/yukihookapi/demo_module/hook/HookEntry.kt @@ -102,7 +102,7 @@ class HookEntry : YukiHookXposedInitProxy { // 在方法执行之前拦截 beforeHook { // 设置 0 号 param - args().set("I am hook method param") + args().first().set("I am hook method param") } } // 注入要 Hook 的方法 @@ -115,7 +115,7 @@ class HookEntry : YukiHookXposedInitProxy { // 在方法执行之前拦截 beforeHook { // 设置 0 号 param - args().array()[0] = "peach" + args().first().array()[0] = "peach" } } // 注入要 Hook 的方法 @@ -151,7 +151,7 @@ class HookEntry : YukiHookXposedInitProxy { // 在方法执行之前拦截 beforeHook { // 设置 0 号 param - args().set("I am hook constructor param") + args().first().set("I am hook constructor param") } } // 注入要 Hook 的方法 diff --git a/docs/api/public/HookParam.md b/docs/api/public/HookParam.md index 18f38dd2..1655cce1 100644 --- a/docs/api/public/HookParam.md +++ b/docs/api/public/HookParam.md @@ -26,33 +26,25 @@ val args: Array > 获取当前 Hook 对象 `member` 或 `constructor` 的参数对象数组。 -### firstArgs [field] - -```kotlin -val firstArgs: Any? -``` +### ~~firstArgs [field]~~ 变更记录 `v1.0` `添加` -功能描述 +`v1.0.75` `移除` -> 获取当前 Hook 对象 `member` 或 `constructor` 的参数对象数组第一位。 +请使用 `args(index = 0)` 或 `args().first()` -### lastArgs [field] - -```kotlin -val lastArgs: Any? -``` +### ~~lastArgs [field]~~ 变更记录 `v1.0` `添加` -功能描述 +`v1.0.75` `移除` -> 获取当前 Hook 对象 `member` 或 `constructor` 的参数对象数组最后一位。 +请使用 `args().last()` ### instance [field] @@ -126,33 +118,35 @@ var result: Any? > 获取、设置当前 Hook 对象的 `method` 或 `constructor` 的返回值。 -### firstArgs [method] +### result [method] ```kotlin -inline fun firstArgs(): T? +inline fun result(): T? ``` 变更记录 +`v1.0.75` `新增` + +功能描述 + +> 获取当前 Hook 对象的 `method` 或 `constructor` 的返回值 `T`。 + +### ~~firstArgs [method]~~ + +变更记录 + `v1.0.66` `新增` -功能描述 +`v1.0.75` `移除` -> 获取当前 Hook 对象 [method] or [constructor] 的参数对象数组第一位 `T`。 - -### lastArgs [method] - -```kotlin -inline fun lastArgs(): T? -``` +### ~~lastArgs [method]~~ 变更记录 `v1.0.66` `新增` -功能描述 - -> 获取当前 Hook 对象 [method] or [constructor] 的参数对象数组最后一位 `T`。 +`v1.0.75` `移除` ### instance [method] @@ -180,6 +174,20 @@ instance().finish() ### args [method] +```kotlin +fun args(): ArgsIndexCondition +``` + +变更记录 + +`v1.0.75` `新增` + +功能描述 + +> 获取当前 Hook 对象的 `method` or `constructor` 的参数数组下标实例化类。 + +### args [method] + ```kotlin fun args(index: Int): ArgsModifyer ``` @@ -188,6 +196,10 @@ fun args(index: Int): ArgsModifyer `v1.0` `添加` +`v1.0.75` `修改` + +默认值 `index = 0` 移动到新的使用方法 `args().first()` + 功能描述 > 获取当前 Hook 对象的 `method` 或 `constructor` 的参数实例化对象类。 @@ -196,8 +208,6 @@ fun args(index: Int): ArgsModifyer 你可以通过 `args` 方法修改当前 Hook 实例的方法、构造方法的参数内容。 -`index` 的默认值为 `0`,如果只有一位 `param` 参数,你可以不写。 - 你可以直接使用 `set` 方法设置 `param` 为你的目标实例,接受 `Any` 类型。 !> 请确保 `param` 类型为你的目标实例类型。 @@ -205,7 +215,23 @@ fun args(index: Int): ArgsModifyer > 示例如下 ```kotlin -args().set("modify the value") +args(index = 0).set("modify the value") +``` + +你可以这样直接设置第一位 `param` 的值。 + +> 示例如下 + +```kotlin +args().first().set("modify the value") +``` + +你还可以直接设置最后一位 `param` 的值。 + +> 示例如下 + +```kotlin +args().last().set("modify the value") ``` 你还可以使用 `setNull` 方法设置 `param` 为空。 @@ -223,7 +249,7 @@ args(index = 1).setNull() > 示例如下 ```kotlin -args(index = 2).setTrue() +args(index = 1).setTrue() ``` 你还可以使用 `setFalse` 方法设置 `param` 为 `false`。 @@ -233,7 +259,7 @@ args(index = 2).setTrue() > 示例如下 ```kotlin -args(index = 3).setFalse() +args(index = 1).setFalse() ``` ### invokeOriginal [method] @@ -306,6 +332,48 @@ fun resultNull() !> 此方法将强制设置 Hook 对象方法的 `result` 为 `null`。 +### ArgsIndexCondition [class] + +```kotlin +inner class ArgsIndexCondition +``` + +变更记录 + +`v1.0.75` `新增` + +功能描述 + +> 对方法参数的数组下标进行实例化类。 + +#### first [method] + +```kotlin +fun first(): ArgsModifyer +``` + +变更记录 + +`v1.0.75` `新增` + +功能描述 + +> 获取当前 Hook 对象的 `method` or `constructor` 的参数数组第一位。 + +#### last [method] + +```kotlin +fun last(): ArgsModifyer +``` + +变更记录 + +`v1.0.75` `新增` + +功能描述 + +> 获取当前 Hook 对象的 `method` or `constructor` 的参数数组最后一位。 + ### ArgsModifyer [class] ```kotlin diff --git a/docs/config/api-exception.md b/docs/config/api-exception.md index c5ceacc6..298aeb6e 100644 --- a/docs/config/api-exception.md +++ b/docs/config/api-exception.md @@ -438,11 +438,11 @@ class MainActivity : AppCompatActivity() { `YukiHookModulePrefs` 支持的类型只有 `String`、`Int`、`Float`、`Long`、`Boolean`,请传入支持的类型。 -!> `IllegalStateException` HookParam args is empty +!> `IllegalStateException` HookParam Method args index must be >= 0 异常原因 -在 `HookParam` 中调用 `firstArgs` 或 `lastArgs` 但原始 `param` 数组为空。 +在 `HookParam` 中调用 `args().last()` 但是目标 `param` 为空或 `args` 中的 `index` 设置了小于 0 的数值。 > 示例如下 @@ -450,17 +450,17 @@ class MainActivity : AppCompatActivity() { injectMember { // ... afterHook { - // 调用了此变量 - firstArgs... - // 调用了此变量 - lastArgs... + // 假设 param 是空的 + args().last()... + // 设置了小于 0 的 index + args(index = -5)... } } ``` 解决方案 -请确认你 Hook 的目标方法、构造方法的方法参数数量是否不为空,否则你无法使用此功能。 +请确认你 Hook 的目标方法、构造方法的方法参数数量是否不为空,且不能对 `args` 的下标设置小于 0 的数值。 !> `IllegalStateException` HookParam instance got null! Is this a static member? diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/HookParam.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/HookParam.kt index 2a02bade..31aaa1e4 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/HookParam.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/HookParam.kt @@ -48,20 +48,6 @@ class HookParam(private val createrInstance: YukiHookCreater, private val wrappe */ val args get() = wrapper.args ?: arrayOf(0) - /** - * 获取当前 Hook 对象 [method] or [constructor] 的参数对象数组第一位 - * @return [Any] or null - * @throws IllegalStateException 如果数组为空 - */ - val firstArgs get() = if (args.isNotEmpty()) args[0] else error("HookParam args is empty") - - /** - * 获取当前 Hook 对象 [method] or [constructor] 的参数对象数组最后一位 - * @return [Any] or null - * @throws IllegalStateException 如果数组为空 - */ - val lastArgs get() = if (args.isNotEmpty()) args[args.lastIndex] else error("HookParam args is empty") - /** * 获取当前 Hook 实例的对象 * @@ -102,18 +88,10 @@ class HookParam(private val createrInstance: YukiHookCreater, private val wrappe } /** - * 获取当前 Hook 对象 [method] or [constructor] 的参数对象数组第一位 [T] + * 获取当前 Hook 对象的 [method] or [constructor] 的返回值 [T] * @return [T] or null - * @throws IllegalStateException 如果数组为空 */ - inline fun firstArgs() = firstArgs as? T? - - /** - * 获取当前 Hook 对象 [method] or [constructor] 的参数对象数组最后一位 [T] - * @return [T] or null - * @throws IllegalStateException 如果数组为空 - */ - inline fun lastArgs() = lastArgs as? T? + inline fun result() = result as? T? /** * 获取当前 Hook 实例的对象 [T] @@ -122,12 +100,18 @@ class HookParam(private val createrInstance: YukiHookCreater, private val wrappe */ inline fun instance() = instance as? T? ?: error("HookParam instance cannot cast to ${T::class.java.name}") + /** + * 获取当前 Hook 对象的 [method] or [constructor] 的参数数组下标实例化类 + * @return [ArgsIndexCondition] + */ + fun args() = ArgsIndexCondition() + /** * 获取当前 Hook 对象的 [method] or [constructor] 的参数实例化对象类 - * @param index 参数对象数组下标 - 默认是 0 + * @param index 参数对象数组下标 * @return [ArgsModifyer] */ - fun args(index: Int = 0) = ArgsModifyer(index) + fun args(index: Int) = ArgsModifyer(index) /** * 执行原始 [Member] @@ -165,8 +149,30 @@ class HookParam(private val createrInstance: YukiHookCreater, private val wrappe result = null } + /** + * 对方法参数的数组下标进行实例化类 + * + * - ❗请使用第一个 [args] 方法来获取 [ArgsIndexCondition] + */ + inner class ArgsIndexCondition { + + /** + * 获取当前 Hook 对象的 [method] or [constructor] 的参数数组第一位 + * @return [ArgsModifyer] + */ + fun first() = args(index = 0) + + /** + * 获取当前 Hook 对象的 [method] or [constructor] 的参数数组最后一位 + * @return [ArgsModifyer] + */ + fun last() = args(index = args.lastIndex) + } + /** * 对方法参数的修改进行实例化类 + * + * - ❗请使用第二个 [args] 方法来获取 [ArgsModifyer] * @param index 参数对象数组下标 */ inner class ArgsModifyer(private val index: Int) { @@ -271,6 +277,7 @@ class HookParam(private val createrInstance: YukiHookCreater, private val wrappe * @throws IllegalStateException 如果目标方法参数对象数组为空或 [index] 下标不存在 */ fun set(any: T?) { + if (index < 0) error("HookParam Method args index must be >= 0") if (args.isEmpty()) error("HookParam Method args is empty, mabe not has args") if (index > args.lastIndex) error("HookParam Method args index out of bounds, max is ${args.lastIndex}") wrapper.setArgs(index, any)