Mod for TikTok 47.1.3 (arm64) in Material 3 style: bottom bar, profile menu, TikTok settings and comments; downloads without the watermark; switches for ads, live streams and the Shop tab; badges of other mods next to names and the niche mod badge (members service); app icons; mod settings screen. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
235 lines
9.7 KiB
Python
235 lines
9.7 KiB
Python
"""Take advertisements and live streams out of TikTok 47.1.3's feed.
|
|
|
|
A page of the feed is a FeedItemList; its items are Aweme objects, and an advertisement or a live
|
|
room is just an Aweme with a flag or a room attached. Every read of the page goes through a few
|
|
getters, so each of them first lets lihont.two.feed.FeedFilter clean the page:
|
|
|
|
FeedItemList.getItems() advertisements and live rooms are removed from the list
|
|
FeedItemList.setItems(List) the same for a list that is put in
|
|
FeedItemList.getPreloadAds() the preloaded advertisements are dropped
|
|
FeedItemList.isHasAd() the page no longer claims to hold one
|
|
|
|
Three other places fetch or hold advertisements outside the pages and are switched off while the
|
|
setting is on: the mid-feed ad response, the candidate ad request and the app-open "top view"
|
|
store. Each gets one early return, taken only when FeedFilter.ads() says so.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
HELPER = "Llihont/two/feed/FeedFilter;"
|
|
ADS = HELPER + "->ads()Z"
|
|
CLEAN = HELPER + "->cleanPage(Ljava/lang/Object;)V"
|
|
FILTER = HELPER + "->filter(Ljava/util/List;)Ljava/util/List;"
|
|
|
|
FEED_PAGE = "Lcom/ss/android/ugc/aweme/feed/model/FeedItemList;"
|
|
MID_AD = "Lcom/ss/android/ugc/aweme/commercialize/feed/assem/midad/MidAdResponse;"
|
|
CANDIDATES = "Lcom/ss/android/ugc/aweme/commercialize/feed/candidate/CandidateRequestImpl;"
|
|
TOP_VIEW = "Lcom/bytedance/ies/ugc/aweme/commercialize/splash/topview/TopViewJsonManager;"
|
|
|
|
# owner -> (dex that holds it in TikTok 47.1.3)
|
|
DEXES = {
|
|
FEED_PAGE: "classes36.dex",
|
|
MID_AD: "classes35.dex",
|
|
CANDIDATES: "classes10.dex",
|
|
TOP_VIEW: "classes17.dex",
|
|
}
|
|
|
|
MARKER = "# lihont: feed filter"
|
|
|
|
_METHOD = re.compile(r"^\.method ([^\n]*)\n(.*?)^\.end method", re.M | re.S)
|
|
_REGISTERS = re.compile(r"^ \.registers (\d+)$", re.M)
|
|
|
|
|
|
def runtime_targets():
|
|
"""Runtime methods the rewrites call, as they appear in a method table."""
|
|
return [ADS, CLEAN, FILTER]
|
|
|
|
|
|
def _parameter_registers(header: str) -> int:
|
|
"""Registers taken by `this` and the arguments of a method header like `public final x(IJ)V`."""
|
|
signature = re.search(r"\((.*?)\)", header).group(1)
|
|
count = 0 if " static " in " " + header + " " else 1
|
|
index = 0
|
|
while index < len(signature):
|
|
char = signature[index]
|
|
if char in "JD":
|
|
count += 2
|
|
index += 1
|
|
elif char in "ZBSCIF":
|
|
count += 1
|
|
index += 1
|
|
else:
|
|
while signature[index] == "[":
|
|
index += 1
|
|
if signature[index] == "L":
|
|
index = signature.index(";", index) + 1
|
|
else:
|
|
index += 1
|
|
count += 1
|
|
return count
|
|
|
|
|
|
def _first_instruction(body: str) -> int:
|
|
"""Offset in `body` of the first line that is code: past .registers, annotations and debug lines."""
|
|
offset = 0
|
|
in_annotation = False
|
|
for line in body.splitlines(keepends=True):
|
|
stripped = line.strip()
|
|
if in_annotation:
|
|
if stripped.startswith(".end annotation"):
|
|
in_annotation = False
|
|
elif stripped.startswith(".annotation"):
|
|
in_annotation = True
|
|
elif stripped and not stripped.startswith(
|
|
(".registers", ".locals", ".prologue", ".line", ".param", ".end param", "#")):
|
|
return offset
|
|
offset += len(line)
|
|
raise ValueError("Method has no code")
|
|
|
|
|
|
def _with_local(header: str, body: str) -> str:
|
|
"""Make sure `v0` is a local register of the method, so a hook can use it before any real code."""
|
|
match = _REGISTERS.search(body)
|
|
if match is None:
|
|
raise ValueError("Method has no .registers line")
|
|
total = int(match.group(1))
|
|
if total - _parameter_registers(header) >= 1:
|
|
return body
|
|
return body[:match.start()] + " .registers %d" % (total + 1) + body[match.end():]
|
|
|
|
|
|
def _rewrite_methods(text: str, owner: str, rewrite) -> tuple[str, dict[str, int]]:
|
|
"""Apply rewrite(header, body, hits) to every method of the class and return the new text."""
|
|
if not re.search(r"^\.class [^\n]*" + re.escape(owner) + r"$", text, re.M):
|
|
raise ValueError("Not the class " + owner)
|
|
hits: dict[str, int] = {}
|
|
|
|
def method(match: re.Match) -> str:
|
|
header, body = match.group(1), match.group(2)
|
|
if MARKER in body:
|
|
raise ValueError("Feed filter already present in " + owner)
|
|
changed = rewrite(header, body, hits)
|
|
if changed is None:
|
|
return match.group(0)
|
|
return ".method %s\n%s.end method" % (header, changed)
|
|
|
|
return _METHOD.sub(method, text), hits
|
|
|
|
|
|
def _early_return(header: str, body: str, code: str) -> str:
|
|
"""Put `if (FeedFilter.ads()) { code }` in front of the method's own code."""
|
|
body = _with_local(header, body)
|
|
at = _first_instruction(body)
|
|
guard = (
|
|
" %s\n invoke-static {}, %s\n\n move-result v0\n\n if-eqz v0, :lihont_keep\n\n%s\n"
|
|
" :lihont_keep\n" % (MARKER, ADS, code)
|
|
)
|
|
return body[:at] + guard + body[at:]
|
|
|
|
|
|
def _entry_call(body: str, call: str) -> str:
|
|
at = _first_instruction(body)
|
|
return body[:at] + " %s\n%s\n" % (MARKER, call) + body[at:]
|
|
|
|
|
|
def rewrite_feed_page(text: str) -> tuple[str, dict[str, int]]:
|
|
"""FeedItemList: clean the page when it is read or written."""
|
|
clean = " invoke-static {p0}, %s\n" % CLEAN
|
|
filtering = (" invoke-static {p1}, %s\n\n move-result-object p1\n\n"
|
|
" invoke-static {p0}, %s\n" % (FILTER, CLEAN))
|
|
|
|
def rewrite(header: str, body: str, hits: dict) -> str | None:
|
|
if header.endswith(" getItems()Ljava/util/List;"):
|
|
if "->items:Ljava/util/List;" not in body:
|
|
raise ValueError("FeedItemList.getItems() changed")
|
|
hits["items_read"] = 1
|
|
return _entry_call(body, clean)
|
|
if header.endswith(" setItems(Ljava/util/List;)V"):
|
|
if "->items:Ljava/util/List;" not in body:
|
|
raise ValueError("FeedItemList.setItems() changed")
|
|
hits["items_write"] = 1
|
|
return _entry_call(body, filtering)
|
|
if header.endswith(" getPreloadAds()Ljava/util/List;"):
|
|
if "->preloadAds:Ljava/util/List;" not in body:
|
|
raise ValueError("FeedItemList.getPreloadAds() changed")
|
|
hits["preload_read"] = 1
|
|
return _entry_call(body, clean)
|
|
if header.endswith(" isHasAd()Z"):
|
|
if "->hasAd:Z" not in body:
|
|
raise ValueError("FeedItemList.isHasAd() changed")
|
|
hits["ad_flag"] = 1
|
|
return _entry_call(body, clean)
|
|
return None
|
|
|
|
result, hits = _rewrite_methods(text, FEED_PAGE, rewrite)
|
|
if len(hits) != 4:
|
|
raise ValueError("FeedItemList is missing some of the methods this rewrite expects: %s" % sorted(hits))
|
|
return result, hits
|
|
|
|
|
|
def rewrite_mid_ad(text: str) -> tuple[str, dict[str, int]]:
|
|
"""MidAdResponse.getAweme(): no mid-feed advertisement while ads are off."""
|
|
def rewrite(header: str, body: str, hits: dict) -> str | None:
|
|
if not header.endswith(" getAweme()Lcom/ss/android/ugc/aweme/feed/model/Aweme;"):
|
|
return None
|
|
if "->aweme:Lcom/ss/android/ugc/aweme/feed/model/Aweme;" not in body:
|
|
raise ValueError("Mid-feed advertisement response changed")
|
|
hits["mid_ad"] = 1
|
|
# the one caller handles a missing advertisement
|
|
return _early_return(header, body, " const/4 v0, 0x0\n\n return-object v0\n")
|
|
|
|
result, hits = _rewrite_methods(text, MID_AD, rewrite)
|
|
if len(hits) != 1:
|
|
raise ValueError("MidAdResponse.getAweme() not found")
|
|
return result, hits
|
|
|
|
|
|
def rewrite_candidates(text: str) -> tuple[str, dict[str, int]]:
|
|
"""CandidateRequestImpl.LIZ(String, String, List): do not ask for candidate advertisements."""
|
|
def rewrite(header: str, body: str, hits: dict) -> str | None:
|
|
if not header.endswith(" LIZ(Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)V"):
|
|
return None
|
|
if "CandidateAdRequestApi;->request(" not in body:
|
|
raise ValueError("Candidate advertisement request changed")
|
|
hits["candidate_request"] = 1
|
|
return _early_return(header, body, " return-void\n")
|
|
|
|
result, hits = _rewrite_methods(text, CANDIDATES, rewrite)
|
|
if len(hits) != 1:
|
|
raise ValueError("CandidateRequestImpl.LIZ(...) not found")
|
|
return result, hits
|
|
|
|
|
|
def rewrite_top_view(text: str) -> tuple[str, dict[str, int]]:
|
|
"""TopViewJsonManager: no app-open advertisement and an empty store of them while ads are off."""
|
|
def rewrite(header: str, body: str, hits: dict) -> str | None:
|
|
if header.endswith(" LIZIZ(Ljava/lang/String;)Lcom/ss/android/ugc/aweme/feed/model/Aweme;"):
|
|
if "->LIZLLL(Ljava/lang/String;)Ljava/util/Map;" not in body:
|
|
raise ValueError("Top view advertisement getter changed")
|
|
hits["top_view"] = 1
|
|
return _early_return(header, body, " const/4 v0, 0x0\n\n return-object v0\n")
|
|
if header.endswith(" LJ()Ljava/util/Map;"):
|
|
if "->LJ:Ljava/util/concurrent/ConcurrentHashMap;" not in body:
|
|
raise ValueError("Top view advertisement store changed")
|
|
hits["top_view_store"] = 1
|
|
return _early_return(
|
|
header, body,
|
|
" new-instance v0, Ljava/util/HashMap;\n\n"
|
|
" invoke-direct {v0}, Ljava/util/HashMap;-><init>()V\n\n return-object v0\n")
|
|
return None
|
|
|
|
result, hits = _rewrite_methods(text, TOP_VIEW, rewrite)
|
|
if len(hits) != 2:
|
|
raise ValueError("TopViewJsonManager is missing some of the methods this rewrite expects: %s" % sorted(hits))
|
|
return result, hits
|
|
|
|
|
|
REWRITES = {
|
|
FEED_PAGE: rewrite_feed_page,
|
|
MID_AD: rewrite_mid_ad,
|
|
CANDIDATES: rewrite_candidates,
|
|
TOP_VIEW: rewrite_top_view,
|
|
}
|