mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-07 11:10:04 +08:00
Modify move try -> catch to runCatching -> getOrElse function
This commit is contained in:
@@ -32,10 +32,10 @@ package com.highcapable.yukihookapi.hook.core
|
|||||||
import com.highcapable.yukihookapi.YukiHookAPI
|
import com.highcapable.yukihookapi.YukiHookAPI
|
||||||
import com.highcapable.yukihookapi.annotation.CauseProblemsApi
|
import com.highcapable.yukihookapi.annotation.CauseProblemsApi
|
||||||
import com.highcapable.yukihookapi.hook.bean.HookClass
|
import com.highcapable.yukihookapi.hook.bean.HookClass
|
||||||
|
import com.highcapable.yukihookapi.hook.core.finder.base.MemberBaseFinder
|
||||||
import com.highcapable.yukihookapi.hook.core.finder.members.ConstructorFinder
|
import com.highcapable.yukihookapi.hook.core.finder.members.ConstructorFinder
|
||||||
import com.highcapable.yukihookapi.hook.core.finder.members.FieldFinder
|
import com.highcapable.yukihookapi.hook.core.finder.members.FieldFinder
|
||||||
import com.highcapable.yukihookapi.hook.core.finder.members.MethodFinder
|
import com.highcapable.yukihookapi.hook.core.finder.members.MethodFinder
|
||||||
import com.highcapable.yukihookapi.hook.core.finder.base.MemberBaseFinder
|
|
||||||
import com.highcapable.yukihookapi.hook.factory.*
|
import com.highcapable.yukihookapi.hook.factory.*
|
||||||
import com.highcapable.yukihookapi.hook.log.yLoggerE
|
import com.highcapable.yukihookapi.hook.log.yLoggerE
|
||||||
import com.highcapable.yukihookapi.hook.log.yLoggerI
|
import com.highcapable.yukihookapi.hook.log.yLoggerI
|
||||||
@@ -325,12 +325,12 @@ class YukiMemberHookCreator(@PublishedApi internal val packageParam: PackagePara
|
|||||||
* @param initiate 方法体
|
* @param initiate 方法体
|
||||||
* @return [MethodFinder.Process]
|
* @return [MethodFinder.Process]
|
||||||
*/
|
*/
|
||||||
inline fun method(initiate: MethodCondition) = try {
|
inline fun method(initiate: MethodCondition) = runCatching {
|
||||||
isHookMemberSetup = true
|
isHookMemberSetup = true
|
||||||
MethodFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.process()
|
MethodFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.process()
|
||||||
} catch (e: Throwable) {
|
}.getOrElse {
|
||||||
findingThrowable = e
|
findingThrowable = it
|
||||||
MethodFinder(hookInstance = this).denied(e)
|
MethodFinder(hookInstance = this).denied(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -340,12 +340,12 @@ class YukiMemberHookCreator(@PublishedApi internal val packageParam: PackagePara
|
|||||||
* @param initiate 方法体
|
* @param initiate 方法体
|
||||||
* @return [ConstructorFinder.Process]
|
* @return [ConstructorFinder.Process]
|
||||||
*/
|
*/
|
||||||
inline fun constructor(initiate: ConstructorCondition = { emptyParam() }) = try {
|
inline fun constructor(initiate: ConstructorCondition = { emptyParam() }) = runCatching {
|
||||||
isHookMemberSetup = true
|
isHookMemberSetup = true
|
||||||
ConstructorFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.process()
|
ConstructorFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.process()
|
||||||
} catch (e: Throwable) {
|
}.getOrElse {
|
||||||
findingThrowable = e
|
findingThrowable = it
|
||||||
ConstructorFinder(hookInstance = this).denied(e)
|
ConstructorFinder(hookInstance = this).denied(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -556,16 +556,16 @@ class YukiMemberHookCreator(@PublishedApi internal val packageParam: PackagePara
|
|||||||
val replaceMent = object : YukiMemberReplacement(priority) {
|
val replaceMent = object : YukiMemberReplacement(priority) {
|
||||||
override fun replaceHookedMember(param: Param) =
|
override fun replaceHookedMember(param: Param) =
|
||||||
replaceHookParam.assign(param).let { assign ->
|
replaceHookParam.assign(param).let { assign ->
|
||||||
try {
|
runCatching {
|
||||||
replaceHookCallback?.invoke(assign).also {
|
replaceHookCallback?.invoke(assign).also {
|
||||||
checkingReturnType((param.member as? Method?)?.returnType, it?.javaClass)
|
checkingReturnType((param.member as? Method?)?.returnType, it?.javaClass)
|
||||||
if (replaceHookCallback != null) onHookLogMsg(msg = "Replace Hook Member [${this@hook}] done [$tag]")
|
if (replaceHookCallback != null) onHookLogMsg(msg = "Replace Hook Member [${this@hook}] done [$tag]")
|
||||||
HookParam.invoke()
|
HookParam.invoke()
|
||||||
}
|
}
|
||||||
} catch (e: Throwable) {
|
}.getOrElse {
|
||||||
onConductFailureCallback?.invoke(assign, e)
|
onConductFailureCallback?.invoke(assign, it)
|
||||||
onAllFailureCallback?.invoke(e)
|
onAllFailureCallback?.invoke(it)
|
||||||
if (onConductFailureCallback == null && onAllFailureCallback == null) onHookFailureMsg(e)
|
if (onConductFailureCallback == null && onAllFailureCallback == null) onHookFailureMsg(it)
|
||||||
/** 若发生异常则会自动调用未经 Hook 的原始 [Member] 保证 Hook APP 正常运行 */
|
/** 若发生异常则会自动调用未经 Hook 的原始 [Member] 保证 Hook APP 正常运行 */
|
||||||
assign.callOriginal()
|
assign.callOriginal()
|
||||||
}
|
}
|
||||||
|
@@ -207,21 +207,21 @@ class ConstructorFinder @PublishedApi internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
override fun build() = try {
|
override fun build() = runCatching {
|
||||||
build(isBind = false)
|
build(isBind = false)
|
||||||
Result()
|
Result()
|
||||||
} catch (e: Throwable) {
|
}.getOrElse {
|
||||||
onFailureMsg(throwable = e)
|
onFailureMsg(throwable = it)
|
||||||
Result(isNoSuch = true, e)
|
Result(isNoSuch = true, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
override fun process() = try {
|
override fun process() = runCatching {
|
||||||
build(isBind = true)
|
build(isBind = true)
|
||||||
Process()
|
Process()
|
||||||
} catch (e: Throwable) {
|
}.getOrElse {
|
||||||
onFailureMsg(throwable = e)
|
onFailureMsg(throwable = it)
|
||||||
Process(isNoSuch = true, e)
|
Process(isNoSuch = true, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
|
@@ -188,7 +188,7 @@ class FieldFinder @PublishedApi internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
override fun build() = try {
|
override fun build() = runCatching {
|
||||||
if (classSet != null) {
|
if (classSet != null) {
|
||||||
classSet.checkingInternal()
|
classSet.checkingInternal()
|
||||||
runBlocking {
|
runBlocking {
|
||||||
@@ -198,9 +198,7 @@ class FieldFinder @PublishedApi internal constructor(
|
|||||||
}
|
}
|
||||||
Result()
|
Result()
|
||||||
} else Result(isNoSuch = true, Throwable("classSet is null"))
|
} else Result(isNoSuch = true, Throwable("classSet is null"))
|
||||||
} catch (e: Throwable) {
|
}.getOrElse { e -> Result(isNoSuch = true, e).await { onFailureMsg(throwable = e) } }
|
||||||
Result(isNoSuch = true, e).await { onFailureMsg(throwable = e) }
|
|
||||||
}
|
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
override fun process() = error("FieldFinder does not contain this usage")
|
override fun process() = error("FieldFinder does not contain this usage")
|
||||||
|
@@ -279,21 +279,21 @@ class MethodFinder @PublishedApi internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
override fun build() = try {
|
override fun build() = runCatching {
|
||||||
build(isBind = false)
|
build(isBind = false)
|
||||||
Result()
|
Result()
|
||||||
} catch (e: Throwable) {
|
}.getOrElse {
|
||||||
onFailureMsg(throwable = e)
|
onFailureMsg(throwable = it)
|
||||||
Result(isNoSuch = true, e)
|
Result(isNoSuch = true, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
override fun process() = try {
|
override fun process() = runCatching {
|
||||||
build(isBind = true)
|
build(isBind = true)
|
||||||
Process()
|
Process()
|
||||||
} catch (e: Throwable) {
|
}.getOrElse {
|
||||||
onFailureMsg(throwable = e)
|
onFailureMsg(throwable = it)
|
||||||
Process(isNoSuch = true, e)
|
Process(isNoSuch = true, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
@YukiPrivateApi
|
@YukiPrivateApi
|
||||||
|
@@ -109,12 +109,7 @@ inline fun <reified T> classOf(loader: ClassLoader? = null) = loader?.let { clas
|
|||||||
* @param loader [Class] 所在的 [ClassLoader] - 不填使用默认 [ClassLoader]
|
* @param loader [Class] 所在的 [ClassLoader] - 不填使用默认 [ClassLoader]
|
||||||
* @return [Boolean] 是否存在
|
* @return [Boolean] 是否存在
|
||||||
*/
|
*/
|
||||||
fun String.hasClass(loader: ClassLoader? = null) = try {
|
fun String.hasClass(loader: ClassLoader? = null) = runCatching { classOf(name = this, loader); true }.getOrNull() ?: false
|
||||||
classOf(name = this, loader)
|
|
||||||
true
|
|
||||||
} catch (_: Throwable) {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找变量是否存在
|
* 查找变量是否存在
|
||||||
|
@@ -331,11 +331,8 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @param loader 当前 [ClassLoader] - 默认使用 [appClassLoader] - 设为 null 使用默认 [ClassLoader]
|
* @param loader 当前 [ClassLoader] - 默认使用 [appClassLoader] - 设为 null 使用默认 [ClassLoader]
|
||||||
* @return [HookClass]
|
* @return [HookClass]
|
||||||
*/
|
*/
|
||||||
fun findClass(name: String, loader: ClassLoader? = appClassLoader) = try {
|
fun findClass(name: String, loader: ClassLoader? = appClassLoader) =
|
||||||
classOf(name, loader).hookClass
|
runCatching { classOf(name, loader).hookClass }.getOrElse { HookClass(name = name, throwable = it) }
|
||||||
} catch (e: Throwable) {
|
|
||||||
HookClass(name = name, throwable = e)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找并装载 [Class]
|
* 查找并装载 [Class]
|
||||||
@@ -414,11 +411,8 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
|
|||||||
* @return [HookClass]
|
* @return [HookClass]
|
||||||
*/
|
*/
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal fun VariousClass.hookClass(loader: ClassLoader? = null) = try {
|
internal fun VariousClass.hookClass(loader: ClassLoader? = null) =
|
||||||
get(loader).hookClass
|
runCatching { get(loader).hookClass }.getOrElse { HookClass(name = "VariousClass", throwable = Throwable(it.message)) }
|
||||||
} catch (e: Throwable) {
|
|
||||||
HookClass(name = "VariousClass", throwable = Throwable(e.message))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [Class] 转换为 [HookClass]
|
* [Class] 转换为 [HookClass]
|
||||||
|
@@ -61,7 +61,7 @@ import java.io.File
|
|||||||
*
|
*
|
||||||
* - 若你正在使用 [PreferenceFragmentCompat] - 请迁移到 [ModulePreferenceFragment] 以适配上述功能特性
|
* - 若你正在使用 [PreferenceFragmentCompat] - 请迁移到 [ModulePreferenceFragment] 以适配上述功能特性
|
||||||
*
|
*
|
||||||
* - 详情请参考 [API 文档 - YukiHookModulePrefs](https://fankes.github.io/YukiHookAPI/#/api/document?id=yukihookmoduleprefs-class)
|
* - 详情请参考 [API 文档 - YukiHookModulePrefs](https://fankes.github.io/YukiHookAPI/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs)
|
||||||
* @param context 上下文实例 - 默认空
|
* @param context 上下文实例 - 默认空
|
||||||
*/
|
*/
|
||||||
class YukiHookModulePrefs private constructor(private var context: Context? = null) {
|
class YukiHookModulePrefs private constructor(private var context: Context? = null) {
|
||||||
@@ -171,10 +171,10 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu
|
|||||||
*/
|
*/
|
||||||
private val sPrefs
|
private val sPrefs
|
||||||
get() = checkApi().let {
|
get() = checkApi().let {
|
||||||
try {
|
runCatching {
|
||||||
context?.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE).also { isUsingNewXSharedPreferences = true }
|
context?.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE).also { isUsingNewXSharedPreferences = true }
|
||||||
?: error("YukiHookModulePrefs missing Context instance")
|
?: error("YukiHookModulePrefs missing Context instance")
|
||||||
} catch (_: Throwable) {
|
}.getOrElse {
|
||||||
context?.getSharedPreferences(prefsName, Context.MODE_PRIVATE).also { isUsingNewXSharedPreferences = false }
|
context?.getSharedPreferences(prefsName, Context.MODE_PRIVATE).also { isUsingNewXSharedPreferences = false }
|
||||||
?: error("YukiHookModulePrefs missing Context instance")
|
?: error("YukiHookModulePrefs missing Context instance")
|
||||||
}
|
}
|
||||||
|
@@ -39,6 +39,7 @@ import androidx.preference.PreferenceFragmentCompat
|
|||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import androidx.preference.PreferenceScreen
|
import androidx.preference.PreferenceScreen
|
||||||
import com.highcapable.yukihookapi.YukiHookAPI
|
import com.highcapable.yukihookapi.YukiHookAPI
|
||||||
|
import com.highcapable.yukihookapi.hook.utils.unit
|
||||||
import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookModulePrefs
|
import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookModulePrefs
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,7 +51,7 @@ import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookModulePrefs
|
|||||||
*
|
*
|
||||||
* 然后请将重写方法由 [onCreatePreferences] 替换为 [onCreatePreferencesInModuleApp] 即可
|
* 然后请将重写方法由 [onCreatePreferences] 替换为 [onCreatePreferencesInModuleApp] 即可
|
||||||
*
|
*
|
||||||
* 详情请参考 [API 文档 - ModulePreferenceFragment](https://fankes.github.io/YukiHookAPI/#/api/document?id=modulepreferencefragment-class)
|
* 详情请参考 [API 文档 - ModulePreferenceFragment](https://fankes.github.io/YukiHookAPI/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/ModulePreferenceFragment)
|
||||||
*/
|
*/
|
||||||
abstract class ModulePreferenceFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
|
abstract class ModulePreferenceFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
@@ -101,9 +102,7 @@ abstract class ModulePreferenceFragment : PreferenceFragmentCompat(), SharedPref
|
|||||||
abstract fun onCreatePreferencesInModuleApp(savedInstanceState: Bundle?, rootKey: String?)
|
abstract fun onCreatePreferencesInModuleApp(savedInstanceState: Bundle?, rootKey: String?)
|
||||||
|
|
||||||
/** 设置自动适配模块 Sp 存储全局可读可写 */
|
/** 设置自动适配模块 Sp 存储全局可读可写 */
|
||||||
private fun makeNewXShareReadableIfPossible() = try {
|
private fun makeNewXShareReadableIfPossible() = runCatching {
|
||||||
currentActivity.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE)
|
currentActivity.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE)
|
||||||
} catch (_: Throwable) {
|
}.onFailure { YukiHookModulePrefs.makeWorldReadable(currentActivity, prefsFileName = "$prefsName.xml") }.unit()
|
||||||
YukiHookModulePrefs.makeWorldReadable(currentActivity, prefsFileName = "$prefsName.xml")
|
|
||||||
}
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user