From 8ab86312bf494e0eda323f72f18093de85d2b138 Mon Sep 17 00:00:00 2001 From: Lucaskyy Date: Thu, 17 Mar 2022 20:04:06 +0100 Subject: [PATCH] Refactor code, add YTVersion, ElementType and signatures for 17.03.38 "signatures" package will be moved to Signatures repository soon. --- .../kotlin/net/revanced/patcher/Patcher.kt | 39 ++++++++++--------- .../patcher/signatures/ElementType.kt | 15 +++++++ .../Sig.kt => signatures/Signature.kt} | 24 +++++++++--- .../patcher/signatures/SignatureSupplier.kt | 5 +++ .../patcher/signatures/v17_03_38/Sigs.kt | 34 ++++++++++++++++ .../{SigScanner.kt => SignatureScanner.kt} | 3 +- .../net/revanced/patcher/version/YTVersion.kt | 26 +++++++++++++ 7 files changed, 121 insertions(+), 25 deletions(-) create mode 100644 src/main/kotlin/net/revanced/patcher/signatures/ElementType.kt rename src/main/kotlin/net/revanced/patcher/{sigscan/Sig.kt => signatures/Signature.kt} (57%) create mode 100644 src/main/kotlin/net/revanced/patcher/signatures/SignatureSupplier.kt create mode 100644 src/main/kotlin/net/revanced/patcher/signatures/v17_03_38/Sigs.kt rename src/main/kotlin/net/revanced/patcher/sigscan/{SigScanner.kt => SignatureScanner.kt} (56%) create mode 100644 src/main/kotlin/net/revanced/patcher/version/YTVersion.kt diff --git a/src/main/kotlin/net/revanced/patcher/Patcher.kt b/src/main/kotlin/net/revanced/patcher/Patcher.kt index c4a8579..e823b6e 100644 --- a/src/main/kotlin/net/revanced/patcher/Patcher.kt +++ b/src/main/kotlin/net/revanced/patcher/Patcher.kt @@ -1,25 +1,26 @@ package net.revanced.patcher -import net.revanced.patcher.sigscan.Sig -import net.revanced.patcher.sigscan.SigScanner -import org.jf.dexlib2.Opcode +import net.revanced.patcher.version.YTVersion +import org.jf.dexlib2.DexFileFactory import java.io.File -import java.lang.reflect.Modifier -class Patcher { - companion object { - /** - * Invokes the patcher on the given input file. - * - * @param input the input file - * @param output the output file - */ - fun invoke(input: File, output: File) { - SigScanner(Sig( - arrayOf(Opcode.ADD_INT), - Modifier.PUBLIC or Modifier.STATIC, - String.Companion::class - )).scan(emptyArray()) - } +/** + * Creates a patcher. + * + * @param input the input dex file + * @param output the output dex file + * @param version the YT version of this dex file + */ +class Patcher(private val input: File, private val output: File, private val version: YTVersion) { + // setting opcodes to null causes it to autodetect, perfect! + private val dexFile = DexFileFactory.loadDexFile(input, null) + + /** + * Runs the patcher. + */ + fun run() { + } } + + diff --git a/src/main/kotlin/net/revanced/patcher/signatures/ElementType.kt b/src/main/kotlin/net/revanced/patcher/signatures/ElementType.kt new file mode 100644 index 0000000..27fa9d3 --- /dev/null +++ b/src/main/kotlin/net/revanced/patcher/signatures/ElementType.kt @@ -0,0 +1,15 @@ +package net.revanced.patcher.signatures + +class ElementType private constructor() { + companion object { + const val Void = "()V" + const val Boolean = "()Z" + const val Byte = "()B" + const val Char = "()C" + const val Short = "()S" + const val Int = "()I" + const val Long = "()J" + const val Float = "()F" + const val Double = "()D" + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/revanced/patcher/sigscan/Sig.kt b/src/main/kotlin/net/revanced/patcher/signatures/Signature.kt similarity index 57% rename from src/main/kotlin/net/revanced/patcher/sigscan/Sig.kt rename to src/main/kotlin/net/revanced/patcher/signatures/Signature.kt index 3d570e8..603093e 100644 --- a/src/main/kotlin/net/revanced/patcher/sigscan/Sig.kt +++ b/src/main/kotlin/net/revanced/patcher/signatures/Signature.kt @@ -1,18 +1,32 @@ -package net.revanced.patcher.sigscan +package net.revanced.patcher.signatures import org.jf.dexlib2.Opcode -import kotlin.reflect.KClass -data class Sig( +/** + * An ASM signature. + * + * ``` + * Signature( + * arrayOf(Opcode.ADD_INT), + * Modifier.PUBLIC or Modifier.STATIC, + * "Ljava/lang/String;" + * ) + * ``` + * + * @param opcodes the opcode signature + * @param attributes the modifiers of the method you are searching for + * @param returnType the return type of the method as string, see: https://stackoverflow.com/a/9909370 + */ +data class Signature( val opcodes: Array, val attributes: Int, - val returnType: KClass<*> + val returnType: String ) { override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false - other as Sig + other as Signature if (!opcodes.contentEquals(other.opcodes)) return false if (attributes != other.attributes) return false diff --git a/src/main/kotlin/net/revanced/patcher/signatures/SignatureSupplier.kt b/src/main/kotlin/net/revanced/patcher/signatures/SignatureSupplier.kt new file mode 100644 index 0000000..f42db46 --- /dev/null +++ b/src/main/kotlin/net/revanced/patcher/signatures/SignatureSupplier.kt @@ -0,0 +1,5 @@ +package net.revanced.patcher.signatures + +interface SignatureSupplier { + fun signatures(): Array +} \ No newline at end of file diff --git a/src/main/kotlin/net/revanced/patcher/signatures/v17_03_38/Sigs.kt b/src/main/kotlin/net/revanced/patcher/signatures/v17_03_38/Sigs.kt new file mode 100644 index 0000000..f39caf4 --- /dev/null +++ b/src/main/kotlin/net/revanced/patcher/signatures/v17_03_38/Sigs.kt @@ -0,0 +1,34 @@ +package net.revanced.patcher.signatures.v17_03_38 + +import net.revanced.patcher.signatures.SignatureSupplier +import net.revanced.patcher.signatures.ElementType +import net.revanced.patcher.signatures.Signature +import java.lang.reflect.Modifier + +import org.jf.dexlib2.Opcode.* + +class Sigs: SignatureSupplier { + override fun signatures(): Array { + return arrayOf( + // public static aT(Landroid/content/Context;I)Z + Signature( + arrayOf( + IF_LT, // if-lt p0, p1, :cond_1 + CONST_4, // const/4 p0, 0x1 + // TODO(Inject): + // invoke-static {p0}, Lfi/razerman/youtube/XGlobals;->getOverride(Z)Z + // move-result p0 + RETURN, // return p0 + // :cond_1 + CONST_4, // const/4 p0, 0x0 + // TODO(Inject): + // invoke-static {p0}, Lfi/razerman/youtube/XGlobals;->getOverride(Z)Z + // move-result p0 + RETURN, // return p0 + ), + Modifier.PUBLIC or Modifier.STATIC, + ElementType.Boolean + ) + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/revanced/patcher/sigscan/SigScanner.kt b/src/main/kotlin/net/revanced/patcher/sigscan/SignatureScanner.kt similarity index 56% rename from src/main/kotlin/net/revanced/patcher/sigscan/SigScanner.kt rename to src/main/kotlin/net/revanced/patcher/sigscan/SignatureScanner.kt index 505c361..c8eed28 100644 --- a/src/main/kotlin/net/revanced/patcher/sigscan/SigScanner.kt +++ b/src/main/kotlin/net/revanced/patcher/sigscan/SignatureScanner.kt @@ -1,8 +1,9 @@ package net.revanced.patcher.sigscan +import net.revanced.patcher.signatures.Signature import org.jf.dexlib2.iface.ClassDef -class SigScanner (sig: Sig) { +class SignatureScanner(signature: Signature) { fun scan(classes: Array) { } diff --git a/src/main/kotlin/net/revanced/patcher/version/YTVersion.kt b/src/main/kotlin/net/revanced/patcher/version/YTVersion.kt new file mode 100644 index 0000000..bf96244 --- /dev/null +++ b/src/main/kotlin/net/revanced/patcher/version/YTVersion.kt @@ -0,0 +1,26 @@ +package net.revanced.patcher.version + +import net.revanced.patcher.signatures.SignatureSupplier +import net.revanced.patcher.signatures.v17_03_38.Sigs + +enum class YTVersion( + val versionNumber: Triple, + val sigs: SignatureSupplier +) { + V17_03_38( + Triple(17, 3, 38), + Sigs() + ); + + companion object { + private val vm: Map, YTVersion> = buildMap { + values().forEach { + this[it.versionNumber] = it + } + } + + fun versionFor(versionNumber: Triple): YTVersion? { + return vm[versionNumber] + } + } +} \ No newline at end of file