|
| 1 | +/* |
| 2 | + * Forked from: |
| 3 | + * https://github.com/ReVanced/revanced-patches/blob/6b06b9d1328b971a06d10b4247f4c10f050e4f61/patches/src/main/kotlin/app/revanced/patches/all/misc/build/BaseSpoofBuildInfoPatch.kt |
| 4 | + */ |
| 5 | +package app.morphe.patches.all.misc.build |
| 6 | + |
| 7 | +import app.morphe.patcher.extensions.InstructionExtensions.getInstruction |
| 8 | +import app.morphe.patcher.extensions.InstructionExtensions.replaceInstruction |
| 9 | +import app.morphe.patcher.patch.bytecodePatch |
| 10 | +import app.morphe.patches.all.misc.transformation.transformInstructionsPatch |
| 11 | +import app.morphe.util.getReference |
| 12 | +import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction |
| 13 | +import com.android.tools.smali.dexlib2.iface.reference.FieldReference |
| 14 | + |
| 15 | +private const val BUILD_CLASS_DESCRIPTOR = "Landroid/os/Build;" |
| 16 | + |
| 17 | +fun baseSpoofBuildInfoPatch(buildInfoSupplier: () -> BuildInfo) = bytecodePatch { |
| 18 | + // Lazy, so that patch options above are initialized before they are accessed. |
| 19 | + val replacements by lazy { |
| 20 | + with(buildInfoSupplier()) { |
| 21 | + buildMap { |
| 22 | + if (board != null) put("BOARD", "const-string" to "\"$board\"") |
| 23 | + if (bootloader != null) put("BOOTLOADER", "const-string" to "\"$bootloader\"") |
| 24 | + if (brand != null) put("BRAND", "const-string" to "\"$brand\"") |
| 25 | + if (cpuAbi != null) put("CPU_ABI", "const-string" to "\"$cpuAbi\"") |
| 26 | + if (cpuAbi2 != null) put("CPU_ABI2", "const-string" to "\"$cpuAbi2\"") |
| 27 | + if (device != null) put("DEVICE", "const-string" to "\"$device\"") |
| 28 | + if (display != null) put("DISPLAY", "const-string" to "\"$display\"") |
| 29 | + if (fingerprint != null) put("FINGERPRINT", "const-string" to "\"$fingerprint\"") |
| 30 | + if (hardware != null) put("HARDWARE", "const-string" to "\"$hardware\"") |
| 31 | + if (host != null) put("HOST", "const-string" to "\"$host\"") |
| 32 | + if (id != null) put("ID", "const-string" to "\"$id\"") |
| 33 | + if (manufacturer != null) put("MANUFACTURER", "const-string" to "\"$manufacturer\"") |
| 34 | + if (model != null) put("MODEL", "const-string" to "\"$model\"") |
| 35 | + if (odmSku != null) put("ODM_SKU", "const-string" to "\"$odmSku\"") |
| 36 | + if (product != null) put("PRODUCT", "const-string" to "\"$product\"") |
| 37 | + if (radio != null) put("RADIO", "const-string" to "\"$radio\"") |
| 38 | + if (serial != null) put("SERIAL", "const-string" to "\"$serial\"") |
| 39 | + if (sku != null) put("SKU", "const-string" to "\"$sku\"") |
| 40 | + if (socManufacturer != null) put("SOC_MANUFACTURER", "const-string" to "\"$socManufacturer\"") |
| 41 | + if (socModel != null) put("SOC_MODEL", "const-string" to "\"$socModel\"") |
| 42 | + if (tags != null) put("TAGS", "const-string" to "\"$tags\"") |
| 43 | + if (time != null) put("TIME", "const-wide" to "$time") |
| 44 | + if (type != null) put("TYPE", "const-string" to "\"$type\"") |
| 45 | + if (user != null) put("USER", "const-string" to "\"$user\"") |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + dependsOn( |
| 51 | + transformInstructionsPatch( |
| 52 | + filterMap = filterMap@{ _, _, instruction, instructionIndex -> |
| 53 | + val reference = instruction.getReference<FieldReference>() ?: return@filterMap null |
| 54 | + if (reference.definingClass != BUILD_CLASS_DESCRIPTOR) return@filterMap null |
| 55 | + |
| 56 | + return@filterMap replacements[reference.name]?.let { instructionIndex to it } |
| 57 | + }, |
| 58 | + transform = { mutableMethod, entry -> |
| 59 | + val (index, replacement) = entry |
| 60 | + val (opcode, operand) = replacement |
| 61 | + val register = mutableMethod.getInstruction<OneRegisterInstruction>(index).registerA |
| 62 | + |
| 63 | + mutableMethod.replaceInstruction(index, "$opcode v$register, $operand") |
| 64 | + }, |
| 65 | + ), |
| 66 | + ) |
| 67 | +} |
0 commit comments