perf(general-ads): initialize blocks once

This commit is contained in:
oSumAtrIX 2022-10-03 01:17:22 +02:00
parent 18a749f87d
commit 5add970f61
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
2 changed files with 122 additions and 108 deletions

View File

@ -1,132 +1,146 @@
package app.revanced.integrations.patches;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
/**
* Helper functions.
*/
final class Extensions {
static boolean containsAny(final String value, final String... targets) {
for (String string : targets)
if (value.contains(string)) return true;
return false;
}
}
final class ComponentRule {
private final SettingsEnum setting;
private final String[] blocks;
/**
* Initialize a new rule for components.
*
* @param setting The setting which controls the blocking of this component.
* @param blocks The rules to block the component on.
*/
public ComponentRule(final SettingsEnum setting, final String... blocks) {
this.setting = setting;
this.blocks = blocks;
}
public boolean isEnabled() {
return setting.getBoolean();
}
public boolean isBlocked(final String string) {
return Extensions.containsAny(string, blocks);
}
}
final class LithoBlockRegister {
private final ArrayList<ComponentRule> blocks = new ArrayList<>();
public void addBlock(final ComponentRule block) {
blocks.add(block);
}
public boolean isBlocked(final String value) {
for (ComponentRule block : blocks)
if (block.isEnabled() && block.isBlocked(value)) return true;
return false;
}
}
public class GeneralBytecodeAdsPatch {
private final static LithoBlockRegister pathBlockRegister = new LithoBlockRegister();
static {
var comments = new ComponentRule(SettingsEnum.ADREMOVER_COMMENTS_REMOVAL, "comments_");
var communityPosts = new ComponentRule(SettingsEnum.ADREMOVER_COMMUNITY_POSTS_REMOVAL, "post_base_wrapper");
var communityGuidelines = new ComponentRule(SettingsEnum.ADREMOVER_COMMUNITY_GUIDELINES_REMOVAL, "community_guidelines");
var compactBanner = new ComponentRule(SettingsEnum.ADREMOVER_COMPACT_BANNER_REMOVAL, "compact_banner");
var inFeedSurvey = new ComponentRule(SettingsEnum.ADREMOVER_FEED_SURVEY_REMOVAL, "in_feed_survey");
var medicalPanel = new ComponentRule(SettingsEnum.ADREMOVER_MEDICAL_PANEL_REMOVAL, "medical_panel");
var paidContent = new ComponentRule(SettingsEnum.ADREMOVER_PAID_CONTECT_REMOVAL, "paid_content_overlay");
var merchandise = new ComponentRule(SettingsEnum.ADREMOVER_MERCHANDISE_REMOVAL, "product_carousel");
var shorts = new ComponentRule(SettingsEnum.ADREMOVER_SHORTS_SHELF_REMOVAL, "shorts_shelf");
var infoPanel = new ComponentRule(SettingsEnum.ADREMOVER_INFO_PANEL_REMOVAL, "publisher_transparency_panel", "single_item_information_panel");
var suggestions = new ComponentRule(SettingsEnum.ADREMOVER_SUGGESTIONS_REMOVAL, "horizontal_video_shelf");
var latestPosts = new ComponentRule(SettingsEnum.ADREMOVER_HIDE_LATEST_POSTS, "post_shelf");
var channelGuidelines = new ComponentRule(SettingsEnum.ADREMOVER_HIDE_CHANNEL_GUIDELINES, "channel_guidelines_entry_banner");
var generalAds = new ComponentRule(
SettingsEnum.ADREMOVER_GENERAL_ADS_REMOVAL,
// could be required
//"full_width_square_image_layout",
"video_display_full_buttoned_layout",
"_ad",
"ad_",
"ads_video_with_context",
"carousel_ad_with_detailed_metadata",
"cell_divider",
"reels_player_overlay",
"shelf_header",
"watch_metadata_app_promo",
"video_display_full_layout"
);
var movieAds = new ComponentRule(
SettingsEnum.ADREMOVER_MOVIE_REMOVAL,
"browsy_bar",
"compact_movie",
"horizontal_movie_shelf",
"movie_and_show_upsell_card"
);
// collect and add the blocks
var blocks = new ComponentRule[]{
generalAds,
communityPosts,
paidContent,
shorts,
suggestions,
latestPosts,
movieAds,
comments,
communityGuidelines,
compactBanner,
inFeedSurvey,
medicalPanel,
merchandise,
infoPanel,
channelGuidelines
};
for (var block : blocks) pathBlockRegister.addBlock(block);
}
//Used by app.revanced.patches.youtube.ad.general.bytecode.patch.GeneralBytecodeAdsPatch
public static boolean isAdComponent(StringBuilder pathBuilder, String identifier) {
public static boolean isAdComponent(StringBuilder pathBuilder) {
var path = pathBuilder.toString();
if (path.isEmpty()) return false;
LogHelper.debug(GeneralBytecodeAdsPatch.class, String.format("Searching (ID: %s): %s", identifier, path));
if (containsAny(path,
LogHelper.debug(GeneralBytecodeAdsPatch.class, String.format("Searching: %s", path));
// Do not block on these
if (Extensions.containsAny(path,
"home_video_with_context",
"related_video_with_context",
"search_video_with_context",
"download_button",
"library_recent_shelf",
"menu",
"root",
"-count",
"-space",
"-button",
"library_recent_shelf",
"download_button"
"-button"
)) return false;
List<String> blockList = new ArrayList<>();
for (var ad : SettingsEnum.ADREMOVER_CUSTOM.getString().split(",")) {
if (ad.isEmpty()) continue;
blockList.add(ad);
}
if (SettingsEnum.ADREMOVER_GENERAL_ADS_REMOVAL.getBoolean()) {
if (identifier != null && identifier.contains("carousel_ad")) {
LogHelper.debug(GeneralBytecodeAdsPatch.class, "Blocking: " + identifier);
return true;
}
blockList.add("video_display_full_buttoned_layout");
blockList.add("_ad");
blockList.add("ad_");
blockList.add("ads_video_with_context");
blockList.add("cell_divider");
blockList.add("reels_player_overlay");
// could be required
// blockList.add("full_width_square_image_layout");
blockList.add("shelf_header");
blockList.add("watch_metadata_app_promo");
blockList.add("video_display_full_layout");
}
if (SettingsEnum.ADREMOVER_MOVIE_REMOVAL.getBoolean()) {
blockList.add("browsy_bar");
blockList.add("compact_movie");
blockList.add("horizontal_movie_shelf");
blockList.add("movie_and_show_upsell_card");
}
if (SettingsEnum.ADREMOVER_COMMENTS_REMOVAL.getBoolean()) {
blockList.add("comments_");
}
if (SettingsEnum.ADREMOVER_COMMUNITY_GUIDELINES.getBoolean()) {
blockList.add("community_guidelines");
}
if (SettingsEnum.ADREMOVER_COMPACT_BANNER_REMOVAL.getBoolean()) {
blockList.add("compact_banner");
}
if (SettingsEnum.ADREMOVER_EMERGENCY_BOX_REMOVAL.getBoolean()) {
blockList.add("emergency_onebox");
}
if (SettingsEnum.ADREMOVER_FEED_SURVEY_REMOVAL.getBoolean()) {
blockList.add("in_feed_survey");
}
if (SettingsEnum.ADREMOVER_MEDICAL_PANEL_REMOVAL.getBoolean()) {
blockList.add("medical_panel");
}
if (SettingsEnum.ADREMOVER_PAID_CONTECT_REMOVAL.getBoolean()) {
blockList.add("paid_content_overlay");
}
if (SettingsEnum.ADREMOVER_COMMUNITY_POSTS_REMOVAL.getBoolean()) {
blockList.add("post_base_wrapper");
}
if (SettingsEnum.ADREMOVER_MERCHANDISE_REMOVAL.getBoolean()) {
blockList.add("product_carousel");
}
if (SettingsEnum.ADREMOVER_SHORTS_SHELF.getBoolean()) {
blockList.add("shorts_shelf");
}
if (SettingsEnum.ADREMOVER_INFO_PANEL_REMOVAL.getBoolean()) {
blockList.add("publisher_transparency_panel");
blockList.add("single_item_information_panel");
}
if (SettingsEnum.ADREMOVER_HIDE_SUGGESTIONS.getBoolean()) {
blockList.add("horizontal_video_shelf");
}
if (SettingsEnum.ADREMOVER_HIDE_LATEST_POSTS.getBoolean()) {
blockList.add("post_shelf");
}
if (SettingsEnum.ADREMOVER_HIDE_CHANNEL_GUIDELINES.getBoolean()) {
blockList.add("channel_guidelines_entry_banner");
}
if (anyMatch(blockList, path::contains)) {
LogHelper.debug(GeneralBytecodeAdsPatch.class, "Blocking: " + path);
if (pathBlockRegister.isBlocked(path)) {
LogHelper.debug(GeneralBytecodeAdsPatch.class, "Blocked: " + path);
return true;
}
return false;
}
private static boolean containsAny(String value, String... targets) {
for (String string : targets)
if (value.contains(string)) return true;
return false;
}
private static <T> boolean anyMatch(List<T> value, APredicate<? super T> predicate) {
for (T t : value) {
if (predicate.test(t)) return true;
}
return false;
}
@FunctionalInterface
public interface APredicate<T> {
boolean test(T t);
}
}

View File

@ -36,13 +36,13 @@ public enum SettingsEnum {
ADREMOVER_COMMENTS_REMOVAL("revanced_adremover_comments_removal", false, ReturnType.BOOLEAN, true),
ADREMOVER_MOVIE_REMOVAL("revanced_adremover_movie", true, ReturnType.BOOLEAN, true),
ADREMOVER_FEED_SURVEY_REMOVAL("revanced_adremover_feed_survey", true, ReturnType.BOOLEAN, true),
ADREMOVER_SHORTS_SHELF("revanced_adremover_shorts_shelf", true, ReturnType.BOOLEAN, true),
ADREMOVER_COMMUNITY_GUIDELINES("revanced_adremover_community_guidelines", true, ReturnType.BOOLEAN, true),
ADREMOVER_SHORTS_SHELF_REMOVAL("revanced_adremover_shorts_shelf", true, ReturnType.BOOLEAN, true),
ADREMOVER_COMMUNITY_GUIDELINES_REMOVAL("revanced_adremover_community_guidelines", true, ReturnType.BOOLEAN, true),
ADREMOVER_EMERGENCY_BOX_REMOVAL("revanced_adremover_emergency_box_removal", true, ReturnType.BOOLEAN, true),
ADREMOVER_INFO_PANEL_REMOVAL("revanced_adremover_info_panel", true, ReturnType.BOOLEAN, true),
ADREMOVER_MEDICAL_PANEL_REMOVAL("revanced_adremover_medical_panel", true, ReturnType.BOOLEAN, true),
ADREMOVER_PAID_CONTECT_REMOVAL("revanced_adremover_paid_content", true, ReturnType.BOOLEAN, true),
ADREMOVER_HIDE_SUGGESTIONS("revanced_adremover_hide_suggestions", true, ReturnType.BOOLEAN, true),
ADREMOVER_SUGGESTIONS_REMOVAL("revanced_adremover_hide_suggestions", true, ReturnType.BOOLEAN, true),
ADREMOVER_HIDE_LATEST_POSTS("revanced_adremover_hide_latest_posts", true, ReturnType.BOOLEAN, true),
ADREMOVER_HIDE_CHANNEL_GUIDELINES("revanced_adremover_hide_channel_guidelines", true, ReturnType.BOOLEAN, true),