Add legacy cache support to DiskStoreZipWriter

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 3 years ago
parent cad6e10eee
commit 007f7d68ef
  1. 14
      cache/src/main/kotlin/org/openrs2/cache/DiskStoreZipWriter.kt
  2. 17
      cache/src/test/kotlin/org/openrs2/cache/DiskStoreZipWriterTest.kt
  3. BIN
      cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache-legacy/cache.zip
  4. 0
      cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache/cache.zip

@ -30,10 +30,12 @@ public class DiskStoreZipWriter(
private val prefix: String = "cache/",
level: Int = Deflater.BEST_COMPRESSION,
timestamp: Instant = Instant.EPOCH,
private val alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT
private val alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,
legacy: Boolean = false
) : Store {
private data class IndexEntry(val len: Int, val block: Int)
private val archiveOffset = if (legacy) 1 else 0
private val timestamp = FileTime.from(timestamp)
private val indexes = arrayOfNulls<Int2ObjectSortedMap<IndexEntry>>(Store.MAX_ARCHIVE + 1)
private val zeroBlock = ByteArray(DiskStore.BLOCK_SIZE)
@ -45,7 +47,13 @@ public class DiskStoreZipWriter(
out.putNextEntry(createZipEntry(""))
out.putNextEntry(createZipEntry("main_file_cache.dat2"))
val dataFile = if (legacy) {
"main_file_cache.dat"
} else {
"main_file_cache.dat2"
}
out.putNextEntry(createZipEntry(dataFile))
out.write(zeroBlock)
}
@ -136,7 +144,7 @@ public class DiskStoreZipWriter(
tempBuf.writeMedium(0)
}
tempBuf.writeByte(archive)
tempBuf.writeByte(archive + archiveOffset)
tempBuf.readBytes(out, tempBuf.readableBytes())
// write data

@ -89,14 +89,13 @@ class DiskStoreZipWriterTest {
}
}
@Test
fun testWrite() {
private fun testWrite(expected: String, legacy: Boolean) {
Jimfs.newFileSystem(Configuration.forCurrentPlatform()).use { fs ->
val actual = fs.rootDirectories.first().resolve("zip")
Files.createDirectories(actual)
Files.newOutputStream(actual.resolve("cache.zip")).use { out ->
DiskStoreZipWriter(ZipOutputStream(out)).use { store ->
DiskStoreZipWriter(ZipOutputStream(out), legacy = legacy).use { store ->
store.create(0)
copiedBuffer("OpenRS2").use { buf ->
@ -113,10 +112,20 @@ class DiskStoreZipWriterTest {
}
}
assertTrue(ROOT.recursiveEquals(actual))
assertTrue(ROOT.resolve(expected).recursiveEquals(actual))
}
}
@Test
fun testWrite() {
testWrite("cache", legacy = false)
}
@Test
fun testWriteLegacy() {
testWrite("cache-legacy", legacy = true)
}
private companion object {
private val ROOT = Path.of(DiskStoreZipWriterTest::class.java.getResource("disk-store-zip").toURI())
}

Loading…
Cancel
Save