From a1a7d51ea1568f4e7f09b32519a15af5f3eed01c Mon Sep 17 00:00:00 2001 From: aAbed Date: Tue, 5 Sep 2023 13:30:13 +0545 Subject: [PATCH 1/2] fix: load patches via `PatchBundle` --- .../revanced/manager/flutter/MainActivity.kt | 26 +++++++++++ lib/services/github_api.dart | 4 +- lib/services/manager_api.dart | 46 +++++++++++-------- lib/services/patcher_api.dart | 1 - 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt b/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt index 2c11653df5..7eae89f3d1 100644 --- a/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt +++ b/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt @@ -11,6 +11,9 @@ import app.revanced.patcher.PatchBundleLoader import app.revanced.patcher.Patcher import app.revanced.patcher.PatcherOptions import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages +import app.revanced.patcher.extensions.PatchExtensions.dependencies +import app.revanced.patcher.extensions.PatchExtensions.description +import app.revanced.patcher.extensions.PatchExtensions.include import app.revanced.patcher.extensions.PatchExtensions.patchName import app.revanced.patcher.patch.PatchResult import io.flutter.embedding.android.FlutterActivity @@ -90,6 +93,29 @@ class MainActivity : FlutterActivity() { stopResult = result } + "getPatches" -> { + val patchBundleFilePath = call.argument("patchBundleFilePath") + if (patchBundleFilePath != null) { + val patches = PatchBundleLoader.Dex( + File(patchBundleFilePath) + ).map { patch -> + val map = HashMap() + map["\"name\""] = "\"${patch.patchName.replace("\"","\\\"")}\"" + map["\"description\""] = "\"${patch.description?.replace("\"","\\\"")}\"" + map["\"excluded\""] = !patch.include + map["\"dependencies\""] = patch.dependencies?.map { "\"${it.java.patchName}\"" } ?: emptyList() + map["\"compatiblePackages\""] = patch.compatiblePackages?.map { + val map2 = HashMap() + map2["\"name\""] = "\"${it.name}\"" + map2["\"versions\""] = it.versions.map { version -> "\"${version}\"" } + map2 + } ?: emptyList() + map + } + result.success(patches) + } else result.notImplemented() + } + else -> result.notImplemented() } } diff --git a/lib/services/github_api.dart b/lib/services/github_api.dart index de8c160b2b..0949f1b9a3 100644 --- a/lib/services/github_api.dart +++ b/lib/services/github_api.dart @@ -222,10 +222,8 @@ class GithubAPI { final String downloadUrl = asset['browser_download_url']; if (extension == '.apk') { _managerAPI.setIntegrationsDownloadURL(downloadUrl); - } else if (extension == '.json') { - _managerAPI.setPatchesDownloadURL(downloadUrl, false); } else { - _managerAPI.setPatchesDownloadURL(downloadUrl, true); + _managerAPI.setPatchesDownloadURL(downloadUrl); } return await DefaultCacheManager().getSingleFile( downloadUrl, diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index 6551d0f445..eeb427dc14 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -11,6 +11,7 @@ import 'package:revanced_manager/app/app.locator.dart'; import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patched_application.dart'; import 'package:revanced_manager/services/github_api.dart'; +import 'package:revanced_manager/services/patcher_api.dart'; import 'package:revanced_manager/services/revanced_api.dart'; import 'package:revanced_manager/services/root_api.dart'; import 'package:revanced_manager/ui/widgets/shared/custom_material_button.dart'; @@ -79,12 +80,12 @@ class ManagerAPI { await _prefs.setString('repoUrl', url); } - String getPatchesDownloadURL(bool bundle) { - return _prefs.getString('patchesDownloadURL-$bundle') ?? ''; + String getPatchesDownloadURL() { + return _prefs.getString('patchesDownloadURL') ?? ''; } - Future setPatchesDownloadURL(String value, bool bundle) async { - await _prefs.setString('patchesDownloadURL-$bundle', value); + Future setPatchesDownloadURL(String value) async { + await _prefs.setString('patchesDownloadURL', value); } String getPatchesRepo() { @@ -300,28 +301,34 @@ class ManagerAPI { } Future> getPatches() async { - try { - final String repoName = getPatchesRepo(); - final String currentVersion = await getCurrentPatchesVersion(); - final String url = getPatchesDownloadURL(false); - return await _githubAPI.getPatches( - repoName, - currentVersion, - url, - ); - } on Exception catch (e) { - if (kDebugMode) { - print(e); + final File? patchBundleFile = await downloadPatches(); + if (patchBundleFile != null) { + try { + final patches = await PatcherAPI.patcherChannel.invokeMethod( + 'getPatches', + { + 'patchBundleFilePath': patchBundleFile.path, + }, + ); + final List> patchesMap = []; + patches.forEach((patch) { + patchesMap.add(jsonDecode('$patch')); + }); + return patchesMap.map((patch) => Patch.fromJson(patch)).toList(); + } on Exception catch (e) { + if (kDebugMode) { + print(e); + } } - return []; } + return List.empty(); } Future downloadPatches() async { try { final String repoName = getPatchesRepo(); final String currentVersion = await getCurrentPatchesVersion(); - final String url = getPatchesDownloadURL(true); + final String url = getPatchesDownloadURL(); return await _githubAPI.getPatchesReleaseFile( '.jar', repoName, @@ -447,8 +454,7 @@ class ManagerAPI { Future setCurrentPatchesVersion(String version) async { await _prefs.setString('patchesVersion', version); - await setPatchesDownloadURL('', false); - await setPatchesDownloadURL('', true); + await setPatchesDownloadURL(''); await downloadPatches(); } diff --git a/lib/services/patcher_api.dart b/lib/services/patcher_api.dart index dfaaf3ec95..bac7d3c1ef 100644 --- a/lib/services/patcher_api.dart +++ b/lib/services/patcher_api.dart @@ -30,7 +30,6 @@ class PatcherAPI { Future initialize() async { await _loadPatches(); - await _managerAPI.downloadPatches(); await _managerAPI.downloadIntegrations(); final Directory appCache = await getTemporaryDirectory(); _dataDir = await getExternalStorageDirectory() ?? appCache; From f1c470f474390a47684c00ac2b813dd1e066ed70 Mon Sep 17 00:00:00 2001 From: aAbed <39409020+TheAabedKhan@users.noreply.github.com> Date: Sat, 16 Sep 2023 09:36:01 +0545 Subject: [PATCH 2/2] refactor: reuse the parsed patches --- lib/services/manager_api.dart | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index eeb427dc14..51c7f23ad8 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -27,6 +27,7 @@ class ManagerAPI { final String patcherRepo = 'revanced-patcher'; final String cliRepo = 'revanced-cli'; late SharedPreferences _prefs; + List patches = []; bool isRooted = false; String storedPatchesFile = '/selected-patches.json'; String keystoreFile = @@ -301,20 +302,24 @@ class ManagerAPI { } Future> getPatches() async { + if (patches.isNotEmpty) { + return patches; + } final File? patchBundleFile = await downloadPatches(); if (patchBundleFile != null) { try { - final patches = await PatcherAPI.patcherChannel.invokeMethod( + final patchesObject = await PatcherAPI.patcherChannel.invokeMethod( 'getPatches', { 'patchBundleFilePath': patchBundleFile.path, }, ); final List> patchesMap = []; - patches.forEach((patch) { + patchesObject.forEach((patch) { patchesMap.add(jsonDecode('$patch')); }); - return patchesMap.map((patch) => Patch.fromJson(patch)).toList(); + patches = patchesMap.map((patch) => Patch.fromJson(patch)).toList(); + return patches; } on Exception catch (e) { if (kDebugMode) { print(e);