1
mirror of https://github.com/revanced/revanced-patcher synced 2024-11-27 18:13:37 +01:00

refactor: Properly abstract Patch#execute function

This commit is contained in:
oSumAtrIX 2024-02-26 16:45:03 +01:00
parent 29adcd5aad
commit 8e1117ed3f
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
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)
}