|
| 1 | +package app.revanced.patches.shared.misc.react |
| 2 | + |
| 3 | +import app.revanced.patcher.PatchClass |
| 4 | +import app.revanced.patcher.data.BytecodeContext |
| 5 | +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions |
| 6 | +import app.revanced.patcher.fingerprint.MethodFingerprint |
| 7 | +import app.revanced.patcher.patch.BytecodePatch |
| 8 | +import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable |
| 9 | +import app.revanced.patches.shared.misc.react.fingerprints.LoadScriptFromAssetsFingerprint |
| 10 | +import app.revanced.patches.shared.misc.react.fingerprints.LoadScriptFromFileFingerprint |
| 11 | +import app.revanced.util.resultOrThrow |
| 12 | + |
| 13 | +abstract class BaseReactPreloadScriptBootstrapperPatch( |
| 14 | + name: String? = null, |
| 15 | + description: String? = null, |
| 16 | + compatiblePackages: Set<CompatiblePackage>? = null, |
| 17 | + dependencies: Set<PatchClass>? = null, |
| 18 | + use: Boolean = true, |
| 19 | + fingerprints: Set<MethodFingerprint> = emptySet(), |
| 20 | + private val mainActivityOnCreateFingerprintInsertIndexPair: Pair<MethodFingerprint, Int>, |
| 21 | +) : BytecodePatch( |
| 22 | + name = name, |
| 23 | + description = description, |
| 24 | + compatiblePackages = compatiblePackages, |
| 25 | + dependencies = dependencies, |
| 26 | + use = use, |
| 27 | + requiresIntegrations = true, |
| 28 | + fingerprints = setOf( |
| 29 | + LoadScriptFromAssetsFingerprint, |
| 30 | + LoadScriptFromFileFingerprint, |
| 31 | + mainActivityOnCreateFingerprintInsertIndexPair.first, |
| 32 | + ) + fingerprints, |
| 33 | +) { |
| 34 | + abstract val integrationsClassDescriptor: String |
| 35 | + |
| 36 | + override fun execute(context: BytecodeContext) { |
| 37 | + val (mainActivityOnCreateFingerprint, insertIndex) = mainActivityOnCreateFingerprintInsertIndexPair |
| 38 | + val loadScriptFromAssetMethod = LoadScriptFromAssetsFingerprint.resultOrThrow().mutableMethod |
| 39 | + val (catalystInstanceImplClassDef, loadScriptFromFileMethod) = LoadScriptFromFileFingerprint.resultOrThrow() |
| 40 | + .let { it.mutableClass to it.mutableMethod } |
| 41 | + |
| 42 | + // Create preload script on main activity creation. |
| 43 | + mainActivityOnCreateFingerprint.resultOrThrow().mutableMethod.addInstructions( |
| 44 | + insertIndex, // Skip super call. |
| 45 | + "invoke-static { p0 }, " + |
| 46 | + "$integrationsClassDescriptor->hookOnCreate(Landroid/app/Activity;)V", |
| 47 | + ) |
| 48 | + |
| 49 | + // Copy of loadScriptFromFile method acts as a preload script loader. |
| 50 | + loadScriptFromFileMethod.toMutable().apply { |
| 51 | + name = "loadPreloadScriptFromFile" |
| 52 | + }.let(catalystInstanceImplClassDef.methods::add) |
| 53 | + |
| 54 | + // Load preload script. |
| 55 | + listOf(loadScriptFromFileMethod, loadScriptFromAssetMethod).forEach { loadScriptMethod -> |
| 56 | + loadScriptMethod.addInstructions( |
| 57 | + 0, |
| 58 | + "invoke-static { v0 }, $integrationsClassDescriptor->" + |
| 59 | + "hookLoadScriptFromFile($catalystInstanceImplClassDef)V", |
| 60 | + ) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments