From 44fb2ebe256aa5bcdb26aba54cdce4048ac58b49 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 2 Apr 2020 19:08:43 +0100 Subject: [PATCH] Add crc32 command This is useful for checking the CRC-32 checksum of files used by the loader. It outputs the checksum in signed decimal format, which is the format used in the file name suffixes in the loader (e.g. unpackclass_-1911426584.pack). Signed-off-by: Graham --- all/build.gradle.kts | 1 + all/src/main/java/dev/openrs2/Command.kt | 2 ++ crc32/build.gradle.kts | 29 +++++++++++++++++ .../java/dev/openrs2/crc32/Crc32Command.kt | 32 +++++++++++++++++++ settings.gradle.kts | 1 + 5 files changed, 65 insertions(+) create mode 100644 crc32/build.gradle.kts create mode 100644 crc32/src/main/java/dev/openrs2/crc32/Crc32Command.kt diff --git a/all/build.gradle.kts b/all/build.gradle.kts index a14ef778..cd89e8f2 100644 --- a/all/build.gradle.kts +++ b/all/build.gradle.kts @@ -17,6 +17,7 @@ application { dependencies { implementation(project(":bundler")) implementation(project(":compress-cli")) + implementation(project(":crc32")) implementation(project(":decompiler")) implementation(project(":deob")) implementation(project(":deob-ast")) diff --git a/all/src/main/java/dev/openrs2/Command.kt b/all/src/main/java/dev/openrs2/Command.kt index 657105cf..7670b8a1 100644 --- a/all/src/main/java/dev/openrs2/Command.kt +++ b/all/src/main/java/dev/openrs2/Command.kt @@ -4,6 +4,7 @@ import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.core.subcommands import dev.openrs2.bundler.BundleCommand import dev.openrs2.compress.cli.CompressCommand +import dev.openrs2.crc32.Crc32Command import dev.openrs2.decompiler.DecompileCommand import dev.openrs2.deob.DeobfuscateCommand import dev.openrs2.deob.ast.AstDeobfuscateCommand @@ -12,6 +13,7 @@ import dev.openrs2.game.GameCommand fun main(args: Array) = Command().subcommands( BundleCommand(), CompressCommand(), + Crc32Command(), DecompileCommand(), DeobfuscateCommand(), AstDeobfuscateCommand(), diff --git a/crc32/build.gradle.kts b/crc32/build.gradle.kts new file mode 100644 index 00000000..ec0f5c71 --- /dev/null +++ b/crc32/build.gradle.kts @@ -0,0 +1,29 @@ +plugins { + `maven-publish` + application + kotlin("jvm") +} + +application { + mainClassName = "dev.openrs2.crc32.Crc32CommandKt" +} + +dependencies { + api(project(":cli")) +} + +publishing { + publications.create("maven") { + from(components["java"]) + + pom { + packaging = "jar" + name.set("OpenRS2 CRC-32") + description.set( + """ + A tool for calculating the CRC-32 checksum of a file. + """.trimIndent() + ) + } + } +} diff --git a/crc32/src/main/java/dev/openrs2/crc32/Crc32Command.kt b/crc32/src/main/java/dev/openrs2/crc32/Crc32Command.kt new file mode 100644 index 00000000..2b89cfe3 --- /dev/null +++ b/crc32/src/main/java/dev/openrs2/crc32/Crc32Command.kt @@ -0,0 +1,32 @@ +package dev.openrs2.crc32 + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.option +import dev.openrs2.cli.defaultStdin +import dev.openrs2.cli.inputStream +import java.util.zip.CRC32 + +fun main(args: Array) = Crc32Command().main(args) + +class Crc32Command : CliktCommand(name = "crc32") { + private val input by option().inputStream().defaultStdin() + + override fun run() { + val crc = CRC32() + + input.use { input -> + val bytes = ByteArray(4096) + + while (true) { + val len = input.read(bytes) + if (len == -1) { + break + } + + crc.update(bytes, 0, len) + } + } + + echo(crc.value.toInt()) + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 55a4b761..bec30dee 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -10,6 +10,7 @@ include( "common", "compress", "compress-cli", + "crc32", "decompiler", "deob", "deob-annotations",