From 2c70a7c1ec95fab9cf2bf4e9003236f0321ac215 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 8 Oct 2022 17:47:28 +0100 Subject: [PATCH] Add methods to allow reading files by group name and file ID Signed-off-by: Graham --- .../main/kotlin/org/openrs2/cache/Archive.kt | 64 +++++++++++++++++++ .../main/kotlin/org/openrs2/cache/Cache.kt | 42 ++++++++++++ 2 files changed, 106 insertions(+) diff --git a/cache/src/main/kotlin/org/openrs2/cache/Archive.kt b/cache/src/main/kotlin/org/openrs2/cache/Archive.kt index 329701cbca..63cc078ca1 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Archive.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Archive.kt @@ -161,6 +161,17 @@ public abstract class Archive internal constructor( return existsNamed(group.krHashCode(), file.krHashCode()) } + public fun existsNamedGroup(groupNameHash: Int, file: Int): Boolean { + require(file >= 0) + + val entry = index.getNamed(groupNameHash) ?: return false + return entry.contains(file) + } + + public fun exists(group: String, file: Int): Boolean { + return existsNamedGroup(group.krHashCode(), file) + } + public fun list(): Iterator> { return index.iterator() } @@ -202,6 +213,20 @@ public abstract class Archive internal constructor( return readNamed(group.krHashCode(), file.krHashCode(), key) } + @JvmOverloads + public fun readNamedGroup(groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + require(file >= 0) + + val entry = index.getNamed(groupNameHash) ?: throw FileNotFoundException() + val unpacked = getUnpacked(entry, key) + return unpacked.read(file) + } + + @JvmOverloads + public fun read(group: String, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + return readNamedGroup(group.krHashCode(), file, key) + } + @JvmOverloads public fun write(group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { require(group >= 0 && file >= 0) @@ -228,6 +253,23 @@ public abstract class Archive internal constructor( return writeNamed(group.krHashCode(), file.krHashCode(), buf, key) } + @JvmOverloads + public fun writeNamedGroup(groupNameHash: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + require(file >= 0) + + val entry = index.createOrGetNamed(groupNameHash) + val unpacked = createOrGetUnpacked(entry, key, isOverwriting(entry, file)) + unpacked.write(file, buf) + + dirty = true + index.hasNames = true + } + + @JvmOverloads + public fun write(group: String, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + return writeNamedGroup(group.krHashCode(), file, buf, key) + } + public fun remove(group: Int) { require(group >= 0) @@ -287,6 +329,28 @@ public abstract class Archive internal constructor( return removeNamed(group.krHashCode(), file.krHashCode(), key) } + @JvmOverloads + public fun removeNamedGroup(groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO) { + require(file >= 0) + + val entry = index.getNamed(groupNameHash) ?: return + + if (isOverwriting(entry, file)) { + removeNamed(groupNameHash) + return + } + + val unpacked = getUnpacked(entry, key) + unpacked.remove(file) + + dirty = true + } + + @JvmOverloads + public fun remove(group: String, file: Int, key: XteaKey = XteaKey.ZERO) { + removeNamedGroup(group.krHashCode(), file, key) + } + public override fun flush() { if (!dirty) { return diff --git a/cache/src/main/kotlin/org/openrs2/cache/Cache.kt b/cache/src/main/kotlin/org/openrs2/cache/Cache.kt index 338a5fe5f5..d6c1aba2bf 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Cache.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Cache.kt @@ -106,6 +106,15 @@ public class Cache private constructor( return existsNamed(archive, group.krHashCode(), file.krHashCode()) } + public fun existsNamedGroup(archive: Int, groupNameHash: Int, file: Int): Boolean { + checkArchive(archive) + return archives[archive]?.existsNamedGroup(groupNameHash, file) ?: false + } + + public fun exists(archive: Int, group: String, file: Int): Boolean { + return existsNamedGroup(archive, group.krHashCode(), file) + } + public fun list(): Iterator { return archives.withIndex() .filter { it.value != null } @@ -149,6 +158,17 @@ public class Cache private constructor( return readNamed(archive, group.krHashCode(), file.krHashCode(), key) } + @JvmOverloads + public fun readNamedGroup(archive: Int, groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + checkArchive(archive) + return archives[archive]?.readNamedGroup(groupNameHash, file, key) ?: throw FileNotFoundException() + } + + @JvmOverloads + public fun read(archive: Int, group: String, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + return readNamedGroup(archive, group.krHashCode(), file, key) + } + @JvmOverloads public fun write(archive: Int, group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { checkArchive(archive) @@ -172,6 +192,17 @@ public class Cache private constructor( writeNamed(archive, group.krHashCode(), file.krHashCode(), buf, key) } + @JvmOverloads + public fun writeNamedGroup(archive: Int, groupNameHash: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + checkArchive(archive) + createOrGetArchive(archive).writeNamedGroup(groupNameHash, file, buf, key) + } + + @JvmOverloads + public fun write(archive: Int, group: String, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + writeNamedGroup(archive, group.krHashCode(), file, buf, key) + } + public fun remove(archive: Int) { checkArchive(archive) @@ -218,6 +249,17 @@ public class Cache private constructor( return removeNamed(archive, group.krHashCode(), file.krHashCode(), key) } + @JvmOverloads + public fun removeNamedGroup(archive: Int, groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO) { + checkArchive(archive) + archives[archive]?.removeNamedGroup(groupNameHash, file, key) + } + + @JvmOverloads + public fun remove(archive: Int, group: String, file: Int, key: XteaKey = XteaKey.ZERO) { + removeNamedGroup(archive, group.krHashCode(), file, key) + } + /** * Writes pending changes back to the underlying [Store]. */