forked from ReVanced/revanced-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate to
DexPatchBundle
and JarPatchBundle
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
- Loading branch information
Showing
6 changed files
with
64 additions
and
6 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
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/app/revanced/patcher/util/patch/base/PatchBundle.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,17 @@ | ||
package app.revanced.patcher.util.patch.base | ||
|
||
import app.revanced.patcher.patch.base.Patch | ||
import java.io.File | ||
|
||
/** | ||
* @param patchBundlePath The path to the patch bundle. | ||
*/ | ||
abstract class PatchBundle(patchBundlePath: String) : File(patchBundlePath) { | ||
internal fun loadPatches(classLoader: ClassLoader, classNames: Iterator<String>) = buildList { | ||
classNames.forEach { className -> | ||
val clazz = classLoader.loadClass(className) | ||
if (!clazz.isAnnotationPresent(app.revanced.patcher.patch.annotations.Patch::class.java)) return@forEach | ||
@Suppress("UNCHECKED_CAST") this.add(clazz as Class<Patch<*>>) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/app/revanced/patcher/util/patch/implementation/DexPatchBundle.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,17 @@ | ||
package app.revanced.patcher.util.patch.implementation | ||
|
||
import app.revanced.patcher.util.patch.base.PatchBundle | ||
import app.revanced.patcher.util.patch.util.StringIterator | ||
import org.jf.dexlib2.DexFileFactory | ||
|
||
/** | ||
* A patch bundle of the ReVanced [DexPatchBundle] format. | ||
* @param patchBundlePath The path to a patch bundle of dex format. | ||
* @param dexClassLoader The dex class loader. | ||
*/ | ||
class DexPatchBundle(patchBundlePath: String, private val dexClassLoader: ClassLoader) : PatchBundle(patchBundlePath) { | ||
fun loadPatches() = loadPatches(dexClassLoader, | ||
StringIterator(DexFileFactory.loadDexFile(path, null).classes.iterator()) { classDef -> | ||
classDef.type.substring(1, classDef.length - 1).replace('/', '.') | ||
}) | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/app/revanced/patcher/util/patch/implementation/JarPatchBundle.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,18 @@ | ||
package app.revanced.patcher.util.patch.implementation | ||
|
||
import app.revanced.patcher.util.patch.base.PatchBundle | ||
import app.revanced.patcher.util.patch.util.StringIterator | ||
import java.net.URLClassLoader | ||
import java.util.jar.JarFile | ||
|
||
/** | ||
* A patch bundle of the ReVanced [JarPatchBundle] format. | ||
* @param patchBundlePath The path to the patch bundle. | ||
*/ | ||
class JarPatchBundle(patchBundlePath: String) : PatchBundle(patchBundlePath) { | ||
fun loadPatches() = loadPatches(URLClassLoader(arrayOf(this.toURI().toURL()), null), StringIterator( | ||
JarFile(this).entries().iterator() | ||
) { | ||
it.realName.replace('/', '.').replace(".class", "") | ||
}) | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/app/revanced/patcher/util/patch/util/StringIterator.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,10 @@ | ||
package app.revanced.patcher.util.patch.util | ||
|
||
internal class StringIterator<T, I : Iterator<T>>( | ||
private val iterator: I, | ||
private val _next: (T) -> String | ||
) : Iterator<String> { | ||
override fun hasNext() = iterator.hasNext() | ||
|
||
override fun next() = _next(iterator.next()) | ||
} |