fix: patcher cleaning and improve patches load

This commit is contained in:
Alberto Ponces 2022-08-22 01:31:27 +01:00
parent af8e753ea6
commit ea532b294e
3 changed files with 90 additions and 77 deletions

View File

@ -28,7 +28,9 @@ class PatcherAPI {
File? _outFile; File? _outFile;
Future<void> initPatcher() async { Future<void> initPatcher() async {
_tmpDir = await getTemporaryDirectory(); Directory appCache = await getTemporaryDirectory();
_tmpDir = Directory('$appCache/patcher');
_tmpDir!.createSync();
_workDir = _tmpDir!.createTempSync('tmp-'); _workDir = _tmpDir!.createTempSync('tmp-');
_inputFile = File('${_workDir!.path}/base.apk'); _inputFile = File('${_workDir!.path}/base.apk');
_patchedFile = File('${_workDir!.path}/patched.apk'); _patchedFile = File('${_workDir!.path}/patched.apk');
@ -37,27 +39,33 @@ class PatcherAPI {
_cacheDir!.createSync(); _cacheDir!.createSync();
} }
Future<void> loadPatches() async { Future<bool> loadPatches() async {
if (_cacheDir == null) { if (_tmpDir == null) {
await initPatcher(); await initPatcher();
} }
if (_jarPatchBundleFile == null) { if (_jarPatchBundleFile == null) {
_jarPatchBundleFile = await _managerAPI.downloadPatches('.jar'); _jarPatchBundleFile = await _managerAPI.downloadPatches('.jar');
if (_jarPatchBundleFile != null) { if (_jarPatchBundleFile != null) {
await patcherChannel.invokeMethod<bool>( try {
'loadPatches', await patcherChannel.invokeMethod<bool>(
{ 'loadPatches',
'jarPatchBundlePath': _jarPatchBundleFile!.path, {
'cacheDirPath': _cacheDir!.path, 'jarPatchBundlePath': _jarPatchBundleFile!.path,
}, 'cacheDirPath': _cacheDir!.path,
); },
);
} on Exception {
return false;
}
} }
} }
return _jarPatchBundleFile != null;
} }
Future<List<ApplicationWithIcon>> getFilteredInstalledApps() async { Future<List<ApplicationWithIcon>> getFilteredInstalledApps() async {
List<ApplicationWithIcon> filteredPackages = []; List<ApplicationWithIcon> filteredPackages = [];
if (_jarPatchBundleFile != null) { bool isLoaded = await loadPatches();
if (isLoaded) {
try { try {
List<String>? patchesPackages = await patcherChannel List<String>? patchesPackages = await patcherChannel
.invokeListMethod<String>('getCompatiblePackages'); .invokeListMethod<String>('getCompatiblePackages');
@ -85,39 +93,42 @@ class PatcherAPI {
PatchedApplication? selectedApp, PatchedApplication? selectedApp,
) async { ) async {
List<Patch> filteredPatches = []; List<Patch> filteredPatches = [];
if (_jarPatchBundleFile != null && selectedApp != null) { if (selectedApp != null) {
try { bool isLoaded = await loadPatches();
var patches = if (isLoaded) {
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>( try {
'getFilteredPatches', var patches =
{ await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
'targetPackage': selectedApp.packageName, 'getFilteredPatches',
'targetVersion': selectedApp.version, {
'ignoreVersion': true, 'targetPackage': selectedApp.packageName,
}, 'targetVersion': selectedApp.version,
); 'ignoreVersion': true,
if (patches != null) { },
for (var patch in patches) { );
if (!filteredPatches if (patches != null) {
.any((element) => element.name == patch['name'])) { for (var patch in patches) {
filteredPatches.add( if (!filteredPatches
Patch( .any((element) => element.name == patch['name'])) {
name: patch['name'], filteredPatches.add(
simpleName: (patch['name'] as String) Patch(
.replaceAll('-', ' ') name: patch['name'],
.split('-') simpleName: (patch['name'] as String)
.join(' ') .replaceAll('-', ' ')
.toTitleCase(), .split('-')
version: patch['version'] ?? '?.?.?', .join(' ')
description: patch['description'] ?? 'N/A', .toTitleCase(),
include: patch['include'] ?? true, version: patch['version'] ?? '?.?.?',
), description: patch['description'] ?? 'N/A',
); include: patch['include'] ?? true,
),
);
}
} }
} }
} on Exception {
return List.empty();
} }
} on Exception {
return List.empty();
} }
} }
return filteredPatches; return filteredPatches;
@ -127,38 +138,41 @@ class PatcherAPI {
PatchedApplication? selectedApp, PatchedApplication? selectedApp,
) async { ) async {
List<Patch> appliedPatches = []; List<Patch> appliedPatches = [];
if (_jarPatchBundleFile != null && selectedApp != null) { if (selectedApp != null) {
try { bool isLoaded = await loadPatches();
var patches = if (isLoaded) {
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>( try {
'getFilteredPatches', var patches =
{ await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
'targetPackage': selectedApp.packageName, 'getFilteredPatches',
'targetVersion': selectedApp.version, {
'ignoreVersion': true, 'targetPackage': selectedApp.packageName,
}, 'targetVersion': selectedApp.version,
); 'ignoreVersion': true,
if (patches != null) { },
for (var patch in patches) { );
if (selectedApp.appliedPatches.contains(patch['name'])) { if (patches != null) {
appliedPatches.add( for (var patch in patches) {
Patch( if (selectedApp.appliedPatches.contains(patch['name'])) {
name: patch['name'], appliedPatches.add(
simpleName: (patch['name'] as String) Patch(
.replaceAll('-', ' ') name: patch['name'],
.split('-') simpleName: (patch['name'] as String)
.join(' ') .replaceAll('-', ' ')
.toTitleCase(), .split('-')
version: patch['version'] ?? '?.?.?', .join(' ')
description: patch['description'] ?? 'N/A', .toTitleCase(),
include: patch['include'] ?? true, version: patch['version'] ?? '?.?.?',
), description: patch['description'] ?? 'N/A',
); include: patch['include'] ?? true,
),
);
}
} }
} }
} on Exception {
return List.empty();
} }
} on Exception {
return List.empty();
} }
} }
return appliedPatches; return appliedPatches;
@ -215,17 +229,16 @@ class PatcherAPI {
} }
void cleanPatcher() { void cleanPatcher() {
if (_workDir != null) { if (_tmpDir != null) {
_workDir!.deleteSync(recursive: true); _tmpDir!.deleteSync(recursive: true);
_tmpDir = null;
} }
} }
bool sharePatchedFile(String appName, String version) { bool sharePatchedFile(String appName, String version) {
if (_outFile != null) { if (_outFile != null) {
String path = _tmpDir!.path;
String prefix = appName.toLowerCase().replaceAll(' ', '-'); String prefix = appName.toLowerCase().replaceAll(' ', '-');
String sharePath = '$path/$prefix-revanced_v$version.apk'; File share = _outFile!.renameSync('$prefix-revanced_v$version.apk');
File share = _outFile!.copySync(sharePath);
ShareExtend.share(share.path, 'file'); ShareExtend.share(share.path, 'file');
return true; return true;
} else { } else {

View File

@ -95,7 +95,7 @@ class InstallerView extends StatelessWidget {
), ),
onWillPop: () async { onWillPop: () async {
if (!model.isPatching) { if (!model.isPatching) {
model.cleanWorkplace(); model.cleanPatcher();
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
return false; return false;

View File

@ -162,7 +162,7 @@ class InstallerViewModel extends BaseViewModel {
} }
} }
Future<void> cleanWorkplace() async { Future<void> cleanPatcher() async {
_patcherAPI.cleanPatcher(); _patcherAPI.cleanPatcher();
locator<PatcherViewModel>().selectedApp = null; locator<PatcherViewModel>().selectedApp = null;
locator<PatcherViewModel>().selectedPatches.clear(); locator<PatcherViewModel>().selectedPatches.clear();