Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8787b83
Merge pull request #16 from runetopic/development
ultraviolet-jordan Sep 30, 2021
50c7335
Merge pull request #17 from runetopic/development
ultraviolet-jordan Oct 16, 2021
629635f
Merge pull request #18 from runetopic/development
ultraviolet-jordan Oct 19, 2021
8fec609
Merge pull request #19 from runetopic/development
ultraviolet-jordan Oct 23, 2021
47f99c8
Add start to cache writing and push branch.
ultraviolet-jordan Oct 24, 2021
747c68e
Merge pull request #20 from runetopic/development
ultraviolet-jordan Oct 24, 2021
7fa3b4b
Merge branch 'development' of github.com:xlite2/cache-lib into featur…
ultraviolet-jordan Oct 26, 2021
5a832f0
Refactor some of the code for decoding/encoding the data to build an …
ultraviolet-jordan Oct 26, 2021
02bb1c7
Merge pull request #22 from runetopic/development
ultraviolet-jordan Oct 26, 2021
d8f26e5
Add more testing for DatIndexSector.
ultraviolet-jordan Oct 26, 2021
1c12077
Add more tests to DatIndexSector and more work to the encoding.
ultraviolet-jordan Oct 31, 2021
7eca120
Update to Kotlin 1.5.31 and update README.md.
ultraviolet-jordan Oct 31, 2021
9893df4
Removing dat file
ultraviolet-jordan Nov 2, 2021
a81c36a
Merging
ultraviolet-jordan Nov 2, 2021
6623f6e
Update README.md
ultraviolet-jordan Jan 1, 2022
ae5ae4c
Fixing java version
tyler27 Jan 2, 2022
6cf4283
Merge branch 'main' of github.com:runetopic/cache-lib into feature/wr…
ultraviolet-jordan Jan 4, 2022
ccfd73a
Format.
ultraviolet-jordan Jan 4, 2022
e49e5b5
Cache and Loader code refactor.
ultraviolet-jordan Jan 4, 2022
d87da6f
Update README.md
tyler27 Jan 4, 2022
ea752f5
Update README.md
tyler27 Jan 4, 2022
14dbbce
Pulling README.md changes from master
tyler27 Jan 5, 2022
8731b24
Removing IInterface pattern
tyler27 Jan 5, 2022
9cd8c56
Fixing failing test
tyler27 Jan 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License
Copyright (c) 2021 Xlite
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License

Copyright (c) 2021 Xlite

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
A cache library written in Kotlin.

# Requirements
- Kotlin Version 1.5
- Java Version 16

# Supported
Expand Down
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ configure(allprojects) {

tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.jvmTarget = "16"
kotlinOptions.freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.jvmTarget = "16"
kotlinOptions.freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
src/test/resources/main_file_cache.dat2
src/test/resources/main_file_cache.idx10
5 changes: 4 additions & 1 deletion cache/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
signing
}

version = "1.4.19-SNAPSHOT"
version = "1.4.20-SNAPSHOT"

java {
withJavadocJar()
Expand Down Expand Up @@ -78,4 +78,7 @@ dependencies {
implementation("org.slf4j:slf4j-simple:1.7.32")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.+")
implementation("com.runetopic.cryptography:cryptography:1.0.6-SNAPSHOT")

testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("io.mockk:mockk:1.12.0")
}
44 changes: 41 additions & 3 deletions cache/src/main/kotlin/com/runetopic/cache/codec/ContainerCodec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.runetopic.cache.extension.readUnsignedShort
import com.runetopic.cache.extension.remainingBytes
import com.runetopic.cache.extension.toByteBuffer
import com.runetopic.cryptography.fromXTEA
import java.lang.Exception
import java.nio.ByteBuffer
import java.util.zip.CRC32

/**
Expand All @@ -17,7 +19,34 @@ import java.util.zip.CRC32
*/
internal object ContainerCodec {

fun decompress(data: ByteArray, keys: IntArray = intArrayOf()): Container {
fun compress(
compression: Int,
revision: Int,
data: ByteArray,
keys: IntArray = intArrayOf()
): ByteBuffer {
val codec = compressionCodec(compression)
val compressed = if (codec != NoCodec) codec.codec.compress(data, keys) else data
val buffer = ByteBuffer.allocate((if (codec != NoCodec) 9 else 5) + compressed.size/* + if (revision == -1) 0 else 2*/)
buffer.put(compressionType(codec).toByte())
buffer.putInt(compressed.size)
if (codec != NoCodec) {
buffer.putInt(data.size)
}
buffer.put(compressed)
if (keys.isNotEmpty()) {
//TODO xtea
}
if (revision != -1) {
//buffer.putShort(revision.toShort())
}
return buffer
}

fun decompress(
data: ByteArray,
keys: IntArray = intArrayOf()
): Container {
val buffer = data.toByteBuffer()
val compression = buffer.readUnsignedByte()
val length = buffer.int
Expand All @@ -29,7 +58,7 @@ internal object ContainerCodec {
val crc32 = CRC32()
crc32.update(data, 0, 5)

return when (val type = compressionType(compression)) {
return when (val type = compressionCodec(compression)) {
BadCodec -> throw CompressionException("Compression type not found with a compression opcode of $compression.")
is NoCodec -> {
val encrypted = ByteArray(length)
Expand Down Expand Up @@ -66,7 +95,16 @@ internal object ContainerCodec {
}
}

private fun compressionType(compression: Int): CodecType {
private fun compressionType(codecType: CodecType): Int {
return when (codecType) {
is NoCodec -> 0
is BZipCodec -> 1
is GZipCodec -> 2
else -> throw Exception("Bad compression type.")
}
}

private fun compressionCodec(compression: Int): CodecType {
return when (compression) {
0 -> NoCodec
1 -> BZipCodec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ package com.runetopic.cache.codec
* @email <xlitersps@gmail.com>
*/
internal interface IFileCodec {
fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray
fun compress(data: ByteArray, keys: IntArray): ByteArray
fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.*
* @email <xlitersps@gmail.com>
*/
internal class BZip2Codec: IFileCodec {
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
override fun compress(data: ByteArray, keys: IntArray): ByteArray {
val stream: InputStream = ByteArrayInputStream(data)
val bout = ByteArrayOutputStream()
BZip2CompressorOutputStream(bout, 1).use { os -> IOUtils.copy(stream, os) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.zip.GZIPOutputStream
* @email <xlitersps@gmail.com>
*/
internal class GZipCodec: IFileCodec {
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
override fun compress(data: ByteArray, keys: IntArray): ByteArray {
val inputStream = ByteArrayInputStream(data)
val outputStream = ByteArrayOutputStream()
GZIPOutputStream(outputStream).use { os -> IOUtils.copy(inputStream, os) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import com.runetopic.cache.codec.IFileCodec
* @email <xlitersps@gmail.com>
*/
internal class NoFileCodec: IFileCodec {
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray = throw NotImplementedError("No codec provided.")
override fun compress(data: ByteArray, keys: IntArray): ByteArray = throw NotImplementedError("No codec provided.")
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray = throw NotImplementedError("No codec provided.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ internal fun ByteBuffer.readUnsignedByte(): Int = get().toInt() and 0xFF
internal fun ByteBuffer.readUnsignedShort(): Int = short.toInt() and 0xFFFF
internal fun ByteBuffer.readUnsignedMedium(): Int = ((readUnsignedByte() shl 16) or (readUnsignedByte() shl 8) or readUnsignedByte())
internal fun ByteBuffer.readUnsignedIntShortSmart(): Int = if (get(position()).toInt() < 0) int and Int.MAX_VALUE else readUnsignedShort()
internal fun ByteBuffer.putIntShortSmart(value: Int) {
val buffer = when {
value >= Short.MAX_VALUE -> ByteBuffer.allocate(Int.SIZE_BYTES).putInt(value - Int.MAX_VALUE - 1)
else -> ByteBuffer.allocate(Short.SIZE_BYTES).putShort(if (value >= 0) value.toShort() else Short.MAX_VALUE)
}
get(buffer.array())
}

internal fun ByteBuffer.remainingBytes(): ByteArray {
val bytes = ByteArray(remaining())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ data class Index(
val protocol: Int,
val revision: Int,
val isNamed: Boolean,
val isUsingWhirlpool: Boolean,
private val groups: Map<Int, Group>
): Comparable<Index> {

@JvmName("getGroups")
fun groups(): Collection<Group> = groups.values

@JvmName("getGroupIds")
fun groupIds(): Collection<Int> = groups.keys

@JvmName("getGroup")
fun group(groupId: Int): Group = groups[groupId] ?: Group.DEFAULT

Expand Down Expand Up @@ -64,6 +68,6 @@ data class Index(
}

internal companion object {
fun default(indexId: Int): Index = Index(indexId, 0, ByteArray(64), -1, -1, 0, false, hashMapOf())
fun default(indexId: Int): Index = Index(indexId, 0, ByteArray(64), -1, -1, 0, false, false, hashMapOf())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ data class Group(
val whirlpool: ByteArray,
val revision: Int,
val keys: IntArray,
private val files: Map<Int, File>,
private val files: MutableMap<Int, File>,
val data: ByteArray
): Comparable<Group> {

@JvmName("getFiles")
fun files(): Collection<File> = files.values

@JvmName("getFileIds")
fun fileIds(): Collection<Int> = files.keys

@JvmName("getFile")
fun file(fileId: Int): File = files[fileId] ?: File.DEFAULT

internal fun putFile(id: Int, file: File) {
files[id] = file
}

override fun compareTo(other: Group): Int = this.id.compareTo(other.id)
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down Expand Up @@ -57,6 +64,6 @@ data class Group(
}

internal companion object {
val DEFAULT = Group(-1, -1, -1, byteArrayOf(), -1, intArrayOf(), mapOf(), byteArrayOf())
val DEFAULT = Group(-1, -1, 0, byteArrayOf(), 0, intArrayOf(), mutableMapOf(), byteArrayOf())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ data class File(
}

internal companion object {
val DEFAULT = File(-1, -1, byteArrayOf(0))
val DEFAULT = File(-1, 0, byteArrayOf(0))
}
}
19 changes: 18 additions & 1 deletion cache/src/main/kotlin/com/runetopic/cache/store/Js5Store.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.runetopic.cache.store

import com.runetopic.cache.hierarchy.index.Index
import com.runetopic.cache.hierarchy.index.group.file.File
import com.runetopic.cache.store.storage.js5.Js5DiskStorage
import com.runetopic.cryptography.toWhirlpool
import java.io.Closeable
Expand All @@ -23,7 +24,7 @@ class Js5Store(
private val indexes = CopyOnWriteArrayList<Index>()

init {
storage.init(this)
storage.open(this)
indexes.sortWith(compareBy { it.id })
}

Expand All @@ -32,6 +33,22 @@ class Js5Store(
indexes.add(index)
}

fun putFile(indexId: Int, groupId: Int, id: Int, data: ByteArray) {
val group = index(indexId).group(groupId)
val exists = group.fileIds().contains(id)
if (exists) {
val file = group.file(id)
group.putFile(id, File(file.id, file.nameHash, data))
return
}
group.putFile(id, File(id, 0, data))
}

fun save(): Boolean {

return true
}

fun index(indexId: Int): Index = indexes.find { it.id == indexId }!!

fun indexReferenceTableSize(indexId: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import java.io.Flushable
* @email <xlitersps@gmail.com>
*/
internal interface IStorage: Closeable, Flushable {
fun init(store: Js5Store)
fun open(indexId: Int, store: Js5Store)
fun open(store: Js5Store)
fun read(indexId: Int, store: Js5Store)
fun write(indexId: Int, store: Js5Store)
fun loadReferenceTable(index: Index, groupId: Int): ByteArray
fun loadMasterReferenceTable(groupId: Int): ByteArray
fun loadReferenceTable(index: Index, groupName: String): ByteArray
Expand Down
Loading