|
| 1 | +package cn.kaicity.common.repository |
| 2 | + |
| 3 | +import cn.kaicity.common.bean.InputBean |
| 4 | +import cn.kaicity.common.platform.IObfuscator |
| 5 | +import kotlinx.coroutines.DelicateCoroutinesApi |
| 6 | +import kotlinx.coroutines.Dispatchers |
| 7 | +import kotlinx.coroutines.GlobalScope |
| 8 | +import kotlinx.coroutines.Job |
| 9 | +import kotlinx.coroutines.flow.catch |
| 10 | +import kotlinx.coroutines.flow.collect |
| 11 | +import kotlinx.coroutines.flow.flow |
| 12 | +import kotlinx.coroutines.flow.flowOn |
| 13 | +import kotlinx.coroutines.flow.onCompletion |
| 14 | +import kotlinx.coroutines.launch |
| 15 | +import java.io.File |
| 16 | +import kotlin.math.log |
| 17 | + |
| 18 | +object DexObf { |
| 19 | + |
| 20 | + private var obfuscatorImpl: IObfuscator? = null |
| 21 | + |
| 22 | + private var job: Job? = null |
| 23 | + |
| 24 | + @OptIn(DelicateCoroutinesApi::class) |
| 25 | + fun run(inputBean: InputBean, logCallback: (log: String) -> Unit) { |
| 26 | + job?.cancel() |
| 27 | + |
| 28 | + job = GlobalScope.launch(Dispatchers.IO) { |
| 29 | + val params = arrayListOf<String>() |
| 30 | + params.add("-i") |
| 31 | + params.add(inputBean.input) |
| 32 | + |
| 33 | + params.add("-o") |
| 34 | + params.add(inputBean.output) |
| 35 | + |
| 36 | + params.add("-d") |
| 37 | + params.add(inputBean.depth) |
| 38 | + |
| 39 | + params.add("-a") |
| 40 | + |
| 41 | + val filterFile = File(File(inputBean.output).parentFile, "filter.txt") |
| 42 | + filterFile.writeText(inputBean.rule) |
| 43 | + params.add(filterFile.absolutePath) |
| 44 | + |
| 45 | + |
| 46 | + flow<String> { }.flowOn(Dispatchers.IO) |
| 47 | + .collect() |
| 48 | + flow { |
| 49 | + obfuscatorImpl = IObfuscator(this) |
| 50 | + obfuscatorImpl?.doObfuscator(params.toTypedArray()) |
| 51 | + }.catch { |
| 52 | + logCallback.invoke("Error") |
| 53 | + }.onCompletion { |
| 54 | + filterFile.delete() |
| 55 | + logCallback.invoke("Finish") |
| 56 | + }.flowOn(Dispatchers.IO) |
| 57 | + .collect { |
| 58 | + logCallback.invoke(it) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments