@@ -5,6 +5,7 @@ import com.runetopic.cache.codec.decompress
55import com.runetopic.cache.exception.ProtocolException
66import com.runetopic.cache.extension.readUnsignedByte
77import com.runetopic.cache.extension.readUnsignedShort
8+ import com.runetopic.cache.extension.readUnsignedSmartShort
89import com.runetopic.cache.hierarchy.index.Js5Index
910import com.runetopic.cache.hierarchy.index.group.Js5Group
1011import 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