From d027217a0ef4dd3e87a09f3ece7ad7e634346654 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sat, 16 Oct 2021 16:06:57 -0400 Subject: [PATCH 1/2] Add support for caches between 660 and 742. --- .../runetopic/cache/extension/ByteBuffer.kt | 20 +++++++++------- .../cache/hierarchy/ReferenceTable.kt | 24 +++++++++++-------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cache/src/main/kotlin/com/runetopic/cache/extension/ByteBuffer.kt b/cache/src/main/kotlin/com/runetopic/cache/extension/ByteBuffer.kt index 40846d1..d1c3c10 100644 --- a/cache/src/main/kotlin/com/runetopic/cache/extension/ByteBuffer.kt +++ b/cache/src/main/kotlin/com/runetopic/cache/extension/ByteBuffer.kt @@ -17,24 +17,28 @@ fun ByteBuffer.readCp1252Char(): Char { } fun ByteBuffer.readUnsignedSmart(): Int { - val peek: Int = get(position()).toInt() and 0xFF + val peek = get(position()).toInt() and 0xFF return if (peek < 128) readUnsignedByte() else (readUnsignedShort()) - 0x8000 } +fun ByteBuffer.readUnsignedSmartShort(): Int { + return if (get(position()).toInt() < 0) int and Int.MAX_VALUE else readUnsignedShort() +} + fun ByteBuffer.readUnsignedByte(): Int = get().toInt() and 0xFF fun ByteBuffer.readUnsignedShort(): Int = short.toInt() and 0xFFFF fun ByteBuffer.readMedium(): Int = (get().toInt() and 0xFF) shl 16 or (get().toInt() and 0xFF shl 8) or (get().toInt() and 0xFF) fun ByteBuffer.skip(amount: Int): ByteBuffer = position(position() + amount) fun ByteBuffer.readUnsignedIntSmartShortCompat(): Int { - var i = 0 - var i2: Int = readUnsignedSmart() - while (i2 == 32767) { - i2 = readUnsignedSmart() - i += 32767 + var value = 0 + var i = readUnsignedSmart() + while (i == 32767) { + i = readUnsignedSmart() + value += 32767 } - i += i2 - return i + value += i + return value } fun ByteBuffer.whirlpool(): ByteArray { diff --git a/cache/src/main/kotlin/com/runetopic/cache/hierarchy/ReferenceTable.kt b/cache/src/main/kotlin/com/runetopic/cache/hierarchy/ReferenceTable.kt index e381282..92e68f7 100644 --- a/cache/src/main/kotlin/com/runetopic/cache/hierarchy/ReferenceTable.kt +++ b/cache/src/main/kotlin/com/runetopic/cache/hierarchy/ReferenceTable.kt @@ -5,6 +5,7 @@ import com.runetopic.cache.codec.decompress import com.runetopic.cache.exception.ProtocolException import com.runetopic.cache.extension.readUnsignedByte import com.runetopic.cache.extension.readUnsignedShort +import com.runetopic.cache.extension.readUnsignedSmartShort import com.runetopic.cache.hierarchy.index.Js5Index import com.runetopic.cache.hierarchy.index.group.Js5Group import com.runetopic.cache.hierarchy.index.group.file.File @@ -70,12 +71,12 @@ internal data class ReferenceTable( val compressionType = decompressed.compression val protocol = buffer.readUnsignedByte() val revision = when { - protocol < 5 || protocol > 6 -> throw ProtocolException("Unhandled protocol $protocol when reading index $this") - protocol == 6 -> buffer.int + protocol < 5 || protocol > 7 -> throw ProtocolException("Unhandled protocol $protocol when reading index $this") + protocol >= 6 -> buffer.int else -> 0 } val hash = buffer.readUnsignedByte() - val count = buffer.readUnsignedShort() + val count = if (protocol >= 7) buffer.readUnsignedSmartShort() else buffer.readUnsignedShort() val groupTables = mutableListOf() (0 until count).forEach { groupTables.add(datFile.readReferenceTable(idxFile.id(), idxFile.loadReferenceTable(it))) @@ -103,7 +104,8 @@ internal data class ReferenceTable( var lastGroupId = 0 var biggest = -1 (0 until count).forEach { - groupIds[it] = buffer.readUnsignedShort().let { id -> lastGroupId += id; lastGroupId } + groupIds[it] = if (protocol >= 7) { buffer.readUnsignedSmartShort() } else { buffer.readUnsignedShort() } + .let { id -> lastGroupId += id; lastGroupId } if (groupIds[it] > biggest) biggest = groupIds[it] } @@ -112,8 +114,8 @@ internal data class ReferenceTable( val groupCrcs = groupCrcs(largestGroupId, count, groupIds, buffer) val groupWhirlpools = groupWhirlpools(largestGroupId, isUsingWhirlPool, count, buffer, groupIds) val groupRevisions = groupRevisions(largestGroupId, count, groupIds, buffer) - val groupFileIds = groupFileIds(largestGroupId, count, groupIds, buffer) - val fileIds = fileIds(largestGroupId, groupFileIds, count, groupIds, buffer) + val groupFileIds = groupFileIds(largestGroupId, count, groupIds, buffer, protocol) + val fileIds = fileIds(largestGroupId, groupFileIds, count, groupIds, buffer, protocol) val fileNameHashes = fileNameHashes(largestGroupId, groupFileIds, count, groupIds, buffer, isNamed) val groups = hashMapOf() @@ -138,10 +140,11 @@ internal data class ReferenceTable( count: Int, groupIds: IntArray, buffer: ByteBuffer, + protocol: Int ): IntArray { val groupFileIds = IntArray(largestGroupId) (0 until count).forEach { - groupFileIds[groupIds[it]] = buffer.readUnsignedShort() + groupFileIds[groupIds[it]] = if (protocol >= 7) buffer.readUnsignedSmartShort() else buffer.readUnsignedShort() } return groupFileIds } @@ -211,15 +214,16 @@ internal data class ReferenceTable( validFileIds: IntArray, count: Int, groupIds: IntArray, - buffer: ByteBuffer + buffer: ByteBuffer, + protocol: Int ): Array { val fileIds = Array(largestGroupId) { IntArray(validFileIds[it]) } (0 until count).forEach { val groupId = groupIds[it] var currentFileId = 0 (0 until validFileIds[groupId]).forEach { fileId -> - buffer.readUnsignedShort() - .let { id -> currentFileId += id; currentFileId } + if (protocol >= 7) { buffer.readUnsignedSmartShort() } else { buffer.readUnsignedShort() } + .let { i -> currentFileId += i; currentFileId } .also { fileIds[groupId][fileId] = currentFileId } } } From 99775c2e9bae53d2394cf1aeefae6d1ebc7b9b62 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sat, 16 Oct 2021 16:12:03 -0400 Subject: [PATCH 2/2] Bump version and update README. --- README.md | 6 +++--- cache/build.gradle.kts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a0991c1..97e553b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A cache library written in Kotlin. - Java Version 16 # Supported -- RS2 (414-659) +- RS2 (414-742) - OSRS (1-current) # Features @@ -21,7 +21,7 @@ A cache library written in Kotlin. # TODO - Cache Writing - Flat file system for unpacking the cache files into a raw format that can be git versioned. -- Support for RS2 caches bigger than revision 659. +- Support for RS2 caches revision 743+. - ~317 cache format support. - RS3 caches. - Testing @@ -29,7 +29,7 @@ A cache library written in Kotlin. # Implementation Just use cache if you do not require any of the revision specific loaders. ``` -cache = { module = "com.runetopic.cache:cache", version.ref "1.4.10-SNAPSHOT" } +cache = { module = "com.runetopic.cache:cache", version.ref "1.4.11-SNAPSHOT" } loader = { module = "com.runetopic.cache:loader", version.ref "647.6.1-SNAPSHOT" } ``` diff --git a/cache/build.gradle.kts b/cache/build.gradle.kts index 3debab0..667b88f 100644 --- a/cache/build.gradle.kts +++ b/cache/build.gradle.kts @@ -4,7 +4,7 @@ plugins { signing } -version = "1.4.10-SNAPSHOT" +version = "1.4.11-SNAPSHOT" java { withJavadocJar()