refactor: merge all util functions to factory

This commit is contained in:
2023-09-23 20:55:43 +08:00
parent d0d1ce1cb9
commit c849690699
14 changed files with 138 additions and 77 deletions

View File

@@ -66,9 +66,9 @@ import com.highcapable.yukihookapi.hook.type.java.JavaConstructorClass
import com.highcapable.yukihookapi.hook.type.java.JavaFieldClass
import com.highcapable.yukihookapi.hook.type.java.JavaMemberClass
import com.highcapable.yukihookapi.hook.type.java.JavaMethodClass
import com.highcapable.yukihookapi.hook.utils.RandomSeed
import com.highcapable.yukihookapi.hook.utils.await
import com.highcapable.yukihookapi.hook.utils.conditions
import com.highcapable.yukihookapi.hook.utils.factory.RandomSeed
import com.highcapable.yukihookapi.hook.utils.factory.await
import com.highcapable.yukihookapi.hook.utils.factory.conditions
import com.highcapable.yukihookapi.hook.xposed.bridge.type.HookEntryType
import java.lang.reflect.Constructor
import java.lang.reflect.Field

View File

@@ -33,8 +33,8 @@ import com.highcapable.yukihookapi.hook.core.YukiMemberHookCreator
import com.highcapable.yukihookapi.hook.core.api.compat.HookApiCategoryHelper
import com.highcapable.yukihookapi.hook.log.yLoggerE
import com.highcapable.yukihookapi.hook.log.yLoggerI
import com.highcapable.yukihookapi.hook.utils.await
import com.highcapable.yukihookapi.hook.utils.unit
import com.highcapable.yukihookapi.hook.utils.factory.await
import com.highcapable.yukihookapi.hook.utils.factory.unit
import java.lang.reflect.Constructor
import java.lang.reflect.Field
import java.lang.reflect.Member

View File

@@ -51,8 +51,8 @@ import com.highcapable.yukihookapi.hook.factory.searchClass
import com.highcapable.yukihookapi.hook.factory.toClass
import com.highcapable.yukihookapi.hook.log.yLoggerW
import com.highcapable.yukihookapi.hook.param.PackageParam
import com.highcapable.yukihookapi.hook.utils.await
import com.highcapable.yukihookapi.hook.utils.runBlocking
import com.highcapable.yukihookapi.hook.utils.factory.await
import com.highcapable.yukihookapi.hook.utils.factory.runBlocking
import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics
import dalvik.system.BaseDexClassLoader
import java.lang.reflect.Constructor

View File

@@ -44,8 +44,8 @@ import com.highcapable.yukihookapi.hook.factory.hasExtends
import com.highcapable.yukihookapi.hook.log.yLoggerW
import com.highcapable.yukihookapi.hook.type.defined.UndefinedType
import com.highcapable.yukihookapi.hook.type.defined.VagueType
import com.highcapable.yukihookapi.hook.utils.runBlocking
import com.highcapable.yukihookapi.hook.utils.unit
import com.highcapable.yukihookapi.hook.utils.factory.runBlocking
import com.highcapable.yukihookapi.hook.utils.factory.unit
import java.lang.reflect.Constructor
import java.lang.reflect.Member

View File

@@ -44,7 +44,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.factory.ObjectCondition
import com.highcapable.yukihookapi.hook.factory.current
import com.highcapable.yukihookapi.hook.factory.hasExtends
import com.highcapable.yukihookapi.hook.log.yLoggerW
import com.highcapable.yukihookapi.hook.utils.runBlocking
import com.highcapable.yukihookapi.hook.utils.factory.runBlocking
import java.lang.reflect.Field
/**

View File

@@ -47,8 +47,8 @@ import com.highcapable.yukihookapi.hook.factory.hasExtends
import com.highcapable.yukihookapi.hook.log.yLoggerW
import com.highcapable.yukihookapi.hook.type.defined.UndefinedType
import com.highcapable.yukihookapi.hook.type.defined.VagueType
import com.highcapable.yukihookapi.hook.utils.runBlocking
import com.highcapable.yukihookapi.hook.utils.unit
import com.highcapable.yukihookapi.hook.utils.factory.runBlocking
import com.highcapable.yukihookapi.hook.utils.factory.unit
import java.lang.reflect.Member
import java.lang.reflect.Method

View File

@@ -44,7 +44,7 @@ import com.highcapable.yukihookapi.hook.type.java.DalvikBaseDexClassLoader
import com.highcapable.yukihookapi.hook.type.java.NoClassDefFoundErrorClass
import com.highcapable.yukihookapi.hook.type.java.NoSuchFieldErrorClass
import com.highcapable.yukihookapi.hook.type.java.NoSuchMethodErrorClass
import com.highcapable.yukihookapi.hook.utils.*
import com.highcapable.yukihookapi.hook.utils.factory.*
import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics
import dalvik.system.BaseDexClassLoader
import java.lang.reflect.Constructor

View File

@@ -34,7 +34,7 @@ import android.util.Log
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.hook.core.api.helper.YukiHookHelper
import com.highcapable.yukihookapi.hook.factory.current
import com.highcapable.yukihookapi.hook.utils.toStackTrace
import com.highcapable.yukihookapi.hook.utils.factory.toStackTrace
import com.highcapable.yukihookapi.hook.xposed.bridge.YukiXposedModule
import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics
import java.io.File

View File

@@ -49,7 +49,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.factory.ClassConditions
import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker
import com.highcapable.yukihookapi.hook.log.yLoggerW
import com.highcapable.yukihookapi.hook.param.wrapper.PackageParamWrapper
import com.highcapable.yukihookapi.hook.utils.value
import com.highcapable.yukihookapi.hook.utils.factory.value
import com.highcapable.yukihookapi.hook.xposed.bridge.YukiXposedModule
import com.highcapable.yukihookapi.hook.xposed.bridge.resources.YukiModuleResources
import com.highcapable.yukihookapi.hook.xposed.bridge.resources.YukiResources

View File

@@ -0,0 +1,80 @@
/*
* YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/fankes/YukiHookAPI
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2023/9/23.
*/
@file:Suppress("unused")
package com.highcapable.yukihookapi.hook.utils.factory
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/**
* 创建当前线程池服务
* @return [ExecutorService]
*/
private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
/**
* 启动 [Thread] 延迟等待 [block] 的结果 [T]
* @param delayMs 延迟毫秒 - 默认 1 ms
* @param block 方法块
* @return [T]
*/
internal inline fun <T> T.await(delayMs: Long = 1, crossinline block: (T) -> Unit): T {
currentThreadPool.apply {
execute {
if (delayMs > 0) Thread.sleep(delayMs)
block(this@await)
shutdown()
}
}
return this
}
/**
* 计算方法执行耗时
* @param block 方法块
* @return [RunBlockResult]
*/
internal inline fun <R> runBlocking(block: () -> R): RunBlockResult {
val start = System.currentTimeMillis()
block()
return RunBlockResult(afterMs = System.currentTimeMillis() - start)
}
/**
* 构造耗时计算结果类
* @param afterMs 耗时
*/
internal class RunBlockResult(internal val afterMs: Long) {
/**
* 获取耗时计算结果
* @param result 回调结果 - ([Long] 耗时)
*/
internal inline fun result(result: (Long) -> Unit) = result(afterMs)
}

View File

@@ -0,0 +1,39 @@
/*
* YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/fankes/YukiHookAPI
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2023/9/23.
*/
@file:Suppress("unused")
package com.highcapable.yukihookapi.hook.utils.factory
import java.io.ByteArrayOutputStream
import java.io.PrintStream
/**
* 获取完整的异常堆栈内容
* @return [String]
*/
internal fun Throwable.toStackTrace() = ByteArrayOutputStream().also { printStackTrace(PrintStream(it)) }.toString()

View File

@@ -23,22 +23,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2022/2/5.
* This file is created by fankes on 2023/9/23.
*/
@file:Suppress("unused")
package com.highcapable.yukihookapi.hook.utils
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/**
* 创建当前线程池服务
* @return [ExecutorService]
*/
private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
package com.highcapable.yukihookapi.hook.utils.factory
/**
* [T] 返回无返回值的 [Unit]
@@ -46,23 +35,6 @@ private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRu
*/
internal fun <T> T?.unit() = let {}
/**
* 启动 [Thread] 延迟等待 [block] 的结果 [T]
* @param delayMs 延迟毫秒 - 默认 1 ms
* @param block 方法块
* @return [T]
*/
internal inline fun <T> T.await(delayMs: Long = 1, crossinline block: (T) -> Unit): T {
currentThreadPool.apply {
execute {
if (delayMs > 0) Thread.sleep(delayMs)
block(this@await)
shutdown()
}
}
return this
}
/**
* 获取数组内容依次列出的字符串表示
* @return [String]
@@ -116,36 +88,6 @@ internal inline fun runOrTrue(block: () -> Boolean) = runCatching { block() }.ge
*/
internal inline fun runOrFalse(block: () -> Boolean) = runCatching { block() }.getOrNull() ?: false
/**
* 获取完整的异常堆栈内容
* @return [String]
*/
internal fun Throwable.toStackTrace() = ByteArrayOutputStream().also { printStackTrace(PrintStream(it)) }.toString()
/**
* 计算方法执行耗时
* @param block 方法块
* @return [RunBlockResult]
*/
internal inline fun <R> runBlocking(block: () -> R): RunBlockResult {
val start = System.currentTimeMillis()
block()
return RunBlockResult(afterMs = System.currentTimeMillis() - start)
}
/**
* 构造耗时计算结果类
* @param afterMs 耗时
*/
internal class RunBlockResult(internal val afterMs: Long) {
/**
* 获取耗时计算结果
* @param result 回调结果 - ([Long] 耗时)
*/
internal inline fun result(result: (Long) -> Unit) = result(afterMs)
}
/**
* 创建多项条件判断 - 条件对象 [T]
* @param initiate 方法体

View File

@@ -49,7 +49,7 @@ import com.highcapable.yukihookapi.hook.log.YukiHookLogger
import com.highcapable.yukihookapi.hook.log.YukiLoggerData
import com.highcapable.yukihookapi.hook.log.yLoggerE
import com.highcapable.yukihookapi.hook.log.yLoggerW
import com.highcapable.yukihookapi.hook.utils.RandomSeed
import com.highcapable.yukihookapi.hook.utils.factory.RandomSeed
import com.highcapable.yukihookapi.hook.xposed.application.ModuleApplication
import com.highcapable.yukihookapi.hook.xposed.bridge.YukiXposedModule
import com.highcapable.yukihookapi.hook.xposed.channel.data.ChannelData

View File

@@ -37,7 +37,7 @@ import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import androidx.preference.PreferenceScreen
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.hook.utils.unit
import com.highcapable.yukihookapi.hook.utils.factory.unit
import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookPrefsBridge
/**