Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
fix: reduce redundant code related to version check
Browse files Browse the repository at this point in the history
  • Loading branch information
mkx173 committed Apr 14, 2024
1 parent 80b801d commit 0cae3d0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 21 deletions.
2 changes: 2 additions & 0 deletions app/src/main/assets/Purge.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
if (this.readyState == 4) {
let propertiesToDelete = [];
if (
// 吧页面更多板块
this.responseURL.match(
/https?:\/\/tieba\.baidu\.com\/c\/f\/frs\/frsBottom.*/g
)
Expand All @@ -19,6 +20,7 @@
"game_card_guide",
];
} else if (
// 一键签到页面
this.responseURL.match(
/https?:\/\/tieba\.baidu\.com\/c\/f\/forum\/getforumlist.*/g
)
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/gm/tieba/tabswitch/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ object Constants {
" ②本模块不会主动发起网络请求,不会上传任何用户数据,隐私泄露或者账号异常行为与本模块无关。\n" +
" ③本模块主要用于学习和交流技术,任何人不得将本模块用于商业或非法用途。",
"dev_tip" to "提示:您当前安装的是非正式版本,可能含有较多错误,如果您希望得到更稳定的使用体验,建议您安装正式版本。",
"exception_rules_incomplete" to "请点击确定并重启应用以重新执行反混淆。若执行反混淆后仍出现此对话框则应尝试更新模块或向作者反馈。",
"version_mismatch" to "当前贴吧版本不受支持。请使用受支持的贴吧版本(%s-%s)或尝试更新模块。",
"exception_rules_incomplete" to "请点击确定并重启应用以重新执行反混淆。若执行反混淆后仍出现此对话框则应尝试更新模块或向作者反馈。\n",
"version_mismatch" to "当前贴吧版本不受支持。请使用受支持的贴吧版本(%s-%s)或尝试更新模块。\n",
"exception_init_preference" to "初始化设置失败,请尝试更换贴吧版本。",
"regex_hint" to "请输入正则表达式,如.*",
"release_uri" to "https://github.com/GuhDoy/TiebaTS/releases",
"ci_uri" to "https://github.com/GuhDoy/TiebaTS/actions",
)
}
1 change: 1 addition & 0 deletions app/src/main/java/gm/tieba/tabswitch/XposedContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public abstract class XposedContext {
protected static String sPath;
protected static AssetManager sAssetManager;
private static Handler sHandler;
protected static boolean sIsModuleBetaVersion;
protected static void attachBaseContext(final Context context) {
if (sContextRef != null) {
throw new IllegalStateException("Base context already set");
Expand Down
19 changes: 11 additions & 8 deletions app/src/main/java/gm/tieba/tabswitch/XposedInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) thr
sClassLoader = lpparam.classLoader;
sAssetManager = XModuleResources.createInstance(sPath, null).getAssets();
mAppComponentFactory = (AppComponentFactory) sClassLoader.loadClass("com.baidu.nps.hook.component.NPSComponentFactory").newInstance();
sIsModuleBetaVersion = BuildConfig.VERSION_NAME.contains("beta");

// For some reason certain flutter page will not load in LSPatch unless we manually load the flutter plugin
XposedHelpers.findAndHookMethod(
Expand Down Expand Up @@ -108,7 +109,7 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
attachBaseContext((Application) param.args[0]);
Preferences.init(getContext());
AcRules.init(getContext());
String currTbVersion = DeobfuscationHelper.getTbVersion(getContext());
DeobfuscationHelper.sCurrentTbVersion = DeobfuscationHelper.getTbVersion(getContext());

// Workaround to address an issue with LSPatch (unable to open personal homepage)
// com.baidu.tieba.flutter.base.view.FlutterPageActivity must be instantiated by com.baidu.nps.hook.component.NPSComponentFactory
Expand Down Expand Up @@ -178,15 +179,14 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
o -> {
if (o.getRequiredVersion() != null) {
boolean isVersionSatisfied = DeobfuscationHelper.isTbSatisfyVersionRequirement(
o.getRequiredVersion(),
currTbVersion
o.getRequiredVersion()
);
if (!isVersionSatisfied) {
XposedBridge.log(
String.format(
"Skipping rule [%s] due to version mismatch (current version: %s)",
o.toString(),
currTbVersion
DeobfuscationHelper.sCurrentTbVersion
)
);
}
Expand Down Expand Up @@ -225,8 +225,7 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
final var activity = (Activity) param.thisObject;
final var messages = new ArrayList<String>();

if (DeobfuscationHelper.isTbSatisfyVersionRequirement(BuildConfig.MIN_VERSION, currTbVersion)
&& (!DeobfuscationHelper.isTbSatisfyVersionRequirement(BuildConfig.TARGET_VERSION, currTbVersion) || currTbVersion.equals(BuildConfig.TARGET_VERSION))) {
if (DeobfuscationHelper.isTbBetweenVersionRequirement(BuildConfig.MIN_VERSION, BuildConfig.TARGET_VERSION)) {
messages.add(Constants.getStrings().get("exception_rules_incomplete"));
} else {
messages.add(String.format(Locale.CHINA,
Expand All @@ -235,7 +234,7 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
}

messages.add(String.format(Locale.CHINA, "贴吧版本:%s, 模块版本:%d",
currTbVersion, BuildConfig.VERSION_CODE));
DeobfuscationHelper.sCurrentTbVersion, BuildConfig.VERSION_CODE));
messages.add(String.format(Locale.CHINA, "%d rule(s) lost: %s", lostList.size(), lostList));
final var message = TextUtils.join("\n", messages);
XposedBridge.log(message);
Expand All @@ -245,7 +244,11 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
.setNeutralButton("更新模块", (dialogInterface, i) -> {
final Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("https://github.com/GuhDoy/TiebaTS/releases"));
if (sIsModuleBetaVersion) {
intent.setData(Uri.parse(Constants.getStrings().get("ci_uri")));
} else {
intent.setData(Uri.parse(Constants.getStrings().get("release_uri")));
}
activity.startActivity(intent);
})
.setNegativeButton(activity.getString(android.R.string.cancel), null)
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/gm/tieba/tabswitch/hooker/TSPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private void proxyPage(final Activity activity, final NavigationBar navigationBa
private void startRootPreferenceActivity(final Activity activity) {
if (!Preferences.getIsEULAAccepted()) {
final StringBuilder stringBuilder = new StringBuilder().append(Constants.getStrings().get("EULA"));
if (BuildConfig.VERSION_NAME.contains("beta")) {
if (sIsModuleBetaVersion) {
stringBuilder.append("\n\n").append(Constants.getStrings().get("dev_tip"));
}
AlertDialog alert = new AlertDialog.Builder(activity, DisplayUtils.isLightMode(getContext()) ?
Expand Down Expand Up @@ -278,7 +278,11 @@ private LinearLayout createRootPreference(final Activity activity) {
preferenceLayout.addView(TSPreferenceHelper.createButton("版本", String.format(Locale.CHINA, "%s", BuildConfig.VERSION_NAME), true, v -> {
final Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("https://github.com/GuhDoy/TiebaTS/releases"));
if (sIsModuleBetaVersion) {
intent.setData(Uri.parse(Constants.getStrings().get("ci_uri")));
} else {
intent.setData(Uri.parse(Constants.getStrings().get("release_uri")));
}
activity.startActivity(intent);
}));
return preferenceLayout;
Expand All @@ -295,7 +299,7 @@ private LinearLayout createModifyTabPreference(final Activity activity) {
preferenceLayout.addView(TSPreferenceHelper.createTextView("其他"));
SwitchButtonHolder transitionAnimation = new SwitchButtonHolder(activity, "修复过渡动画", "transition_animation", SwitchButtonHolder.TYPE_SWITCH);

boolean shouldEnableTransitionAnimationFix = Build.VERSION.SDK_INT >= 34 && DeobfuscationHelper.isTbSatisfyVersionRequirement("12.58.2.1", DeobfuscationHelper.getTbVersion(getContext()));
boolean shouldEnableTransitionAnimationFix = Build.VERSION.SDK_INT >= 34 && DeobfuscationHelper.isTbSatisfyVersionRequirement("12.58.2.1");
if (!shouldEnableTransitionAnimationFix && Preferences.getTransitionAnimationEnabled()) {
transitionAnimation.bdSwitch.turnOff();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String key() {

@Override
public void hook() throws Throwable {
if (!(Build.VERSION.SDK_INT >= 34 && DeobfuscationHelper.isTbSatisfyVersionRequirement("12.58.2.1", DeobfuscationHelper.getTbVersion(getContext())))) {
if (!(Build.VERSION.SDK_INT >= 34 && DeobfuscationHelper.isTbSatisfyVersionRequirement("12.58.2.1"))) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class DeobfuscationHelper {
private static final int SIGNATURE_DATA_START_OFFSET = 32;
private static final int SIGNATURE_SIZE = 20;
public static String sCurrentTbVersion;

static byte[] calcSignature(final InputStream dataStoreInput) throws IOException {
final MessageDigest md;
Expand Down Expand Up @@ -108,8 +109,8 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
}

// Adapted from https://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java
public static boolean isTbSatisfyVersionRequirement(final String requiredVersion, final String currentVersion) {
String[] currParts = currentVersion.split("\\.");
public static boolean isTbSatisfyVersionRequirement(final String requiredVersion) {
String[] currParts = sCurrentTbVersion.split("\\.");
String[] reqParts = requiredVersion.split("\\.");
int length = Math.max(currParts.length, reqParts.length);
for(int i = 0; i < length; i++) {
Expand All @@ -127,4 +128,10 @@ public static boolean isTbSatisfyVersionRequirement(final String requiredVersion
}
return true;
}

// Inclusive of both ends
public static boolean isTbBetweenVersionRequirement(final String lower, final String upper) {
return isTbSatisfyVersionRequirement(lower)
&& (!isTbSatisfyVersionRequirement(upper) || sCurrentTbVersion.equals(upper));
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/gm/tieba/tabswitch/hooker/eliminate/Purge.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
JSONObject syncData = ReflectUtils.getObjectField(param.thisObject, JSONObject.class);
JSONArray indexTabInfo = syncData.getJSONArray("index_tab_info");
JSONArray newIndexTabInfo = new JSONArray();
if (DeobfuscationHelper.isTbSatisfyVersionRequirement("12.59", DeobfuscationHelper.getTbVersion(getContext()))) {
if (DeobfuscationHelper.isTbSatisfyVersionRequirement("12.59")) {
for (int i = 0; i < indexTabInfo.length(); i++) {
JSONObject currTab = indexTabInfo.getJSONObject(i);
if (currTab.getString("is_main_tab").equals("1") && !currTab.getString("tab_type").equals("6")) {
Expand Down Expand Up @@ -335,14 +335,14 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
XposedBridge.hookMethod(method, XC_MethodReplacement.returnConstant(false));
}
}
// 更多板块 (吧友直播,友情吧)
final String jsPurgeFrsBottom = FileUtils.getAssetFileContent("Purge.js");
if (jsPurgeFrsBottom != null) {
// 更多板块 (吧友直播,友情吧), 一键签到页面广告
final String jsPurgeScript = FileUtils.getAssetFileContent("Purge.js");
if (jsPurgeScript != null) {
XposedHelpers.findAndHookMethod(WebViewClient.class, "onPageStarted", WebView.class, String.class, Bitmap.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
WebView mWebView = (WebView) param.args[0];
mWebView.evaluateJavascript(jsPurgeFrsBottom, null);
mWebView.evaluateJavascript(jsPurgeScript, null);
}
});
}
Expand Down

0 comments on commit 0cae3d0

Please sign in to comment.