Store CRC as module file name

This commit is contained in:
Nullptr 2021-08-26 23:39:18 +08:00
parent 113b2c93b8
commit 4e3e2a8637
2 changed files with 47 additions and 20 deletions

View File

@ -15,7 +15,6 @@ import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.os.Parcel; import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.SharedMemory; import android.os.SharedMemory;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.Os; import android.system.Os;
@ -47,6 +46,7 @@ import java.lang.reflect.Method;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFilePermissions; import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList; import java.util.ArrayList;
@ -173,8 +173,6 @@ public class LSPApplication extends ApplicationServiceClient {
// TODO: set module config // TODO: set module config
public static void loadModules(Context context) { public static void loadModules(Context context) {
var configFile = new File(context.getExternalFilesDir(null), "lspatch.json"); var configFile = new File(context.getExternalFilesDir(null), "lspatch.json");
var cacheDir = new File(context.getExternalCacheDir(), "modules");
cacheDir.mkdirs();
JSONObject moduleConfigs = new JSONObject(); JSONObject moduleConfigs = new JSONObject();
try (var is = new FileInputStream(configFile)) { try (var is = new FileInputStream(configFile)) {
moduleConfigs = new JSONObject(FileUtils.readTextFromInputStream(is)); moduleConfigs = new JSONObject(FileUtils.readTextFromInputStream(is));
@ -192,27 +190,27 @@ public class LSPApplication extends ApplicationServiceClient {
HashSet<String> embedded_modules = new HashSet<>(); HashSet<String> embedded_modules = new HashSet<>();
HashSet<String> disabled_modules = new HashSet<>(); HashSet<String> disabled_modules = new HashSet<>();
try { try {
var lastInstalledTime = new File(context.getApplicationInfo().sourceDir).lastModified();
for (var name : context.getAssets().list("modules")) { for (var name : context.getAssets().list("modules")) {
var target = new File(cacheDir, name + ".apk"); String modulePath = context.getCacheDir() + "/lspatch/" + name + "/";
if (target.lastModified() > lastInstalledTime) { String cacheApkPath;
embedded_modules.add(name); try (ZipFile sourceFile = new ZipFile(context.getApplicationInfo().sourceDir)) {
var module = new Module(); cacheApkPath = modulePath + sourceFile.getEntry("assets/modules/" + name).getCrc();
module.apkPath = target.getAbsolutePath();
module.packageName = target.getName();
LSPApplication.modules.add(module);
continue;
} }
try (var is = context.getAssets().open("modules/" + name)) {
Files.copy(is, target.toPath());
embedded_modules.add(name);
var module = new Module();
module.apkPath = target.getAbsolutePath();
module.packageName = target.getName();
LSPApplication.modules.add(module);
} catch (IOException ignored) {
if (!Files.exists(Paths.get(cacheApkPath))) {
Log.i(TAG, "extract module apk: " + name);
FileUtils.deleteFolderIfExists(Paths.get(modulePath));
Files.createDirectories(Paths.get(modulePath));
try (var is = context.getAssets().open("modules/" + name)) {
Files.copy(is, Paths.get(cacheApkPath));
}
} }
embedded_modules.add(name);
var module = new Module();
module.apkPath = cacheApkPath;
module.packageName = name;
LSPApplication.modules.add(module);
} }
} catch (Throwable ignored) { } catch (Throwable ignored) {

View File

@ -3,9 +3,15 @@ package org.lsposed.lspatch.loader.util;
import android.content.Context; import android.content.Context;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class FileUtils { public class FileUtils {
@ -32,4 +38,27 @@ public class FileUtils {
} }
return null; return null;
} }
public static void deleteFolderIfExists(Path target) throws IOException {
if (Files.notExists(target)) return;
Files.walkFileTree(target, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException {
if (e == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
throw e;
}
}
});
}
} }