From f1433ec97fac64648c5257cf9966638a96db530c Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 24 Dec 2020 22:03:42 +0000 Subject: [PATCH] Add method for creating a Js5MasterIndex Signed-off-by: Graham --- .../org/openrs2/cache/Js5MasterIndex.kt | 30 ++++++++++++++++++ .../org/openrs2/cache/Js5MasterIndexTest.kt | 26 +++++++++++++++ .../org/openrs2/cache/master-index/255/0.dat | Bin 0 -> 4 bytes .../org/openrs2/cache/master-index/255/1.dat | Bin 0 -> 8 bytes .../org/openrs2/cache/master-index/255/3.dat | Bin 0 -> 8 bytes .../org/openrs2/cache/master-index/255/6.dat | Bin 0 -> 8 bytes 6 files changed, 56 insertions(+) create mode 100644 cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt create mode 100644 cache/src/test/resources/org/openrs2/cache/master-index/255/0.dat create mode 100644 cache/src/test/resources/org/openrs2/cache/master-index/255/1.dat create mode 100644 cache/src/test/resources/org/openrs2/cache/master-index/255/3.dat create mode 100644 cache/src/test/resources/org/openrs2/cache/master-index/255/6.dat diff --git a/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt b/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt index 3ffcc75cb2..8107dc94b3 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt @@ -1,6 +1,8 @@ package org.openrs2.cache import io.netty.buffer.ByteBuf +import org.openrs2.buffer.crc32 +import org.openrs2.buffer.use public inline class Js5MasterIndex(public val entries: MutableList = mutableListOf()) { public data class Entry(public var version: Int, public var checksum: Int) @@ -13,6 +15,34 @@ public inline class Js5MasterIndex(public val entries: MutableList = muta } public companion object { + public fun create(store: Store): Js5MasterIndex { + val index = Js5MasterIndex() + + var nextArchive = 0 + for (archive in store.list(Js5Archive.ARCHIVESET)) { + /* + * Fill in gaps with zeroes. I think this is consistent with + * the official implementation: the TFU client warns that + * entries with a zero CRC are probably invalid. + */ + for (i in nextArchive until archive) { + index.entries += Entry(0, 0) + } + + val entry = store.read(Js5Archive.ARCHIVESET, archive).use { buf -> + val checksum = buf.crc32() + val version = Js5Index.read(buf).version + // TODO(gpe): should we throw an exception if there are trailing bytes here? + Entry(version, checksum) + } + + index.entries += entry + nextArchive = archive + 1 + } + + return index + } + public fun read(buf: ByteBuf): Js5MasterIndex { require(buf.readableBytes() % 8 == 0) diff --git a/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt b/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt new file mode 100644 index 0000000000..49244d24e6 --- /dev/null +++ b/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt @@ -0,0 +1,26 @@ +package org.openrs2.cache + +import java.nio.file.Paths +import kotlin.test.Test +import kotlin.test.assertEquals + +object Js5MasterIndexTest { + private val ROOT = Paths.get(FlatFileStoreTest::class.java.getResource("master-index").toURI()) + + @Test + fun testCreate() { + val index = Store.open(ROOT).use { store -> + Js5MasterIndex.create(store) + } + + assertEquals(Js5MasterIndex(mutableListOf( + Js5MasterIndex.Entry(0, 379203374), + Js5MasterIndex.Entry(0x12345678, -717247318), + Js5MasterIndex.Entry(0, 0), + Js5MasterIndex.Entry(0x9ABCDEF0.toInt(), 895417101), + Js5MasterIndex.Entry(0, 0), + Js5MasterIndex.Entry(0, 0), + Js5MasterIndex.Entry(0xAA55AA55.toInt(), -627983571) + )), index) + } +} diff --git a/cache/src/test/resources/org/openrs2/cache/master-index/255/0.dat b/cache/src/test/resources/org/openrs2/cache/master-index/255/0.dat new file mode 100644 index 0000000000000000000000000000000000000000..a786e127004dd9e94e88fda7742d248237ad8885 GIT binary patch literal 4 LcmZQ&U|;|M02lxU literal 0 HcmV?d00001 diff --git a/cache/src/test/resources/org/openrs2/cache/master-index/255/1.dat b/cache/src/test/resources/org/openrs2/cache/master-index/255/1.dat new file mode 100644 index 0000000000000000000000000000000000000000..a8c24c5d92e730bbd43118188954e843531d41b4 GIT binary patch literal 8 PcmZP*G6}0-U|;|M1$+S; literal 0 HcmV?d00001 diff --git a/cache/src/test/resources/org/openrs2/cache/master-index/255/3.dat b/cache/src/test/resources/org/openrs2/cache/master-index/255/3.dat new file mode 100644 index 0000000000000000000000000000000000000000..540f477942d9a57337d5da5f60844387684640f3 GIT binary patch literal 8 PcmZRZwddXk1_lNI5bOgh literal 0 HcmV?d00001 diff --git a/cache/src/test/resources/org/openrs2/cache/master-index/255/6.dat b/cache/src/test/resources/org/openrs2/cache/master-index/255/6.dat new file mode 100644 index 0000000000000000000000000000000000000000..35a00753a1fa705ca653ddffd10690c3ca29996b GIT binary patch literal 8 PcmZQu6}l>vfq?-43xWa# literal 0 HcmV?d00001