refactor: Properly abstract Patch#execute function

This commit is contained in:
oSumAtrIX 2024-02-26 16:45:03 +01:00
parent 26d449e6d9
commit 90b7631d9e
5 changed files with 23 additions and 14 deletions

View file

@ -3,7 +3,6 @@ package app.revanced.patcher
import app.revanced.patcher.PatchBundleLoader.Utils.getInstance
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.fingerprint.LookupMap
import app.revanced.patcher.fingerprint.MethodFingerprint.Companion.resolveUsingLookupMap
import app.revanced.patcher.patch.*
import kotlinx.coroutines.flow.flow
import java.io.Closeable
@ -166,19 +165,7 @@ class Patcher(
}
return try {
// TODO: Implement this in a more polymorphic way.
when (patch) {
is BytecodePatch -> {
patch.fingerprints.resolveUsingLookupMap(context.bytecodeContext)
patch.execute(context.bytecodeContext)
}
is RawResourcePatch -> {
patch.execute(context.resourceContext)
}
is ResourcePatch -> {
patch.execute(context.resourceContext)
}
}
patch.execute(context)
PatchResult(patch)
} catch (exception: PatchException) {

View file

@ -2,8 +2,10 @@ package app.revanced.patcher.patch
import app.revanced.patcher.PatchClass
import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherContext
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patcher.fingerprint.MethodFingerprint.Companion.resolveUsingLookupMap
import java.io.Closeable
/**
@ -58,4 +60,9 @@ abstract class BytecodePatch : Patch<BytecodeContext> {
ReplaceWith("BytecodePatch(emptySet())"),
)
constructor() : this(emptySet())
override fun execute(context: PatcherContext) {
fingerprints.resolveUsingLookupMap(context.bytecodeContext)
execute(context.bytecodeContext)
}
}

View file

@ -4,6 +4,7 @@ package app.revanced.patcher.patch
import app.revanced.patcher.PatchClass
import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherContext
import app.revanced.patcher.data.Context
import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively
import app.revanced.patcher.patch.options.PatchOptions
@ -90,6 +91,14 @@ sealed class Patch<out T : Context<*>> {
*/
val options = PatchOptions()
/**
* The execution function of the patch.
* This function is called by [Patcher].
*
* @param context The [PatcherContext] the patch will work on.
*/
internal abstract fun execute(context: PatcherContext)
/**
* The execution function of the patch.
*

View file

@ -2,6 +2,7 @@ package app.revanced.patcher.patch
import app.revanced.patcher.PatchClass
import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherContext
import app.revanced.patcher.data.ResourceContext
import java.io.Closeable
@ -40,4 +41,6 @@ abstract class RawResourcePatch : Patch<ResourceContext> {
use: Boolean = true,
requiresIntegrations: Boolean = false,
) : super(name, description, compatiblePackages, dependencies, use, requiresIntegrations)
override fun execute(context: PatcherContext) = execute(context.resourceContext)
}

View file

@ -2,6 +2,7 @@ package app.revanced.patcher.patch
import app.revanced.patcher.PatchClass
import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherContext
import app.revanced.patcher.data.ResourceContext
import java.io.Closeable
@ -40,4 +41,6 @@ abstract class ResourcePatch : Patch<ResourceContext> {
use: Boolean = true,
requiresIntegrations: Boolean = false,
) : super(name, description, compatiblePackages, dependencies, use, requiresIntegrations)
override fun execute(context: PatcherContext) = execute(context.resourceContext)
}