Skip to content

Commit 50c7335

Browse files
Merge pull request #17 from runetopic/development
Support for cache revisions 660-742.
2 parents 8787b83 + 99775c2 commit 50c7335

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A cache library written in Kotlin.
99
- Java Version 16
1010

1111
# Supported
12-
- RS2 (414-659)
12+
- RS2 (414-742)
1313
- OSRS (1-current)
1414

1515
# Features
@@ -21,15 +21,15 @@ A cache library written in Kotlin.
2121
# TODO
2222
- Cache Writing
2323
- Flat file system for unpacking the cache files into a raw format that can be git versioned.
24-
- Support for RS2 caches bigger than revision 659.
24+
- Support for RS2 caches revision 743+.
2525
- ~317 cache format support.
2626
- RS3 caches.
2727
- Testing
2828

2929
# Implementation
3030
Just use cache if you do not require any of the revision specific loaders.
3131
```
32-
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.10-SNAPSHOT" }
32+
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.11-SNAPSHOT" }
3333
loader = { module = "com.runetopic.cache:loader", version.ref "647.6.1-SNAPSHOT" }
3434
```
3535

cache/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
signing
55
}
66

7-
version = "1.4.10-SNAPSHOT"
7+
version = "1.4.11-SNAPSHOT"
88

99
java {
1010
withJavadocJar()

cache/src/main/kotlin/com/runetopic/cache/extension/ByteBuffer.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@ fun ByteBuffer.readCp1252Char(): Char {
1717
}
1818

1919
fun ByteBuffer.readUnsignedSmart(): Int {
20-
val peek: Int = get(position()).toInt() and 0xFF
20+
val peek = get(position()).toInt() and 0xFF
2121
return if (peek < 128) readUnsignedByte() else (readUnsignedShort()) - 0x8000
2222
}
2323

24+
fun ByteBuffer.readUnsignedSmartShort(): Int {
25+
return if (get(position()).toInt() < 0) int and Int.MAX_VALUE else readUnsignedShort()
26+
}
27+
2428
fun ByteBuffer.readUnsignedByte(): Int = get().toInt() and 0xFF
2529
fun ByteBuffer.readUnsignedShort(): Int = short.toInt() and 0xFFFF
2630
fun ByteBuffer.readMedium(): Int = (get().toInt() and 0xFF) shl 16 or (get().toInt() and 0xFF shl 8) or (get().toInt() and 0xFF)
2731
fun ByteBuffer.skip(amount: Int): ByteBuffer = position(position() + amount)
2832

2933
fun ByteBuffer.readUnsignedIntSmartShortCompat(): Int {
30-
var i = 0
31-
var i2: Int = readUnsignedSmart()
32-
while (i2 == 32767) {
33-
i2 = readUnsignedSmart()
34-
i += 32767
34+
var value = 0
35+
var i = readUnsignedSmart()
36+
while (i == 32767) {
37+
i = readUnsignedSmart()
38+
value += 32767
3539
}
36-
i += i2
37-
return i
40+
value += i
41+
return value
3842
}
3943

4044
fun ByteBuffer.whirlpool(): ByteArray {

cache/src/main/kotlin/com/runetopic/cache/hierarchy/ReferenceTable.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.runetopic.cache.codec.decompress
55
import com.runetopic.cache.exception.ProtocolException
66
import com.runetopic.cache.extension.readUnsignedByte
77
import com.runetopic.cache.extension.readUnsignedShort
8+
import com.runetopic.cache.extension.readUnsignedSmartShort
89
import com.runetopic.cache.hierarchy.index.Js5Index
910
import com.runetopic.cache.hierarchy.index.group.Js5Group
1011
import com.runetopic.cache.hierarchy.index.group.file.File
@@ -70,12 +71,12 @@ internal data class ReferenceTable(
7071
val compressionType = decompressed.compression
7172
val protocol = buffer.readUnsignedByte()
7273
val revision = when {
73-
protocol < 5 || protocol > 6 -> throw ProtocolException("Unhandled protocol $protocol when reading index $this")
74-
protocol == 6 -> buffer.int
74+
protocol < 5 || protocol > 7 -> throw ProtocolException("Unhandled protocol $protocol when reading index $this")
75+
protocol >= 6 -> buffer.int
7576
else -> 0
7677
}
7778
val hash = buffer.readUnsignedByte()
78-
val count = buffer.readUnsignedShort()
79+
val count = if (protocol >= 7) buffer.readUnsignedSmartShort() else buffer.readUnsignedShort()
7980
val groupTables = mutableListOf<ByteArray>()
8081
(0 until count).forEach {
8182
groupTables.add(datFile.readReferenceTable(idxFile.id(), idxFile.loadReferenceTable(it)))
@@ -103,7 +104,8 @@ internal data class ReferenceTable(
103104
var lastGroupId = 0
104105
var biggest = -1
105106
(0 until count).forEach {
106-
groupIds[it] = buffer.readUnsignedShort().let { id -> lastGroupId += id; lastGroupId }
107+
groupIds[it] = if (protocol >= 7) { buffer.readUnsignedSmartShort() } else { buffer.readUnsignedShort() }
108+
.let { id -> lastGroupId += id; lastGroupId }
107109
if (groupIds[it] > biggest) biggest = groupIds[it]
108110
}
109111

@@ -112,8 +114,8 @@ internal data class ReferenceTable(
112114
val groupCrcs = groupCrcs(largestGroupId, count, groupIds, buffer)
113115
val groupWhirlpools = groupWhirlpools(largestGroupId, isUsingWhirlPool, count, buffer, groupIds)
114116
val groupRevisions = groupRevisions(largestGroupId, count, groupIds, buffer)
115-
val groupFileIds = groupFileIds(largestGroupId, count, groupIds, buffer)
116-
val fileIds = fileIds(largestGroupId, groupFileIds, count, groupIds, buffer)
117+
val groupFileIds = groupFileIds(largestGroupId, count, groupIds, buffer, protocol)
118+
val fileIds = fileIds(largestGroupId, groupFileIds, count, groupIds, buffer, protocol)
117119
val fileNameHashes = fileNameHashes(largestGroupId, groupFileIds, count, groupIds, buffer, isNamed)
118120

119121
val groups = hashMapOf<Int, Js5Group>()
@@ -138,10 +140,11 @@ internal data class ReferenceTable(
138140
count: Int,
139141
groupIds: IntArray,
140142
buffer: ByteBuffer,
143+
protocol: Int
141144
): IntArray {
142145
val groupFileIds = IntArray(largestGroupId)
143146
(0 until count).forEach {
144-
groupFileIds[groupIds[it]] = buffer.readUnsignedShort()
147+
groupFileIds[groupIds[it]] = if (protocol >= 7) buffer.readUnsignedSmartShort() else buffer.readUnsignedShort()
145148
}
146149
return groupFileIds
147150
}
@@ -211,15 +214,16 @@ internal data class ReferenceTable(
211214
validFileIds: IntArray,
212215
count: Int,
213216
groupIds: IntArray,
214-
buffer: ByteBuffer
217+
buffer: ByteBuffer,
218+
protocol: Int
215219
): Array<IntArray> {
216220
val fileIds = Array(largestGroupId) { IntArray(validFileIds[it]) }
217221
(0 until count).forEach {
218222
val groupId = groupIds[it]
219223
var currentFileId = 0
220224
(0 until validFileIds[groupId]).forEach { fileId ->
221-
buffer.readUnsignedShort()
222-
.let { id -> currentFileId += id; currentFileId }
225+
if (protocol >= 7) { buffer.readUnsignedSmartShort() } else { buffer.readUnsignedShort() }
226+
.let { i -> currentFileId += i; currentFileId }
223227
.also { fileIds[groupId][fileId] = currentFileId }
224228
}
225229
}

0 commit comments

Comments
 (0)