Skip to content

Commit

Permalink
feat: utility functions to get metadata of patch & sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sculas authored and oSumAtrIX committed Jun 5, 2022
1 parent 94f3c91 commit 54511a4
Showing 1 changed file with 32 additions and 0 deletions.
@@ -0,0 +1,32 @@
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.patch.base.Patch
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.signature.implementation.method.annotation.FuzzyPatternScanMethod
import app.revanced.patcher.signature.implementation.method.annotation.MatchingMethod

private inline fun <reified T : Annotation> Any.firstAnnotation() =
this::class.annotations.first { it is T } as T

private inline fun <reified T : Annotation> Any.recursiveAnnotation() =
this::class.java.findAnnotationRecursively(T::class.java)!!

object PatchExtensions {
val Patch<*>.name get() = firstAnnotation<Name>().name
val Patch<*>.version get() = firstAnnotation<Version>().version
val Patch<*>.description get() = firstAnnotation<Description>().description
val Patch<*>.compatiblePackages get() = recursiveAnnotation<Compatibility>().compatiblePackages
}

object MethodSignatureExtensions {
val MethodSignature.name get() = firstAnnotation<Name>().name
val MethodSignature.version get() = firstAnnotation<Version>().version
val MethodSignature.description get() = firstAnnotation<Description>().description
val MethodSignature.compatiblePackages get() = recursiveAnnotation<Compatibility>().compatiblePackages
val MethodSignature.matchingMethod get() = firstAnnotation<MatchingMethod>()
val MethodSignature.fuzzyThreshold get() = firstAnnotation<FuzzyPatternScanMethod>().threshold
}

0 comments on commit 54511a4

Please sign in to comment.