diff --git a/src/main/kotlin/app/revanced/patcher/extensions/AnnotationExtensions.kt b/src/main/kotlin/app/revanced/patcher/extensions/AnnotationExtensions.kt index 21836e5..290174c 100644 --- a/src/main/kotlin/app/revanced/patcher/extensions/AnnotationExtensions.kt +++ b/src/main/kotlin/app/revanced/patcher/extensions/AnnotationExtensions.kt @@ -1,83 +1,33 @@ package app.revanced.patcher.extensions -import app.revanced.patcher.annotation.* -import app.revanced.patcher.data.Context -import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod -import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint -import app.revanced.patcher.patch.OptionsContainer -import app.revanced.patcher.patch.Patch -import app.revanced.patcher.patch.PatchOptions -import app.revanced.patcher.patch.annotations.DependsOn -import app.revanced.patcher.patch.annotations.RequiresIntegrations import kotlin.reflect.KClass -import kotlin.reflect.KVisibility -import kotlin.reflect.full.companionObject -import kotlin.reflect.full.companionObjectInstance -/** - * Recursively find a given annotation on a class. - * @param targetAnnotation The annotation to find. - * @return The annotation. - */ -private fun Class<*>.findAnnotationRecursively(targetAnnotation: KClass) = - this.findAnnotationRecursively(targetAnnotation.java, mutableSetOf()) +internal object AnnotationExtensions { + /** + * Recursively find a given annotation on a class. + * + * @param targetAnnotation The annotation to find. + * @return The annotation. + */ + fun Class<*>.findAnnotationRecursively(targetAnnotation: KClass): T? { + fun Class<*>.findAnnotationRecursively( + targetAnnotation: Class, traversed: MutableSet + ): T? { + val found = this.annotations.firstOrNull { it.annotationClass.java.name == targetAnnotation.name } + @Suppress("UNCHECKED_CAST") if (found != null) return found as T -private fun Class<*>.findAnnotationRecursively( - targetAnnotation: Class, traversed: MutableSet -): T? { - val found = this.annotations.firstOrNull { it.annotationClass.java.name == targetAnnotation.name } + for (annotation in this.annotations) { + if (traversed.contains(annotation)) continue + traversed.add(annotation) - @Suppress("UNCHECKED_CAST") if (found != null) return found as T - - for (annotation in this.annotations) { - if (traversed.contains(annotation)) continue - traversed.add(annotation) - - return (annotation.annotationClass.java.findAnnotationRecursively(targetAnnotation, traversed)) ?: continue - } - - return null -} - -object PatchExtensions { - val Class>.patchName: String - get() = findAnnotationRecursively(Name::class)?.name ?: this.simpleName - - val Class>.version - get() = findAnnotationRecursively(Version::class)?.version - - val Class>.include - get() = findAnnotationRecursively(app.revanced.patcher.patch.annotations.Patch::class)!!.include - - val Class>.description - get() = findAnnotationRecursively(Description::class)?.description - - val Class>.dependencies - get() = findAnnotationRecursively(DependsOn::class)?.dependencies - - val Class>.compatiblePackages - get() = findAnnotationRecursively(Compatibility::class)?.compatiblePackages - - internal val Class>.requiresIntegrations - get() = findAnnotationRecursively(RequiresIntegrations::class) != null - - val Class>.options: PatchOptions? - get() = kotlin.companionObject?.let { cl -> - if (cl.visibility != KVisibility.PUBLIC) return null - kotlin.companionObjectInstance?.let { - (it as? OptionsContainer)?.options + return (annotation.annotationClass.java.findAnnotationRecursively(targetAnnotation, traversed)) + ?: continue } + + return null } -} -object MethodFingerprintExtensions { - val MethodFingerprint.name: String - get() = this.javaClass.simpleName - - val MethodFingerprint.fuzzyPatternScanMethod - get() = javaClass.findAnnotationRecursively(FuzzyPatternScanMethod::class) - - val MethodFingerprint.fuzzyScanThreshold - get() = fuzzyPatternScanMethod?.threshold ?: 0 + return this.findAnnotationRecursively(targetAnnotation.java, mutableSetOf()) + } } \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patcher/extensions/MethodFingerprintExtensions.kt b/src/main/kotlin/app/revanced/patcher/extensions/MethodFingerprintExtensions.kt new file mode 100644 index 0000000..1901044 --- /dev/null +++ b/src/main/kotlin/app/revanced/patcher/extensions/MethodFingerprintExtensions.kt @@ -0,0 +1,20 @@ +package app.revanced.patcher.extensions + +import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively +import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint + +object MethodFingerprintExtensions { + + /** + * The name of a [MethodFingerprint]. + */ + val MethodFingerprint.name: String + get() = this.javaClass.simpleName + + /** + * The [FuzzyPatternScanMethod] annotation of a [MethodFingerprint]. + */ + val MethodFingerprint.fuzzyPatternScanMethod + get() = javaClass.findAnnotationRecursively(FuzzyPatternScanMethod::class) +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patcher/extensions/PatchExtensions.kt b/src/main/kotlin/app/revanced/patcher/extensions/PatchExtensions.kt new file mode 100644 index 0000000..06f2224 --- /dev/null +++ b/src/main/kotlin/app/revanced/patcher/extensions/PatchExtensions.kt @@ -0,0 +1,71 @@ +package app.revanced.patcher.extensions + +import app.revanced.patcher.annotation.Compatibility +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.Context +import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively +import app.revanced.patcher.patch.OptionsContainer +import app.revanced.patcher.patch.Patch +import app.revanced.patcher.patch.PatchOptions +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.RequiresIntegrations +import kotlin.reflect.KVisibility +import kotlin.reflect.full.companionObject +import kotlin.reflect.full.companionObjectInstance + +object PatchExtensions { + /** + * The name of a [Patch]. + */ + val Class>.patchName: String + get() = findAnnotationRecursively(Name::class)?.name ?: this.simpleName + + /** + * The version of a [Patch]. + */ + val Class>.version + get() = findAnnotationRecursively(Version::class)?.version + + /** + * Weather or not a [Patch] should be included. + */ + val Class>.include + get() = findAnnotationRecursively(app.revanced.patcher.patch.annotations.Patch::class)!!.include + + /** + * The description of a [Patch]. + */ + val Class>.description + get() = findAnnotationRecursively(Description::class)?.description + + /** + * The dependencies of a [Patch]. + */ + val Class>.dependencies + get() = findAnnotationRecursively(DependsOn::class)?.dependencies + + /** + * The packages a [Patch] is compatible with. + */ + val Class>.compatiblePackages + get() = findAnnotationRecursively(Compatibility::class)?.compatiblePackages + + /** + * Weather or not a [Patch] requires integrations. + */ + internal val Class>.requiresIntegrations + get() = findAnnotationRecursively(RequiresIntegrations::class) != null + + /** + * The options of a [Patch]. + */ + val Class>.options: PatchOptions? + get() = kotlin.companionObject?.let { cl -> + if (cl.visibility != KVisibility.PUBLIC) return null + kotlin.companionObjectInstance?.let { + (it as? OptionsContainer)?.options + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt b/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt index c5fd920..1a7bbf3 100644 --- a/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt +++ b/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt @@ -2,7 +2,6 @@ package app.revanced.patcher.fingerprint.method.impl import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.MethodFingerprintExtensions.fuzzyPatternScanMethod -import app.revanced.patcher.extensions.MethodFingerprintExtensions.fuzzyScanThreshold import app.revanced.patcher.fingerprint.Fingerprint import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod import app.revanced.patcher.util.proxy.ClassProxy @@ -165,7 +164,7 @@ abstract class MethodFingerprint( fingerprint: MethodFingerprint ): MethodFingerprintResult.MethodFingerprintScanResult.PatternScanResult? { val instructions = this.implementation!!.instructions - val fingerprintFuzzyPatternScanThreshold = fingerprint.fuzzyScanThreshold + val fingerprintFuzzyPatternScanThreshold = fingerprint.fuzzyPatternScanMethod?.threshold ?: 0 val pattern = fingerprint.opcodes!! val instructionLength = instructions.count()