From 0936f6272c7d1e41b7e9550bb17fe84af5d976c3 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 10 Dec 2023 16:54:57 +0100 Subject: Move utilities around --- archenemyexample/settings.gradle.kts | 4 +- plugin/build.gradle.kts | 2 + .../main/kotlin/moe/nea/archenemy/DownloadUtils.kt | 52 ---------------------- .../mojang/IdentityMinecraftTransformer.kt | 1 + .../archenemy/mojang/MappedRepositoryProvider.kt | 3 +- .../moe/nea/archenemy/mojang/MinecraftProvider.kt | 14 +----- .../archenemy/mojang/OfficialMappingDependency.kt | 2 + .../kotlin/moe/nea/archenemy/util/DigestUtils.kt | 14 ++++++ .../kotlin/moe/nea/archenemy/util/DownloadUtils.kt | 52 ++++++++++++++++++++++ 9 files changed, 77 insertions(+), 67 deletions(-) delete mode 100644 plugin/src/main/kotlin/moe/nea/archenemy/DownloadUtils.kt create mode 100644 plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt create mode 100644 plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt diff --git a/archenemyexample/settings.gradle.kts b/archenemyexample/settings.gradle.kts index d69f7a0..b538163 100644 --- a/archenemyexample/settings.gradle.kts +++ b/archenemyexample/settings.gradle.kts @@ -3,5 +3,7 @@ pluginManagement { repositories { mavenCentral() gradlePluginPortal() + maven("https://maven.fabricmc.net") maven("https://maven.neoforged.net/releases") - }} \ No newline at end of file + } +} \ No newline at end of file diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index d0a7eff..4ea04aa 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -7,6 +7,7 @@ plugins { repositories { mavenCentral() maven("https://maven.neoforged.net/releases") + maven("https://maven.fabricmc.net") } dependencies { @@ -14,6 +15,7 @@ dependencies { implementation(platform("org.jetbrains.kotlin:kotlin-bom")) implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2") + implementation("net.fabricmc:mapping-io:0.1.8") testImplementation("org.jetbrains.kotlin:kotlin-test") testImplementation("org.jetbrains.kotlin:kotlin-test-junit") diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/DownloadUtils.kt b/plugin/src/main/kotlin/moe/nea/archenemy/DownloadUtils.kt deleted file mode 100644 index c849349..0000000 --- a/plugin/src/main/kotlin/moe/nea/archenemy/DownloadUtils.kt +++ /dev/null @@ -1,52 +0,0 @@ -package moe.nea.archenemy - -import java.io.File -import java.io.IOException -import java.net.URL -import java.security.MessageDigest - -object DownloadUtils { - fun bytesToHex(hash: ByteArray): String { - val hexString = StringBuilder(2 * hash.size) - for (i in hash.indices) { - val hex = Integer.toHexString(0xff and hash[i].toInt()) - if (hex.length == 1) { - hexString.append('0') - } - hexString.append(hex) - } - return hexString.toString() - } - - - fun sha1Hash(file: File): String { - if (!file.exists()) return "" - val messageDigest = MessageDigest.getInstance("SHA-1") - - file.inputStream().use { - val r = ByteArray(4096) - while (true) { - val d = it.read(r) - if (d < 0) break - messageDigest.update(r, 0, d) - } - } - return bytesToHex(messageDigest.digest()) - } - - fun areHashesEqual(a: String, b: String): Boolean { - return b.equals(a, ignoreCase = true) - } - - fun downloadFile(source: URL, sha1: String, targetFile: File) { - targetFile.parentFile.mkdirs() - if (areHashesEqual(sha1Hash(targetFile), sha1)) return - source.openStream().use { inp -> - targetFile.outputStream().use { out -> - inp.copyTo(out) - } - } - if (!areHashesEqual(sha1Hash(targetFile), sha1)) - throw IOException("$targetFile should hash to $sha1, but does not") - } -} \ No newline at end of file diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/IdentityMinecraftTransformer.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/IdentityMinecraftTransformer.kt index 9d23c60..bb7a5cb 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/IdentityMinecraftTransformer.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/IdentityMinecraftTransformer.kt @@ -1,5 +1,6 @@ package moe.nea.archenemy.mojang +import moe.nea.archenemy.util.update import java.io.File import java.security.MessageDigest diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt index d176d45..ddfff0d 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt @@ -1,7 +1,8 @@ package moe.nea.archenemy.mojang -import moe.nea.archenemy.DownloadUtils +import moe.nea.archenemy.util.DownloadUtils import moe.nea.archenemy.util.getNullsafeIdentifier +import moe.nea.archenemy.util.updateField import net.minecraftforge.artifactural.api.artifact.Artifact import net.minecraftforge.artifactural.api.artifact.ArtifactIdentifier import net.minecraftforge.artifactural.api.artifact.ArtifactType diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MinecraftProvider.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MinecraftProvider.kt index 64c1d04..605d485 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MinecraftProvider.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MinecraftProvider.kt @@ -2,7 +2,7 @@ package moe.nea.archenemy.mojang import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream -import moe.nea.archenemy.DownloadUtils +import moe.nea.archenemy.util.DownloadUtils import moe.nea.archenemy.MCSide import moe.nea.archenemy.util.getNullsafeIdentifier import net.minecraftforge.artifactural.api.artifact.Artifact @@ -14,20 +14,8 @@ import net.minecraftforge.artifactural.base.artifact.StreamableArtifact import java.io.File import java.io.IOException import java.net.URL -import java.security.MessageDigest import java.util.concurrent.ConcurrentHashMap -fun MessageDigest.updateField(text: String, value: String) { - this.update(text) - this.update(":") - this.update(value) - this.update(";") -} - -fun MessageDigest.update(text: String) { - this.update(text.encodeToByteArray()) -} - class MinecraftProvider(val sharedExtension: ArchenemySharedExtension) : Repository { data class MinecraftCoordinate( diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/OfficialMappingDependency.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/OfficialMappingDependency.kt index 8617ede..4e44146 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/OfficialMappingDependency.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/OfficialMappingDependency.kt @@ -1,6 +1,8 @@ package moe.nea.archenemy.mojang import moe.nea.archenemy.MCSide +import moe.nea.archenemy.util.update +import moe.nea.archenemy.util.updateField import org.gradle.api.artifacts.Dependency import org.gradle.api.provider.Provider import java.io.File diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt b/plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt new file mode 100644 index 0000000..4f50000 --- /dev/null +++ b/plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt @@ -0,0 +1,14 @@ +package moe.nea.archenemy.util + +import java.security.MessageDigest + +fun MessageDigest.updateField(text: String, value: String) { + this.update(text) + this.update(":") + this.update(value) + this.update(";") +} + +fun MessageDigest.update(text: String) { + this.update(text.encodeToByteArray()) +} \ No newline at end of file diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt b/plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt new file mode 100644 index 0000000..cec2c55 --- /dev/null +++ b/plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt @@ -0,0 +1,52 @@ +package moe.nea.archenemy.util + +import java.io.File +import java.io.IOException +import java.net.URL +import java.security.MessageDigest + +object DownloadUtils { + fun bytesToHex(hash: ByteArray): String { + val hexString = StringBuilder(2 * hash.size) + for (i in hash.indices) { + val hex = Integer.toHexString(0xff and hash[i].toInt()) + if (hex.length == 1) { + hexString.append('0') + } + hexString.append(hex) + } + return hexString.toString() + } + + + fun sha1Hash(file: File): String { + if (!file.exists()) return "" + val messageDigest = MessageDigest.getInstance("SHA-1") + + file.inputStream().use { + val r = ByteArray(4096) + while (true) { + val d = it.read(r) + if (d < 0) break + messageDigest.update(r, 0, d) + } + } + return bytesToHex(messageDigest.digest()) + } + + fun areHashesEqual(a: String, b: String): Boolean { + return b.equals(a, ignoreCase = true) + } + + fun downloadFile(source: URL, sha1: String, targetFile: File) { + targetFile.parentFile.mkdirs() + if (areHashesEqual(sha1Hash(targetFile), sha1)) return + source.openStream().use { inp -> + targetFile.outputStream().use { out -> + inp.copyTo(out) + } + } + if (!areHashesEqual(sha1Hash(targetFile), sha1)) + throw IOException("$targetFile should hash to $sha1, but does not") + } +} \ No newline at end of file -- cgit