Provide a better WTC interface

This commit is contained in:
Stedoss 2025-02-23 20:46:58 +00:00
parent cc10fa221e
commit 3ea6c2e8e4
2 changed files with 263 additions and 258 deletions

View File

@ -8,11 +8,15 @@ import kotlin.math.round
// JVM implementation of https://github.com/circleguard/wtc-lzma-compressor/tree/master
class WTC {
companion object {
private const val CURRENT_VERSION_HEADER: Short = 1
private val VERSION_HEADER_BYTE_ARRAY = byteArrayOf((CURRENT_VERSION_HEADER.toInt() and 0xFF).toByte(), ((CURRENT_VERSION_HEADER.toInt() shr 8) and 0xFF).toByte())
fun wtcCompress(stream: String): ByteArray {
fun compress(stream: String): ByteArray {
val lists = seperate(stream)
val xs = unsortedDiffPackShortsToBytes(lists.x)
@ -44,7 +48,7 @@ fun wtcCompress(stream: String): ByteArray {
return byteStream.toByteArray()
}
fun wtcDecompress(data: ByteArray, hasVersionHeader: Boolean = true): String {
fun decompress(data: ByteArray, hasVersionHeader: Boolean = true): String {
val buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN)
fun unpackBytes(): ByteArray {
@ -272,3 +276,5 @@ private fun cumSum(arr: ShortArray): ShortArray {
return cumArr
}
}
}

View File

@ -1,7 +1,6 @@
package com.nisemoe.nise.osu
import com.nisemoe.nise.replays.wtcCompress
import com.nisemoe.nise.replays.wtcDecompress
import com.nisemoe.nise.replays.WTC
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.junit.jupiter.api.Assertions
@ -21,7 +20,7 @@ class WtcTest {
val expected = resourcesPath.resolve("${replayName}_compressed.dat").toFile().readBytes()
val replayEvents = resourcesPath.resolve("${replayName}_events.txt").toFile().readText()
val wtcCompressed = wtcCompress(replayEvents)
val wtcCompressed = WTC.compress(replayEvents)
// We include a version header at the start of the compressed byte array - create a new array excluding these
// so we can compare just the raw data with the Python WTC implementation's output.
@ -37,7 +36,7 @@ class WtcTest {
val expected = resourcesPath.resolve("${replayName}_decompressed.txt").toFile().readText()
val compressedReplay = resourcesPath.resolve("${replayName}_compressed.dat").toFile().readBytes()
val wtcDecompressed = wtcDecompress(compressedReplay, false)
val wtcDecompressed = WTC.decompress(compressedReplay, false)
assertEquals(expected, wtcDecompressed)
}