Skip to content

Commit

Permalink
feat: Add patch metadata
Browse files Browse the repository at this point in the history
Fixes ReVancedTeam/revanced-patches#1
  • Loading branch information
Sculas authored and oSumAtrIX committed Jun 5, 2022
1 parent 7a56dca commit 642e903
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/main/kotlin/app/revanced/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app.revanced.patcher
import app.revanced.patcher.cache.Cache
import app.revanced.patcher.cache.findIndexed
import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.PatchMetadata
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.signature.MethodSignature
import app.revanced.patcher.signature.resolver.SignatureResolver
Expand Down Expand Up @@ -118,14 +119,14 @@ class Patcher(
fun applyPatches(
stopOnError: Boolean = false,
callback: (String) -> Unit = {}
): Map<String, Result<PatchResultSuccess>> {
): Map<PatchMetadata, Result<PatchResultSuccess>> {
if (!sigsResolved) {
SignatureResolver(cache.classes, signatures).resolve(cache.methodMap)
sigsResolved = true
}
return buildMap {
for (patch in patches) {
callback(patch.patchName)
callback(patch.metadata.shortName)
val result: Result<PatchResultSuccess> = try {
val pr = patch.execute(cache)
if (pr.isSuccess()) {
Expand All @@ -136,7 +137,7 @@ class Patcher(
} catch (e: Exception) {
Result.failure(e)
}
this[patch.patchName] = result
this[patch.metadata] = result
if (result.isFailure && stopOnError) break
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/app/revanced/patcher/patch/Patch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package app.revanced.patcher.patch

import app.revanced.patcher.cache.Cache

abstract class Patch(val patchName: String) {
abstract class Patch(val metadata: PatchMetadata) {
abstract fun execute(cache: Cache): PatchResult
}
}

data class PatchMetadata(
val shortName: String,
val fullName: String,
val description: String,
)
7 changes: 6 additions & 1 deletion src/test/kotlin/app/revanced/patcher/PatcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app.revanced.patcher.cache.Cache
import app.revanced.patcher.extensions.AccessFlagExtensions.Companion.or
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.PatchMetadata
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.proxy.mutableTypes.MutableField.Companion.toMutable
Expand Down Expand Up @@ -61,7 +62,11 @@ internal class PatcherTest {
)

patcher.addPatches(listOf(
object : Patch("TestPatch") {
object : Patch(PatchMetadata(
"test-patch",
"My Test Patch",
"A very good description."
)) {
override fun execute(cache: Cache): PatchResult {
// Get the result from the resolver cache
val result = cache.methodMap["main-method"]
Expand Down

0 comments on commit 642e903

Please sign in to comment.