Skip to content

Commit 747c68e

Browse files
Merge pull request #20 from runetopic/development
Speed improvements and some code refactoring.
2 parents 8fec609 + da79082 commit 747c68e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+194
-401
lines changed

README.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A cache library written in Kotlin.
1616
# Features
1717
- Cache Reading
1818
- Definitions/Providers Loading
19-
- Fast (Limited by I/O)
19+
- Very Fast (Limited by I/O)
2020

2121
# TODO
2222
- Cache Writing
@@ -28,8 +28,8 @@ A cache library written in Kotlin.
2828
# Implementation
2929
Just use cache if you do not require any of the revision specific loaders.
3030
```
31-
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.16-SNAPSHOT" }
32-
loader = { module = "com.runetopic.cache:loader", version.ref "647.6.3-SNAPSHOT" }
31+
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.18-SNAPSHOT" }
32+
loader = { module = "com.runetopic.cache:loader", version.ref "647.6.4-SNAPSHOT" }
3333
```
3434

3535
```
@@ -55,33 +55,33 @@ val index = store.index(indexId = 5)
5555
### Getting a group by group id
5656
```
5757
val index = store.index(indexId = 5)
58-
val group = index.getGroup(groupId = 360)
58+
val group = index.group(groupId = 360)
5959
```
6060

6161
### Getting a group by group name
6262
```
6363
val index = store.index(indexId = 5)
64-
val group = index.getGroup(groupName = "m50_50")
64+
val group = index.group(groupName = "m50_50")
6565
```
6666

6767
### Getting a file from a group by id
6868
```
6969
val index = store.index(indexId = 2)
70-
val group = index.getGroup(groupId = 26)
71-
val file = group.getFile(fileId = 1000)
70+
val group = index.group(groupId = 26)
71+
val file = group.file(fileId = 1000)
7272
```
7373

7474
### Looping multiple groups from an index
7575
store.index(indexId = 19).use { index ->
7676
(0 until index.expand()).forEach {
77-
val data = index.getGroup(it ushr 8).getFile(it and 0xFF).getData()
77+
val data = index.group(it ushr 8).file(it and 0xFF).data
7878
}
7979
}
8080

8181
### Looping multiple files from a group
82-
store.index(indexId = 2).getGroup(groupId = 26).getFiles().forEach {
83-
val id = it.getId()
84-
val data = it.getData()
82+
store.index(indexId = 2).group(groupId = 26).files().forEach {
83+
val id = it.id
84+
val data = it.data
8585
}
8686

8787
### Getting the reference table of an index and group by id.
@@ -102,13 +102,6 @@ val file = group.getFile(fileId = 1000)
102102
### Getting 255, 255 checksums without RSA/Whirlpool
103103
```val checksums = store.checksumsWithoutRSA()```
104104

105-
### Decompressing a group
106-
```
107-
val index = store.index(indexId = 5)
108-
val group = index.getGroup(groupName = "m50_50")
109-
val decompressed = group.getData().decompress()
110-
```
111-
112105
### An example of a single thread loading providers
113106
```
114107
objs().load(store)

cache/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
signing
55
}
66

7-
version = "1.4.16-SNAPSHOT"
7+
version = "1.4.18-SNAPSHOT"
88

99
java {
1010
withJavadocJar()

cache/src/main/kotlin/com/runetopic/cache/codec/ContainerCodec.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import com.runetopic.cache.exception.CompressionException
55
import com.runetopic.cache.extension.readUnsignedByte
66
import com.runetopic.cache.extension.readUnsignedShort
77
import com.runetopic.cache.extension.remainingBytes
8+
import com.runetopic.cache.extension.toByteBuffer
89
import com.runetopic.cryptography.fromXTEA
9-
import java.nio.ByteBuffer
1010
import java.util.zip.CRC32
1111

1212
/**
@@ -18,8 +18,7 @@ import java.util.zip.CRC32
1818
internal object ContainerCodec {
1919

2020
fun decompress(data: ByteArray, keys: IntArray = intArrayOf()): Container {
21-
val buffer = ByteBuffer.wrap(data)
22-
21+
val buffer = data.toByteBuffer()
2322
val compression = buffer.readUnsignedByte()
2423
val length = buffer.int
2524

@@ -54,7 +53,7 @@ internal object ContainerCodec {
5453
revision = buffer.readUnsignedShort()
5554
}
5655

57-
val byteBuffer = ByteBuffer.wrap(decrypted)
56+
val byteBuffer = decrypted.toByteBuffer()
5857
val decompressedLength = byteBuffer.int
5958
val decompressedData = type.codec.decompress(byteBuffer.remainingBytes(), length, keys)
6059

cache/src/main/kotlin/com/runetopic/cache/codec/Decompression.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,5 @@ package com.runetopic.cache.codec
44
/**
55
* @author Jordan Abraham
66
*/
7-
fun ByteArray.decompress(keys: IntArray = intArrayOf()): ByteArray {
8-
return ContainerCodec.decompress(this, keys).data
9-
}
10-
11-
fun ByteArray.decompress(): ByteArray {
12-
return ContainerCodec.decompress(this, intArrayOf()).data
13-
}
7+
fun ByteArray.decompress(keys: IntArray): ByteArray = ContainerCodec.decompress(this, keys).data
8+
fun ByteArray.decompress(): ByteArray = ContainerCodec.decompress(this, intArrayOf()).data

cache/src/main/kotlin/com/runetopic/cache/codec/impl/BZip2Codec.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ internal class BZip2Codec: IFileCodec {
3737
System.arraycopy(data, 0, buffer, BZIP_HEADER.size, length)
3838

3939
val stream = ByteArrayOutputStream()
40-
41-
BZip2CompressorInputStream(ByteArrayInputStream(buffer)).use {
42-
IOUtils.copy(it, stream)
43-
}
44-
40+
BZip2CompressorInputStream(ByteArrayInputStream(buffer)).use { IOUtils.copy(it, stream) }
4541
return stream.toByteArray()
4642
}
4743
}

cache/src/main/kotlin/com/runetopic/cache/codec/impl/NoFileCodec.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import com.runetopic.cache.codec.IFileCodec
77
* @email <[email protected]>
88
*/
99
internal class NoFileCodec: IFileCodec {
10-
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
11-
throw NotImplementedError("No codec provided.")
12-
}
13-
14-
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
15-
throw NotImplementedError("No codec provided.")
16-
}
10+
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray = throw NotImplementedError("No codec provided.")
11+
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray = throw NotImplementedError("No codec provided.")
1712
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.runetopic.cache.extension
2+
3+
import java.nio.ByteBuffer
4+
5+
/**
6+
* @author Jordan Abraham
7+
*/
8+
fun ByteArray.toByteBuffer(): ByteBuffer = ByteBuffer.wrap(this)

cache/src/main/kotlin/com/runetopic/cache/extension/ByteBuffer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.nio.ByteBuffer
44

55
internal fun ByteBuffer.readUnsignedByte(): Int = get().toInt() and 0xFF
66
internal fun ByteBuffer.readUnsignedShort(): Int = short.toInt() and 0xFFFF
7-
internal fun ByteBuffer.readUnsignedMedium(): Int = ((get().toInt() and 0xFF shl 16) or (get().toInt() and 0xFF shl 8) or (get().toInt() and 0xFF))
7+
internal fun ByteBuffer.readUnsignedMedium(): Int = ((readUnsignedByte() shl 16) or (readUnsignedByte() shl 8) or readUnsignedByte())
88
internal fun ByteBuffer.readUnsignedIntShortSmart(): Int = if (get(position()).toInt() < 0) int and Int.MAX_VALUE else readUnsignedShort()
99

1010
internal fun ByteBuffer.remainingBytes(): ByteArray {
Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
package com.runetopic.cache.hierarchy
22

3-
import com.runetopic.cache.store.storage.js5.IIdxFile
4-
import java.util.*
5-
63
/**
74
* @author Tyler Telis
85
* @email <[email protected]>
96
*
107
* @author Jordan Abraham
118
*/
129
internal data class ReferenceTable(
13-
val idxFile: IIdxFile,
1410
val id: Int,
1511
val sector: Int,
1612
val length: Int
1713
) {
1814
fun exists(): Boolean = (length != 0 && sector != 0)
1915

20-
override fun hashCode(): Int {
21-
var hash = 7
22-
hash = 19 * hash + Objects.hashCode(this.idxFile)
23-
hash = 19 * hash + this.id
24-
hash = 19 * hash + this.sector
25-
hash = 19 * hash + this.length
26-
return hash
16+
override fun equals(other: Any?): Boolean {
17+
if (this === other) return true
18+
if (javaClass != other?.javaClass) return false
19+
20+
other as ReferenceTable
21+
22+
if (id != other.id) return false
23+
if (sector != other.sector) return false
24+
if (length != other.length) return false
25+
26+
return true
2727
}
2828

29-
override fun equals(other: Any?): Boolean {
30-
when (other) {
31-
null -> return false
32-
else -> return when {
33-
javaClass != other.javaClass -> false
34-
else -> {
35-
val referenceTable: ReferenceTable = other as ReferenceTable
36-
when {
37-
idxFile != referenceTable.idxFile -> false
38-
id != referenceTable.id -> false
39-
sector != referenceTable.sector -> false
40-
length != referenceTable.length -> false
41-
else -> true
42-
}
43-
}
44-
}
45-
}
29+
override fun hashCode(): Int {
30+
var result = id
31+
result = 31 * result + sector
32+
result = 31 * result + length
33+
return result
4634
}
4735
}

cache/src/main/kotlin/com/runetopic/cache/store/Constants.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ internal object Constants {
1010
const val MAIN_FILE_DAT = "$PREFIX.dat2"
1111
const val MAIN_FILE_255 = "$PREFIX.idx255"
1212

13-
const val SECTOR_SIZE = 520
13+
const val IDX_SIZE = 6
14+
const val DAT_SIZE = 520
1415
const val MASTER_INDEX_ID = 255
1516

16-
17-
18-
1917
val BZIP_HEADER = byteArrayOf(
2018
'B'.code.toByte(),
2119
'Z'.code.toByte(),

0 commit comments

Comments
 (0)