Skip to content

Cache writing/editing. #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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.
78 changes: 45 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# RuneTopic Cache Library

[![Discord](https://img.shields.io/discord/212385463418355713?color=%237289DA&logo=Discord&logoColor=%237289DA)](https://discord.gg/3scgBkrfMG)
[![License](https://img.shields.io/github/license/xlite2/xlite)](#)

A cache library written in Kotlin.

# Requirements
- Java Version 16

# Supported
- RS2 (414-772)
- RS3 (773-~788)
Expand All @@ -27,91 +23,107 @@ A cache library written in Kotlin.

# Implementation
Just use cache if you do not require any of the revision specific loaders.
```
```groovy
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.19-SNAPSHOT" }
loader = { module = "com.runetopic.cache:loader", version.ref "647.6.4-SNAPSHOT" }
```

```
```groovy
//SNAPSHOTS
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
```

# Usage
Index -> Group -> File

### Creating a new JS5 store
```
```kotlin
val store = Js5Store(path = Path.of("/path/"), parallel = true)
```

### Getting an index
```
```kotlin
val index = store.index(indexId = 5)
```

### Getting a group by group id
```
```kotlin
val index = store.index(indexId = 5)
val group = index.group(groupId = 360)
```

### Getting a group by group name
```
```kotlin
val index = store.index(indexId = 5)
val group = index.group(groupName = "m50_50")
```

### Getting a file from a group by id
```
```kotlin
val index = store.index(indexId = 2)
val group = index.group(groupId = 26)
val file = group.file(fileId = 1000)
```

### Looping multiple groups from an index
store.index(indexId = 19).use { index ->
(0 until index.expand()).forEach {
val data = index.group(it ushr 8).file(it and 0xFF).data
}
```kotlin
store.index(indexId = 19).use { index ->
(0 until index.expand()).forEach {
val data = index.group(it ushr 8).file(it and 0xFF).data
}
}
```

### Looping multiple files from a group
store.index(indexId = 2).group(groupId = 26).files().forEach {
val id = it.id
val data = it.data
}

```kotlin
store.index(indexId = 2).group(groupId = 26).files().forEach {
val id = it.id
val data = it.data
}
```
### Getting the reference table of an index and group by id.
```store.groupReferenceTable(indexId = 255, groupId = 255)```
```kotlin
val size = store.groupReferenceTable(indexId = 255, groupId = 255)
```

### Getting an index reference table size by id
```store.indexReferenceTableSize(indexId = 28)```
```kotlin
val size = store.indexReferenceTableSize(indexId = 28)
```

### Getting a group reference table size by name
```store.groupReferenceTableSize(indexId = 30, groupName = "windows/x86/jaclib.dll")```
```kotlin
val size = store.groupReferenceTableSize(indexId = 30, groupName = "windows/x86/jaclib.dll")
```

### Getting a group reference table size by id
```store.groupReferenceTableSize(indexId = 30, groupId = 6)```
```kotlin
val size = store.groupReferenceTableSize(indexId = 30, groupId = 6)
```

### Getting 255, 255 checksums with RSA/Whirlpool
```val checksums = store.checksumsWithRSA(exponent = BigInteger(""), modulus = BigInteger(""))```
```kotlin
val checksums = store.checksumsWithRSA(exponent = BigInteger(""), modulus = BigInteger(""))
```

### Getting 255, 255 checksums without RSA/Whirlpool
```val checksums = store.checksumsWithoutRSA()```
```kotlin
val checksums = store.checksumsWithoutRSA()
```

### An example of a single thread loading providers
```
objs().load(store)
npcs().load(store)
locs().load(store)
particles().load(store)

```kotlin
val objProvider = objs().load(store)
val npcProvider = npcs().load(store)
val locProvider = locs().load(store)
val particleProvider = particles().load(store)
```

### An example of multiple threads parallel loading providers
```
```kotlin
val pool = Executors.newFixedThreadPool(4)
val providers = listOf(
objs(),
Expand Down
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ configure(allprojects) {
group = "com.runetopic.cache"

plugins.withType<KotlinPluginWrapper> {
java.sourceCompatibility = JavaVersion.VERSION_16
java.targetCompatibility = JavaVersion.VERSION_16
java.sourceCompatibility = JavaVersion.VERSION_17
java.targetCompatibility = JavaVersion.VERSION_17

tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
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
6 changes: 4 additions & 2 deletions 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 @@ -67,7 +67,6 @@ publishing {
}
}


signing {
sign(publishing.publications["mavenJava"])
}
Expand All @@ -78,4 +77,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")
}
12 changes: 6 additions & 6 deletions cache/src/main/kotlin/com/runetopic/cache/codec/CodecType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import com.runetopic.cache.codec.impl.NoFileCodec
* @author Jordan Abraham
*/
internal sealed class CodecType(
val codec: IFileCodec
val codec: FileCodec
) {
object BadCodec: CodecType(NoFileCodec())
object NoCodec: CodecType(NoFileCodec())
object BZipCodec: CodecType(BZip2Codec())
object GZipCodec: CodecType(GZipCodec())
}
object BadCodec : CodecType(NoFileCodec())
object NoCodec : CodecType(NoFileCodec())
object BZipCodec : CodecType(BZip2Codec())
object GZipCodec : CodecType(GZipCodec())
}
53 changes: 47 additions & 6 deletions cache/src/main/kotlin/com/runetopic/cache/codec/ContainerCodec.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.runetopic.cache.codec

import com.runetopic.cache.codec.CodecType.*
import com.runetopic.cache.codec.CodecType.BZipCodec
import com.runetopic.cache.codec.CodecType.BadCodec
import com.runetopic.cache.codec.CodecType.GZipCodec
import com.runetopic.cache.codec.CodecType.NoCodec
import com.runetopic.cache.exception.CompressionException
import com.runetopic.cache.extension.readUnsignedByte
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 +22,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 +61,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 All @@ -41,7 +73,7 @@ internal object ContainerCodec {

Container(decrypted, compression, revision, crc32.value.toInt())
}
GZipCodec, BZipCodec-> {
GZipCodec, BZipCodec -> {
val encrypted = ByteArray(length + 4)
buffer.get(encrypted)
crc32.update(encrypted, 0, encrypted.size)
Expand All @@ -66,12 +98,21 @@ 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
2 -> GZipCodec
else -> BadCodec
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ package com.runetopic.cache.codec
* @author Jordan Abraham
*/
fun ByteArray.decompress(keys: IntArray): ByteArray = ContainerCodec.decompress(this, keys).data
fun ByteArray.decompress(): ByteArray = ContainerCodec.decompress(this, intArrayOf()).data
fun ByteArray.decompress(): ByteArray = ContainerCodec.decompress(this, intArrayOf()).data
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.runetopic.cache.codec
* @author Tyler Telis
* @email <xlitersps@gmail.com>
*/
internal interface IFileCodec {
fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray
internal interface FileCodec {
fun compress(data: ByteArray, keys: IntArray): ByteArray
fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray
}
}
Loading