-
-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate revanced patcher (#22)
- Loading branch information
Showing
36 changed files
with
1,462 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/app/revanced/manager/compose/di/WorkerModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package app.revanced.manager.compose.di | ||
|
||
import app.revanced.manager.compose.patcher.worker.PatcherWorker | ||
import org.koin.androidx.workmanager.dsl.workerOf | ||
import org.koin.dsl.module | ||
|
||
val workerModule = module { | ||
workerOf(::PatcherWorker) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/app/revanced/manager/compose/patcher/Aligning.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package app.revanced.manager.compose.patcher | ||
|
||
import app.revanced.manager.compose.patcher.alignment.ZipAligner | ||
import app.revanced.manager.compose.patcher.alignment.zip.ZipFile | ||
import app.revanced.manager.compose.patcher.alignment.zip.structures.ZipEntry | ||
import app.revanced.patcher.PatcherResult | ||
import java.io.File | ||
|
||
// This is the same aligner used by the CLI. | ||
// It will be removed eventually. | ||
object Aligning { | ||
fun align(result: PatcherResult, inputFile: File, outputFile: File) { | ||
// logger.info("Aligning ${inputFile.name} to ${outputFile.name}") | ||
|
||
if (outputFile.exists()) outputFile.delete() | ||
|
||
ZipFile(outputFile).use { file -> | ||
result.dexFiles.forEach { | ||
file.addEntryCompressData( | ||
ZipEntry.createWithName(it.name), | ||
it.stream.readBytes() | ||
) | ||
} | ||
|
||
result.resourceFile?.let { | ||
file.copyEntriesFromFileAligned( | ||
ZipFile(it), | ||
ZipAligner::getEntryAlignment | ||
) | ||
} | ||
|
||
file.copyEntriesFromFileAligned( | ||
ZipFile(inputFile), | ||
ZipAligner::getEntryAlignment | ||
) | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
app/src/main/java/app/revanced/manager/compose/patcher/Session.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package app.revanced.manager.compose.patcher | ||
|
||
import app.revanced.patcher.Patcher | ||
import app.revanced.patcher.PatcherOptions | ||
import app.revanced.patcher.logging.Logger | ||
import android.util.Log | ||
import app.revanced.manager.compose.patcher.worker.Progress | ||
import app.revanced.patcher.data.Context | ||
import app.revanced.patcher.patch.Patch | ||
import java.io.Closeable | ||
import java.io.File | ||
import java.nio.file.Files | ||
import java.nio.file.StandardCopyOption | ||
|
||
internal typealias PatchClass = Class<out Patch<Context>> | ||
internal typealias PatchList = List<PatchClass> | ||
|
||
class Session( | ||
cacheDir: String, | ||
frameworkDir: String, | ||
aaptPath: String, | ||
private val input: File, | ||
private val onProgress: suspend (Progress) -> Unit = { } | ||
) : Closeable { | ||
class PatchFailedException(val patchName: String, cause: Throwable?) : Exception("Got exception while executing $patchName", cause) | ||
|
||
private val logger = LogcatLogger | ||
private val temporary = File(cacheDir).resolve("manager").also { it.mkdirs() } | ||
private val patcher = Patcher( | ||
PatcherOptions( | ||
inputFile = input, | ||
resourceCacheDirectory = temporary.resolve("aapt-resources").path, | ||
frameworkFolderLocation = frameworkDir, | ||
aaptPath = aaptPath, | ||
logger = logger, | ||
) | ||
) | ||
|
||
private suspend fun Patcher.applyPatchesVerbose() { | ||
this.executePatches(true).forEach { (patch, result) -> | ||
if (result.isSuccess) { | ||
logger.info("$patch succeeded") | ||
onProgress(Progress.PatchSuccess(patch)) | ||
return@forEach | ||
} | ||
logger.error("$patch failed:") | ||
result.exceptionOrNull()!!.printStackTrace() | ||
|
||
throw PatchFailedException(patch, result.exceptionOrNull()) | ||
} | ||
} | ||
|
||
suspend fun run(output: File, selectedPatches: PatchList, integrations: List<File>) { | ||
onProgress(Progress.Merging) | ||
|
||
with(patcher) { | ||
logger.info("Merging integrations") | ||
addIntegrations(integrations) {} | ||
addPatches(selectedPatches) | ||
|
||
logger.info("Applying patches...") | ||
onProgress(Progress.PatchingStart) | ||
|
||
applyPatchesVerbose() | ||
} | ||
|
||
onProgress(Progress.Saving) | ||
logger.info("Writing patched files...") | ||
val result = patcher.save() | ||
|
||
val aligned = temporary.resolve("aligned.apk").also { Aligning.align(result, input, it) } | ||
|
||
logger.info("Patched apk saved to $aligned") | ||
|
||
Files.move(aligned.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING) | ||
} | ||
|
||
override fun close() { | ||
temporary.delete() | ||
} | ||
} | ||
|
||
private object LogcatLogger : Logger { | ||
private const val tag = "revanced-patcher" | ||
override fun error(msg: String) { | ||
Log.e(tag, msg) | ||
} | ||
|
||
override fun warn(msg: String) { | ||
Log.w(tag, msg) | ||
} | ||
|
||
override fun info(msg: String) { | ||
Log.i(tag, msg) | ||
} | ||
|
||
override fun trace(msg: String) { | ||
Log.v(tag, msg) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/app/revanced/manager/compose/patcher/aapt/Aapt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package app.revanced.manager.compose.patcher.aapt | ||
|
||
import android.content.Context | ||
import java.io.File | ||
|
||
object Aapt { | ||
fun binary(context: Context): File? { | ||
return File(context.applicationInfo.nativeLibraryDir).resolveAapt() | ||
} | ||
} | ||
|
||
private fun File.resolveAapt() = | ||
list { _, f -> !File(f).isDirectory && f.contains("aapt") }?.firstOrNull()?.let { resolve(it) } |
Oops, something went wrong.