From 007f7d68ef705ed966363ca4f544e3038de66563 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 11 Dec 2021 14:02:10 +0000 Subject: [PATCH] Add legacy cache support to DiskStoreZipWriter Signed-off-by: Graham --- .../org/openrs2/cache/DiskStoreZipWriter.kt | 14 +++++++++++--- .../openrs2/cache/DiskStoreZipWriterTest.kt | 17 +++++++++++++---- .../disk-store-zip/cache-legacy/cache.zip | Bin 0 -> 1138 bytes .../cache/disk-store-zip/{ => cache}/cache.zip | Bin 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache-legacy/cache.zip rename cache/src/test/resources/org/openrs2/cache/disk-store-zip/{ => cache}/cache.zip (100%) diff --git a/cache/src/main/kotlin/org/openrs2/cache/DiskStoreZipWriter.kt b/cache/src/main/kotlin/org/openrs2/cache/DiskStoreZipWriter.kt index 47cb5961..30cb4b8f 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/DiskStoreZipWriter.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/DiskStoreZipWriter.kt @@ -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>(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 diff --git a/cache/src/test/kotlin/org/openrs2/cache/DiskStoreZipWriterTest.kt b/cache/src/test/kotlin/org/openrs2/cache/DiskStoreZipWriterTest.kt index a970b83e..55f44e15 100644 --- a/cache/src/test/kotlin/org/openrs2/cache/DiskStoreZipWriterTest.kt +++ b/cache/src/test/kotlin/org/openrs2/cache/DiskStoreZipWriterTest.kt @@ -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()) } diff --git a/cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache-legacy/cache.zip b/cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache-legacy/cache.zip new file mode 100644 index 0000000000000000000000000000000000000000..9a5472762e414d5d8f340ced3c3b94cb7780a56d GIT binary patch literal 1138 zcmWIWW@Zs#;Nak3U|>)LVgz7g5M)SBOwLHv4-MgEU`I$WFfcO&c(a3aFfc)d18^E6 z2{$G;F*7edEi)%I9&D^$N@59y$;k;4tcw}=X8h$2`rOpi>*k!)()9oT!IKQr_}o?s zdNE#RRMt@BN%ik}mZrtv#&OgtN~tGAjgusur zx6giO2Bq?1#={e51LJ}X2m`zsnM4>+vkNQ-urY9=xf7OoSRt7QT{Chzl|<3(4m1d& z8EcloVIIhKm}8_+%)5uvJV;u{VIZ>4Q4O>Nh6i>B0u2PFaJYf2Y#>*$0HHomi!L*W F2LL(QgRcMp literal 0 HcmV?d00001 diff --git a/cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache.zip b/cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache/cache.zip similarity index 100% rename from cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache.zip rename to cache/src/test/resources/org/openrs2/cache/disk-store-zip/cache/cache.zip