Skip to content

Commit

Permalink
Merge pull request #50 from Displee/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Displee authored Mar 12, 2023
2 parents 1314c1b + 5d82d09 commit a0fb4ff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This library is able to read this data, and write manipulated data back to the c

## Gradle
```
implementation("com.displee:rs-cache-library:6.8")
implementation 'com.displee:rs-cache-library:6.9'
```
## Initialize your cache
```kotlin
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {
}

group = 'com.displee'
version = '6.8'
version = '6.9'

description = "A library written in Kotlin used to read and write to all cache formats of RuneScape."

Expand Down
41 changes: 19 additions & 22 deletions src/main/kotlin/com/displee/cache/CacheLibrary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -265,31 +265,28 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa
indices.remove(id)
}

fun generateOldUkeys(): ByteArray {
val buffer = OutputBuffer(indices.size * 8)
for (index in indices()) {
buffer.writeInt(index.crc)
buffer.writeInt(index.revision)
}
return buffer.array()
}

fun generateNewUkeys(exponent: BigInteger, modulus: BigInteger): ByteArray {
@JvmOverloads
fun generateUkeys(writeWhirlpool: Boolean = true, exponent: BigInteger? = null, modulus: BigInteger? = null): ByteArray {
val buffer = OutputBuffer(6 + indices.size * 72)
buffer.offset = 5
buffer.writeByte(indices.size)
if (writeWhirlpool) {
buffer.writeByte(indices.size)
}
val emptyWhirlpool = ByteArray(WHIRLPOOL_SIZE)
for (index in indices()) {
buffer.writeInt(index.crc).writeInt(index.revision).writeBytes(index.whirlpool ?: emptyWhirlpool)
buffer.writeInt(index.crc).writeInt(index.revision)
if (writeWhirlpool) {
buffer.writeBytes(index.whirlpool ?: emptyWhirlpool)
}
}
if (writeWhirlpool) {
val indexData = buffer.array()
val whirlpoolBuffer = OutputBuffer(WHIRLPOOL_SIZE + 1)
.writeByte(0)
.writeBytes(indexData.generateWhirlpool(5, indexData.size - 5))
if (exponent != null && modulus != null) {
buffer.writeBytes(Buffer.cryptRSA(whirlpoolBuffer.array(), exponent, modulus))
}
}
val indexArray = buffer.array()
val whirlpoolBuffer = OutputBuffer(WHIRLPOOL_SIZE + 1).writeByte(1).writeBytes(indexArray.generateWhirlpool(5, indexArray.size - 5))
buffer.writeBytes(Buffer.cryptRSA(whirlpoolBuffer.array(), exponent, modulus))
val end = buffer.offset
buffer.offset = 0
buffer.writeByte(0)
buffer.writeInt(end - 5)
buffer.offset = end
return buffer.array()
}

Expand Down Expand Up @@ -376,7 +373,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa

companion object {
const val CACHE_FILE_NAME = "main_file_cache"

@JvmStatic
@JvmOverloads
fun create(path: String, clearDataAfterUpdate: Boolean = false, listener: ProgressListener? = null): CacheLibrary {
Expand Down
17 changes: 15 additions & 2 deletions src/main/kotlin/com/displee/cache/index/ReferenceTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com
private var mask = 0x0
private var needUpdate = false
protected var archives: SortedMap<Int, Archive> = TreeMap()
private var archiveNames = mutableListOf<Int>()

var version = 0

Expand Down Expand Up @@ -53,8 +54,14 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com
archiveIds[i] = archiveId.also { archives[it] = Archive(it) }
}
val archives = archives()
archiveNames = ArrayList(archives.size)
if (named) {
archives.forEach { it.hashName = buffer.readInt() }
archives.forEach {
it.hashName = buffer.readInt()
if (it.hashName != 0) {
archiveNames.add(it.hashName)
}
}
}
if (origin.isRS3()) {
archives.forEach { it.crc = buffer.readInt() }
Expand Down Expand Up @@ -230,6 +237,9 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com
existing = Archive(id, if (hashName == -1) 0 else hashName, xtea)
existing.compressionType = CompressionType.GZIP
}
if (hashName != -1) {
archiveNames.add(existing.hashName)
}
archives[id] = existing
existing.new = true
existing.flag()
Expand All @@ -243,7 +253,9 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com
flag = true
}
if (hashName != -1 && existing.hashName != hashName) {
archiveNames.remove(existing.hashName)
existing.hashName = hashName
archiveNames.add(existing.hashName)
flag = true
}
if (flag) {
Expand Down Expand Up @@ -314,11 +326,12 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com
}

fun contains(name: String): Boolean {
return archiveId(name) != -1
return archiveNames.contains(toHash(name))
}

fun remove(id: Int): Archive? {
val archive = archives.remove(id) ?: return null
archiveNames.remove(archive.hashName)
flag()
return archive
}
Expand Down

0 comments on commit a0fb4ff

Please sign in to comment.