refactor: abstract integrations patch away from YouTube and YT Music

This commit is contained in:
inotia00 2023-04-16 01:07:26 +09:00
parent 60aa9d648f
commit a34aabe454
3 changed files with 7 additions and 8 deletions

View File

@ -5,12 +5,10 @@ import app.revanced.patcher.patch.annotations.RequiresIntegrations
import app.revanced.patches.music.misc.integrations.fingerprints.InitFingerprint import app.revanced.patches.music.misc.integrations.fingerprints.InitFingerprint
import app.revanced.patches.shared.patch.integrations.AbstractIntegrationsPatch import app.revanced.patches.shared.patch.integrations.AbstractIntegrationsPatch
import app.revanced.patches.shared.annotation.YouTubeMusicCompatibility import app.revanced.patches.shared.annotation.YouTubeMusicCompatibility
import app.revanced.util.integrations.Constants.MUSIC_SETTINGS_PATH
@Name("music-integrations") @Name("music-integrations")
@YouTubeMusicCompatibility @YouTubeMusicCompatibility
@RequiresIntegrations @RequiresIntegrations
class MusicIntegrationsPatch : AbstractIntegrationsPatch( class MusicIntegrationsPatch : AbstractIntegrationsPatch(
"$MUSIC_SETTINGS_PATH",
listOf(InitFingerprint), listOf(InitFingerprint),
) )

View File

@ -10,12 +10,12 @@ import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultError import app.revanced.patcher.patch.PatchResultError
import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.extensions.toErrorResult import app.revanced.extensions.toErrorResult
import app.revanced.util.integrations.Constants.INTEGRATIONS_PATH
import org.jf.dexlib2.iface.Method import org.jf.dexlib2.iface.Method
@Description("Applies mandatory patches to implement the ReVanced integrations into the application.") @Description("Applies mandatory patches to implement the ReVanced integrations into the application.")
@Version("0.0.1") @Version("0.0.1")
abstract class AbstractIntegrationsPatch( abstract class AbstractIntegrationsPatch(
private val integrationsDescriptor: String,
private val hooks: Iterable<IntegrationsFingerprint> private val hooks: Iterable<IntegrationsFingerprint>
) : BytecodePatch(hooks) { ) : BytecodePatch(hooks) {
/** /**
@ -29,14 +29,14 @@ abstract class AbstractIntegrationsPatch(
customFingerprint: ((methodDef: Method) -> Boolean)? = null, customFingerprint: ((methodDef: Method) -> Boolean)? = null,
private val contextRegisterResolver: (Method) -> Int = object : RegisterResolver {} private val contextRegisterResolver: (Method) -> Int = object : RegisterResolver {}
) : MethodFingerprint(strings = strings, customFingerprint = customFingerprint) { ) : MethodFingerprint(strings = strings, customFingerprint = customFingerprint) {
fun invoke(integrationsDescriptor: String): PatchResult { fun invoke(): PatchResult {
result?.mutableMethod?.let { method -> result?.mutableMethod?.let { method ->
val contextRegister = contextRegisterResolver(method) val contextRegister = contextRegisterResolver(method)
method.addInstruction( method.addInstruction(
0, 0,
"sput-object v$contextRegister, " + "sput-object v$contextRegister, " +
"$integrationsDescriptor->context:Landroid/content/Context;" "$INTEGRATIONS_CLASS_DESCRIPTOR->context:Landroid/content/Context;"
) )
} ?: return toErrorResult() } ?: return toErrorResult()
return PatchResultSuccess() return PatchResultSuccess()
@ -48,9 +48,9 @@ abstract class AbstractIntegrationsPatch(
} }
override fun execute(context: BytecodeContext): PatchResult { override fun execute(context: BytecodeContext): PatchResult {
if (context.findClass(integrationsDescriptor) == null) return MISSING_INTEGRATIONS if (context.findClass(INTEGRATIONS_CLASS_DESCRIPTOR) == null) return MISSING_INTEGRATIONS
for (hook in hooks) hook.invoke(integrationsDescriptor).let { for (hook in hooks) hook.invoke().let {
if (it is PatchResultError) return it if (it is PatchResultError) return it
} }
@ -58,6 +58,8 @@ abstract class AbstractIntegrationsPatch(
} }
private companion object { private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"$INTEGRATIONS_PATH/utils/ReVancedUtils;"
val MISSING_INTEGRATIONS = PatchResultError( val MISSING_INTEGRATIONS = PatchResultError(
"Integrations have not been merged yet. " + "Integrations have not been merged yet. " +
"This patch can not succeed without merging the integrations." "This patch can not succeed without merging the integrations."

View File

@ -10,6 +10,5 @@ import app.revanced.patches.youtube.misc.integrations.fingerprints.*
@YouTubeCompatibility @YouTubeCompatibility
@RequiresIntegrations @RequiresIntegrations
class IntegrationsPatch : AbstractIntegrationsPatch( class IntegrationsPatch : AbstractIntegrationsPatch(
"Lapp/revanced/integrations/utils/ReVancedUtils;",
listOf(InitFingerprint, StandalonePlayerFingerprint, ServiceFingerprint), listOf(InitFingerprint, StandalonePlayerFingerprint, ServiceFingerprint),
) )