Skip to content

Commit

Permalink
refactor: Move functions to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Aug 3, 2024
1 parent cc47c0b commit 27621e1
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 170 deletions.
14 changes: 14 additions & 0 deletions api/android/revanced-library.api
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public final class app/revanced/library/Options$Patch$Option {
public final fun getValue ()Ljava/lang/Object;
}

public final class app/revanced/library/OptionsKt {
public static final fun setOptions (Ljava/util/Set;Ljava/util/Map;)V
}

public final class app/revanced/library/PatchKt {
public static final fun mostCommonCompatibleVersions (Ljava/util/Set;Ljava/util/Set;Z)Ljava/util/Map;
public static synthetic fun mostCommonCompatibleVersions$default (Ljava/util/Set;Ljava/util/Set;ZILjava/lang/Object;)Ljava/util/Map;
}

public final class app/revanced/library/PatchUtils {
public static final field INSTANCE Lapp/revanced/library/PatchUtils;
public final fun getMostCommonCompatibleVersions (Ljava/util/Set;Ljava/util/Set;Z)Ljava/util/Map;
Expand Down Expand Up @@ -122,6 +131,11 @@ public final class app/revanced/library/PatchUtils$Json$FullJsonPatch$FullJsonPa
public abstract interface class app/revanced/library/PatchUtils$Json$JsonPatch {
}

public final class app/revanced/library/SerializationKt {
public static final fun serializeTo (Ljava/util/Set;Ljava/io/OutputStream;Z)V
public static synthetic fun serializeTo$default (Ljava/util/Set;Ljava/io/OutputStream;ZILjava/lang/Object;)V
}

public final class app/revanced/library/Utils {
public static final field INSTANCE Lapp/revanced/library/Utils;
public final fun isAndroidEnvironment ()Z
Expand Down
14 changes: 14 additions & 0 deletions api/jvm/revanced-library.api
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public final class app/revanced/library/Options$Patch$Option {
public final fun getValue ()Ljava/lang/Object;
}

public final class app/revanced/library/OptionsKt {
public static final fun setOptions (Ljava/util/Set;Ljava/util/Map;)V
}

public final class app/revanced/library/PatchKt {
public static final fun mostCommonCompatibleVersions (Ljava/util/Set;Ljava/util/Set;Z)Ljava/util/Map;
public static synthetic fun mostCommonCompatibleVersions$default (Ljava/util/Set;Ljava/util/Set;ZILjava/lang/Object;)Ljava/util/Map;
}

public final class app/revanced/library/PatchUtils {
public static final field INSTANCE Lapp/revanced/library/PatchUtils;
public final fun getMostCommonCompatibleVersions (Ljava/util/Set;Ljava/util/Set;Z)Ljava/util/Map;
Expand Down Expand Up @@ -122,6 +131,11 @@ public final class app/revanced/library/PatchUtils$Json$FullJsonPatch$FullJsonPa
public abstract interface class app/revanced/library/PatchUtils$Json$JsonPatch {
}

public final class app/revanced/library/SerializationKt {
public static final fun serializeTo (Ljava/util/Set;Ljava/io/OutputStream;Z)V
public static synthetic fun serializeTo$default (Ljava/util/Set;Ljava/io/OutputStream;ZILjava/lang/Object;)V
}

public final class app/revanced/library/Utils {
public static final field INSTANCE Lapp/revanced/library/Utils;
public final fun isAndroidEnvironment ()Z
Expand Down
20 changes: 11 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.android.library)
alias(libs.plugins.binary.compatibility.validator)
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlin.serialization)
`maven-publish`
signing
}
Expand Down Expand Up @@ -47,25 +48,26 @@ kotlin {

sourceSets {
androidMain.dependencies {
implementation(libs.core.ktx)
implementation(libs.libsu.nio)
implementation(libs.libsu.service)
implementation(libs.core.ktx)
}

commonMain.dependencies {
implementation(libs.revanced.patcher)
implementation(libs.kotlin.reflect)
implementation(libs.jadb) // Fork with Shell v2 support.
implementation(libs.bcpkix.jdk15on)
implementation(libs.jackson.module.kotlin)
implementation(libs.apkzlib)
implementation(libs.apksig)
implementation(libs.apkzlib)
implementation(libs.bcpkix.jdk15on)
implementation(libs.guava)
implementation(libs.jadb)
implementation(libs.jackson.module.kotlin)
implementation(libs.kotlin.reflect)
implementation(libs.kotlinx.serialization.json)
implementation(libs.revanced.patcher)
}

commonTest.dependencies {
implementation(libs.revanced.patcher)
implementation(libs.kotlin.test.junit)
implementation(libs.revanced.patcher)
}
}
}
Expand Down
31 changes: 18 additions & 13 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
[versions]
jackson-module-kotlin = "2.16.1"
jadb = "1.2.1"
kotlin = "2.0.0"
revanced-patcher = "20.0.0-dev.2"
binary-compatibility-validator = "0.15.1"
android = "8.5.1"
bcpkix-jdk15on = "1.70"
binary-compatibility-validator = "0.15.1"
core-ktx = "1.13.1"
guava = "33.0.0-jre"
jackson-module-kotlin = "2.16.1"
jadb = "1.2.1"
kotlin = "2.0.0"
kotlinx-coroutines = "1.8.1"
kotlinx-serialization = "1.7.1"
libsu = "5.2.2"
core-ktx = "1.13.1"
revanced-patcher = "20.0.0-dev.2"

[libraries]
jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson-module-kotlin" }
jadb = { module = "app.revanced:jadb", version.ref = "jadb" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
revanced-patcher = { module = "app.revanced:revanced-patcher", version.ref = "revanced-patcher" }
apkzlib = { module = "com.android.tools.build:apkzlib", version.ref = "android" }
apksig = { module = "com.android.tools.build:apksig", version.ref = "android" }
bcpkix-jdk15on = { module = "org.bouncycastle:bcpkix-jdk15on", version.ref = "bcpkix-jdk15on" }
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson-module-kotlin" }
jadb = { module = "app.revanced:jadb", version.ref = "jadb" } # Fork with Shell v2 support.
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
libsu-core = { module = "com.github.topjohnwu.libsu:core", version.ref = "libsu" }
libsu-nio = { module = "com.github.topjohnwu.libsu:nio", version.ref = "libsu" }
libsu-service = { module = "com.github.topjohnwu.libsu:service", version.ref = "libsu" }
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
revanced-patcher = { module = "app.revanced:revanced-patcher", version.ref = "revanced-patcher" }

[plugins]
binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binary-compatibility-validator" }
android-library = { id = "com.android.library", version.ref = "android" }
binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binary-compatibility-validator" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
50 changes: 34 additions & 16 deletions src/commonMain/kotlin/app/revanced/library/Options.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,32 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.io.File
import java.util.logging.Logger

private val logger = Logger.getLogger("Options")

typealias PatchName = String
typealias OptionKey = String
typealias OptionValue = Any?
typealias PatchesOptions = Map<PatchName, Map<OptionKey, OptionValue>>

/**
* Set the options for a set of patches that have a name.
*
* @param options The options to set. The key is the patch name and the value is a map of option keys to option values.
*/
fun Set<Patch<*>>.setOptions(options: PatchesOptions) = filter { it.name != null }.forEach { patch ->
val patchOptions = options[patch.name] ?: return@forEach

patch.options.forEach option@{ option ->
try {
patch.options[option.key] = patchOptions[option.key] ?: return@option
} catch (e: OptionException) {
logger.warning("Could not set option value for the \"${patch.name}\" patch: ${e.message}")
}
}
}

@Suppress("unused")
@Deprecated("Functions have been moved to top level.")
object Options {
private val logger = Logger.getLogger(Options::class.java.name)

Expand All @@ -21,6 +46,7 @@ object Options {
* @param prettyPrint Whether to pretty print the JSON.
* @return The JSON string containing the options.
*/
@Deprecated("Functions have been moved to the Serialization class.")
fun serialize(
patches: Set<app.revanced.patcher.patch.Patch<*>>,
prettyPrint: Boolean = false,
Expand All @@ -35,7 +61,7 @@ object Options {
try {
option.value
} catch (e: OptionException) {
logger.warning("Using default option value for the ${patch.name} patch: ${e.message}")
logger.warning("Using default option value for the \"${patch.name}\" patch: ${e.message}")
option.default
}

Expand All @@ -60,33 +86,24 @@ object Options {
* @return A set of [Patch]s.
* @see Patch
*/
@Deprecated("Functions have been moved to the Serialization class.")
fun deserialize(json: String): Array<Patch> = mapper.readValue(json, Array<Patch>::class.java)

/**
* Sets the options for a set of patches.
*
* @param json The JSON string containing the options.
*/
@Deprecated("Function has been moved to top level.")
fun Set<app.revanced.patcher.patch.Patch<*>>.setOptions(json: String) {
filter { it.options.any() }.let { patches ->
if (patches.isEmpty()) return

val jsonPatches =
deserialize(json).associate {
it.patchName to it.options.associate { option -> option.key to option.value }
}

patches.forEach { patch ->
jsonPatches[patch.name]?.let { jsonPatchOptions ->
jsonPatchOptions.forEach { (option, value) ->
try {
patch.options[option] = value
} catch (e: OptionException) {
logger.warning("Could not set option value for the ${patch.name} patch: ${e.message}")
}
}
}
val jsonPatches = deserialize(json).associate {
it.patchName to it.options.associate { option -> option.key to option.value }
}

setOptions(jsonPatches)
}
}

Expand All @@ -96,6 +113,7 @@ object Options {
* @param file The file containing the JSON string containing the options.
* @see setOptions
*/
@Deprecated("Function has been moved to top level.")
fun Set<app.revanced.patcher.patch.Patch<*>>.setOptions(file: File) = setOptions(file.readText())

/**
Expand Down
52 changes: 52 additions & 0 deletions src/commonMain/kotlin/app/revanced/library/Patch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package app.revanced.library

import app.revanced.patcher.patch.Package
import app.revanced.patcher.patch.Patch

typealias PackageName = String
typealias Version = String
typealias Count = Int

typealias VersionMap = LinkedHashMap<Version, Count>
typealias PackageNameMap = Map<PackageName, VersionMap>

/**
* Get the count of versions for each compatible package from the set of [Patch] ordered by the most common version.
*
* @param packageNames The names of the compatible packages to include. If null, all packages will be included.
* @param countUnusedPatches Whether to count patches that are not used.
* @return A map of package names to a map of versions to their count.
*/
fun Set<Patch<*>>.mostCommonCompatibleVersions(
packageNames: Set<String>? = null,
countUnusedPatches: Boolean = false,
): PackageNameMap = buildMap {
fun filterWantedPackages(compatiblePackages: List<Package>): List<Package> {
val wantedPackages = packageNames?.toHashSet() ?: return compatiblePackages
return compatiblePackages.filter { (name, _) -> name in wantedPackages }
}

this@mostCommonCompatibleVersions.filter { it.use || countUnusedPatches }
.flatMap { it.compatiblePackages ?: emptyList() }
.let(::filterWantedPackages)
.forEach { (name, versions) ->
if (versions?.isEmpty() == true) {
return@forEach
}

val versionMap = getOrPut(name) { linkedMapOf() }

versions?.forEach { version ->
versionMap[version] = versionMap.getOrDefault(version, 0) + 1
}
}

// Sort the version maps by the most common version.
forEach { (packageName, versionMap) ->
this[packageName] =
versionMap
.asIterable()
.sortedWith(compareByDescending { it.value })
.associate { it.key to it.value } as VersionMap
}
}
60 changes: 9 additions & 51 deletions src/commonMain/kotlin/app/revanced/library/PatchUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,19 @@ import java.io.InputStream
import java.io.OutputStream
import kotlin.reflect.KType

typealias PackageName = String
typealias Version = String
typealias Count = Int

typealias VersionMap = LinkedHashMap<Version, Count>
typealias PackageNameMap = Map<PackageName, VersionMap>

/**
* Utility functions for working with patches.
*/
@Suppress("MemberVisibilityCanBePrivate", "unused")
@Deprecated("Functions have been moved to top level.")
object PatchUtils {
/**
* Get the count of versions for each compatible package from a supplied set of [patches] ordered by the most common version.
*
* @param patches The set of patches to check.
* @param packageNames The names of the compatible packages to include. If null, all packages will be included.
* @param countUnusedPatches Whether to count patches that are not used.
* @return A map of package names to a map of versions to their count.
*/
@Deprecated(
"Function has been moved to top level.",
ReplaceWith("patches.mostCommonCompatibleVersions(packageNames, countUnusedPatches)"),
)
fun getMostCommonCompatibleVersions(
patches: Set<Patch<*>>,
packageNames: Set<String>? = null,
countUnusedPatches: Boolean = false,
): PackageNameMap =
buildMap {
fun filterWantedPackages(compatiblePackages: Iterable<Package>): Iterable<Package> {
val wantedPackages = packageNames?.toHashSet() ?: return compatiblePackages
return compatiblePackages.filter { (name, _) -> name in wantedPackages }
}

patches
.filter { it.use || countUnusedPatches }
.flatMap { it.compatiblePackages ?: emptyList() }
.let(::filterWantedPackages)
.forEach { (name, versions) ->
if (versions?.isEmpty() == true) {
return@forEach
}

val versionMap = getOrPut(name) { linkedMapOf() }

versions?.forEach { version ->
versionMap[version] = versionMap.getOrDefault(version, 0) + 1
}
}

// Sort the version maps by the most common version.
forEach { (packageName, versionMap) ->
this[packageName] =
versionMap
.asIterable()
.sortedWith(compareByDescending { it.value })
.associate { it.key to it.value } as VersionMap
}
}
): PackageNameMap = patches.mostCommonCompatibleVersions(packageNames, countUnusedPatches)

@Deprecated("Functions have been moved to the Serialization class.")
object Json {
private val mapper = jacksonObjectMapper()

Expand All @@ -76,6 +32,7 @@ object PatchUtils {
* @param prettyPrint Whether to pretty print the JSON.
* @param outputStream The output stream to write the JSON to.
*/
@Deprecated("Functions have been moved to the Serialization class.")
fun serialize(
patches: Set<Patch<*>>,
transform: (Patch<*>) -> JsonPatch = { patch -> FullJsonPatch.fromPatch(patch) },
Expand All @@ -99,6 +56,7 @@ object PatchUtils {
* @return A set of [JsonPatch]es.
* @see FullJsonPatch
*/
@Deprecated("This function will be removed in the future.")
fun <T : JsonPatch> deserialize(
inputStream: InputStream,
jsonPatchElementClass: Class<T>,
Expand Down
Loading

0 comments on commit 27621e1

Please sign in to comment.