Skip to content

Commit

Permalink
feat: add a write function which takes name + id for file
Browse files Browse the repository at this point in the history
  • Loading branch information
Z-Kris committed Jun 3, 2024
1 parent c50b131 commit 32177b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {

allprojects {
group = "org.openrs2"
version = "0.1.0-SNAPSHOT"
version = "0.1.2-SNAPSHOT"

plugins.withType<BasePlugin> {
configure<BasePluginExtension> {
Expand Down
18 changes: 18 additions & 0 deletions cache/src/main/kotlin/org/openrs2/cache/Archive.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public abstract class Archive internal constructor(
dirty = true
}

fun writeNamed(fileNameHash: Int, fileId: Int, buf: ByteBuf) {
ensureWritable()

val fileEntry = entry.createOrGetNamed(fileNameHash, fileId)
files.put(fileEntry.id, buf.copy().asReadOnly())?.release()
dirty = true
}

fun remove(file: Int) {
ensureWritable()

Expand Down Expand Up @@ -248,6 +256,16 @@ public abstract class Archive internal constructor(
index.hasNames = true
}

@JvmOverloads
public fun writeNamed(groupNameHash: Int, fileNameHash: Int, fileId: Int, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) {
val entry = index.createOrGetNamed(groupNameHash)
val unpacked = createOrGetUnpacked(entry, key, isOverwritingNamed(entry, fileNameHash))
unpacked.writeNamed(fileNameHash, fileId, buf)

dirty = true
index.hasNames = true
}

@JvmOverloads
public fun write(group: String, file: String, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) {
return writeNamed(group.krHashCode(), file.krHashCode(), buf, key)
Expand Down
25 changes: 25 additions & 0 deletions cache/src/main/kotlin/org/openrs2/cache/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,31 @@ public class Cache private constructor(
createOrGetArchive(archive).write(group, file, buf, key)
}

@JvmOverloads
public fun write(
archive: Int,
group: String,
file: String,
fileId: Int,
buf: ByteBuf,
key: SymmetricKey
) {
writeNamed(archive, group.krHashCode(), file.krHashCode(), fileId, buf, key)
}

@JvmOverloads
public fun writeNamed(
archive: Int,
groupNameHash: Int,
fileNameHash: Int,
fileId: Int,
buf: ByteBuf,
key: SymmetricKey
) {
checkArchive(archive)
createOrGetArchive(archive).writeNamed(groupNameHash, fileNameHash, fileId, buf, key)
}

@JvmOverloads
public fun writeNamed(
archive: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ public abstract class MutableNamedEntryCollection<T : MutableNamedEntry>(
return entry
}

public fun createOrGetNamed(nameHash: Int, fileId: Int): T {
var entry = getNamed(nameHash)
if (entry != null) {
return entry
}

entry = createOrGet(fileId)
entry.nameHash = nameHash
return entry
}

public fun createOrGet(name: String): T {
return createOrGetNamed(name.krHashCode())
}
Expand Down

0 comments on commit 32177b9

Please sign in to comment.