refactor: add callbacks for applyPatches & addFiles

This commit is contained in:
Lucaskyy 2022-06-22 15:37:33 +02:00
parent 273dd86b65
commit 4fc63a4d8a
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89

View File

@ -111,9 +111,13 @@ class Patcher(private val options: PatcherOptions) {
* @param throwOnDuplicates If this is set to true, the patcher will throw an exception if a duplicate class has been found. * @param throwOnDuplicates If this is set to true, the patcher will throw an exception if a duplicate class has been found.
*/ */
fun addFiles( fun addFiles(
files: List<File>, allowedOverwrites: Iterable<String> = emptyList(), throwOnDuplicates: Boolean = false files: List<File>,
allowedOverwrites: Iterable<String> = emptyList(),
throwOnDuplicates: Boolean = false,
callback: (File) -> Unit
) { ) {
for (file in files) { for (file in files) {
var modified = false
for (classDef in MultiDexIO.readDexFile(true, file, NAMER, null, null).classes) { for (classDef in MultiDexIO.readDexFile(true, file, NAMER, null, null).classes) {
val e = data.bytecodeData.classes.internalClasses.findIndexed { it.type == classDef.type } val e = data.bytecodeData.classes.internalClasses.findIndexed { it.type == classDef.type }
if (e != null) { if (e != null) {
@ -123,11 +127,14 @@ class Patcher(private val options: PatcherOptions) {
val (_, idx) = e val (_, idx) = e
if (allowedOverwrites.contains(classDef.type)) { if (allowedOverwrites.contains(classDef.type)) {
data.bytecodeData.classes.internalClasses[idx] = classDef data.bytecodeData.classes.internalClasses[idx] = classDef
modified = true
} }
continue continue
} }
data.bytecodeData.classes.internalClasses.add(classDef) data.bytecodeData.classes.internalClasses.add(classDef)
modified = true
} }
if (modified) callback(file)
} }
} }
@ -223,7 +230,8 @@ class Patcher(private val options: PatcherOptions) {
* @return The result of executing the [patch]. * @return The result of executing the [patch].
*/ */
private fun applyPatch( private fun applyPatch(
patch: Class<out Patch<Data>>, appliedPatches: MutableList<String> patch: Class<out Patch<Data>>,
appliedPatches: MutableList<String>
): PatchResult { ): PatchResult {
val patchName = patch.patchName val patchName = patch.patchName
@ -273,7 +281,8 @@ class Patcher(private val options: PatcherOptions) {
* If the [Patch] failed to apply, an Exception will always be returned to the wrapping Result object. * If the [Patch] failed to apply, an Exception will always be returned to the wrapping Result object.
*/ */
fun applyPatches( fun applyPatches(
stopOnError: Boolean = false, callback: (String) -> Unit = {} stopOnError: Boolean = false,
callback: (Class<out Patch<Data>>, Boolean) -> Unit = { _, _ -> }
): Map<String, Result<PatchResultSuccess>> { ): Map<String, Result<PatchResultSuccess>> {
val appliedPatches = mutableListOf<String>() val appliedPatches = mutableListOf<String>()
@ -281,15 +290,13 @@ class Patcher(private val options: PatcherOptions) {
for (patch in data.patches) { for (patch in data.patches) {
val result = applyPatch(patch, appliedPatches) val result = applyPatch(patch, appliedPatches)
val name = patch.patchName this[patch.patchName] = if (result.isSuccess()) {
callback(name)
this[name] = if (result.isSuccess()) {
Result.success(result.success()!!) Result.success(result.success()!!)
} else { } else {
Result.failure(result.error()!!) Result.failure(result.error()!!)
} }
callback(patch, result.isSuccess())
if (stopOnError && result.isError()) break if (stopOnError && result.isError()) break
} }
} }