mirror of
https://github.com/revanced/revanced-manager.git
synced 2025-06-12 12:47:37 +02:00
feat: use provided patches.json to load patches
This commit is contained in:
@ -6,6 +6,7 @@ import 'package:revanced_manager/app/app.locator.dart';
|
||||
import 'package:revanced_manager/app/app.router.dart';
|
||||
import 'package:revanced_manager/main_viewmodel.dart';
|
||||
import 'package:revanced_manager/services/manager_api.dart';
|
||||
import 'package:revanced_manager/services/patcher_api.dart';
|
||||
import 'package:revanced_manager/theme.dart';
|
||||
import 'package:revanced_manager/ui/views/home/home_view.dart';
|
||||
import 'package:revanced_manager/ui/views/patcher/patcher_view.dart';
|
||||
@ -69,6 +70,7 @@ class MyApp extends StatelessWidget {
|
||||
|
||||
Future<Widget> _init() async {
|
||||
await locator<ManagerAPI>().initialize();
|
||||
await locator<PatcherAPI>().initialize();
|
||||
bool? isRooted = locator<ManagerAPI>().isRooted();
|
||||
if (isRooted != null) {
|
||||
return const Navigation();
|
||||
|
@ -1,15 +1,47 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:revanced_manager/utils/string.dart';
|
||||
|
||||
part 'patch.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class Patch {
|
||||
final String name;
|
||||
final String simpleName;
|
||||
final String version;
|
||||
final String description;
|
||||
final bool include;
|
||||
final String version;
|
||||
final bool excluded;
|
||||
final List<String> dependencies;
|
||||
final List<Package> compatiblePackages;
|
||||
|
||||
Patch({
|
||||
required this.name,
|
||||
required this.simpleName,
|
||||
required this.version,
|
||||
required this.description,
|
||||
required this.include,
|
||||
required this.version,
|
||||
required this.excluded,
|
||||
required this.dependencies,
|
||||
required this.compatiblePackages,
|
||||
});
|
||||
|
||||
factory Patch.fromJson(Map<String, dynamic> json) => _$PatchFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$PatchToJson(this);
|
||||
|
||||
String getSimpleName() {
|
||||
return name.replaceAll('-', ' ').split('-').join(' ').toTitleCase();
|
||||
}
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class Package {
|
||||
final String name;
|
||||
final List<String> versions;
|
||||
|
||||
Package({
|
||||
required this.name,
|
||||
required this.versions,
|
||||
});
|
||||
|
||||
factory Package.fromJson(Map<String, dynamic> json) =>
|
||||
_$PackageFromJson(json);
|
||||
|
||||
Map toJson() => _$PackageToJson(this);
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ class PatchedApplication {
|
||||
factory PatchedApplication.fromJson(Map<String, dynamic> json) =>
|
||||
_$PatchedApplicationFromJson(json);
|
||||
|
||||
Map toJson() => _$PatchedApplicationToJson(this);
|
||||
Map<String, dynamic> toJson() => _$PatchedApplicationToJson(this);
|
||||
|
||||
static Uint8List decodeBase64(String icon) => base64.decode(icon);
|
||||
|
||||
|
@ -6,7 +6,7 @@ import 'package:timeago/timeago.dart';
|
||||
class GithubAPI {
|
||||
final GitHub _github = GitHub();
|
||||
|
||||
Future<String?> latestReleaseVersion(String org, repoName) async {
|
||||
Future<String?> latestReleaseVersion(String org, String repoName) async {
|
||||
try {
|
||||
var latestRelease = await _github.repositories.getLatestRelease(
|
||||
RepositorySlug(org, repoName),
|
||||
@ -20,7 +20,7 @@ class GithubAPI {
|
||||
Future<File?> latestReleaseFile(
|
||||
String extension,
|
||||
String org,
|
||||
repoName,
|
||||
String repoName,
|
||||
) async {
|
||||
try {
|
||||
var latestRelease = await _github.repositories.getLatestRelease(
|
||||
@ -42,7 +42,7 @@ class GithubAPI {
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String> latestCommitTime(String org, repoName) async {
|
||||
Future<String> latestCommitTime(String org, String repoName) async {
|
||||
try {
|
||||
var repo = await _github.repositories.getRepository(
|
||||
RepositorySlug(org, repoName),
|
||||
@ -55,13 +55,13 @@ class GithubAPI {
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Contributor>> getContributors(String org, repoName) async {
|
||||
Future<List<Contributor>> getContributors(String org, String repoName) async {
|
||||
return await (_github.repositories.listContributors(
|
||||
RepositorySlug(org, repoName),
|
||||
)).toList();
|
||||
}
|
||||
|
||||
Future<List<RepositoryCommit>> getCommits(String org, repoName) async {
|
||||
Future<List<RepositoryCommit>> getCommits(String org, String repoName) async {
|
||||
return await (_github.repositories.listCommits(
|
||||
RepositorySlug(org, repoName),
|
||||
)).toList();
|
||||
|
@ -1,3 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:app_installer/app_installer.dart';
|
||||
import 'package:device_apps/device_apps.dart';
|
||||
@ -9,7 +10,6 @@ import 'package:revanced_manager/models/patch.dart';
|
||||
import 'package:revanced_manager/models/patched_application.dart';
|
||||
import 'package:revanced_manager/services/manager_api.dart';
|
||||
import 'package:revanced_manager/services/root_api.dart';
|
||||
import 'package:revanced_manager/utils/string.dart';
|
||||
import 'package:share_extend/share_extend.dart';
|
||||
|
||||
@lazySingleton
|
||||
@ -19,194 +19,123 @@ class PatcherAPI {
|
||||
);
|
||||
final ManagerAPI _managerAPI = locator<ManagerAPI>();
|
||||
final RootAPI _rootAPI = RootAPI();
|
||||
List<Patch> _patches = [];
|
||||
Directory? _tmpDir;
|
||||
Directory? _workDir;
|
||||
Directory? _cacheDir;
|
||||
File? _jarPatchBundleFile;
|
||||
File? _integrations;
|
||||
File? _inputFile;
|
||||
File? _patchedFile;
|
||||
File? _outFile;
|
||||
|
||||
Future<void> initPatcher() async {
|
||||
Directory appCache = await getTemporaryDirectory();
|
||||
_tmpDir = Directory('${appCache.path}/patcher');
|
||||
_tmpDir!.createSync();
|
||||
_workDir = _tmpDir!.createTempSync('tmp-');
|
||||
_inputFile = File('${_workDir!.path}/base.apk');
|
||||
_patchedFile = File('${_workDir!.path}/patched.apk');
|
||||
_outFile = File('${_workDir!.path}/out.apk');
|
||||
_cacheDir = Directory('${_workDir!.path}/cache');
|
||||
_cacheDir!.createSync();
|
||||
Future<void> initialize() async {
|
||||
await _loadPatches();
|
||||
}
|
||||
|
||||
Future<bool> loadPatches() async {
|
||||
if (_tmpDir == null) {
|
||||
await initPatcher();
|
||||
}
|
||||
if (_jarPatchBundleFile == null) {
|
||||
_jarPatchBundleFile = await _managerAPI.downloadPatches('.jar');
|
||||
if (_jarPatchBundleFile != null) {
|
||||
try {
|
||||
await patcherChannel.invokeMethod<bool>(
|
||||
'loadPatches',
|
||||
{
|
||||
'jarPatchBundlePath': _jarPatchBundleFile!.path,
|
||||
'cacheDirPath': _cacheDir!.path,
|
||||
},
|
||||
);
|
||||
} on Exception {
|
||||
return false;
|
||||
Future<void> _loadPatches() async {
|
||||
try {
|
||||
if (_patches.isEmpty) {
|
||||
File? patchJsonFile = await _managerAPI.downloadPatches('.json');
|
||||
if (patchJsonFile != null) {
|
||||
List<dynamic> list = json.decode(patchJsonFile.readAsStringSync());
|
||||
_patches = list.map((patch) => Patch.fromJson(patch)).toList();
|
||||
}
|
||||
}
|
||||
} on Exception {
|
||||
_patches = List.empty();
|
||||
}
|
||||
return _jarPatchBundleFile != null;
|
||||
}
|
||||
|
||||
Future<List<ApplicationWithIcon>> getFilteredInstalledApps() async {
|
||||
List<ApplicationWithIcon> filteredPackages = [];
|
||||
bool isLoaded = await loadPatches();
|
||||
if (isLoaded) {
|
||||
try {
|
||||
List<String>? patchesPackages = await patcherChannel
|
||||
.invokeListMethod<String>('getCompatiblePackages');
|
||||
if (patchesPackages != null) {
|
||||
for (String package in patchesPackages) {
|
||||
try {
|
||||
ApplicationWithIcon? app = await DeviceApps.getApp(package, true)
|
||||
as ApplicationWithIcon?;
|
||||
if (app != null) {
|
||||
filteredPackages.add(app);
|
||||
}
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
} on Exception {
|
||||
return List.empty();
|
||||
}
|
||||
}
|
||||
return filteredPackages;
|
||||
}
|
||||
|
||||
Future<List<Patch>> getFilteredPatches(
|
||||
PatchedApplication? selectedApp,
|
||||
) async {
|
||||
List<Patch> filteredPatches = [];
|
||||
if (selectedApp != null) {
|
||||
bool isLoaded = await loadPatches();
|
||||
if (isLoaded) {
|
||||
List<ApplicationWithIcon> filteredApps = [];
|
||||
await _loadPatches();
|
||||
for (Patch patch in _patches) {
|
||||
for (Package package in patch.compatiblePackages) {
|
||||
try {
|
||||
var patches =
|
||||
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
|
||||
'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 (!filteredApps.any((app) => app.packageName == package.name)) {
|
||||
ApplicationWithIcon? app =
|
||||
await DeviceApps.getApp(package.name, true)
|
||||
as ApplicationWithIcon?;
|
||||
if (app != null) {
|
||||
filteredApps.add(app);
|
||||
}
|
||||
}
|
||||
} on Exception {
|
||||
return List.empty();
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return filteredPatches;
|
||||
return filteredApps;
|
||||
}
|
||||
|
||||
Future<List<Patch>> getAppliedPatches(
|
||||
PatchedApplication? selectedApp,
|
||||
) async {
|
||||
List<Patch> appliedPatches = [];
|
||||
if (selectedApp != null) {
|
||||
bool isLoaded = await loadPatches();
|
||||
if (isLoaded) {
|
||||
try {
|
||||
var patches =
|
||||
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
|
||||
'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();
|
||||
}
|
||||
}
|
||||
}
|
||||
return appliedPatches;
|
||||
Future<List<Patch>> getFilteredPatches(String packageName) async {
|
||||
await _loadPatches();
|
||||
return _patches
|
||||
.where((patch) =>
|
||||
!patch.name.contains('settings') &&
|
||||
patch.compatiblePackages.any((pack) => pack.name == packageName))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> mergeIntegrations(bool mergeIntegrations) async {
|
||||
if (mergeIntegrations) {
|
||||
_integrations = await _managerAPI.downloadIntegrations('.apk');
|
||||
} else {
|
||||
_integrations = null;
|
||||
}
|
||||
Future<List<Patch>> getAppliedPatches(List<String> appliedPatches) async {
|
||||
await _loadPatches();
|
||||
return _patches
|
||||
.where((patch) => appliedPatches.contains(patch.name))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> runPatcher(
|
||||
String packageName,
|
||||
String originalFilePath,
|
||||
List<Patch> selectedPatches,
|
||||
bool mergeIntegrations,
|
||||
bool resourcePatching,
|
||||
) async {
|
||||
await patcherChannel.invokeMethod(
|
||||
'runPatcher',
|
||||
{
|
||||
'originalFilePath': originalFilePath,
|
||||
'inputFilePath': _inputFile!.path,
|
||||
'patchedFilePath': _patchedFile!.path,
|
||||
'outFilePath': _outFile!.path,
|
||||
'integrationsPath': _integrations != null ? _integrations!.path : '',
|
||||
'selectedPatches': selectedPatches.map((p) => p.name).toList(),
|
||||
'cacheDirPath': _cacheDir!.path,
|
||||
'mergeIntegrations': mergeIntegrations,
|
||||
'resourcePatching': resourcePatching,
|
||||
},
|
||||
bool mergeIntegrations = selectedPatches.any(
|
||||
(patch) => patch.dependencies.contains('integrations'),
|
||||
);
|
||||
bool resourcePatching = selectedPatches.any(
|
||||
(patch) => patch.dependencies.any((dep) => dep.contains('resource-')),
|
||||
);
|
||||
bool includeSettings = selectedPatches.any(
|
||||
(patch) => patch.dependencies.contains('settings'),
|
||||
);
|
||||
if (includeSettings) {
|
||||
try {
|
||||
Patch settingsPatch = _patches.firstWhere(
|
||||
(patch) =>
|
||||
patch.name.contains('settings') &&
|
||||
patch.compatiblePackages.any((pack) => pack.name == packageName),
|
||||
);
|
||||
selectedPatches.add(settingsPatch);
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
File? patchBundleFile = await _managerAPI.downloadPatches('.jar');
|
||||
File? integrationsFile;
|
||||
if (mergeIntegrations) {
|
||||
integrationsFile = await _managerAPI.downloadIntegrations('.apk');
|
||||
}
|
||||
if (patchBundleFile != null) {
|
||||
Directory appCache = await getTemporaryDirectory();
|
||||
_tmpDir = Directory('${appCache.path}/patcher');
|
||||
_tmpDir!.createSync();
|
||||
Directory workDir = _tmpDir!.createTempSync('tmp-');
|
||||
File inputFile = File('${workDir.path}/base.apk');
|
||||
File patchedFile = File('${workDir.path}/patched.apk');
|
||||
_outFile = File('${workDir.path}/out.apk');
|
||||
Directory cacheDir = Directory('${workDir.path}/cache');
|
||||
cacheDir.createSync();
|
||||
await patcherChannel.invokeMethod(
|
||||
'runPatcher',
|
||||
{
|
||||
'patchBundleFilePath': patchBundleFile.path,
|
||||
'originalFilePath': originalFilePath,
|
||||
'inputFilePath': inputFile.path,
|
||||
'patchedFilePath': patchedFile.path,
|
||||
'outFilePath': _outFile!.path,
|
||||
'integrationsPath': mergeIntegrations ? integrationsFile!.path : '',
|
||||
'selectedPatches': selectedPatches.map((p) => p.name).toList(),
|
||||
'cacheDirPath': cacheDir.path,
|
||||
'mergeIntegrations': mergeIntegrations,
|
||||
'resourcePatching': resourcePatching,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> installPatchedFile(PatchedApplication patchedApp) async {
|
||||
|
@ -27,7 +27,6 @@ class HomeViewModel extends BaseViewModel {
|
||||
|
||||
Future<void> initialize() async {
|
||||
await _getPatchedApps();
|
||||
await _patcherAPI.loadPatches();
|
||||
await flutterLocalNotificationsPlugin.initialize(
|
||||
const InitializationSettings(
|
||||
android: AndroidInitializationSettings('ic_notification'),
|
||||
@ -45,7 +44,7 @@ class HomeViewModel extends BaseViewModel {
|
||||
void navigateToPatcher(PatchedApplication app) async {
|
||||
locator<PatcherViewModel>().selectedApp = app;
|
||||
locator<PatcherViewModel>().selectedPatches =
|
||||
await _patcherAPI.getAppliedPatches(app);
|
||||
await _patcherAPI.getAppliedPatches(app.appliedPatches);
|
||||
locator<PatcherViewModel>().notifyListeners();
|
||||
locator<MainViewModel>().setIndex(1);
|
||||
}
|
||||
|
@ -108,22 +108,7 @@ class InstallerViewModel extends BaseViewModel {
|
||||
}
|
||||
}
|
||||
update(0.0, '', 'Creating working directory');
|
||||
bool mergeIntegrations = false;
|
||||
bool resourcePatching = false;
|
||||
if (_app!.packageName == 'com.google.android.youtube') {
|
||||
mergeIntegrations = true;
|
||||
resourcePatching = true;
|
||||
} else if (_app!.packageName ==
|
||||
'com.google.android.apps.youtube.music') {
|
||||
resourcePatching = true;
|
||||
}
|
||||
await _patcherAPI.mergeIntegrations(mergeIntegrations);
|
||||
await _patcherAPI.runPatcher(
|
||||
apkFilePath,
|
||||
_patches,
|
||||
mergeIntegrations,
|
||||
resourcePatching,
|
||||
);
|
||||
await _patcherAPI.runPatcher(_app!.packageName, apkFilePath, _patches);
|
||||
} on Exception {
|
||||
update(1.0, 'Aborting...', 'An error occurred! Aborting');
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
|
||||
.getQueriedPatches(_query)
|
||||
.map((patch) => PatchItem(
|
||||
name: patch.name,
|
||||
simpleName: patch.simpleName,
|
||||
simpleName: patch.getSimpleName(),
|
||||
version: patch.version,
|
||||
description: patch.description,
|
||||
isSelected: model.isSelected(patch),
|
||||
|
@ -12,11 +12,11 @@ class PatchesSelectorViewModel extends BaseViewModel {
|
||||
|
||||
Future<void> initialize() async {
|
||||
patches.addAll(await _patcherAPI.getFilteredPatches(
|
||||
locator<PatcherViewModel>().selectedApp,
|
||||
locator<PatcherViewModel>().selectedApp!.packageName,
|
||||
));
|
||||
patches.sort((a, b) => a.simpleName.compareTo(b.simpleName));
|
||||
patches.sort((a, b) => a.name.compareTo(b.name));
|
||||
for (Patch p in patches) {
|
||||
if (p.include) {
|
||||
if (!p.excluded) {
|
||||
selectedPatches.add(p);
|
||||
}
|
||||
}
|
||||
@ -56,7 +56,7 @@ class PatchesSelectorViewModel extends BaseViewModel {
|
||||
.where((patch) =>
|
||||
query.isEmpty ||
|
||||
query.length < 2 ||
|
||||
patch.simpleName.toLowerCase().contains(
|
||||
patch.name.toLowerCase().contains(
|
||||
query.toLowerCase(),
|
||||
))
|
||||
.toList();
|
||||
|
@ -72,7 +72,7 @@ class PatchSelectorCard extends StatelessWidget {
|
||||
String _getPatchesSelection() {
|
||||
String text = '';
|
||||
for (Patch p in locator<PatcherViewModel>().selectedPatches) {
|
||||
text += '${p.simpleName} (v${p.version})\n';
|
||||
text += '${p.getSimpleName()} (v${p.version})\n';
|
||||
}
|
||||
return text.substring(0, text.length - 1);
|
||||
}
|
||||
|
Reference in New Issue
Block a user