Skip to content

Commit

Permalink
refactor: Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios committed Mar 17, 2024
1 parent 83545b4 commit 75220ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import app.revanced.patches.shared.misc.settings.preference.PreferenceScreen
import app.revanced.patches.shared.misc.settings.preference.PreferenceScreen.Sorting
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
import app.revanced.patches.shared.misc.settings.preference.TextPreference
import app.revanced.patches.youtube.misc.navigation.NavigationBarHookPatch
import app.revanced.patches.youtube.layout.hide.general.fingerprints.ParseElementFromBufferFingerprint
import app.revanced.patches.youtube.layout.hide.general.fingerprints.PlayerOverlayFingerprint
import app.revanced.patches.youtube.layout.hide.general.fingerprints.ShowWatermarkFingerprint
import app.revanced.patches.youtube.misc.litho.filter.LithoFilterPatch
import app.revanced.patches.youtube.misc.navigation.NavigationBarHookPatch
import app.revanced.patches.youtube.misc.playertype.PlayerTypeHookPatch
import app.revanced.patches.youtube.misc.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
Expand Down Expand Up @@ -162,15 +162,14 @@ object HideLayoutComponentsPatch : BytecodePatch(

// region Mix playlists

ParseElementFromBufferFingerprint.result?.let { result ->
val returnEmptyComponentInstruction =
result.mutableMethod.getInstructions().last { it.opcode == Opcode.INVOKE_STATIC }
ParseElementFromBufferFingerprint.resultOrThrow().let { result ->
val consumeByteBufferIndex = result.scanResult.patternScanResult!!.startIndex

result.mutableMethod.apply {
val consumeByteBufferIndex = result.scanResult.patternScanResult!!.startIndex
val conversionContextRegister =
getInstruction<TwoRegisterInstruction>(consumeByteBufferIndex - 2).registerA
val byteBufferRegister = getInstruction<FiveRegisterInstruction>(consumeByteBufferIndex).registerD
val returnEmptyComponentInstruction = getInstructions().last { it.opcode == Opcode.INVOKE_STATIC }

addInstructionsWithLabels(
consumeByteBufferIndex,
Expand All @@ -182,15 +181,15 @@ object HideLayoutComponentsPatch : BytecodePatch(
ExternalLabel("return_empty_component", returnEmptyComponentInstruction),
)
}
} ?: throw ParseElementFromBufferFingerprint.exception
}

// endregion

// region Watermark (legacy code for old versions of YouTube)

ShowWatermarkFingerprint.also {
it.resolve(context, PlayerOverlayFingerprint.result?.classDef ?: throw PlayerOverlayFingerprint.exception)
}.result?.mutableMethod?.apply {
it.resolve(context, PlayerOverlayFingerprint.resultOrThrow().classDef)
}.resultOrThrow().mutableMethod.apply {
val index = implementation!!.instructions.size - 5

removeInstruction(index)
Expand All @@ -201,7 +200,7 @@ object HideLayoutComponentsPatch : BytecodePatch(
move-result p2
""",
)
} ?: throw ShowWatermarkFingerprint.exception
}

// endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import app.revanced.patches.youtube.misc.navigation.fingerprints.PivotBarCreateB
import app.revanced.patches.youtube.misc.navigation.fingerprints.PivotBarEnumFingerprint
import app.revanced.patches.youtube.misc.navigation.utils.InjectionUtils.REGISTER_TEMPLATE_REPLACEMENT
import app.revanced.patches.youtube.misc.navigation.utils.InjectionUtils.injectHook
import app.revanced.util.exception
import app.revanced.util.getReference
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
Expand Down Expand Up @@ -46,29 +46,25 @@ object NavigationBarHookPatch : BytecodePatch(
private lateinit var navigationTabCreatedCallbackMethod: MutableMethod

override fun execute(context: BytecodeContext) {
PivotBarConstructorFingerprint.result?.let {
PivotBarConstructorFingerprint.resultOrThrow().let {
InitializeButtonsFingerprint.resolve(
context,
it.classDef
)
} ?: throw PivotBarConstructorFingerprint.exception
}

val initializeButtonsResult = InitializeButtonsFingerprint.result
?: throw InitializeButtonsFingerprint.exception
val initializeButtonsResult = InitializeButtonsFingerprint.resultOrThrow()

val fingerprintResults =
arrayOf(PivotBarEnumFingerprint, PivotBarButtonsViewFingerprint)
.onEach {
if (!it.resolve(
context,
initializeButtonsResult.mutableMethod,
initializeButtonsResult.mutableClass,
)
) {
throw it.exception
}
it.resolve(
context,
initializeButtonsResult.mutableMethod,
initializeButtonsResult.mutableClass,
)
}
.map { it.result!!.scanResult.patternScanResult!! }
.map { it.resultOrThrow().scanResult.patternScanResult!! }

val enumScanResult = fingerprintResults[0]
val buttonViewResult = fingerprintResults[1]
Expand All @@ -93,31 +89,29 @@ object NavigationBarHookPatch : BytecodePatch(
initializeButtonsResult.mutableMethod.injectHook(insertIndex, hook)
}

InitializeButtonsFingerprint.result?.let {
if (!PivotBarCreateButtonViewFingerprint.resolve(context, it.mutableMethod, it.mutableClass)) {
throw PivotBarCreateButtonViewFingerprint.exception
}
} ?: throw InitializeButtonsFingerprint.exception
InitializeButtonsFingerprint.resultOrThrow().let {
PivotBarCreateButtonViewFingerprint.resolve(context, it.mutableMethod, it.mutableClass)
}

/**
* Unique hook just for the Create tab button.
*/
PivotBarCreateButtonViewFingerprint.result?.apply {
PivotBarCreateButtonViewFingerprint.resultOrThrow().apply {
val insertIndex = scanResult.patternScanResult!!.endIndex
mutableMethod.injectHook(
insertIndex,
"invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->createTabLoaded(Landroid/view/View;)V"
)
} ?: PivotBarCreateButtonViewFingerprint.exception
}


/**
* Callback for other patches.
*/
NavigationBarHookCallbackFingerprint.result?.apply {
NavigationBarHookCallbackFingerprint.resultOrThrow().apply {
navigationTabCreatedCallbackMethod = mutableMethod
} ?: NavigationBarHookCallbackFingerprint.exception
}


/**
Expand All @@ -127,7 +121,7 @@ object NavigationBarHookPatch : BytecodePatch(
// Two different layouts are used at the hooked code.
// Insert before the first ViewGroup method call after inflating,
// so this works regardless which layout is used.
ActionBarSearchResultsFingerprint.result?.mutableMethod?.apply {
ActionBarSearchResultsFingerprint.resultOrThrow().mutableMethod.apply {
val instructionIndex = implementation!!.instructions.indexOfFirst {
it.opcode == Opcode.INVOKE_VIRTUAL &&
it.getReference<MethodReference>()?.name == "setLayoutDirection"
Expand All @@ -138,7 +132,7 @@ object NavigationBarHookPatch : BytecodePatch(
"invoke-static { v$register }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->searchBarResultsViewLoaded(Landroid/view/View;)V"
)
} ?: ActionBarSearchResultsFingerprint.exception
}
}

fun hookNavigationButtonCreated(classDescriptor: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal object NavigationBarHookResourcePatch : ResourcePatch() {
internal var actionBarSearchResultsViewMicId: Long = -1

override fun execute(context: ResourceContext) {
imageOnlyTabResourceId = ResourceMappingPatch.resourceMappings.single {
imageOnlyTabResourceId = ResourceMappingPatch.resourceMappings.first {
it.type == "layout" && it.name == "image_only_tab"
}.id

actionBarSearchResultsViewMicId = ResourceMappingPatch.resourceMappings.single {
actionBarSearchResultsViewMicId = ResourceMappingPatch.resourceMappings.first {
it.type == "layout" && it.name == "action_bar_search_results_view_mic"
}.id
}
Expand Down

0 comments on commit 75220ac

Please sign in to comment.