From 10b18f037ccf99042b97820dca4af9c544c27c13 Mon Sep 17 00:00:00 2001 From: displee Date: Mon, 27 Jun 2022 23:09:03 +0200 Subject: [PATCH 1/2] Merge ukeys functions. --- .../kotlin/com/displee/cache/CacheLibrary.kt | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index 0f0356b..1eb2a0d 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -265,31 +265,34 @@ 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.offset = 5 + 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(1) + .writeBytes(indexData.generateWhirlpool(5, indexData.size - 5)) + if (exponent != null && modulus != null) { + 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 } - 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() } @@ -376,7 +379,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 { From 103485bda76b606441f63808ba6d957111bb3e89 Mon Sep 17 00:00:00 2001 From: Displee Date: Sun, 12 Mar 2023 13:55:48 +0100 Subject: [PATCH 2/2] Fix ukeys function for high rev. Fix issue when creating new indices. --- README.md | 2 +- build.gradle | 2 +- .../kotlin/com/displee/cache/CacheLibrary.kt | 10 ++-------- .../com/displee/cache/index/ReferenceTable.kt | 17 +++++++++++++++-- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d54f331..51bdbc7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.gradle b/build.gradle index a8ad4d2..828f183 100644 --- a/build.gradle +++ b/build.gradle @@ -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." diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index 1eb2a0d..50471fe 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -265,11 +265,10 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa indices.remove(id) } - @JvmOverloads() + @JvmOverloads fun generateUkeys(writeWhirlpool: Boolean = true, exponent: BigInteger? = null, modulus: BigInteger? = null): ByteArray { val buffer = OutputBuffer(6 + indices.size * 72) if (writeWhirlpool) { - buffer.offset = 5 buffer.writeByte(indices.size) } val emptyWhirlpool = ByteArray(WHIRLPOOL_SIZE) @@ -282,16 +281,11 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa if (writeWhirlpool) { val indexData = buffer.array() val whirlpoolBuffer = OutputBuffer(WHIRLPOOL_SIZE + 1) - .writeByte(1) + .writeByte(0) .writeBytes(indexData.generateWhirlpool(5, indexData.size - 5)) if (exponent != null && modulus != null) { 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() } diff --git a/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt b/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt index 2e56f58..9522544 100644 --- a/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt +++ b/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt @@ -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 = TreeMap() + private var archiveNames = mutableListOf() var version = 0 @@ -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() } @@ -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() @@ -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) { @@ -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 }