From ea532b294ebaea7e464fe947a6c2a15f5ac802a7 Mon Sep 17 00:00:00 2001 From: Alberto Ponces Date: Mon, 22 Aug 2022 01:31:27 +0100 Subject: [PATCH] fix: patcher cleaning and improve patches load --- lib/services/patcher_api.dart | 163 ++++++++++-------- lib/ui/views/installer/installer_view.dart | 2 +- .../views/installer/installer_viewmodel.dart | 2 +- 3 files changed, 90 insertions(+), 77 deletions(-) diff --git a/lib/services/patcher_api.dart b/lib/services/patcher_api.dart index b7681e94..241fb458 100644 --- a/lib/services/patcher_api.dart +++ b/lib/services/patcher_api.dart @@ -28,7 +28,9 @@ class PatcherAPI { File? _outFile; Future initPatcher() async { - _tmpDir = await getTemporaryDirectory(); + Directory appCache = await getTemporaryDirectory(); + _tmpDir = Directory('$appCache/patcher'); + _tmpDir!.createSync(); _workDir = _tmpDir!.createTempSync('tmp-'); _inputFile = File('${_workDir!.path}/base.apk'); _patchedFile = File('${_workDir!.path}/patched.apk'); @@ -37,27 +39,33 @@ class PatcherAPI { _cacheDir!.createSync(); } - Future loadPatches() async { - if (_cacheDir == null) { + Future loadPatches() async { + if (_tmpDir == null) { await initPatcher(); } if (_jarPatchBundleFile == null) { _jarPatchBundleFile = await _managerAPI.downloadPatches('.jar'); if (_jarPatchBundleFile != null) { - await patcherChannel.invokeMethod( - 'loadPatches', - { - 'jarPatchBundlePath': _jarPatchBundleFile!.path, - 'cacheDirPath': _cacheDir!.path, - }, - ); + try { + await patcherChannel.invokeMethod( + 'loadPatches', + { + 'jarPatchBundlePath': _jarPatchBundleFile!.path, + 'cacheDirPath': _cacheDir!.path, + }, + ); + } on Exception { + return false; + } } } + return _jarPatchBundleFile != null; } Future> getFilteredInstalledApps() async { List filteredPackages = []; - if (_jarPatchBundleFile != null) { + bool isLoaded = await loadPatches(); + if (isLoaded) { try { List? patchesPackages = await patcherChannel .invokeListMethod('getCompatiblePackages'); @@ -85,39 +93,42 @@ class PatcherAPI { PatchedApplication? selectedApp, ) async { List filteredPatches = []; - if (_jarPatchBundleFile != null && selectedApp != null) { - try { - var patches = - await patcherChannel.invokeListMethod>( - 'getFilteredPatches', - { - 'targetPackage': selectedApp.packageName, - 'targetVersion': selectedApp.version, - 'ignoreVersion': true, - }, - ); - if (patches != null) { - for (var patch in patches) { - if (!filteredPatches - .any((element) => element.name == patch['name'])) { - filteredPatches.add( - Patch( - name: patch['name'], - simpleName: (patch['name'] as String) - .replaceAll('-', ' ') - .split('-') - .join(' ') - .toTitleCase(), - version: patch['version'] ?? '?.?.?', - description: patch['description'] ?? 'N/A', - include: patch['include'] ?? true, - ), - ); + if (selectedApp != null) { + bool isLoaded = await loadPatches(); + if (isLoaded) { + try { + var patches = + await patcherChannel.invokeListMethod>( + 'getFilteredPatches', + { + 'targetPackage': selectedApp.packageName, + 'targetVersion': selectedApp.version, + 'ignoreVersion': true, + }, + ); + if (patches != null) { + for (var patch in patches) { + if (!filteredPatches + .any((element) => element.name == patch['name'])) { + filteredPatches.add( + Patch( + name: patch['name'], + simpleName: (patch['name'] as String) + .replaceAll('-', ' ') + .split('-') + .join(' ') + .toTitleCase(), + version: patch['version'] ?? '?.?.?', + description: patch['description'] ?? 'N/A', + include: patch['include'] ?? true, + ), + ); + } } } + } on Exception { + return List.empty(); } - } on Exception { - return List.empty(); } } return filteredPatches; @@ -127,38 +138,41 @@ class PatcherAPI { PatchedApplication? selectedApp, ) async { List appliedPatches = []; - if (_jarPatchBundleFile != null && selectedApp != null) { - try { - var patches = - await patcherChannel.invokeListMethod>( - 'getFilteredPatches', - { - 'targetPackage': selectedApp.packageName, - 'targetVersion': selectedApp.version, - 'ignoreVersion': true, - }, - ); - if (patches != null) { - for (var patch in patches) { - if (selectedApp.appliedPatches.contains(patch['name'])) { - appliedPatches.add( - Patch( - name: patch['name'], - simpleName: (patch['name'] as String) - .replaceAll('-', ' ') - .split('-') - .join(' ') - .toTitleCase(), - version: patch['version'] ?? '?.?.?', - description: patch['description'] ?? 'N/A', - include: patch['include'] ?? true, - ), - ); + if (selectedApp != null) { + bool isLoaded = await loadPatches(); + if (isLoaded) { + try { + var patches = + await patcherChannel.invokeListMethod>( + 'getFilteredPatches', + { + 'targetPackage': selectedApp.packageName, + 'targetVersion': selectedApp.version, + 'ignoreVersion': true, + }, + ); + if (patches != null) { + for (var patch in patches) { + if (selectedApp.appliedPatches.contains(patch['name'])) { + appliedPatches.add( + Patch( + name: patch['name'], + simpleName: (patch['name'] as String) + .replaceAll('-', ' ') + .split('-') + .join(' ') + .toTitleCase(), + version: patch['version'] ?? '?.?.?', + description: patch['description'] ?? 'N/A', + include: patch['include'] ?? true, + ), + ); + } } } + } on Exception { + return List.empty(); } - } on Exception { - return List.empty(); } } return appliedPatches; @@ -215,17 +229,16 @@ class PatcherAPI { } void cleanPatcher() { - if (_workDir != null) { - _workDir!.deleteSync(recursive: true); + if (_tmpDir != null) { + _tmpDir!.deleteSync(recursive: true); + _tmpDir = null; } } bool sharePatchedFile(String appName, String version) { if (_outFile != null) { - String path = _tmpDir!.path; String prefix = appName.toLowerCase().replaceAll(' ', '-'); - String sharePath = '$path/$prefix-revanced_v$version.apk'; - File share = _outFile!.copySync(sharePath); + File share = _outFile!.renameSync('$prefix-revanced_v$version.apk'); ShareExtend.share(share.path, 'file'); return true; } else { diff --git a/lib/ui/views/installer/installer_view.dart b/lib/ui/views/installer/installer_view.dart index f21bbc4f..b4851448 100644 --- a/lib/ui/views/installer/installer_view.dart +++ b/lib/ui/views/installer/installer_view.dart @@ -95,7 +95,7 @@ class InstallerView extends StatelessWidget { ), onWillPop: () async { if (!model.isPatching) { - model.cleanWorkplace(); + model.cleanPatcher(); Navigator.of(context).pop(); } return false; diff --git a/lib/ui/views/installer/installer_viewmodel.dart b/lib/ui/views/installer/installer_viewmodel.dart index d6ec89a5..d4d10112 100644 --- a/lib/ui/views/installer/installer_viewmodel.dart +++ b/lib/ui/views/installer/installer_viewmodel.dart @@ -162,7 +162,7 @@ class InstallerViewModel extends BaseViewModel { } } - Future cleanWorkplace() async { + Future cleanPatcher() async { _patcherAPI.cleanPatcher(); locator().selectedApp = null; locator().selectedPatches.clear();