From 407aff8dd8c5dff7ef227e6261cc4e000f8575c8 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 21 Nov 2021 13:02:44 +0000 Subject: [PATCH] Add support for .dat (instead of .dat2) file to DiskStore Signed-off-by: Graham --- .../kotlin/org/openrs2/cache/DiskStore.kt | 34 ++++++++++++++---- .../main/kotlin/org/openrs2/cache/Store.kt | 4 ++- .../kotlin/org/openrs2/cache/DiskStoreTest.kt | 24 +++++++++++-- .../kotlin/org/openrs2/cache/StoreTest.kt | 6 ++++ .../single-block-legacy/main_file_cache.dat | Bin 0 -> 535 bytes .../main_file_cache.idx255 | Bin 0 -> 12 bytes 6 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.dat create mode 100644 cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.idx255 diff --git a/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt b/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt index f5963b5c84..3c43f70963 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt @@ -15,7 +15,8 @@ import kotlin.math.min /** * A [Store] implementation compatible with the native `main_file_cache.dat2` - * and `main_file_cache.idx*` format used by the client. + * (or `main_file_cache.dat`) and `main_file_cache.idx*` format used by the + * client. * * It supports opening existing caches with a `main_file_cache.dat2m` file for * compatibility purposes. It does not support creating new caches with a @@ -484,6 +485,10 @@ public class DiskStore private constructor( return root.resolve("main_file_cache.dat2") } + internal fun legacyDataPath(root: Path): Path { + return root.resolve("main_file_cache.dat") + } + private fun musicDataPath(root: Path): Path { return root.resolve("main_file_cache.dat2m") } @@ -493,15 +498,23 @@ public class DiskStore private constructor( } public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { + val js5DataPath = dataPath(root) + val legacyDataPath = legacyDataPath(root) + + val dataPath = if (Files.exists(js5DataPath)) { + js5DataPath + } else { + legacyDataPath + } val data = BufferedFileChannel( - FileChannel.open(dataPath(root), READ, WRITE), + FileChannel.open(dataPath, READ, WRITE), DATA_BUFFER_SIZE, DATA_BUFFER_SIZE, alloc ) - val path = musicDataPath(root) - val musicData = if (Files.exists(path)) { + val musicDataPath = musicDataPath(root) + val musicData = if (Files.exists(musicDataPath)) { BufferedFileChannel( FileChannel.open(musicDataPath(root), READ, WRITE), DATA_BUFFER_SIZE, @@ -529,11 +542,20 @@ public class DiskStore private constructor( return DiskStore(root, data, musicData, archives, alloc) } - public fun create(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { + public fun create( + root: Path, + alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT, + legacyDataPath: Boolean = false + ): Store { Files.createDirectories(root) + val dataPath = if (legacyDataPath) { + legacyDataPath(root) + } else { + dataPath(root) + } val data = BufferedFileChannel( - FileChannel.open(dataPath(root), CREATE, READ, WRITE), + FileChannel.open(dataPath, CREATE, READ, WRITE), DATA_BUFFER_SIZE, DATA_BUFFER_SIZE, alloc diff --git a/cache/src/main/kotlin/org/openrs2/cache/Store.kt b/cache/src/main/kotlin/org/openrs2/cache/Store.kt index 9724eeb6f1..29256c2d36 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Store.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Store.kt @@ -136,7 +136,9 @@ public interface Store : Flushable, Closeable { * @throws IOException if an underlying I/O error occurs. */ public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { - return if (Files.isRegularFile(DiskStore.dataPath(root))) { + val hasDataFile = Files.isRegularFile(DiskStore.dataPath(root)) + val hasLegacyDataFile = Files.isRegularFile(DiskStore.legacyDataPath(root)) + return if (hasDataFile || hasLegacyDataFile) { DiskStore.open(root, alloc) } else { FlatFileStore.open(root, alloc) diff --git a/cache/src/test/kotlin/org/openrs2/cache/DiskStoreTest.kt b/cache/src/test/kotlin/org/openrs2/cache/DiskStoreTest.kt index a3e15c1462..208d0dccf8 100644 --- a/cache/src/test/kotlin/org/openrs2/cache/DiskStoreTest.kt +++ b/cache/src/test/kotlin/org/openrs2/cache/DiskStoreTest.kt @@ -760,16 +760,36 @@ class DiskStoreTest { } } + @Test + fun testCreateLegacyDataFile() { + writeTest("single-block-legacy", legacyDataPath = true) { store -> + copiedBuffer("OpenRS2").use { buf -> + store.write(255, 1, buf) + } + } + } + + @Test + fun testOpenLegacyDataFile() { + readTest("single-block-legacy") { store -> + copiedBuffer("OpenRS2").use { expected -> + store.read(255, 1).use { actual -> + assertEquals(expected, actual) + } + } + } + } + private fun readTest(name: String, f: (Store) -> Unit) { DiskStore.open(ROOT.resolve(name)).use { store -> f(store) } } - private fun writeTest(name: String, f: (Store) -> Unit) { + private fun writeTest(name: String, legacyDataPath: Boolean = false, f: (Store) -> Unit) { Jimfs.newFileSystem(Configuration.forCurrentPlatform()).use { fs -> val actual = fs.rootDirectories.first().resolve("cache") - DiskStore.create(actual).use { store -> + DiskStore.create(actual, legacyDataPath = legacyDataPath).use { store -> f(store) } diff --git a/cache/src/test/kotlin/org/openrs2/cache/StoreTest.kt b/cache/src/test/kotlin/org/openrs2/cache/StoreTest.kt index 118133a85b..cb12965713 100644 --- a/cache/src/test/kotlin/org/openrs2/cache/StoreTest.kt +++ b/cache/src/test/kotlin/org/openrs2/cache/StoreTest.kt @@ -11,6 +11,10 @@ class StoreTest { assertTrue(store is DiskStore) } + Store.open(LEGACY_DISK_ROOT).use { store -> + assertTrue(store is DiskStore) + } + Store.open(FLAT_FILE_ROOT).use { store -> assertTrue(store is FlatFileStore) } @@ -18,6 +22,8 @@ class StoreTest { private companion object { private val DISK_ROOT = Path.of(StoreTest::class.java.getResource("disk-store/empty").toURI()) + private val LEGACY_DISK_ROOT = + Path.of(StoreTest::class.java.getResource("disk-store/single-block-legacy").toURI()) private val FLAT_FILE_ROOT = Path.of(StoreTest::class.java.getResource("flat-file-store/empty").toURI()) } } diff --git a/cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.dat b/cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.dat new file mode 100644 index 0000000000000000000000000000000000000000..98a426d1d82802721dc2e6fb46fc7600b9db6a2e GIT binary patch literal 535 bcmZQz7zLvt01N>}a47uuFG$S`3N``&6lDWy literal 0 HcmV?d00001 diff --git a/cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.idx255 b/cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.idx255 new file mode 100644 index 0000000000000000000000000000000000000000..e52747c6a6f3accf3b67701d9c28e8bf202e9689 GIT binary patch literal 12 OcmZQzfB<#|21Wn?DF6uo literal 0 HcmV?d00001