-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add constructor to initialize patches without annotations
- Loading branch information
Showing
7 changed files
with
152 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 53 additions & 5 deletions
58
src/main/kotlin/app/revanced/patcher/patch/BytecodePatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,61 @@ | ||
package app.revanced.patcher.patch | ||
|
||
import app.revanced.patcher.PatchClass | ||
import app.revanced.patcher.Patcher | ||
import app.revanced.patcher.data.BytecodeContext | ||
import app.revanced.patcher.fingerprint.MethodFingerprint | ||
import java.io.Closeable | ||
|
||
/** | ||
* A ReVanced [Patch] that works on [BytecodeContext]. | ||
* A ReVanced [Patch] that accesses a [BytecodeContext]. | ||
* | ||
* @param fingerprints A list of [MethodFingerprint]s which will be resolved before the patch is executed. | ||
* If an implementation of [Patch] also implements [Closeable] | ||
* it will be closed in reverse execution order of patches executed by ReVanced [Patcher]. | ||
*/ | ||
abstract class BytecodePatch( | ||
internal val fingerprints: Set<MethodFingerprint> = emptySet(), | ||
) : Patch<BytecodeContext>() | ||
@Suppress("unused") | ||
abstract class BytecodePatch : Patch<BytecodeContext> { | ||
/** | ||
* The fingerprints to resolve before executing the patch. | ||
*/ | ||
internal val fingerprints: Set<MethodFingerprint> | ||
|
||
/** | ||
* Create a new [BytecodePatch]. | ||
* | ||
* @param fingerprints The fingerprints to resolve before executing the patch. | ||
*/ | ||
constructor(fingerprints: Set<MethodFingerprint> = emptySet()) { | ||
this.fingerprints = fingerprints | ||
} | ||
|
||
/** | ||
* Create a new [BytecodePatch]. | ||
* | ||
* @param name The name of the patch. | ||
* @param description The description of the patch. | ||
* @param compatiblePackages The packages the patch is compatible with. | ||
* @param dependencies Other patches this patch depends on. | ||
* @param use Weather or not the patch should be used. | ||
* @param requiresIntegrations Weather or not the patch requires integrations. | ||
*/ | ||
constructor( | ||
name: String? = null, | ||
description: String? = null, | ||
compatiblePackages: Set<CompatiblePackage>? = null, | ||
dependencies: Set<PatchClass>? = null, | ||
use: Boolean = true, | ||
requiresIntegrations: Boolean = false, | ||
fingerprints: Set<MethodFingerprint> = emptySet(), | ||
) : super(name, description, compatiblePackages, dependencies, use, requiresIntegrations) { | ||
this.fingerprints = fingerprints | ||
} | ||
|
||
/** | ||
* Create a new [BytecodePatch]. | ||
*/ | ||
@Deprecated( | ||
"Use the constructor with fingerprints instead.", | ||
ReplaceWith("BytecodePatch(emptySet())"), | ||
) | ||
constructor() : this(emptySet()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 32 additions & 2 deletions
34
src/main/kotlin/app/revanced/patcher/patch/ResourcePatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
package app.revanced.patcher.patch | ||
|
||
import app.revanced.patcher.PatchClass | ||
import app.revanced.patcher.Patcher | ||
import app.revanced.patcher.data.ResourceContext | ||
import java.io.Closeable | ||
|
||
/** | ||
* A ReVanced [Patch] that works on [ResourceContext]. | ||
* A ReVanced [Patch] that accesses a [ResourceContext]. | ||
* | ||
* If an implementation of [Patch] also implements [Closeable] | ||
* it will be closed in reverse execution order of patches executed by ReVanced [Patcher]. | ||
*/ | ||
abstract class ResourcePatch : Patch<ResourceContext>() | ||
abstract class ResourcePatch : Patch<ResourceContext> { | ||
/** | ||
* Create a new [ResourcePatch]. | ||
*/ | ||
constructor() | ||
|
||
/** | ||
* Create a new [ResourcePatch]. | ||
* | ||
* @param name The name of the patch. | ||
* @param description The description of the patch. | ||
* @param compatiblePackages The packages the patch is compatible with. | ||
* @param dependencies Other patches this patch depends on. | ||
* @param use Weather or not the patch should be used. | ||
* @param requiresIntegrations Weather or not the patch requires integrations. | ||
*/ | ||
constructor( | ||
name: String? = null, | ||
description: String? = null, | ||
compatiblePackages: Set<CompatiblePackage>? = null, | ||
dependencies: Set<PatchClass>? = null, | ||
use: Boolean = true, | ||
requiresIntegrations: Boolean = false, | ||
) : super(name, description, compatiblePackages, dependencies, use, requiresIntegrations) | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/kotlin/app/revanced/patcher/patch/PatchInitializationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package app.revanced.patcher.patch | ||
|
||
import app.revanced.patcher.data.ResourceContext | ||
import org.junit.jupiter.api.assertThrows | ||
import kotlin.test.Test | ||
import app.revanced.patcher.patch.annotation.Patch as PatchAnnotation | ||
|
||
object PatchInitializationTest { | ||
@Test | ||
fun `initialize using constructor`() { | ||
val patch = | ||
object : ResourcePatch(name = "Resource patch test") { | ||
override fun execute(context: ResourceContext) {} | ||
} | ||
|
||
assert(patch.name == "Resource patch test") | ||
} | ||
|
||
@Test | ||
fun `initialize using annotation`() { | ||
val patch = | ||
@PatchAnnotation("Resource patch test") | ||
object : ResourcePatch() { | ||
override fun execute(context: ResourceContext) {} | ||
} | ||
|
||
assert(patch.name == "Resource patch test") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters