Skip to content

Commit

Permalink
feat(OpeningHours): fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Linus789 committed Feb 8, 2024
1 parent 501fc48 commit 09f4a07
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/revanced-patches.api
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ public final class app/revanced/patches/nyx/misc/pro/UnlockProPatch : app/revanc
public synthetic fun execute (Lapp/revanced/patcher/data/Context;)V
}

public final class app/revanced/patches/openinghours/crash/CrashPatch : app/revanced/patcher/patch/BytecodePatch {
public static final field INSTANCE Lapp/revanced/patches/openinghours/crash/CrashPatch;
public fun execute (Lapp/revanced/patcher/data/BytecodeContext;)V
public synthetic fun execute (Lapp/revanced/patcher/data/Context;)V
}

public final class app/revanced/patches/photomath/detection/deviceid/SpoofDeviceIdPatch : app/revanced/patcher/patch/BytecodePatch {
public static final field INSTANCE Lapp/revanced/patches/photomath/detection/deviceid/SpoofDeviceIdPatch;
public fun execute (Lapp/revanced/patcher/data/BytecodeContext;)V
Expand Down
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
}

}
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"
}
)

0 comments on commit 09f4a07

Please sign in to comment.