Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A cache library written in Kotlin.
- Java Version 16

# Supported
- RS2 (414-659)
- RS2 (414-742)
- OSRS (1-current)

# Features
Expand All @@ -21,15 +21,15 @@ 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

# 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" }
```

Expand Down
2 changes: 1 addition & 1 deletion cache/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
signing
}

version = "1.4.10-SNAPSHOT"
version = "1.4.11-SNAPSHOT"

java {
withJavadocJar()
Expand Down
20 changes: 12 additions & 8 deletions cache/src/main/kotlin/com/runetopic/cache/extension/ByteBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ByteArray>()
(0 until count).forEach {
groupTables.add(datFile.readReferenceTable(idxFile.id(), idxFile.loadReferenceTable(it)))
Expand Down Expand Up @@ -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]
}

Expand All @@ -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<Int, Js5Group>()
Expand All @@ -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
}
Expand Down Expand Up @@ -211,15 +214,16 @@ internal data class ReferenceTable(
validFileIds: IntArray,
count: Int,
groupIds: IntArray,
buffer: ByteBuffer
buffer: ByteBuffer,
protocol: Int
): Array<IntArray> {
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 }
}
}
Expand Down