Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 5add970

Browse files
committed
perf(general-ads): initialize blocks once
1 parent 18a749f commit 5add970

2 files changed

Lines changed: 122 additions & 108 deletions

File tree

Lines changed: 119 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,146 @@
11
package app.revanced.integrations.patches;
22

3+
import java.nio.ByteBuffer;
34
import java.util.ArrayList;
4-
import java.util.List;
55

66
import app.revanced.integrations.settings.SettingsEnum;
77
import app.revanced.integrations.utils.LogHelper;
88

9+
/**
10+
* Helper functions.
11+
*/
12+
final class Extensions {
13+
static boolean containsAny(final String value, final String... targets) {
14+
for (String string : targets)
15+
if (value.contains(string)) return true;
16+
return false;
17+
}
18+
}
19+
20+
final class ComponentRule {
21+
private final SettingsEnum setting;
22+
private final String[] blocks;
23+
24+
/**
25+
* Initialize a new rule for components.
26+
*
27+
* @param setting The setting which controls the blocking of this component.
28+
* @param blocks The rules to block the component on.
29+
*/
30+
public ComponentRule(final SettingsEnum setting, final String... blocks) {
31+
this.setting = setting;
32+
this.blocks = blocks;
33+
}
34+
35+
public boolean isEnabled() {
36+
return setting.getBoolean();
37+
}
38+
39+
public boolean isBlocked(final String string) {
40+
return Extensions.containsAny(string, blocks);
41+
}
42+
}
43+
44+
final class LithoBlockRegister {
45+
private final ArrayList<ComponentRule> blocks = new ArrayList<>();
46+
47+
public void addBlock(final ComponentRule block) {
48+
blocks.add(block);
49+
}
50+
51+
public boolean isBlocked(final String value) {
52+
for (ComponentRule block : blocks)
53+
if (block.isEnabled() && block.isBlocked(value)) return true;
54+
return false;
55+
}
56+
}
57+
958
public class GeneralBytecodeAdsPatch {
59+
private final static LithoBlockRegister pathBlockRegister = new LithoBlockRegister();
60+
61+
static {
62+
var comments = new ComponentRule(SettingsEnum.ADREMOVER_COMMENTS_REMOVAL, "comments_");
63+
var communityPosts = new ComponentRule(SettingsEnum.ADREMOVER_COMMUNITY_POSTS_REMOVAL, "post_base_wrapper");
64+
var communityGuidelines = new ComponentRule(SettingsEnum.ADREMOVER_COMMUNITY_GUIDELINES_REMOVAL, "community_guidelines");
65+
var compactBanner = new ComponentRule(SettingsEnum.ADREMOVER_COMPACT_BANNER_REMOVAL, "compact_banner");
66+
var inFeedSurvey = new ComponentRule(SettingsEnum.ADREMOVER_FEED_SURVEY_REMOVAL, "in_feed_survey");
67+
var medicalPanel = new ComponentRule(SettingsEnum.ADREMOVER_MEDICAL_PANEL_REMOVAL, "medical_panel");
68+
var paidContent = new ComponentRule(SettingsEnum.ADREMOVER_PAID_CONTECT_REMOVAL, "paid_content_overlay");
69+
var merchandise = new ComponentRule(SettingsEnum.ADREMOVER_MERCHANDISE_REMOVAL, "product_carousel");
70+
var shorts = new ComponentRule(SettingsEnum.ADREMOVER_SHORTS_SHELF_REMOVAL, "shorts_shelf");
71+
var infoPanel = new ComponentRule(SettingsEnum.ADREMOVER_INFO_PANEL_REMOVAL, "publisher_transparency_panel", "single_item_information_panel");
72+
var suggestions = new ComponentRule(SettingsEnum.ADREMOVER_SUGGESTIONS_REMOVAL, "horizontal_video_shelf");
73+
var latestPosts = new ComponentRule(SettingsEnum.ADREMOVER_HIDE_LATEST_POSTS, "post_shelf");
74+
var channelGuidelines = new ComponentRule(SettingsEnum.ADREMOVER_HIDE_CHANNEL_GUIDELINES, "channel_guidelines_entry_banner");
75+
var generalAds = new ComponentRule(
76+
SettingsEnum.ADREMOVER_GENERAL_ADS_REMOVAL,
77+
// could be required
78+
//"full_width_square_image_layout",
79+
"video_display_full_buttoned_layout",
80+
"_ad",
81+
"ad_",
82+
"ads_video_with_context",
83+
"carousel_ad_with_detailed_metadata",
84+
"cell_divider",
85+
"reels_player_overlay",
86+
"shelf_header",
87+
"watch_metadata_app_promo",
88+
"video_display_full_layout"
89+
);
90+
var movieAds = new ComponentRule(
91+
SettingsEnum.ADREMOVER_MOVIE_REMOVAL,
92+
"browsy_bar",
93+
"compact_movie",
94+
"horizontal_movie_shelf",
95+
"movie_and_show_upsell_card"
96+
);
97+
98+
// collect and add the blocks
99+
var blocks = new ComponentRule[]{
100+
generalAds,
101+
communityPosts,
102+
paidContent,
103+
shorts,
104+
suggestions,
105+
latestPosts,
106+
movieAds,
107+
comments,
108+
communityGuidelines,
109+
compactBanner,
110+
inFeedSurvey,
111+
medicalPanel,
112+
merchandise,
113+
infoPanel,
114+
channelGuidelines
115+
};
116+
for (var block : blocks) pathBlockRegister.addBlock(block);
117+
}
118+
10119
//Used by app.revanced.patches.youtube.ad.general.bytecode.patch.GeneralBytecodeAdsPatch
11-
public static boolean isAdComponent(StringBuilder pathBuilder, String identifier) {
120+
public static boolean isAdComponent(StringBuilder pathBuilder) {
12121
var path = pathBuilder.toString();
13122
if (path.isEmpty()) return false;
14123

15-
LogHelper.debug(GeneralBytecodeAdsPatch.class, String.format("Searching (ID: %s): %s", identifier, path));
16-
17-
if (containsAny(path,
124+
LogHelper.debug(GeneralBytecodeAdsPatch.class, String.format("Searching: %s", path));
125+
// Do not block on these
126+
if (Extensions.containsAny(path,
18127
"home_video_with_context",
19128
"related_video_with_context",
20129
"search_video_with_context",
130+
"download_button",
131+
"library_recent_shelf",
21132
"menu",
22133
"root",
23134
"-count",
24135
"-space",
25-
"-button",
26-
"library_recent_shelf",
27-
"download_button"
136+
"-button"
28137
)) return false;
29138

30-
List<String> blockList = new ArrayList<>();
31-
32-
for (var ad : SettingsEnum.ADREMOVER_CUSTOM.getString().split(",")) {
33-
if (ad.isEmpty()) continue;
34-
blockList.add(ad);
35-
}
36-
37-
if (SettingsEnum.ADREMOVER_GENERAL_ADS_REMOVAL.getBoolean()) {
38-
if (identifier != null && identifier.contains("carousel_ad")) {
39-
LogHelper.debug(GeneralBytecodeAdsPatch.class, "Blocking: " + identifier);
40-
return true;
41-
}
42-
43-
blockList.add("video_display_full_buttoned_layout");
44-
blockList.add("_ad");
45-
blockList.add("ad_");
46-
blockList.add("ads_video_with_context");
47-
blockList.add("cell_divider");
48-
blockList.add("reels_player_overlay");
49-
// could be required
50-
// blockList.add("full_width_square_image_layout");
51-
blockList.add("shelf_header");
52-
blockList.add("watch_metadata_app_promo");
53-
blockList.add("video_display_full_layout");
54-
}
55-
56-
if (SettingsEnum.ADREMOVER_MOVIE_REMOVAL.getBoolean()) {
57-
blockList.add("browsy_bar");
58-
blockList.add("compact_movie");
59-
blockList.add("horizontal_movie_shelf");
60-
blockList.add("movie_and_show_upsell_card");
61-
}
62-
63-
if (SettingsEnum.ADREMOVER_COMMENTS_REMOVAL.getBoolean()) {
64-
blockList.add("comments_");
65-
}
66-
if (SettingsEnum.ADREMOVER_COMMUNITY_GUIDELINES.getBoolean()) {
67-
blockList.add("community_guidelines");
68-
}
69-
if (SettingsEnum.ADREMOVER_COMPACT_BANNER_REMOVAL.getBoolean()) {
70-
blockList.add("compact_banner");
71-
}
72-
if (SettingsEnum.ADREMOVER_EMERGENCY_BOX_REMOVAL.getBoolean()) {
73-
blockList.add("emergency_onebox");
74-
}
75-
if (SettingsEnum.ADREMOVER_FEED_SURVEY_REMOVAL.getBoolean()) {
76-
blockList.add("in_feed_survey");
77-
}
78-
if (SettingsEnum.ADREMOVER_MEDICAL_PANEL_REMOVAL.getBoolean()) {
79-
blockList.add("medical_panel");
80-
}
81-
if (SettingsEnum.ADREMOVER_PAID_CONTECT_REMOVAL.getBoolean()) {
82-
blockList.add("paid_content_overlay");
83-
}
84-
if (SettingsEnum.ADREMOVER_COMMUNITY_POSTS_REMOVAL.getBoolean()) {
85-
blockList.add("post_base_wrapper");
86-
}
87-
if (SettingsEnum.ADREMOVER_MERCHANDISE_REMOVAL.getBoolean()) {
88-
blockList.add("product_carousel");
89-
}
90-
if (SettingsEnum.ADREMOVER_SHORTS_SHELF.getBoolean()) {
91-
blockList.add("shorts_shelf");
92-
}
93-
if (SettingsEnum.ADREMOVER_INFO_PANEL_REMOVAL.getBoolean()) {
94-
blockList.add("publisher_transparency_panel");
95-
blockList.add("single_item_information_panel");
96-
}
97-
if (SettingsEnum.ADREMOVER_HIDE_SUGGESTIONS.getBoolean()) {
98-
blockList.add("horizontal_video_shelf");
99-
}
100-
if (SettingsEnum.ADREMOVER_HIDE_LATEST_POSTS.getBoolean()) {
101-
blockList.add("post_shelf");
102-
}
103-
if (SettingsEnum.ADREMOVER_HIDE_CHANNEL_GUIDELINES.getBoolean()) {
104-
blockList.add("channel_guidelines_entry_banner");
105-
}
106-
107-
if (anyMatch(blockList, path::contains)) {
108-
LogHelper.debug(GeneralBytecodeAdsPatch.class, "Blocking: " + path);
139+
if (pathBlockRegister.isBlocked(path)) {
140+
LogHelper.debug(GeneralBytecodeAdsPatch.class, "Blocked: " + path);
109141
return true;
110142
}
111143

112144
return false;
113145
}
114-
115-
private static boolean containsAny(String value, String... targets) {
116-
for (String string : targets)
117-
if (value.contains(string)) return true;
118-
return false;
119-
}
120-
121-
private static <T> boolean anyMatch(List<T> value, APredicate<? super T> predicate) {
122-
for (T t : value) {
123-
if (predicate.test(t)) return true;
124-
}
125-
return false;
126-
}
127-
128-
@FunctionalInterface
129-
public interface APredicate<T> {
130-
boolean test(T t);
131-
}
132146
}

app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public enum SettingsEnum {
3636
ADREMOVER_COMMENTS_REMOVAL("revanced_adremover_comments_removal", false, ReturnType.BOOLEAN, true),
3737
ADREMOVER_MOVIE_REMOVAL("revanced_adremover_movie", true, ReturnType.BOOLEAN, true),
3838
ADREMOVER_FEED_SURVEY_REMOVAL("revanced_adremover_feed_survey", true, ReturnType.BOOLEAN, true),
39-
ADREMOVER_SHORTS_SHELF("revanced_adremover_shorts_shelf", true, ReturnType.BOOLEAN, true),
40-
ADREMOVER_COMMUNITY_GUIDELINES("revanced_adremover_community_guidelines", true, ReturnType.BOOLEAN, true),
39+
ADREMOVER_SHORTS_SHELF_REMOVAL("revanced_adremover_shorts_shelf", true, ReturnType.BOOLEAN, true),
40+
ADREMOVER_COMMUNITY_GUIDELINES_REMOVAL("revanced_adremover_community_guidelines", true, ReturnType.BOOLEAN, true),
4141
ADREMOVER_EMERGENCY_BOX_REMOVAL("revanced_adremover_emergency_box_removal", true, ReturnType.BOOLEAN, true),
4242
ADREMOVER_INFO_PANEL_REMOVAL("revanced_adremover_info_panel", true, ReturnType.BOOLEAN, true),
4343
ADREMOVER_MEDICAL_PANEL_REMOVAL("revanced_adremover_medical_panel", true, ReturnType.BOOLEAN, true),
4444
ADREMOVER_PAID_CONTECT_REMOVAL("revanced_adremover_paid_content", true, ReturnType.BOOLEAN, true),
45-
ADREMOVER_HIDE_SUGGESTIONS("revanced_adremover_hide_suggestions", true, ReturnType.BOOLEAN, true),
45+
ADREMOVER_SUGGESTIONS_REMOVAL("revanced_adremover_hide_suggestions", true, ReturnType.BOOLEAN, true),
4646
ADREMOVER_HIDE_LATEST_POSTS("revanced_adremover_hide_latest_posts", true, ReturnType.BOOLEAN, true),
4747
ADREMOVER_HIDE_CHANNEL_GUIDELINES("revanced_adremover_hide_channel_guidelines", true, ReturnType.BOOLEAN, true),
4848

0 commit comments

Comments
 (0)