Add support for .dat (instead of .dat2) file to DiskStore

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 3 years ago
parent f38253f2fc
commit 407aff8dd8
  1. 34
      cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt
  2. 4
      cache/src/main/kotlin/org/openrs2/cache/Store.kt
  3. 24
      cache/src/test/kotlin/org/openrs2/cache/DiskStoreTest.kt
  4. 6
      cache/src/test/kotlin/org/openrs2/cache/StoreTest.kt
  5. BIN
      cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.dat
  6. BIN
      cache/src/test/resources/org/openrs2/cache/disk-store/single-block-legacy/main_file_cache.idx255

@ -15,7 +15,8 @@ import kotlin.math.min
/** /**
* A [Store] implementation compatible with the native `main_file_cache.dat2` * 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 * It supports opening existing caches with a `main_file_cache.dat2m` file for
* compatibility purposes. It does not support creating new caches with a * 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") 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 { private fun musicDataPath(root: Path): Path {
return root.resolve("main_file_cache.dat2m") 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 { 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( val data = BufferedFileChannel(
FileChannel.open(dataPath(root), READ, WRITE), FileChannel.open(dataPath, READ, WRITE),
DATA_BUFFER_SIZE, DATA_BUFFER_SIZE,
DATA_BUFFER_SIZE, DATA_BUFFER_SIZE,
alloc alloc
) )
val path = musicDataPath(root) val musicDataPath = musicDataPath(root)
val musicData = if (Files.exists(path)) { val musicData = if (Files.exists(musicDataPath)) {
BufferedFileChannel( BufferedFileChannel(
FileChannel.open(musicDataPath(root), READ, WRITE), FileChannel.open(musicDataPath(root), READ, WRITE),
DATA_BUFFER_SIZE, DATA_BUFFER_SIZE,
@ -529,11 +542,20 @@ public class DiskStore private constructor(
return DiskStore(root, data, musicData, archives, alloc) 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) Files.createDirectories(root)
val dataPath = if (legacyDataPath) {
legacyDataPath(root)
} else {
dataPath(root)
}
val data = BufferedFileChannel( val data = BufferedFileChannel(
FileChannel.open(dataPath(root), CREATE, READ, WRITE), FileChannel.open(dataPath, CREATE, READ, WRITE),
DATA_BUFFER_SIZE, DATA_BUFFER_SIZE,
DATA_BUFFER_SIZE, DATA_BUFFER_SIZE,
alloc alloc

@ -136,7 +136,9 @@ public interface Store : Flushable, Closeable {
* @throws IOException if an underlying I/O error occurs. * @throws IOException if an underlying I/O error occurs.
*/ */
public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { 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) DiskStore.open(root, alloc)
} else { } else {
FlatFileStore.open(root, alloc) FlatFileStore.open(root, alloc)

@ -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) { private fun readTest(name: String, f: (Store) -> Unit) {
DiskStore.open(ROOT.resolve(name)).use { store -> DiskStore.open(ROOT.resolve(name)).use { store ->
f(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 -> Jimfs.newFileSystem(Configuration.forCurrentPlatform()).use { fs ->
val actual = fs.rootDirectories.first().resolve("cache") val actual = fs.rootDirectories.first().resolve("cache")
DiskStore.create(actual).use { store -> DiskStore.create(actual, legacyDataPath = legacyDataPath).use { store ->
f(store) f(store)
} }

@ -11,6 +11,10 @@ class StoreTest {
assertTrue(store is DiskStore) assertTrue(store is DiskStore)
} }
Store.open(LEGACY_DISK_ROOT).use { store ->
assertTrue(store is DiskStore)
}
Store.open(FLAT_FILE_ROOT).use { store -> Store.open(FLAT_FILE_ROOT).use { store ->
assertTrue(store is FlatFileStore) assertTrue(store is FlatFileStore)
} }
@ -18,6 +22,8 @@ class StoreTest {
private companion object { private companion object {
private val DISK_ROOT = Path.of(StoreTest::class.java.getResource("disk-store/empty").toURI()) 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()) private val FLAT_FILE_ROOT = Path.of(StoreTest::class.java.getResource("flat-file-store/empty").toURI())
} }
} }

Loading…
Cancel
Save