feat: SincePatcher annotation

This commit is contained in:
Sculas 2022-09-08 14:41:42 +02:00
parent 6e73631d4d
commit 25f74dc5e9
No known key found for this signature in database
GPG key ID: 1530BFF96D1EEB89
7 changed files with 69 additions and 1 deletions

View file

@ -37,6 +37,9 @@ tasks {
events("PASSED", "SKIPPED", "FAILED")
}
}
processResources {
expand("projectVersion" to project.version)
}
}
java {

View file

@ -6,6 +6,7 @@ import app.revanced.patcher.data.impl.findIndexed
import app.revanced.patcher.extensions.PatchExtensions.dependencies
import app.revanced.patcher.extensions.PatchExtensions.deprecated
import app.revanced.patcher.extensions.PatchExtensions.patchName
import app.revanced.patcher.extensions.PatchExtensions.sincePatcherVersion
import app.revanced.patcher.extensions.nullOutputStream
import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils.resolve
import app.revanced.patcher.patch.Patch
@ -15,6 +16,7 @@ import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.impl.BytecodePatch
import app.revanced.patcher.patch.impl.ResourcePatch
import app.revanced.patcher.util.ListBackedSet
import app.revanced.patcher.util.VersionReader
import brut.androlib.Androlib
import brut.androlib.meta.UsesFramework
import brut.androlib.options.BuildOptions
@ -36,7 +38,8 @@ import java.io.Closeable
import java.io.File
import java.nio.file.Files
val NAMER = BasicDexFileNamer()
private val NAMER = BasicDexFileNamer()
private val VERSION = VersionReader.read()
/**
* The ReVanced Patcher.
@ -244,6 +247,15 @@ class Patcher(private val options: PatcherOptions) {
* @param patches [Patch]es The patches to add.
*/
fun addPatches(patches: Iterable<Class<out Patch<Data>>>) {
for (patch in patches) {
val needsVersion = patch.sincePatcherVersion
if (needsVersion != null && needsVersion > VERSION) {
logger.error("Patch '${patch.patchName}' requires Patcher version $needsVersion or higher")
logger.error("Current Patcher version is $VERSION")
logger.warn("Skipping '${patch.patchName}'!")
continue // TODO: continue or halt/throw?
}
}
data.patches.addAll(patches)
}

View file

@ -0,0 +1,13 @@
package app.revanced.patcher.annotation
import app.revanced.patcher.patch.Patch
import app.revanced.patcher.Patcher
/**
* Declares a [Patch] deprecated for removal.
* @param version The minimum version of the [Patcher] this [Patch] supports.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class SincePatcher(val version: String)

View file

@ -58,6 +58,7 @@ object PatchExtensions {
if (cl == Patch::class) null else cl
}
}
val Class<out Patch<Data>>.sincePatcherVersion get() = recursiveAnnotation(SincePatcher::class)?.version
@JvmStatic
fun Class<out Patch<Data>>.dependsOn(patch: Class<out Patch<Data>>): Boolean {

View file

@ -0,0 +1,18 @@
package app.revanced.patcher.util
import java.util.*
internal object VersionReader {
@JvmStatic
private val props = Properties().apply {
load(
VersionReader::class.java.getResourceAsStream("/revanced-patcher/version.properties")
?: throw IllegalStateException("Could not load version.properties")
)
}
@JvmStatic
fun read(): String {
return props.getProperty("version") ?: throw IllegalStateException("Version not found")
}
}

View file

@ -0,0 +1 @@
version=${projectVersion}

View file

@ -0,0 +1,20 @@
package app.revanced.patcher.util
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
internal class VersionReaderTest {
@Test
fun read() {
val version = VersionReader.read()
assertNotNull(version)
assertTrue(version.isNotEmpty())
val parts = version.split(".")
assertEquals(3, parts.size)
parts.forEach {
assertTrue(it.toInt() >= 0)
}
println(version)
}
}