mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-19 07:34:07 +00:00
Update resources load
addAssetPath supports apk and directory
This commit is contained in:
@@ -35,9 +35,6 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.zip.InflaterInputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.CipherInputStream;
|
||||
@@ -162,47 +159,36 @@ public class DownloadActivity extends Activity {
|
||||
}
|
||||
|
||||
private void decryptResources(OutputStream out) throws Exception {
|
||||
try (var zip = new ZipOutputStream(out)) {
|
||||
zip.putNextEntry(new ZipEntry("resources.arsc"));
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
SecretKey key = new SecretKeySpec(Bytes.key(), "AES");
|
||||
IvParameterSpec iv = new IvParameterSpec(Bytes.iv());
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
var is = new InflaterInputStream(new CipherInputStream(
|
||||
new ByteArrayInputStream(Bytes.res()), cipher));
|
||||
try (is) {
|
||||
APKInstall.transfer(is, zip);
|
||||
}
|
||||
zip.closeEntry();
|
||||
|
||||
zip.putNextEntry(new ZipEntry("AndroidManifest.xml"));
|
||||
var apk = new ZipFile(getPackageResourcePath());
|
||||
var xml = apk.getInputStream(apk.getEntry("AndroidManifest.xml"));
|
||||
try (apk; xml) {
|
||||
APKInstall.transfer(xml, zip);
|
||||
}
|
||||
zip.closeEntry();
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
SecretKey key = new SecretKeySpec(Bytes.key(), "AES");
|
||||
IvParameterSpec iv = new IvParameterSpec(Bytes.iv());
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
var is = new InflaterInputStream(new CipherInputStream(
|
||||
new ByteArrayInputStream(Bytes.res()), cipher));
|
||||
try (is; out) {
|
||||
APKInstall.transfer(is, out);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadResources() throws Exception {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
var fd = Os.memfd_create("res.apk", 0);
|
||||
var fd = Os.memfd_create("res", 0);
|
||||
try {
|
||||
decryptResources(new FileOutputStream(fd));
|
||||
Os.lseek(fd, 0, OsConstants.SEEK_SET);
|
||||
try (var pfd = ParcelFileDescriptor.dup(fd)) {
|
||||
var loader = new ResourcesLoader();
|
||||
loader.addProvider(ResourcesProvider.loadFromApk(pfd));
|
||||
var loader = new ResourcesLoader();
|
||||
try (var pfd = ParcelFileDescriptor.dup(fd);
|
||||
var provider = ResourcesProvider.loadFromTable(pfd, null)) {
|
||||
loader.addProvider(provider);
|
||||
getResources().addLoaders(loader);
|
||||
}
|
||||
} finally {
|
||||
Os.close(fd);
|
||||
}
|
||||
} else {
|
||||
File apk = new File(getCacheDir(), "res.apk");
|
||||
decryptResources(new FileOutputStream(apk));
|
||||
StubApk.addAssetPath(getResources(), apk.getPath());
|
||||
File dir = new File(getCodeCacheDir(), "res");
|
||||
decryptResources(new FileOutputStream(new File(dir, "resources.arsc")));
|
||||
StubApk.addAssetPath(getResources(), dir.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user