mirror of
https://github.com/inotia00/revanced-patches.git
synced 2025-05-02 07:34:31 +02:00
feat(Utils): Use more Contexts for activities/services to avoid NullPointerException in Context.getResources()
This commit is contained in:
parent
f540fb8cc5
commit
e5361e90fe
@ -359,7 +359,17 @@ public class Utils {
|
|||||||
//
|
//
|
||||||
// Info level also helps debug if a patch hook is called before
|
// Info level also helps debug if a patch hook is called before
|
||||||
// the context is set since debug logging is off by default.
|
// the context is set since debug logging is off by default.
|
||||||
Logger.initializationInfo(Utils.class, "Set context: " + appContext);
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("Set context: ");
|
||||||
|
sb.append(appContext);
|
||||||
|
StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
|
||||||
|
if (stackTraceElement.length > 3) {
|
||||||
|
sb.append("\n");
|
||||||
|
sb.append("Called from method: ");
|
||||||
|
sb.append(stackTraceElement[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.initializationInfo(Utils.class, sb.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setClipboard(@NonNull String text) {
|
public static void setClipboard(@NonNull String text) {
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
package app.revanced.patches.music.utils.extension
|
package app.revanced.patches.music.utils.extension
|
||||||
|
|
||||||
import app.revanced.patches.music.utils.extension.hooks.applicationInitHook
|
import app.revanced.patches.music.utils.extension.hooks.applicationInitHook
|
||||||
|
import app.revanced.patches.music.utils.extension.hooks.mainActivityBaseContextHook
|
||||||
|
import app.revanced.patches.shared.extension.hooks.cronetEngineContextHook
|
||||||
|
import app.revanced.patches.shared.extension.hooks.firebaseInitProviderContextHook
|
||||||
import app.revanced.patches.shared.extension.sharedExtensionPatch
|
import app.revanced.patches.shared.extension.sharedExtensionPatch
|
||||||
|
|
||||||
val sharedExtensionPatch = sharedExtensionPatch(applicationInitHook)
|
val sharedExtensionPatch = sharedExtensionPatch(
|
||||||
|
applicationInitHook,
|
||||||
|
cronetEngineContextHook,
|
||||||
|
firebaseInitProviderContextHook,
|
||||||
|
mainActivityBaseContextHook,
|
||||||
|
)
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package app.revanced.patches.music.utils.extension.hooks
|
||||||
|
|
||||||
|
import app.revanced.patches.shared.extension.extensionHook
|
||||||
|
import app.revanced.util.getReference
|
||||||
|
import app.revanced.util.indexOfFirstInstructionOrThrow
|
||||||
|
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
|
||||||
|
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
|
||||||
|
|
||||||
|
private var attachBaseContextIndex = -1
|
||||||
|
|
||||||
|
internal val mainActivityBaseContextHook = extensionHook(
|
||||||
|
insertIndexResolver = { method ->
|
||||||
|
attachBaseContextIndex = method.indexOfFirstInstructionOrThrow {
|
||||||
|
getReference<MethodReference>()?.name == "attachBaseContext"
|
||||||
|
}
|
||||||
|
|
||||||
|
attachBaseContextIndex + 1
|
||||||
|
},
|
||||||
|
contextRegisterResolver = { method ->
|
||||||
|
val overrideInstruction =
|
||||||
|
method.implementation!!.instructions.elementAt(attachBaseContextIndex)
|
||||||
|
as FiveRegisterInstruction
|
||||||
|
"v${overrideInstruction.registerD}"
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
returns("V")
|
||||||
|
parameters("Landroid/content/Context;")
|
||||||
|
custom { method, classDef ->
|
||||||
|
classDef.type == "Lcom/google/android/apps/youtube/music/activities/MusicActivity;" &&
|
||||||
|
method.name == "attachBaseContext"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package app.revanced.patches.shared.extension.hooks
|
||||||
|
|
||||||
|
import app.revanced.patches.shared.extension.extensionHook
|
||||||
|
import app.revanced.util.getReference
|
||||||
|
import app.revanced.util.indexOfFirstInstruction
|
||||||
|
import app.revanced.util.indexOfFirstInstructionOrThrow
|
||||||
|
import com.android.tools.smali.dexlib2.AccessFlags
|
||||||
|
import com.android.tools.smali.dexlib2.Opcode
|
||||||
|
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
|
||||||
|
import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction3rc
|
||||||
|
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
|
||||||
|
|
||||||
|
private var initIndex = -1
|
||||||
|
private var isRange = true
|
||||||
|
|
||||||
|
internal val cronetEngineContextHook = extensionHook(
|
||||||
|
insertIndexResolver = { method ->
|
||||||
|
initIndex = method.indexOfFirstInstruction(Opcode.INVOKE_DIRECT_RANGE)
|
||||||
|
|
||||||
|
if (initIndex < 0) {
|
||||||
|
initIndex = method.indexOfFirstInstructionOrThrow(Opcode.INVOKE_DIRECT)
|
||||||
|
isRange = false
|
||||||
|
}
|
||||||
|
|
||||||
|
initIndex
|
||||||
|
},
|
||||||
|
contextRegisterResolver = { method ->
|
||||||
|
val initInstruction =
|
||||||
|
method.implementation!!.instructions.elementAt(initIndex)
|
||||||
|
if (isRange) {
|
||||||
|
val overrideInstruction = initInstruction as Instruction3rc
|
||||||
|
"v${overrideInstruction.startRegister + 1}"
|
||||||
|
} else {
|
||||||
|
val overrideInstruction = initInstruction as FiveRegisterInstruction
|
||||||
|
"v${overrideInstruction.registerD}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
returns("Lorg/chromium/net/CronetEngine;")
|
||||||
|
accessFlags(AccessFlags.PUBLIC, AccessFlags.STATIC)
|
||||||
|
strings("Could not create CronetEngine")
|
||||||
|
custom { method, classDef ->
|
||||||
|
method.indexOfFirstInstruction {
|
||||||
|
(opcode == Opcode.INVOKE_DIRECT || opcode == Opcode.INVOKE_DIRECT_RANGE) &&
|
||||||
|
getReference<MethodReference>()?.parameterTypes?.firstOrNull() == "Landroid/content/Context;"
|
||||||
|
} >= 0
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package app.revanced.patches.shared.extension.hooks
|
||||||
|
|
||||||
|
import app.revanced.patches.shared.extension.extensionHook
|
||||||
|
import app.revanced.util.getReference
|
||||||
|
import app.revanced.util.indexOfFirstInstruction
|
||||||
|
import com.android.tools.smali.dexlib2.Opcode
|
||||||
|
import com.android.tools.smali.dexlib2.iface.Method
|
||||||
|
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
|
||||||
|
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
|
||||||
|
|
||||||
|
private var getResourcesIndex = -1
|
||||||
|
|
||||||
|
internal val firebaseInitProviderContextHook = extensionHook(
|
||||||
|
insertIndexResolver = { method ->
|
||||||
|
getResourcesIndex = indexOfGerResourcesInstruction(method)
|
||||||
|
|
||||||
|
getResourcesIndex + 2
|
||||||
|
},
|
||||||
|
contextRegisterResolver = { method ->
|
||||||
|
val overrideInstruction =
|
||||||
|
method.implementation!!.instructions.elementAt(getResourcesIndex)
|
||||||
|
as FiveRegisterInstruction
|
||||||
|
|
||||||
|
"v${overrideInstruction.registerC}"
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
strings("firebase_database_url")
|
||||||
|
custom { method, _ ->
|
||||||
|
indexOfGerResourcesInstruction(method) >= 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun indexOfGerResourcesInstruction(method: Method) =
|
||||||
|
method.indexOfFirstInstruction {
|
||||||
|
opcode == Opcode.INVOKE_VIRTUAL &&
|
||||||
|
getReference<MethodReference>()?.toString() =="Landroid/content/Context;->getResources()Landroid/content/res/Resources;"
|
||||||
|
}
|
@ -1,11 +1,17 @@
|
|||||||
package app.revanced.patches.youtube.utils.extension
|
package app.revanced.patches.youtube.utils.extension
|
||||||
|
|
||||||
|
import app.revanced.patches.shared.extension.hooks.cronetEngineContextHook
|
||||||
|
import app.revanced.patches.shared.extension.hooks.firebaseInitProviderContextHook
|
||||||
import app.revanced.patches.shared.extension.sharedExtensionPatch
|
import app.revanced.patches.shared.extension.sharedExtensionPatch
|
||||||
import app.revanced.patches.youtube.utils.extension.hooks.applicationInitHook
|
import app.revanced.patches.youtube.utils.extension.hooks.applicationInitHook
|
||||||
import app.revanced.patches.youtube.utils.extension.hooks.mainActivityBaseContextHook
|
import app.revanced.patches.youtube.utils.extension.hooks.mainActivityBaseContextHook
|
||||||
|
import app.revanced.patches.youtube.utils.extension.hooks.urlActivityBaseContextHook
|
||||||
|
|
||||||
// TODO: Move this to a "Hook.kt" file. Same for other extension hook patches.
|
// TODO: Move this to a "Hook.kt" file. Same for other extension hook patches.
|
||||||
val sharedExtensionPatch = sharedExtensionPatch(
|
val sharedExtensionPatch = sharedExtensionPatch(
|
||||||
applicationInitHook,
|
applicationInitHook,
|
||||||
|
cronetEngineContextHook,
|
||||||
|
firebaseInitProviderContextHook,
|
||||||
mainActivityBaseContextHook,
|
mainActivityBaseContextHook,
|
||||||
|
urlActivityBaseContextHook,
|
||||||
)
|
)
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package app.revanced.patches.youtube.utils.extension.hooks
|
||||||
|
|
||||||
|
import app.revanced.patches.shared.extension.extensionHook
|
||||||
|
import app.revanced.util.getReference
|
||||||
|
import app.revanced.util.indexOfFirstInstructionOrThrow
|
||||||
|
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
|
||||||
|
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
|
||||||
|
|
||||||
|
private var attachBaseContextIndex = -1
|
||||||
|
|
||||||
|
internal val urlActivityBaseContextHook = extensionHook(
|
||||||
|
insertIndexResolver = { method ->
|
||||||
|
attachBaseContextIndex = method.indexOfFirstInstructionOrThrow {
|
||||||
|
getReference<MethodReference>()?.name == "attachBaseContext"
|
||||||
|
}
|
||||||
|
|
||||||
|
attachBaseContextIndex + 1
|
||||||
|
},
|
||||||
|
contextRegisterResolver = { method ->
|
||||||
|
val overrideInstruction =
|
||||||
|
method.implementation!!.instructions.elementAt(attachBaseContextIndex)
|
||||||
|
as FiveRegisterInstruction
|
||||||
|
"v${overrideInstruction.registerD}"
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
returns("V")
|
||||||
|
parameters("Landroid/content/Context;")
|
||||||
|
custom { method, classDef ->
|
||||||
|
classDef.endsWith("/Shell_UrlActivity;") &&
|
||||||
|
method.name == "attachBaseContext"
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user