From 8e1117ed3fd36150fd0ff8dd57420559a2ddf449 Mon Sep 17 00:00:00 2001
From: oSumAtrIX <johan.melkonyan1@web.de>
Date: Mon, 26 Feb 2024 16:45:03 +0100
Subject: [PATCH] refactor: Properly abstract `Patch#execute` function

---
 src/main/kotlin/app/revanced/patcher/Patcher.kt   | 15 +--------------
 .../app/revanced/patcher/patch/BytecodePatch.kt   |  7 +++++++
 .../kotlin/app/revanced/patcher/patch/Patch.kt    |  9 +++++++++
 .../revanced/patcher/patch/RawResourcePatch.kt    |  3 +++
 .../app/revanced/patcher/patch/ResourcePatch.kt   |  3 +++
 5 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/src/main/kotlin/app/revanced/patcher/Patcher.kt b/src/main/kotlin/app/revanced/patcher/Patcher.kt
index c2e84b2..e0d8cc3 100644
--- a/src/main/kotlin/app/revanced/patcher/Patcher.kt
+++ b/src/main/kotlin/app/revanced/patcher/Patcher.kt
@@ -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) {
diff --git a/src/main/kotlin/app/revanced/patcher/patch/BytecodePatch.kt b/src/main/kotlin/app/revanced/patcher/patch/BytecodePatch.kt
index 59e28ba..74c7d46 100644
--- a/src/main/kotlin/app/revanced/patcher/patch/BytecodePatch.kt
+++ b/src/main/kotlin/app/revanced/patcher/patch/BytecodePatch.kt
@@ -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)
+    }
 }
diff --git a/src/main/kotlin/app/revanced/patcher/patch/Patch.kt b/src/main/kotlin/app/revanced/patcher/patch/Patch.kt
index 8ec2caf..73bbd77 100644
--- a/src/main/kotlin/app/revanced/patcher/patch/Patch.kt
+++ b/src/main/kotlin/app/revanced/patcher/patch/Patch.kt
@@ -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.
      *
diff --git a/src/main/kotlin/app/revanced/patcher/patch/RawResourcePatch.kt b/src/main/kotlin/app/revanced/patcher/patch/RawResourcePatch.kt
index b610242..a767ebb 100644
--- a/src/main/kotlin/app/revanced/patcher/patch/RawResourcePatch.kt
+++ b/src/main/kotlin/app/revanced/patcher/patch/RawResourcePatch.kt
@@ -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)
 }
diff --git a/src/main/kotlin/app/revanced/patcher/patch/ResourcePatch.kt b/src/main/kotlin/app/revanced/patcher/patch/ResourcePatch.kt
index 1ab3327..d25f1bb 100644
--- a/src/main/kotlin/app/revanced/patcher/patch/ResourcePatch.kt
+++ b/src/main/kotlin/app/revanced/patcher/patch/ResourcePatch.kt
@@ -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)
 }