Skip to content

Commit

Permalink
Merge pull request #14 from runetopic/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
ultraviolet-jordan authored Sep 27, 2021
2 parents 29026d7 + 54f4c10 commit bb743d8
Show file tree
Hide file tree
Showing 61 changed files with 231 additions and 218 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ A cache library written in Kotlin.
# Implementation
Just use cache if you do not require any of the revision specific loaders.
```
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.6-SNAPSHOT" }
loader = { module = "com.runetopic.cache:loader", version.ref "647.6.0-SNAPSHOT" }
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.7-SNAPSHOT" }
loader = { module = "com.runetopic.cache:loader", version.ref "647.6.1-SNAPSHOT" }
//For the SNAPSHOTS
maven {
Expand All @@ -41,9 +41,9 @@ maven {
# Usage
Index -> Group -> File

### Creating a new store
### Creating a new JS5 store
```
val store = Store(File("/filepath/"))
val store = Js5Store(Path.of("/path/"))
```

### Getting an index
Expand Down Expand Up @@ -101,6 +101,13 @@ val file = group.getFile(fileId = 1000)
### Getting 255, 255 checksums without RSA/Whirlpool
```val checksums = store.checksumsWithoutRSA()```

### Decompressing a group
```
val index = store.index(indexId = 5)
val group = index.getGroup(groupName = "m50_50")
val decompressed = group.getData().decompress()
```

### An example of a single thread loading providers
```
objs().load(store)
Expand Down
2 changes: 1 addition & 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.6-SNAPSHOT"
version = "1.4.7-SNAPSHOT"

java {
withJavadocJar()
Expand Down
20 changes: 20 additions & 0 deletions cache/src/main/kotlin/com/runetopic/cache/codec/CodecType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.runetopic.cache.codec

import com.runetopic.cache.codec.impl.BZip2Codec
import com.runetopic.cache.codec.impl.GZipCodec
import com.runetopic.cache.codec.impl.NoFileCodec

/**
* @author Tyler Telis
* @email <xlitersps@gmail.com>
*
* @author Jordan Abraham
*/
internal sealed class CodecType(
val codec: IFileCodec
) {
object BadCodec: CodecType(NoFileCodec())
object NoCodec: CodecType(NoFileCodec())
object BZipCodec: CodecType(BZip2Codec())
object GZipCodec: CodecType(GZipCodec())
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.runetopic.cache.compression
package com.runetopic.cache.codec

/**
* @author Tyler Telis
* @email <xlitersps@gmail.com>
*/
data class Container(
internal data class Container(
val data: ByteArray,
val compression: Int,
val revision: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.runetopic.cache.compression
package com.runetopic.cache.codec

import com.runetopic.cache.compression.CompressionType.*
import com.runetopic.cache.codec.CodecType.*
import com.runetopic.cache.exception.CompressionException
import com.runetopic.cache.extension.readUnsignedShort
import com.runetopic.cache.extension.remainingBytes
Expand All @@ -14,9 +14,9 @@ import java.util.zip.CRC32
*
* @author Jordan Abraham
*/
object Compression {
internal object ContainerCodec {

fun decompress(data: ByteArray, keys: Array<Int>): Container {
fun decompress(data: ByteArray, keys: IntArray = intArrayOf()): Container {
val buffer = ByteBuffer.wrap(data)

val compression = buffer.get().toInt() and 0xFF
Expand All @@ -30,22 +30,22 @@ object Compression {
crc32.update(data, 0, 5)

return when (val type = compressionType(compression)) {
BadCompression -> throw CompressionException("Compression type not found with a compression opcode of $compression.")
is NoCompression -> {
BadCodec -> throw CompressionException("Compression type not found with a compression opcode of $compression.")
is NoCodec -> {
val encrypted = ByteArray(length)
buffer.get(encrypted, 0, length)
crc32.update(encrypted, 0, length)
val decrypted = if (keys.isEmpty()) encrypted else encrypted.fromXTEA(32, keys.toIntArray())
val decrypted = if (keys.isEmpty()) encrypted else encrypted.fromXTEA(32, keys)

val revision = -1 /*buffer.short.toInt() and 0xFFFF*/

Container(decrypted, compression, revision, crc32.value.toInt())
}
GZipCompression, BZipCompression-> {
GZipCodec, BZipCodec-> {
val encrypted = ByteArray(length + 4)
buffer.get(encrypted)
crc32.update(encrypted, 0, encrypted.size)
val decrypted = if (keys.isEmpty()) encrypted else encrypted.fromXTEA(32, keys.toIntArray())
val decrypted = if (keys.isEmpty()) encrypted else encrypted.fromXTEA(32, keys)

var revision = -1

Expand All @@ -66,12 +66,12 @@ object Compression {
}
}

private fun compressionType(compression: Int): CompressionType {
private fun compressionType(compression: Int): CodecType {
return when (compression) {
0 -> NoCompression
1 -> BZipCompression
2 -> GZipCompression
else -> BadCompression
0 -> NoCodec
1 -> BZipCodec
2 -> GZipCodec
else -> BadCodec
}
}
}
13 changes: 13 additions & 0 deletions cache/src/main/kotlin/com/runetopic/cache/codec/Decompression.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@file:JvmName("Decompression")
package com.runetopic.cache.codec

/**
* @author Jordan Abraham
*/
fun ByteArray.decompress(keys: IntArray = intArrayOf()): ByteArray {
return ContainerCodec.decompress(this, keys).data
}

fun ByteArray.decompress(): ByteArray {
return ContainerCodec.decompress(this, intArrayOf()).data
}
10 changes: 10 additions & 0 deletions cache/src/main/kotlin/com/runetopic/cache/codec/IFileCodec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.runetopic.cache.codec

/**
* @author Tyler Telis
* @email <xlitersps@gmail.com>
*/
internal interface IFileCodec {
fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray
fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.runetopic.cache.compression.impl
package com.runetopic.cache.codec.impl

import com.runetopic.cache.compression.IFileCodec
import com.runetopic.cache.codec.IFileCodec
import com.runetopic.cache.store.Constants.BZIP_HEADER
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
Expand All @@ -15,7 +15,7 @@ import java.util.*
* @email <xlitersps@gmail.com>
*/
internal class BZip2Codec: IFileCodec {
override fun compress(data: ByteArray, length: Int, keys: Array<Int>): ByteArray {
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
val stream: InputStream = ByteArrayInputStream(data)
val bout = ByteArrayOutputStream()
BZip2CompressorOutputStream(bout, 1).use { os -> IOUtils.copy(stream, os) }
Expand All @@ -30,7 +30,7 @@ internal class BZip2Codec: IFileCodec {
return Arrays.copyOfRange(buffer, BZIP_HEADER.size, buffer.size)
}

override fun decompress(data: ByteArray, length: Int, keys: Array<Int>): ByteArray {
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
val buffer = ByteArray(length + BZIP_HEADER.size)

System.arraycopy(BZIP_HEADER, 0, buffer,0, BZIP_HEADER.size)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.runetopic.cache.compression.impl
package com.runetopic.cache.codec.impl

import com.runetopic.cache.compression.IFileCodec
import com.runetopic.cache.codec.IFileCodec
import org.apache.commons.compress.utils.IOUtils
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
Expand All @@ -12,14 +12,14 @@ import java.util.zip.GZIPOutputStream
* @email <xlitersps@gmail.com>
*/
internal class GZipCodec: IFileCodec {
override fun compress(data: ByteArray, length: Int, keys: Array<Int>): ByteArray {
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
val inputStream = ByteArrayInputStream(data)
val outputStream = ByteArrayOutputStream()
GZIPOutputStream(outputStream).use { os -> IOUtils.copy(inputStream, os) }
return outputStream.toByteArray()
}

override fun decompress(data: ByteArray, length: Int, keys: Array<Int>): ByteArray {
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
val outputStream = ByteArrayOutputStream()
GZIPInputStream(ByteArrayInputStream(data, 0, length)).use {
IOUtils.copy(it, outputStream)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.runetopic.cache.compression.impl
package com.runetopic.cache.codec.impl

import com.runetopic.cache.compression.IFileCodec
import com.runetopic.cache.codec.IFileCodec

/**
* @author Tyler Telis
* @email <xlitersps@gmail.com>
*/
internal class NoFileCodec: IFileCodec {
override fun compress(data: ByteArray, length: Int, keys: Array<Int>): ByteArray {
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
throw NotImplementedError("No codec provided.")
}

override fun decompress(data: ByteArray, length: Int, keys: Array<Int>): ByteArray {
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
throw NotImplementedError("No codec provided.")
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.runetopic.cache.hierarchy

import com.runetopic.cache.compression.Compression
import com.runetopic.cache.codec.ContainerCodec
import com.runetopic.cache.codec.decompress
import com.runetopic.cache.exception.ProtocolException
import com.runetopic.cache.extension.readUnsignedByte
import com.runetopic.cache.extension.readUnsignedShort
import com.runetopic.cache.hierarchy.index.Js5Index
import com.runetopic.cache.hierarchy.index.group.Js5Group
import com.runetopic.cache.hierarchy.index.group.file.IFile
import com.runetopic.cache.hierarchy.index.group.file.File
import com.runetopic.cache.hierarchy.index.group.file.Js5File
import com.runetopic.cache.store.js5.IDatFile
import com.runetopic.cache.store.js5.IIdxFile
import com.runetopic.cache.store.js5.impl.IdxFile
import com.runetopic.cache.store.storage.js5.IDatFile
import com.runetopic.cache.store.storage.js5.IIdxFile
import java.nio.ByteBuffer
import java.util.*
import java.util.zip.ZipException
Expand All @@ -22,7 +22,7 @@ import java.util.zip.ZipException
* @author Jordan Abraham
*/
internal data class ReferenceTable(
val idxFile: IdxFile,
val idxFile: IIdxFile,
val id: Int,
val sector: Int,
val length: Int,
Expand Down Expand Up @@ -60,7 +60,7 @@ internal data class ReferenceTable(
fun exists(): Boolean = (length != 0 && sector != 0)

fun loadIndex(datFile: IDatFile, idxFile: IIdxFile, whirlpool: ByteArray, data: ByteArray): Js5Index {
val container = Compression.decompress(data, emptyArray())
val container = ContainerCodec.decompress(data)
val buffer = ByteBuffer.wrap(container.data)
val protocol = buffer.readUnsignedByte()
var revision = 0
Expand Down Expand Up @@ -235,11 +235,11 @@ internal data class ReferenceTable(
groupReferenceTableData: ByteArray,
count: Int,
groupId: Int
): Map<Int, IFile> {
): Map<Int, File> {
if (groupReferenceTableData.isEmpty()) return hashMapOf(Pair(0, Js5File.DEFAULT))

val src: ByteArray = try {
Compression.decompress(groupReferenceTableData, emptyArray()).data
groupReferenceTableData.decompress()
} catch (exception: ZipException) {
groupReferenceTableData
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.runetopic.cache.hierarchy.index

import com.runetopic.cache.hierarchy.index.group.IGroup
import com.runetopic.cache.hierarchy.index.group.Group

/**
* @author Jordan Abraham
*/
interface IIndex: Comparable<IIndex> {
interface Index: Comparable<Index> {
fun getId(): Int
fun getCRC(): Int
fun getWhirlpool(): ByteArray
fun getCompression(): Int
fun getProtocol(): Int
fun getRevision(): Int
fun getIsNamed(): Boolean
fun getGroups(): Collection<IGroup>
fun getGroup(groupId: Int): IGroup
fun getGroup(groupName: String): IGroup
fun getGroups(): Collection<Group>
fun getGroup(groupId: Int): Group
fun getGroup(groupName: String): Group
fun expand(): Int

fun use(block: (IIndex) -> Unit) = block.invoke(this)
fun use(block: (Index) -> Unit) = block.invoke(this)

override fun compareTo(other: IIndex): Int {
override fun compareTo(other: Index): Int {
return getId().compareTo(other.getId())
}
}
Loading

0 comments on commit bb743d8

Please sign in to comment.