perf: Do not load patches twice

This commit is contained in:
oSumAtrIX 2023-10-03 18:54:51 +02:00
parent 4de274bf62
commit 012110f008
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
3 changed files with 25 additions and 28 deletions

View File

@ -8,6 +8,7 @@ import app.revanced.manager.flutter.utils.signing.Signer
import app.revanced.manager.flutter.utils.zip.ZipFile import app.revanced.manager.flutter.utils.zip.ZipFile
import app.revanced.manager.flutter.utils.zip.structures.ZipEntry import app.revanced.manager.flutter.utils.zip.structures.ZipEntry
import app.revanced.patcher.PatchBundleLoader import app.revanced.patcher.PatchBundleLoader
import app.revanced.patcher.PatchSet
import app.revanced.patcher.Patcher import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherOptions import app.revanced.patcher.PatcherOptions
import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResult
@ -31,6 +32,8 @@ class MainActivity : FlutterActivity() {
private var cancel: Boolean = false private var cancel: Boolean = false
private var stopResult: MethodChannel.Result? = null private var stopResult: MethodChannel.Result? = null
private lateinit var patches: PatchSet
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine) super.configureFlutterEngine(flutterEngine)
@ -46,7 +49,6 @@ class MainActivity : FlutterActivity() {
mainChannel.setMethodCallHandler { call, result -> mainChannel.setMethodCallHandler { call, result ->
when (call.method) { when (call.method) {
"runPatcher" -> { "runPatcher" -> {
val patchBundleFilePath = call.argument<String>("patchBundleFilePath")
val originalFilePath = call.argument<String>("originalFilePath") val originalFilePath = call.argument<String>("originalFilePath")
val inputFilePath = call.argument<String>("inputFilePath") val inputFilePath = call.argument<String>("inputFilePath")
val patchedFilePath = call.argument<String>("patchedFilePath") val patchedFilePath = call.argument<String>("patchedFilePath")
@ -57,7 +59,7 @@ class MainActivity : FlutterActivity() {
val keyStoreFilePath = call.argument<String>("keyStoreFilePath") val keyStoreFilePath = call.argument<String>("keyStoreFilePath")
val keystorePassword = call.argument<String>("keystorePassword") val keystorePassword = call.argument<String>("keystorePassword")
if (patchBundleFilePath != null && if (
originalFilePath != null && originalFilePath != null &&
inputFilePath != null && inputFilePath != null &&
patchedFilePath != null && patchedFilePath != null &&
@ -71,7 +73,6 @@ class MainActivity : FlutterActivity() {
cancel = false cancel = false
runPatcher( runPatcher(
result, result,
patchBundleFilePath,
originalFilePath, originalFilePath,
inputFilePath, inputFilePath,
patchedFilePath, patchedFilePath,
@ -94,9 +95,8 @@ class MainActivity : FlutterActivity() {
val patchBundleFilePath = call.argument<String>("patchBundleFilePath")!! val patchBundleFilePath = call.argument<String>("patchBundleFilePath")!!
val cacheDirPath = call.argument<String>("cacheDirPath")!! val cacheDirPath = call.argument<String>("cacheDirPath")!!
JSONArray().apply {
try { try {
PatchBundleLoader.Dex( patches = PatchBundleLoader.Dex(
File(patchBundleFilePath), File(patchBundleFilePath),
optimizedDexDirectory = File(cacheDirPath) optimizedDexDirectory = File(cacheDirPath)
) )
@ -104,7 +104,10 @@ class MainActivity : FlutterActivity() {
return@setMethodCallHandler result.notImplemented() return@setMethodCallHandler result.notImplemented()
} catch (err: Error) { } catch (err: Error) {
return@setMethodCallHandler result.notImplemented() return@setMethodCallHandler result.notImplemented()
}.forEach { }
JSONArray().apply {
patches.forEach {
JSONObject().apply { JSONObject().apply {
put("name", it.name) put("name", it.name)
put("description", it.description) put("description", it.description)
@ -136,7 +139,6 @@ class MainActivity : FlutterActivity() {
private fun runPatcher( private fun runPatcher(
result: MethodChannel.Result, result: MethodChannel.Result,
patchBundleFilePath: String,
originalFilePath: String, originalFilePath: String,
inputFilePath: String, inputFilePath: String,
patchedFilePath: String, patchedFilePath: String,
@ -223,10 +225,7 @@ class MainActivity : FlutterActivity() {
updateProgress(0.1, "Loading patches...", "Loading patches") updateProgress(0.1, "Loading patches...", "Loading patches")
val patches = PatchBundleLoader.Dex( val patches = patches.filter { patch ->
File(patchBundleFilePath),
optimizedDexDirectory = cacheDir
).filter { patch ->
val isCompatible = patch.compatiblePackages?.any { val isCompatible = patch.compatiblePackages?.any {
it.name == patcher.context.packageMetadata.packageName it.name == patcher.context.packageMetadata.packageName
} ?: false } ?: false

View File

@ -59,9 +59,7 @@ class PatcherAPI {
} }
List<Patch> getUniversalPatches() { List<Patch> getUniversalPatches() {
return _patches return _patches.where((patch) => patch.compatiblePackages.isEmpty).toList();
.where((patch) => patch.compatiblePackages.isEmpty)
.toList();
} }
Future<void> _loadPatches() async { Future<void> _loadPatches() async {
@ -85,15 +83,14 @@ class PatcherAPI {
) async { ) async {
final List<ApplicationWithIcon> filteredApps = []; final List<ApplicationWithIcon> filteredApps = [];
final bool allAppsIncluded = final bool allAppsIncluded =
_universalPatches.isNotEmpty && _universalPatches.isNotEmpty && showUniversalPatches;
showUniversalPatches;
if (allAppsIncluded) { if (allAppsIncluded) {
final appList = await DeviceApps.getInstalledApplications( final appList = await DeviceApps.getInstalledApplications(
includeAppIcons: true, includeAppIcons: true,
onlyAppsWithLaunchIntent: true, onlyAppsWithLaunchIntent: true,
); );
for(final app in appList) { for (final app in appList) {
filteredApps.add(app as ApplicationWithIcon); filteredApps.add(app as ApplicationWithIcon);
} }
} }
@ -154,9 +151,9 @@ class PatcherAPI {
String apkFilePath, String apkFilePath,
List<Patch> selectedPatches, List<Patch> selectedPatches,
) async { ) async {
final File? patchBundleFile = await _managerAPI.downloadPatches();
final File? integrationsFile = await _managerAPI.downloadIntegrations(); final File? integrationsFile = await _managerAPI.downloadIntegrations();
if (patchBundleFile != null) {
if (integrationsFile != null) {
_dataDir.createSync(); _dataDir.createSync();
_tmpDir.createSync(); _tmpDir.createSync();
final Directory workDir = _tmpDir.createTempSync('tmp-'); final Directory workDir = _tmpDir.createTempSync('tmp-');
@ -170,12 +167,11 @@ class PatcherAPI {
await patcherChannel.invokeMethod( await patcherChannel.invokeMethod(
'runPatcher', 'runPatcher',
{ {
'patchBundleFilePath': patchBundleFile.path,
'originalFilePath': originalFilePath, 'originalFilePath': originalFilePath,
'inputFilePath': inputFile.path, 'inputFilePath': inputFile.path,
'patchedFilePath': patchedFile.path, 'patchedFilePath': patchedFile.path,
'outFilePath': outFile!.path, 'outFilePath': outFile!.path,
'integrationsPath': integrationsFile!.path, 'integrationsPath': integrationsFile.path,
'selectedPatches': selectedPatches.map((p) => p.name).toList(), 'selectedPatches': selectedPatches.map((p) => p.name).toList(),
'cacheDirPath': cacheDir.path, 'cacheDirPath': cacheDir.path,
'keyStoreFilePath': _keyStoreFile.path, 'keyStoreFilePath': _keyStoreFile.path,

View File

@ -95,6 +95,8 @@ class InstallerViewModel extends BaseViewModel {
isInstalled = false; isInstalled = false;
hasErrors = false; hasErrors = false;
} else if (value == 1.0) { } else if (value == 1.0) {
_managerAPI.patches.clear();
isPatching = false; isPatching = false;
hasErrors = false; hasErrors = false;
await _managerAPI.savePatches( await _managerAPI.savePatches(