File tree 6 files changed +26
-37
lines changed
src/main/kotlin/app/revanced
6 files changed +26
-37
lines changed Original file line number Diff line number Diff line change 1
1
package app.revanced.cli
2
2
3
- import app.revanced.patch.PatchLoader
4
3
import app.revanced.patch.Patches
5
4
import app.revanced.utils.adb.Adb
6
5
import picocli.CommandLine.*
@@ -49,8 +48,7 @@ internal object MainCommand : Runnable {
49
48
override fun run () {
50
49
if (listOnly) {
51
50
patchBundles.forEach {
52
- PatchLoader .injectPatches(it)
53
- Patches .loadPatches().forEach {
51
+ Patches .load(it).forEach {
54
52
println (it().metadata)
55
53
}
56
54
}
Original file line number Diff line number Diff line change 1
1
package app.revanced.cli
2
2
3
- import app.revanced.patch.PatchLoader
4
3
import app.revanced.patch.Patches
5
4
import app.revanced.patcher.data.base.Data
6
5
import app.revanced.patcher.patch.base.Patch
@@ -59,9 +58,8 @@ internal class Patcher {
59
58
val checkInclude = MainCommand .includedPatches.isNotEmpty()
60
59
61
60
MainCommand .patchBundles.forEach { bundle ->
62
- PatchLoader .injectPatches(bundle)
63
61
val includedPatches = mutableListOf<Patch <Data >>()
64
- Patches .loadPatches( ).forEach patch@{
62
+ Patches .load(bundle ).forEach patch@{
65
63
val patch = it()
66
64
67
65
// TODO: filter out incompatible patches with package metadata
Original file line number Diff line number Diff line change 1
1
package app.revanced.patch
2
2
3
3
import java.io.File
4
- import java.net.URL
5
4
import java.net.URLClassLoader
6
5
7
6
internal class PatchLoader {
8
7
internal companion object {
9
- internal fun injectPatches (file : File ) {
10
- // This function will fail on Java 9 and above.
11
- try {
12
- val url = file.toURI().toURL()
13
- val classLoader = Thread .currentThread().contextClassLoader as URLClassLoader
14
- val method = URLClassLoader ::class .java.getDeclaredMethod(" addURL" , URL ::class .java)
15
- method.isAccessible = true
16
- method.invoke(classLoader, url)
17
- } catch (e: Exception ) {
18
- throw Exception (
19
- " Failed to inject patches! The CLI does NOT work on Java 9 and above, please use Java 8!" ,
20
- e // propagate exception
21
- )
22
- }
23
- }
8
+
24
9
}
25
10
}
Original file line number Diff line number Diff line change 1
1
package app.revanced.patch
2
2
3
+ import app.revanced.patcher.data.base.Data
4
+ import app.revanced.patcher.patch.base.Patch
3
5
import app.revanced.patches.Index
6
+ import java.io.File
7
+ import java.net.URLClassLoader
4
8
5
- internal class Patches {
6
- internal companion object {
7
- // You may ask yourself, "why do this?".
8
- // We do it like this, because we don't want the Index class
9
- // to be loaded while the dependency hasn't been injected yet.
10
- // You can see this as "controlled class loading".
11
- // Whenever this class is loaded (because it is invoked), all the imports
12
- // will be loaded too. We don't want to do this until we've injected the class.
13
- internal fun loadPatches () = Index .patches
9
+ internal object Patches {
10
+
11
+
12
+ /* *
13
+ * This method loads patches from a given patch file
14
+ * @return the loaded patches represented as a list of functions returning instances of [Patch]
15
+ */
16
+ internal fun load (patchFile : File ): List <() -> Patch <Data >> {
17
+ val url = patchFile.toURI().toURL()
18
+ val classLoader = URLClassLoader (arrayOf(url))
19
+ return loadIndex(classLoader).patches
14
20
}
15
- }
21
+ private fun loadIndex (classLoader : ClassLoader ) = classLoader
22
+ .loadClass(Index ::class .java.canonicalName)
23
+ .fields
24
+ .first()
25
+ .get(null ) as Index
26
+ }
Original file line number Diff line number Diff line change @@ -12,10 +12,7 @@ internal class FileSystemUtils(
12
12
private var fileSystem: FileSystem
13
13
14
14
init {
15
- fileSystem = FileSystems .newFileSystem(
16
- file.toPath(),
17
- null
18
- )
15
+ fileSystem = FileSystems .newFileSystem(file.toPath(), null as ClassLoader ? )
19
16
}
20
17
21
18
private fun deleteDirectory (dirPath : String ) {
Original file line number Diff line number Diff line change @@ -91,7 +91,7 @@ object Signer {
91
91
(keyStore.getKey(alias, PASSWORD ) as PrivateKey )
92
92
)
93
93
94
- val zip = FileSystems .newFileSystem(apkFile.toPath(), null )
94
+ val zip = FileSystems .newFileSystem(apkFile.toPath(), null as ClassLoader ? )
95
95
96
96
val dig = MessageDigest .getInstance(" SHA1" )
97
97
val digests: MutableMap <String , String > = LinkedHashMap ()
You can’t perform that action at this time.
0 commit comments