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,13 +39,14 @@ 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) {
try {
await patcherChannel.invokeMethod<bool>( await patcherChannel.invokeMethod<bool>(
'loadPatches', 'loadPatches',
{ {
@ -51,13 +54,18 @@ class PatcherAPI {
'cacheDirPath': _cacheDir!.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,7 +93,9 @@ class PatcherAPI {
PatchedApplication? selectedApp, PatchedApplication? selectedApp,
) async { ) async {
List<Patch> filteredPatches = []; List<Patch> filteredPatches = [];
if (_jarPatchBundleFile != null && selectedApp != null) { if (selectedApp != null) {
bool isLoaded = await loadPatches();
if (isLoaded) {
try { try {
var patches = var patches =
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>( await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
@ -120,6 +130,7 @@ class PatcherAPI {
return List.empty(); return List.empty();
} }
} }
}
return filteredPatches; return filteredPatches;
} }
@ -127,7 +138,9 @@ class PatcherAPI {
PatchedApplication? selectedApp, PatchedApplication? selectedApp,
) async { ) async {
List<Patch> appliedPatches = []; List<Patch> appliedPatches = [];
if (_jarPatchBundleFile != null && selectedApp != null) { if (selectedApp != null) {
bool isLoaded = await loadPatches();
if (isLoaded) {
try { try {
var patches = var patches =
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>( await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
@ -161,6 +174,7 @@ class PatcherAPI {
return List.empty(); 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();