feat: Add patch metadata

Fixes ReVancedTeam/revanced-patches#1
This commit is contained in:
Lucaskyy 2022-04-13 20:23:58 +02:00 committed by oSumAtrIX
parent 7a56dca004
commit 642e9031eb
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4
3 changed files with 18 additions and 6 deletions

View file

@ -3,6 +3,7 @@ package app.revanced.patcher
import app.revanced.patcher.cache.Cache import app.revanced.patcher.cache.Cache
import app.revanced.patcher.cache.findIndexed import app.revanced.patcher.cache.findIndexed
import app.revanced.patcher.patch.Patch import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.PatchMetadata
import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.signature.MethodSignature import app.revanced.patcher.signature.MethodSignature
import app.revanced.patcher.signature.resolver.SignatureResolver import app.revanced.patcher.signature.resolver.SignatureResolver
@ -118,14 +119,14 @@ class Patcher(
fun applyPatches( fun applyPatches(
stopOnError: Boolean = false, stopOnError: Boolean = false,
callback: (String) -> Unit = {} callback: (String) -> Unit = {}
): Map<String, Result<PatchResultSuccess>> { ): Map<PatchMetadata, Result<PatchResultSuccess>> {
if (!sigsResolved) { if (!sigsResolved) {
SignatureResolver(cache.classes, signatures).resolve(cache.methodMap) SignatureResolver(cache.classes, signatures).resolve(cache.methodMap)
sigsResolved = true sigsResolved = true
} }
return buildMap { return buildMap {
for (patch in patches) { for (patch in patches) {
callback(patch.patchName) callback(patch.metadata.shortName)
val result: Result<PatchResultSuccess> = try { val result: Result<PatchResultSuccess> = try {
val pr = patch.execute(cache) val pr = patch.execute(cache)
if (pr.isSuccess()) { if (pr.isSuccess()) {
@ -136,7 +137,7 @@ class Patcher(
} catch (e: Exception) { } catch (e: Exception) {
Result.failure(e) Result.failure(e)
} }
this[patch.patchName] = result this[patch.metadata] = result
if (result.isFailure && stopOnError) break if (result.isFailure && stopOnError) break
} }
} }

View file

@ -2,6 +2,12 @@ package app.revanced.patcher.patch
import app.revanced.patcher.cache.Cache import app.revanced.patcher.cache.Cache
abstract class Patch(val patchName: String) { abstract class Patch(val metadata: PatchMetadata) {
abstract fun execute(cache: Cache): PatchResult abstract fun execute(cache: Cache): PatchResult
} }
data class PatchMetadata(
val shortName: String,
val fullName: String,
val description: String,
)

View file

@ -4,6 +4,7 @@ import app.revanced.patcher.cache.Cache
import app.revanced.patcher.extensions.AccessFlagExtensions.Companion.or import app.revanced.patcher.extensions.AccessFlagExtensions.Companion.or
import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.patch.Patch import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.PatchMetadata
import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.proxy.mutableTypes.MutableField.Companion.toMutable import app.revanced.patcher.proxy.mutableTypes.MutableField.Companion.toMutable
@ -61,7 +62,11 @@ internal class PatcherTest {
) )
patcher.addPatches(listOf( patcher.addPatches(listOf(
object : Patch("TestPatch") { object : Patch(PatchMetadata(
"test-patch",
"My Test Patch",
"A very good description."
)) {
override fun execute(cache: Cache): PatchResult { override fun execute(cache: Cache): PatchResult {
// Get the result from the resolver cache // Get the result from the resolver cache
val result = cache.methodMap["main-method"] val result = cache.methodMap["main-method"]