refactor: cleanup code & fix warnings

This commit is contained in:
Sculas 2022-09-14 23:09:18 +02:00
parent 84813d34c6
commit eef9c2bf31
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89
4 changed files with 252 additions and 254 deletions

View File

@ -16,12 +16,12 @@ import dalvik.system.DexClassLoader
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.Result
import java.io.File import java.io.File
private const val PATCHER_CHANNEL = "app.revanced.manager.flutter/patcher"
private const val INSTALLER_CHANNEL = "app.revanced.manager.flutter/installer"
class MainActivity : FlutterActivity() { class MainActivity : FlutterActivity() {
private val PATCHER_CHANNEL = "app.revanced.manager.flutter/patcher"
private val INSTALLER_CHANNEL = "app.revanced.manager.flutter/installer"
private val handler = Handler(Looper.getMainLooper()) private val handler = Handler(Looper.getMainLooper())
private lateinit var installerChannel: MethodChannel private lateinit var installerChannel: MethodChannel
@ -44,53 +44,54 @@ class MainActivity : FlutterActivity() {
val resourcePatching = call.argument<Boolean>("resourcePatching") val resourcePatching = call.argument<Boolean>("resourcePatching")
val keyStoreFilePath = call.argument<String>("keyStoreFilePath") val keyStoreFilePath = call.argument<String>("keyStoreFilePath")
if (patchBundleFilePath != null && if (patchBundleFilePath != null &&
originalFilePath != null && originalFilePath != null &&
inputFilePath != null && inputFilePath != null &&
patchedFilePath != null && patchedFilePath != null &&
outFilePath != null && outFilePath != null &&
integrationsPath != null && integrationsPath != null &&
selectedPatches != null && selectedPatches != null &&
cacheDirPath != null && cacheDirPath != null &&
mergeIntegrations != null && mergeIntegrations != null &&
resourcePatching != null && resourcePatching != null &&
keyStoreFilePath != null keyStoreFilePath != null
) { ) {
runPatcher( runPatcher(
result, result,
patchBundleFilePath, patchBundleFilePath,
originalFilePath, originalFilePath,
inputFilePath, inputFilePath,
patchedFilePath, patchedFilePath,
outFilePath, outFilePath,
integrationsPath, integrationsPath,
selectedPatches, selectedPatches,
cacheDirPath, cacheDirPath,
mergeIntegrations, mergeIntegrations,
resourcePatching, resourcePatching,
keyStoreFilePath keyStoreFilePath
) )
} else { } else {
result.notImplemented() result.notImplemented()
} }
} }
else -> result.notImplemented() else -> result.notImplemented()
} }
} }
} }
fun runPatcher( private fun runPatcher(
result: MethodChannel.Result, result: MethodChannel.Result,
patchBundleFilePath: String, patchBundleFilePath: String,
originalFilePath: String, originalFilePath: String,
inputFilePath: String, inputFilePath: String,
patchedFilePath: String, patchedFilePath: String,
outFilePath: String, outFilePath: String,
integrationsPath: String, integrationsPath: String,
selectedPatches: List<String>, selectedPatches: List<String>,
cacheDirPath: String, cacheDirPath: String,
mergeIntegrations: Boolean, mergeIntegrations: Boolean,
resourcePatching: Boolean, resourcePatching: Boolean,
keyStoreFilePath: String keyStoreFilePath: String
) { ) {
val originalFile = File(originalFilePath) val originalFile = File(originalFilePath)
val inputFile = File(inputFilePath) val inputFile = File(inputFilePath)
@ -99,235 +100,230 @@ class MainActivity : FlutterActivity() {
val integrations = File(integrationsPath) val integrations = File(integrationsPath)
val keyStoreFile = File(keyStoreFilePath) val keyStoreFile = File(keyStoreFilePath)
val patches = val patches = DexPatchBundle(
DexPatchBundle( patchBundleFilePath,
patchBundleFilePath, DexClassLoader(
DexClassLoader( patchBundleFilePath,
patchBundleFilePath, cacheDirPath,
cacheDirPath, null,
null, javaClass.classLoader
javaClass.classLoader )
) ).loadPatches().filter { patch -> selectedPatches.any { it == patch.patchName } }
)
.loadPatches()
.filter { patch -> selectedPatches.any { it == patch.patchName } }
Thread( Thread {
Runnable { handler.post {
handler.post { installerChannel.invokeMethod(
installerChannel.invokeMethod( "update",
"update", mapOf(
mapOf( "progress" to 0.1,
"progress" to 0.1, "header" to "",
"header" to "", "log" to "Copying original apk"
"log" to "Copying original apk" )
) )
) }
} originalFile.copyTo(inputFile, true)
originalFile.copyTo(inputFile, true)
handler.post { handler.post {
installerChannel.invokeMethod( installerChannel.invokeMethod(
"update", "update",
mapOf( mapOf(
"progress" to 0.2, "progress" to 0.2,
"header" to "Unpacking apk...", "header" to "Unpacking apk...",
"log" to "Unpacking input apk" "log" to "Unpacking input apk"
) )
) )
} }
val patcher = val patcher =
Patcher( Patcher(
PatcherOptions( PatcherOptions(
inputFile, inputFile,
cacheDirPath, cacheDirPath,
resourcePatching, resourcePatching,
Aapt.binary(applicationContext).absolutePath, Aapt.binary(applicationContext).absolutePath,
cacheDirPath, cacheDirPath,
logger = logger =
object : object :
app.revanced.patcher.logging.Logger { app.revanced.patcher.logging.Logger {
override fun error(msg: String) { override fun error(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"update",
mapOf(
"progress" to
-1.0,
"header" to
"",
"log" to
msg
)
)
}
}
override fun warn(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"update",
mapOf(
"progress" to
-1.0,
"header" to
"",
"log" to
msg
)
)
}
}
override fun info(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"update",
mapOf(
"progress" to
-1.0,
"header" to
"",
"log" to
msg
)
)
}
}
override fun trace(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"update",
mapOf(
"progress" to
-1.0,
"header" to
"",
"log" to
msg
)
)
}
}
}
)
)
handler.post {
installerChannel.invokeMethod(
"update",
mapOf("progress" to 0.3, "header" to "", "log" to "")
)
}
if (mergeIntegrations) {
handler.post { handler.post {
installerChannel.invokeMethod( installerChannel
.invokeMethod(
"update", "update",
mapOf( mapOf(
"progress" to 0.4, "progress" to
"header" to "Merging integrations...", -1.0,
"log" to "Merging integrations" "header" to
"",
"log" to
msg
) )
) )
} }
patcher.addFiles(listOf(integrations)) {}
} }
handler.post { override fun warn(msg: String) {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.5,
"header" to "Applying patches...",
"log" to ""
)
)
}
patcher.addPatches(patches)
patcher.applyPatches().forEach { (patch, res) ->
if (res.isSuccess) {
val msg = "[success] $patch"
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.5,
"header" to "",
"log" to msg
)
)
}
return@forEach
}
val msg = "[error] $patch:" + res.exceptionOrNull()!!
handler.post { handler.post {
installerChannel.invokeMethod( installerChannel
.invokeMethod(
"update", "update",
mapOf("progress" to 0.5, "header" to "", "log" to msg) mapOf(
) "progress" to
-1.0,
"header" to
"",
"log" to
msg
)
)
} }
} }
handler.post { override fun info(msg: String) {
installerChannel.invokeMethod( handler.post {
"update", installerChannel
mapOf( .invokeMethod(
"progress" to 0.7, "update",
"header" to "Repacking apk...", mapOf(
"log" to "Repacking patched apk" "progress" to
-1.0,
"header" to
"",
"log" to
msg
)
) )
)
}
val res = patcher.save()
ZipFile(patchedFile).use { file ->
res.dexFiles.forEach {
file.addEntryCompressData(
ZipEntry.createWithName(it.name),
it.stream.readBytes()
)
} }
res.resourceFile?.let {
file.copyEntriesFromFileAligned(
ZipFile(it),
ZipAligner::getEntryAlignment
)
}
file.copyEntriesFromFileAligned(
ZipFile(inputFile),
ZipAligner::getEntryAlignment
)
}
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.9,
"header" to "Signing apk...",
"log" to ""
)
)
}
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile, keyStoreFile)
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 1.0,
"header" to "Finished!",
"log" to "Finished!"
)
)
} }
handler.post { result.success(null) } override fun trace(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"update",
mapOf(
"progress" to
-1.0,
"header" to
"",
"log" to
msg
)
)
}
}
} }
)
) )
.start()
handler.post {
installerChannel.invokeMethod(
"update",
mapOf("progress" to 0.3, "header" to "", "log" to "")
)
}
if (mergeIntegrations) {
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.4,
"header" to "Merging integrations...",
"log" to "Merging integrations"
)
)
}
patcher.addFiles(listOf(integrations)) {}
}
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.5,
"header" to "Applying patches...",
"log" to ""
)
)
}
patcher.addPatches(patches)
patcher.applyPatches().forEach { (patch, res) ->
if (res.isSuccess) {
val msg = "[success] $patch"
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.5,
"header" to "",
"log" to msg
)
)
}
return@forEach
}
val msg = "[error] $patch:" + res.exceptionOrNull()!!
handler.post {
installerChannel.invokeMethod(
"update",
mapOf("progress" to 0.5, "header" to "", "log" to msg)
)
}
}
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.7,
"header" to "Repacking apk...",
"log" to "Repacking patched apk"
)
)
}
val res = patcher.save()
ZipFile(patchedFile).use { file ->
res.dexFiles.forEach {
file.addEntryCompressData(
ZipEntry.createWithName(it.name),
it.stream.readBytes()
)
}
res.resourceFile?.let {
file.copyEntriesFromFileAligned(
ZipFile(it),
ZipAligner::getEntryAlignment
)
}
file.copyEntriesFromFileAligned(
ZipFile(inputFile),
ZipAligner::getEntryAlignment
)
}
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.9,
"header" to "Signing apk...",
"log" to ""
)
)
}
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile, keyStoreFile)
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 1.0,
"header" to "Finished!",
"log" to "Finished!"
)
)
}
handler.post { result.success(null) }
}
.start()
} }
} }

View File

@ -1,3 +1,5 @@
@file:Suppress("unused")
package app.revanced.manager.flutter.utils.zip package app.revanced.manager.flutter.utils.zip
import java.io.DataInput import java.io.DataInput
@ -17,8 +19,8 @@ fun UShort.toBigEndian() = (this.toUInt() shl 16).toBigEndian().toUShort()
fun ByteBuffer.getUShort() = this.short.toUShort() fun ByteBuffer.getUShort() = this.short.toUShort()
fun ByteBuffer.getUInt() = this.int.toUInt() fun ByteBuffer.getUInt() = this.int.toUInt()
fun ByteBuffer.putUShort(ushort: UShort) = this.putShort(ushort.toShort()) fun ByteBuffer.putUShort(ushort: UShort): ByteBuffer = this.putShort(ushort.toShort())
fun ByteBuffer.putUInt(uint: UInt) = this.putInt(uint.toInt()) fun ByteBuffer.putUInt(uint: UInt): ByteBuffer = this.putInt(uint.toInt())
fun DataInput.readUShort() = this.readShort().toUShort() fun DataInput.readUShort() = this.readShort().toUShort()
fun DataInput.readUInt() = this.readInt().toUInt() fun DataInput.readUInt() = this.readInt().toUInt()

View File

@ -10,7 +10,7 @@ import java.nio.channels.FileChannel
import java.util.zip.CRC32 import java.util.zip.CRC32
import java.util.zip.Deflater import java.util.zip.Deflater
class ZipFile(val file: File) : Closeable { class ZipFile(file: File) : Closeable {
var entries: MutableList<ZipEntry> = mutableListOf() var entries: MutableList<ZipEntry> = mutableListOf()
private val filePointer: RandomAccessFile = RandomAccessFile(file, "rw") private val filePointer: RandomAccessFile = RandomAccessFile(file, "rw")
@ -134,8 +134,8 @@ class ZipFile(val file: File) : Closeable {
addEntry(entry, compressedBuffer) addEntry(entry, compressedBuffer)
} }
fun addEntryCopyData(entry: ZipEntry, data: ByteBuffer, alignment: Int? = null) { private fun addEntryCopyData(entry: ZipEntry, data: ByteBuffer, alignment: Int? = null) {
alignment?.let { alignment -> alignment?.let {
//calculate where data would end up //calculate where data would end up
val dataOffset = filePointer.filePointer + entry.LFHSize val dataOffset = filePointer.filePointer + entry.LFHSize

View File

@ -76,7 +76,7 @@ data class ZipEntry(
val fileNameLength = input.readUShortLE() val fileNameLength = input.readUShortLE()
var fileName = "" var fileName = ""
val extraFieldLength = input.readUShortLE() val extraFieldLength = input.readUShortLE()
var extraField = ByteArray(extraFieldLength.toInt()) val extraField = ByteArray(extraFieldLength.toInt())
val fileCommentLength = input.readUShortLE() val fileCommentLength = input.readUShortLE()
var fileComment = "" var fileComment = ""
val diskNumber = input.readUShortLE() val diskNumber = input.readUShortLE()