mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-04 01:35:17 +08:00
refactor: optimize some code
This commit is contained in:
@@ -266,7 +266,7 @@ object YukiHookAPI {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否开启调试模式 - 默认启用
|
||||
* 是否启用调试模式 - 默认启用
|
||||
*
|
||||
* 启用后将交由日志输出管理器打印详细 Hook 日志到控制台
|
||||
*
|
||||
@@ -357,6 +357,9 @@ object YukiHookAPI {
|
||||
*/
|
||||
@Deprecated(message = "此方法及功能已被移除,请删除此方法")
|
||||
var isEnableMemberCache = false
|
||||
|
||||
/** 结束方法体 */
|
||||
internal fun build() = Unit
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,9 +370,7 @@ object YukiHookAPI {
|
||||
* For English version, see [configs Method](https://fankes.github.io/YukiHookAPI/en/config/api-example#configs-method)
|
||||
* @param initiate 方法体
|
||||
*/
|
||||
inline fun configs(initiate: Configs.() -> Unit) {
|
||||
Configs.apply(initiate)
|
||||
}
|
||||
inline fun configs(initiate: Configs.() -> Unit) = Configs.apply(initiate).build()
|
||||
|
||||
/**
|
||||
* 作为 Xposed 模块装载调用入口方法
|
||||
@@ -477,7 +478,7 @@ object YukiHookAPI {
|
||||
internal fun printSplashInfo() {
|
||||
if (Configs.isDebug.not() || isShowSplashLogOnceTime.not()) return
|
||||
isShowSplashLogOnceTime = false
|
||||
YLog.innerI("Welcome to YukiHookAPI $VERSION! Using ${Status.Executor.name} API ${Status.Executor.apiLevel}", isImplicit = true)
|
||||
YLog.innerD("Welcome to YukiHookAPI $VERSION! Using ${Status.Executor.name} API ${Status.Executor.apiLevel}", isImplicit = true)
|
||||
}
|
||||
|
||||
/** 输出找不到 Hook API 的错误日志 */
|
||||
|
@@ -598,7 +598,7 @@ class YukiMemberHookCreator internal constructor(private val packageParam: Packa
|
||||
runCatching {
|
||||
replaceHookCallback?.invoke(assign).also {
|
||||
checkingReturnType((param.member as? Method?)?.returnType, it?.javaClass)
|
||||
if (replaceHookCallback != null) hookDebugMsg(msg = "Replace Hook Member [${this@hook}] done [$tag]")
|
||||
if (replaceHookCallback != null) YLog.innerD("Replace Hook Member [${this@hook}] done [$tag]")
|
||||
HookParam.invoke()
|
||||
}
|
||||
}.getOrElse {
|
||||
@@ -624,7 +624,7 @@ class YukiMemberHookCreator internal constructor(private val packageParam: Packa
|
||||
runCatching {
|
||||
beforeHookCallback?.invoke(assign)
|
||||
checkingReturnType((param.member as? Method?)?.returnType, param.result?.javaClass)
|
||||
if (beforeHookCallback != null) hookDebugMsg(msg = "Before Hook Member [${this@hook}] done [$tag]")
|
||||
if (beforeHookCallback != null) YLog.innerD("Before Hook Member [${this@hook}] done [$tag]")
|
||||
HookParam.invoke()
|
||||
}.onFailure {
|
||||
onConductFailureCallback?.invoke(assign, it)
|
||||
@@ -639,7 +639,7 @@ class YukiMemberHookCreator internal constructor(private val packageParam: Packa
|
||||
afterHookParam.assign(afterHookId, param).also { assign ->
|
||||
runCatching {
|
||||
afterHookCallback?.invoke(assign)
|
||||
if (afterHookCallback != null) hookDebugMsg(msg = "After Hook Member [${this@hook}] done [$tag]")
|
||||
if (afterHookCallback != null) YLog.innerD("After Hook Member [${this@hook}] done [$tag]")
|
||||
HookParam.invoke()
|
||||
}.onFailure {
|
||||
onConductFailureCallback?.invoke(assign, it)
|
||||
@@ -669,21 +669,13 @@ class YukiMemberHookCreator internal constructor(private val packageParam: Packa
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook 过程中开启了 [YukiHookAPI.Configs.isDebug] 输出调试信息
|
||||
* @param msg 调试日志内容
|
||||
*/
|
||||
private fun hookDebugMsg(msg: String) {
|
||||
if (YukiHookAPI.Configs.isDebug) YLog.innerD(msg)
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook 失败但未设置 [onAllFailureCallback] 将默认输出失败信息
|
||||
* @param e 异常堆栈
|
||||
* @param member 异常 [Member] - 可空
|
||||
*/
|
||||
private fun hookErrorMsg(e: Throwable, member: Member? = null) =
|
||||
YLog.innerE(msg = "Try to hook [${hookClass.instance ?: hookClass.name}]${member?.let { "[$it]" } ?: ""} got an Exception [$tag]", e = e)
|
||||
YLog.innerE("Try to hook [${hookClass.instance ?: hookClass.name}]${member?.let { "[$it]" } ?: ""} got an Exception [$tag]", e)
|
||||
|
||||
/**
|
||||
* 判断是否没有设置 Hook 过程中的任何异常拦截
|
||||
@@ -837,7 +829,7 @@ class YukiMemberHookCreator internal constructor(private val packageParam: Packa
|
||||
hookedMembers.takeIf { it.isNotEmpty() }?.apply {
|
||||
forEach {
|
||||
it.remove()
|
||||
hookDebugMsg(msg = "Remove Hooked Member [${it.member}] done [$tag]")
|
||||
YLog.innerD("Remove Hooked Member [${it.member}] done [$tag]")
|
||||
}
|
||||
runCatching { preHookMembers.remove(this@MemberHookCreator.toString()) }
|
||||
clear()
|
||||
|
@@ -180,17 +180,17 @@ class YukiResourcesHookCreator internal constructor(internal val packageParam: P
|
||||
hookResources.instance?.hookLayout(
|
||||
packageParam.packageName, conditions!!.type,
|
||||
conditions!!.name, layoutInstance!!
|
||||
) { hookDebugMsg(msg = "Hook Resources Layout $conditions done [$tag]") }
|
||||
) { YLog.innerD("Hook Resources Layout $conditions done [$tag]") }
|
||||
else -> hookResources.instance?.setReplacement(
|
||||
packageParam.packageName, conditions!!.type,
|
||||
conditions!!.name, compat(replaceInstance)
|
||||
) { hookDebugMsg(msg = "Hook Resources Value $conditions done [$tag]") }
|
||||
) { YLog.innerD("Hook Resources Value $conditions done [$tag]") }
|
||||
} else when {
|
||||
layoutInstance != null -> hookResources.instance?.hookLayout(resourceId, layoutInstance!!) {
|
||||
hookDebugMsg(msg = "Hook Resources Layout Id $resourceId done [$tag]")
|
||||
YLog.innerD("Hook Resources Layout Id $resourceId done [$tag]")
|
||||
}
|
||||
else -> hookResources.instance?.setReplacement(resourceId, compat(replaceInstance)) {
|
||||
hookDebugMsg(msg = "Hook Resources Value Id $resourceId done [$tag]")
|
||||
YLog.innerD("Hook Resources Value Id $resourceId done [$tag]")
|
||||
}
|
||||
}
|
||||
packageParam.wrapper?.type == HookEntryType.ZYGOTE ->
|
||||
@@ -199,17 +199,17 @@ class YukiResourcesHookCreator internal constructor(internal val packageParam: P
|
||||
YukiResources.hookSystemWideLayout(
|
||||
packageParam.packageName, conditions!!.type,
|
||||
conditions!!.name, layoutInstance!!
|
||||
) { hookDebugMsg(msg = "Hook Wide Resources Layout $conditions done [$tag]") }
|
||||
) { YLog.innerD("Hook Wide Resources Layout $conditions done [$tag]") }
|
||||
else -> YukiResources.setSystemWideReplacement(
|
||||
packageParam.packageName, conditions!!.type,
|
||||
conditions!!.name, compat(replaceInstance)
|
||||
) { hookDebugMsg(msg = "Hook Wide Resources Value $conditions done [$tag]") }
|
||||
) { YLog.innerD("Hook Wide Resources Value $conditions done [$tag]") }
|
||||
} else when {
|
||||
layoutInstance != null -> YukiResources.hookSystemWideLayout(resourceId, layoutInstance!!) {
|
||||
hookDebugMsg(msg = "Hook Wide Resources Layout Id $resourceId done [$tag]")
|
||||
YLog.innerD("Hook Wide Resources Layout Id $resourceId done [$tag]")
|
||||
}
|
||||
else -> YukiResources.setSystemWideReplacement(resourceId, compat(replaceInstance)) {
|
||||
hookDebugMsg(msg = "Hook Wide Resources Value Id $resourceId done [$tag]")
|
||||
YLog.innerD("Hook Wide Resources Value Id $resourceId done [$tag]")
|
||||
}
|
||||
}
|
||||
else -> YLog.innerE("Resources Hook type is invalid [$tag]")
|
||||
@@ -221,14 +221,6 @@ class YukiResourcesHookCreator internal constructor(internal val packageParam: P
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook 过程中开启了 [YukiHookAPI.Configs.isDebug] 输出调试信息
|
||||
* @param msg 调试日志内容
|
||||
*/
|
||||
private fun hookDebugMsg(msg: String) {
|
||||
if (YukiHookAPI.Configs.isDebug) YLog.innerD(msg)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resources 查找条件实现类
|
||||
*/
|
||||
|
@@ -62,7 +62,7 @@ abstract class ClassBaseFinder internal constructor(internal open val loaderSet:
|
||||
* @param msg 消息内容
|
||||
*/
|
||||
internal fun debugMsg(msg: String) {
|
||||
if (YukiHookAPI.Configs.isDebug && HookApiCategoryHelper.hasAvailableHookApi) YLog.innerD(msg)
|
||||
if (HookApiCategoryHelper.hasAvailableHookApi) YLog.innerD(msg)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -94,7 +94,7 @@ abstract class MemberBaseFinder internal constructor(private val tag: String, in
|
||||
* @param msg 消息内容
|
||||
*/
|
||||
internal fun debugMsg(msg: String) {
|
||||
if (YukiHookAPI.Configs.isDebug && HookApiCategoryHelper.hasAvailableHookApi && hookerManager.instance != null)
|
||||
if (HookApiCategoryHelper.hasAvailableHookApi && hookerManager.instance != null)
|
||||
YLog.innerD("$msg${hookerManager.tailTag}")
|
||||
}
|
||||
|
||||
|
@@ -25,6 +25,8 @@
|
||||
*
|
||||
* This file is created by fankes on 2023/9/27.
|
||||
*/
|
||||
@file:Suppress("unused")
|
||||
|
||||
package com.highcapable.yukihookapi.hook.log
|
||||
|
||||
import android.system.ErrnoException
|
||||
@@ -189,8 +191,7 @@ object YLog {
|
||||
content += "${it.head}Dump stack trace for \"${e.current().name}\":\n"
|
||||
content += e.dumpToString()
|
||||
}
|
||||
}
|
||||
return content
|
||||
}; return content
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,10 +272,9 @@ object YLog {
|
||||
* @param msg 日志打印的内容 - 默认空 - 如果你仅想打印异常堆栈可只设置 [e]
|
||||
* @param e 可填入异常堆栈信息 - 将自动完整打印到控制台
|
||||
* @param isImplicit 是否隐式打印 - 不会记录 - 也不会显示包名和用户 ID
|
||||
* @param isDisable 禁止打印日志 - 标识后将什么也不做 - 默认否
|
||||
*/
|
||||
internal fun innerD(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false, isDisable: Boolean = false) {
|
||||
if (Configs.isEnable.not() || isDisable) return
|
||||
internal fun innerD(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false) {
|
||||
if (Configs.isEnable.not() || YukiHookAPI.Configs.isDebug.not()) return
|
||||
log(EnvType.BOTH, YLogData(priority = "D", msg = msg, throwable = e), isImplicit)
|
||||
}
|
||||
|
||||
@@ -283,10 +283,9 @@ object YLog {
|
||||
* @param msg 日志打印的内容 - 默认空 - 如果你仅想打印异常堆栈可只设置 [e]
|
||||
* @param e 可填入异常堆栈信息 - 将自动完整打印到控制台
|
||||
* @param isImplicit 是否隐式打印 - 不会记录 - 也不会显示包名和用户 ID
|
||||
* @param isDisable 禁止打印日志 - 标识后将什么也不做 - 默认否
|
||||
*/
|
||||
internal fun innerI(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false, isDisable: Boolean = false) {
|
||||
if (Configs.isEnable.not() || isDisable) return
|
||||
internal fun innerI(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false) {
|
||||
if (Configs.isEnable.not()) return
|
||||
log(EnvType.BOTH, YLogData(priority = "I", msg = msg, throwable = e), isImplicit)
|
||||
}
|
||||
|
||||
@@ -295,10 +294,9 @@ object YLog {
|
||||
* @param msg 日志打印的内容 - 默认空 - 如果你仅想打印异常堆栈可只设置 [e]
|
||||
* @param e 可填入异常堆栈信息 - 将自动完整打印到控制台
|
||||
* @param isImplicit 是否隐式打印 - 不会记录 - 也不会显示包名和用户 ID
|
||||
* @param isDisable 禁止打印日志 - 标识后将什么也不做 - 默认否
|
||||
*/
|
||||
internal fun innerW(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false, isDisable: Boolean = false) {
|
||||
if (Configs.isEnable.not() || isDisable) return
|
||||
internal fun innerW(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false) {
|
||||
if (Configs.isEnable.not()) return
|
||||
log(EnvType.BOTH, YLogData(priority = "W", msg = msg, throwable = e), isImplicit)
|
||||
}
|
||||
|
||||
@@ -307,10 +305,9 @@ object YLog {
|
||||
* @param msg 日志打印的内容 - 默认空 - 如果你仅想打印异常堆栈可只设置 [e]
|
||||
* @param e 可填入异常堆栈信息 - 将自动完整打印到控制台
|
||||
* @param isImplicit 是否隐式打印 - 不会记录 - 也不会显示包名和用户 ID
|
||||
* @param isDisable 禁止打印日志 - 标识后将什么也不做 - 默认否
|
||||
*/
|
||||
internal fun innerE(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false, isDisable: Boolean = false) {
|
||||
if (Configs.isEnable.not() || isDisable) return
|
||||
internal fun innerE(msg: String = "", e: Throwable? = null, isImplicit: Boolean = false) {
|
||||
if (Configs.isEnable.not()) return
|
||||
log(EnvType.BOTH, YLogData(priority = "E", msg = msg, throwable = e), isImplicit)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user