generated from ReVanced/revanced-patches-template
-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/kotlin/app/revanced/patches/openinghours/crash/CrashPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package app.revanced.patches.openinghours.crash | ||
|
||
import app.revanced.patcher.data.BytecodeContext | ||
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels | ||
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction | ||
import app.revanced.patcher.extensions.InstructionExtensions.removeInstructions | ||
import app.revanced.patcher.patch.BytecodePatch | ||
import app.revanced.patcher.patch.annotation.CompatiblePackage | ||
import app.revanced.patcher.patch.annotation.Patch | ||
import app.revanced.patcher.util.smali.ExternalLabel | ||
import app.revanced.patches.openinghours.crash.fingerprints.SetPlaceFingerprint | ||
import app.revanced.util.exception | ||
import com.android.tools.smali.dexlib2.builder.BuilderInstruction | ||
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction | ||
import com.android.tools.smali.dexlib2.iface.reference.MethodReference | ||
|
||
@Patch( | ||
name = "Fix crash", | ||
compatiblePackages = [CompatiblePackage("de.simon.openinghours", ["1.0"])] | ||
) | ||
@Suppress("unused") | ||
object CrashPatch : BytecodePatch(setOf(SetPlaceFingerprint)) { | ||
|
||
override fun execute(context: BytecodeContext) { | ||
with(SetPlaceFingerprint.result ?: throw SetPlaceFingerprint.exception) { | ||
val indexedInstructions = mutableMethod.implementation!!.instructions.withIndex().toList() | ||
val getOpeningHoursIndex = getIndicesOfInvoke(indexedInstructions, "Lde/simon/openinghours/models/Place;", "getOpeningHours") | ||
val setWeekDayTextIndex = getIndicesOfInvoke(indexedInstructions, "Lde/simon/openinghours/views/custom/PlaceCard;", "setWeekDayText")[0] | ||
val startCalculateStatusIndex = getIndicesOfInvoke(indexedInstructions, "Lde/simon/openinghours/views/custom/PlaceCard;", "startCalculateStatus")[0] | ||
val getOpeningHoursIndex1 = getOpeningHoursIndex[0] | ||
val getOpeningHoursIndex2 = getOpeningHoursIndex[1] | ||
val continueLabel2 = ExternalLabel("continue_1", mutableMethod.getInstruction(startCalculateStatusIndex + 1)) | ||
|
||
// We remove the old instructions which cause NullPointerExceptions and replace it | ||
// with the same instructions but use explicit null checks and jump to the newly | ||
// added label continue_1 if the null check returns true. | ||
mutableMethod.removeInstructions(getOpeningHoursIndex2, startCalculateStatusIndex - getOpeningHoursIndex2 + 1) | ||
mutableMethod.addInstructionsWithLabels( | ||
getOpeningHoursIndex2, | ||
""" | ||
invoke-virtual {p1}, Lde/simon/openinghours/models/Place;->getOpeningHours()Lde/simon/openinghours/models/OpeningHours; | ||
move-result-object p1 | ||
if-eqz p1, :continue_1 | ||
invoke-virtual {p1}, Lde/simon/openinghours/models/OpeningHours;->getPeriods()Lio/realm/RealmList; | ||
move-result-object p1 | ||
if-eqz p1, :continue_1 | ||
check-cast p1, Ljava/util/List; | ||
invoke-direct {p0, p1}, Lde/simon/openinghours/views/custom/PlaceCard;->startCalculateStatus(Ljava/util/List;)V | ||
""", | ||
continueLabel2, | ||
) | ||
|
||
val continueLabel1 = ExternalLabel("continue_0", mutableMethod.getInstruction(setWeekDayTextIndex + 1)) | ||
|
||
// These instructions happen before the above instructions, but because of the | ||
// changing indices for the instructions it was important to have the later | ||
// instructions modified first. | ||
// | ||
// Again, we only added null checks and jump to label continue_0 if they return true. | ||
mutableMethod.removeInstructions(getOpeningHoursIndex1, setWeekDayTextIndex - getOpeningHoursIndex1 + 1) | ||
mutableMethod.addInstructionsWithLabels( | ||
getOpeningHoursIndex1, | ||
""" | ||
invoke-virtual {p1}, Lde/simon/openinghours/models/Place;->getOpeningHours()Lde/simon/openinghours/models/OpeningHours; | ||
move-result-object v0 | ||
if-eqz v0, :continue_0 | ||
invoke-virtual {v0}, Lde/simon/openinghours/models/OpeningHours;->getWeekdayText()Lio/realm/RealmList; | ||
move-result-object v0 | ||
if-eqz v0, :continue_0 | ||
check-cast v0, Ljava/util/List; | ||
invoke-direct {p0, v0}, Lde/simon/openinghours/views/custom/PlaceCard;->setWeekDayText(Ljava/util/List;)V | ||
""", | ||
continueLabel1, | ||
) | ||
} | ||
} | ||
|
||
private fun getIndicesOfInvoke( | ||
instructions: List<IndexedValue<BuilderInstruction>>, | ||
className: String, | ||
methodName: String, | ||
): List<Int> = instructions.mapNotNull { (index, instruction) -> | ||
val invokeInstruction = instruction as? ReferenceInstruction ?: return@mapNotNull null | ||
val methodRef = invokeInstruction.reference as? MethodReference ?: return@mapNotNull null | ||
|
||
if (methodRef.definingClass != className || methodRef.name != methodName) { | ||
return@mapNotNull null | ||
} | ||
|
||
index | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/app/revanced/patches/openinghours/crash/fingerprints/SetPlaceFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.revanced.patches.openinghours.crash.fingerprints | ||
|
||
import app.revanced.patcher.fingerprint.MethodFingerprint | ||
|
||
internal object SetPlaceFingerprint : MethodFingerprint( | ||
"V", | ||
parameters = listOf("Lde/simon/openinghours/models/Place;"), | ||
customFingerprint = { methodDef, _ -> | ||
methodDef.definingClass == "Lde/simon/openinghours/views/custom/PlaceCard;" | ||
&& methodDef.name == "setPlace" | ||
} | ||
) |