Compare commits

..

1 Commits

Author SHA1 Message Date
Graham 2d19d3f90d Add initial libbzip2 Java port 2 years ago
  1. 6
      .editorconfig
  2. 9
      .idea/runConfigurations/AstDeobfuscator.xml
  3. 9
      .idea/runConfigurations/BytecodeDeobfuscator.xml
  4. 8
      .idea/runConfigurations/Decompiler.xml
  5. 8
      .idea/runConfigurations/Deobfuscator.xml
  6. 8
      .idea/runConfigurations/GameServer.xml
  7. 9
      .idea/runConfigurations/GenerateBuffer.xml
  8. 9
      .idea/runConfigurations/Patcher.xml
  9. 2
      LICENSE
  10. 6
      README.md
  11. 3
      all/build.gradle.kts
  12. 2
      all/src/main/kotlin/org/openrs2/Command.kt
  13. 1
      archive/build.gradle.kts
  14. 12
      archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt
  15. 3
      archive/src/main/kotlin/org/openrs2/archive/cache/CacheCommand.kt
  16. 17
      archive/src/main/kotlin/org/openrs2/archive/cache/CacheDownloader.kt
  17. 357
      archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt
  18. 306
      archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt
  19. 16
      archive/src/main/kotlin/org/openrs2/archive/cache/CrossPollinateCommand.kt
  20. 223
      archive/src/main/kotlin/org/openrs2/archive/cache/CrossPollinator.kt
  21. 5
      archive/src/main/kotlin/org/openrs2/archive/cache/ExportCommand.kt
  22. 15
      archive/src/main/kotlin/org/openrs2/archive/cache/Js5ChannelHandler.kt
  23. 4
      archive/src/main/kotlin/org/openrs2/archive/cache/NxtJs5ChannelHandler.kt
  24. 24
      archive/src/main/kotlin/org/openrs2/archive/cache/OsrsJs5ChannelHandler.kt
  25. 7
      archive/src/main/kotlin/org/openrs2/archive/cache/OsrsJs5ChannelInitializer.kt
  26. 149
      archive/src/main/kotlin/org/openrs2/archive/cache/finder/CacheFinderExtractor.kt
  27. 25
      archive/src/main/kotlin/org/openrs2/archive/cache/finder/ExtractCommand.kt
  28. 10
      archive/src/main/kotlin/org/openrs2/archive/cache/nxt/InitJs5RemoteConnectionCodec.kt
  29. 25
      archive/src/main/kotlin/org/openrs2/archive/cache/nxt/Js5OkCodec.kt
  30. 1
      archive/src/main/kotlin/org/openrs2/archive/cache/nxt/Js5RequestEncoder.kt
  31. 6
      archive/src/main/kotlin/org/openrs2/archive/cache/nxt/Js5ResponseDecoder.kt
  32. 7
      archive/src/main/kotlin/org/openrs2/archive/cache/nxt/LoginResponse.kt
  33. 3
      archive/src/main/kotlin/org/openrs2/archive/game/Game.kt
  34. 7
      archive/src/main/kotlin/org/openrs2/archive/game/GameDatabase.kt
  35. 2
      archive/src/main/kotlin/org/openrs2/archive/jav/JavConfig.kt
  36. 57
      archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt
  37. 2
      archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyReader.kt
  38. 30
      archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt
  39. 4
      archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt
  40. 20
      archive/src/main/kotlin/org/openrs2/archive/key/KeyImporter.kt
  41. 3
      archive/src/main/kotlin/org/openrs2/archive/key/KeySource.kt
  42. 19
      archive/src/main/kotlin/org/openrs2/archive/key/OpenOsrsKeyDownloader.kt
  43. 50
      archive/src/main/kotlin/org/openrs2/archive/key/PolarKeyDownloader.kt
  44. 115
      archive/src/main/kotlin/org/openrs2/archive/map/MapRenderer.kt
  45. 2
      archive/src/main/kotlin/org/openrs2/archive/name/NameImporter.kt
  46. 12
      archive/src/main/kotlin/org/openrs2/archive/name/RuneStarNameDownloader.kt
  47. 152
      archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt
  48. 8
      archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt
  49. 120
      archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt
  50. 176
      archive/src/main/resources/org/openrs2/archive/migrations/V12__scopes.sql
  51. 2
      archive/src/main/resources/org/openrs2/archive/migrations/V13__hidden_flag.sql
  52. 95
      archive/src/main/resources/org/openrs2/archive/migrations/V14__scopes_fix.sql
  53. 95
      archive/src/main/resources/org/openrs2/archive/migrations/V15__empty_index_stats.sql
  54. 95
      archive/src/main/resources/org/openrs2/archive/migrations/V16__empty_index_size.sql
  55. 53
      archive/src/main/resources/org/openrs2/archive/migrations/V17__split_version_list_stats.sql
  56. 2
      archive/src/main/resources/org/openrs2/archive/migrations/V18__hdos.sql
  57. 3
      archive/src/main/resources/org/openrs2/archive/migrations/V19__source_type_cross_pollination.sql
  58. 7
      archive/src/main/resources/org/openrs2/archive/migrations/V20__cross_pollination.sql
  59. 1
      archive/src/main/resources/org/openrs2/archive/migrations/V2__game_url.sql
  60. 1
      archive/src/main/resources/org/openrs2/archive/migrations/V5__keys.sql
  61. 43
      archive/src/main/resources/org/openrs2/archive/static/js/openrs2.js
  62. 285
      archive/src/main/resources/org/openrs2/archive/templates/api/index.html
  63. 36
      archive/src/main/resources/org/openrs2/archive/templates/caches/index.html
  64. 110
      archive/src/main/resources/org/openrs2/archive/templates/caches/show.html
  65. 3
      archive/src/main/resources/org/openrs2/archive/templates/index.html
  66. 4
      archive/src/main/resources/org/openrs2/archive/templates/layout.html
  67. 2
      asm/build.gradle.kts
  68. 2
      asm/src/main/kotlin/org/openrs2/asm/ClassNodeRemapper.kt
  69. 2
      asm/src/main/kotlin/org/openrs2/asm/InsnNodeUtils.kt
  70. 2
      asm/src/main/kotlin/org/openrs2/asm/MethodNodeUtils.kt
  71. 3
      asm/src/main/kotlin/org/openrs2/asm/StackMetadata.kt
  72. 1
      asm/src/main/kotlin/org/openrs2/asm/filter/Glob.kt
  73. 2
      asm/src/main/kotlin/org/openrs2/asm/packclass/ConstantPool.kt
  74. 24
      asm/src/main/kotlin/org/openrs2/asm/packclass/PackClass.kt
  75. 2
      buffer-generator/build.gradle.kts
  76. 2
      buffer-generator/src/main/kotlin/org/openrs2/buffer/generator/ByteBufExtensionGenerator.kt
  77. 1
      buffer-generator/src/main/kotlin/org/openrs2/buffer/generator/ByteOrder.kt
  78. 2
      buffer/build.gradle.kts
  79. 12
      build.gradle.kts
  80. 2
      cache-550/build.gradle.kts
  81. 2
      cache-550/src/main/kotlin/org/openrs2/cache/config/enum/EnumType.kt
  82. 3
      cache-550/src/main/kotlin/org/openrs2/cache/config/struct/StructType.kt
  83. 1
      cache-550/src/main/kotlin/org/openrs2/cache/config/varbit/VarbitType.kt
  84. 19
      cache-550/src/main/kotlin/org/openrs2/cache/midi/Song.kt
  85. 2
      cache-550/src/main/kotlin/org/openrs2/cache/sprite/Sprite.kt
  86. 15
      cache-cli/src/main/kotlin/org/openrs2/cache/cli/CacheCommand.kt
  87. 26
      cache-cli/src/main/kotlin/org/openrs2/cache/cli/OpenNxtUnpackCommand.kt
  88. 27
      cache-cli/src/main/kotlin/org/openrs2/cache/cli/RuneLiteUnpackCommand.kt
  89. 3
      cache/build.gradle.kts
  90. 73
      cache/src/main/kotlin/org/openrs2/cache/Archive.kt
  91. 57
      cache/src/main/kotlin/org/openrs2/cache/Cache.kt
  92. 11
      cache/src/main/kotlin/org/openrs2/cache/ChecksumTable.kt
  93. 6
      cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt
  94. 6
      cache/src/main/kotlin/org/openrs2/cache/FlatFileStore.kt
  95. 3
      cache/src/main/kotlin/org/openrs2/cache/JagArchive.kt
  96. 62
      cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt
  97. 1
      cache/src/main/kotlin/org/openrs2/cache/Js5CompressionType.kt
  98. 3
      cache/src/main/kotlin/org/openrs2/cache/Js5Index.kt
  99. 7
      cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt
  100. 8
      cache/src/main/kotlin/org/openrs2/cache/Js5Pack.kt
  101. Some files were not shown because too many files have changed in this diff Show More

@ -15,15 +15,11 @@ indent_style = space
indent_size = 4
# see https://github.com/pinterest/ktlint/issues/764
# noinspection EditorConfigKeyCorrectness
disabled_rules = argument-list-wrapping, parameter-list-wrapping, wrapping
disabled_rules = indent, parameter-list-wrapping
[*.md]
max_line_length = 80
[*.sql]
indent_style = space
indent_size = 4
# @formatter:off
[*.{json,xml,yaml,yml}]
# @formatter:on

@ -1,8 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="AstDeobfuscator" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.openrs2.deob.ast.DeobfuscateAstCommandKt" />
<module name="openrs2.deob-ast.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="org.openrs2.deob.ast.DeobfuscateAstCommandKt" />
<option name="WORKING_DIRECTORY" value="" />
<method v="2">
<option name="Make" enabled="true" />
</method>

@ -1,8 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="BytecodeDeobfuscator" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.openrs2.deob.bytecode.DeobfuscateBytecodeCommandKt" />
<module name="openrs2.deob-bytecode.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="org.openrs2.deob.bytecode.DeobfuscateBytecodeCommandKt" />
<option name="WORKING_DIRECTORY" value="" />
<method v="2">
<option name="Make" enabled="true" />
</method>

@ -1,9 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Decompiler" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.openrs2.decompiler.DecompileCommandKt" />
<module name="openrs2.decompiler.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="-Xmx3G" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="org.openrs2.decompiler.DecompileCommandKt" />
<option name="WORKING_DIRECTORY" value="" />
<method v="2">
<option name="Make" enabled="true" />
</method>

@ -1,9 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Deobfuscator" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.openrs2.deob.DeobfuscateCommandKt" />
<module name="openrs2.deob.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="-Xmx3G" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="org.openrs2.deob.DeobfuscateCommandKt" />
<option name="WORKING_DIRECTORY" value="" />
<method v="2">
<option name="Make" enabled="true" />
</method>

@ -1,9 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="GameServer" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.openrs2.game.GameCommandKt" />
<module name="openrs2.game.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="-Dio.netty.leakDetection.level=PARANOID" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="org.openrs2.game.GameCommandKt" />
<option name="WORKING_DIRECTORY" value="" />
<method v="2">
<option name="Make" enabled="true" />
</method>

@ -1,8 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="GenerateBuffer" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.openrs2.buffer.generator.GenerateBufferCommandKt" />
<module name="openrs2.buffer-generator.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="org.openrs2.buffer.generator.GenerateBufferCommandKt" />
<option name="WORKING_DIRECTORY" value="" />
<method v="2">
<option name="Make" enabled="true" />
</method>

@ -1,8 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Patcher" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.openrs2.patcher.PatchCommandKt" />
<module name="openrs2.patcher.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="org.openrs2.patcher.PatchCommandKt" />
<option name="WORKING_DIRECTORY" value="" />
<method v="2">
<option name="Make" enabled="true" />
</method>

@ -1,4 +1,4 @@
Copyright (c) 2019-2023 OpenRS2 Authors
Copyright (c) 2019-2022 OpenRS2 Authors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above

@ -76,6 +76,12 @@ OpenRS2 is available under the terms of the [ISC license][isc], which is similar
to the 2-clause BSD license. The full copyright notice and terms are available
in the `LICENSE` file.
The `compress-bzip2` module is derived from the reference [bzip2][bzip2]
implementation. The reference implementation's license is available in
`compress-bzip2/LICENSE`. Both this license and OpenRS2's license apply to the
derived work.
[bzip2]: https://sourceware.org/bzip2/
[discord-badge]: https://img.shields.io/discord/684495254145335298
[discord]: https://chat.openrs2.org/
[drone-badge]: https://build.openrs2.org/api/badges/openrs2/openrs2/status.svg

@ -17,7 +17,6 @@ application {
dependencies {
implementation(projects.archive)
implementation(projects.bufferGenerator)
implementation(projects.cacheCli)
implementation(projects.compressCli)
implementation(projects.crc32)
implementation(projects.deob)
@ -30,8 +29,6 @@ tasks.shadowJar {
archiveFileName.set("openrs2.jar")
minimize {
exclude(dependency("ch.qos.logback:logback-classic"))
exclude(dependency("com.github.jnr:jnr-ffi"))
exclude(dependency("org.flywaydb:flyway-core"))
exclude(dependency("org.jetbrains.kotlin:kotlin-reflect"))
}

@ -4,7 +4,6 @@ import com.github.ajalt.clikt.core.NoOpCliktCommand
import com.github.ajalt.clikt.core.subcommands
import org.openrs2.archive.ArchiveCommand
import org.openrs2.buffer.generator.GenerateBufferCommand
import org.openrs2.cache.cli.CacheCommand
import org.openrs2.compress.cli.CompressCommand
import org.openrs2.crc32.Crc32Command
import org.openrs2.deob.DeobfuscateCommand
@ -17,7 +16,6 @@ public class Command : NoOpCliktCommand(name = "openrs2") {
init {
subcommands(
ArchiveCommand(),
CacheCommand(),
CompressCommand(),
Crc32Command(),
DeobfuscateCommand(),

@ -9,7 +9,6 @@ application {
}
dependencies {
api(libs.bundles.guice)
api(libs.clikt)
implementation(projects.buffer)

@ -5,8 +5,9 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.google.inject.AbstractModule
import com.google.inject.Scopes
import com.google.inject.multibindings.Multibinder
import org.openrs2.archive.key.HdosKeyDownloader
import org.openrs2.archive.key.KeyDownloader
import org.openrs2.archive.key.OpenOsrsKeyDownloader
import org.openrs2.archive.key.PolarKeyDownloader
import org.openrs2.archive.key.RuneLiteKeyDownloader
import org.openrs2.archive.name.NameDownloader
import org.openrs2.archive.name.RuneStarNameDownloader
@ -40,13 +41,14 @@ public object ArchiveModule : AbstractModule() {
.toProvider(DatabaseProvider::class.java)
.`in`(Scopes.SINGLETON)
Multibinder.newSetBinder(binder(), Module::class.java)
.addBinding().to(JavaTimeModule::class.java)
val keyBinder = Multibinder.newSetBinder(binder(), KeyDownloader::class.java)
keyBinder.addBinding().to(HdosKeyDownloader::class.java)
keyBinder.addBinding().to(OpenOsrsKeyDownloader::class.java)
keyBinder.addBinding().to(PolarKeyDownloader::class.java)
keyBinder.addBinding().to(RuneLiteKeyDownloader::class.java)
val moduleBinder = Multibinder.newSetBinder(binder(), Module::class.java)
moduleBinder.addBinding().to(JavaTimeModule::class.java)
val nameBinder = Multibinder.newSetBinder(binder(), NameDownloader::class.java)
nameBinder.addBinding().to(RuneStarNameDownloader::class.java)
}

@ -2,14 +2,11 @@ package org.openrs2.archive.cache
import com.github.ajalt.clikt.core.NoOpCliktCommand
import com.github.ajalt.clikt.core.subcommands
import org.openrs2.archive.cache.finder.ExtractCommand
public class CacheCommand : NoOpCliktCommand(name = "cache") {
init {
subcommands(
CrossPollinateCommand(),
DownloadCommand(),
ExtractCommand(),
ImportCommand(),
ImportMasterIndexCommand(),
ExportCommand(),

@ -30,7 +30,7 @@ public class CacheDownloader @Inject constructor(
val group = bootstrapFactory.createEventLoopGroup()
try {
suspendCoroutine { continuation ->
suspendCoroutine<Unit> { continuation ->
val bootstrap = bootstrapFactory.createBootstrap(group)
val hostname: String
@ -43,36 +43,29 @@ public class CacheDownloader @Inject constructor(
OsrsJs5ChannelInitializer(
OsrsJs5ChannelHandler(
bootstrap,
game.scopeId,
game.id,
hostname,
PORT,
buildMajor,
game.lastMasterIndexId,
continuation,
importer
importer,
)
)
}
"runescape" -> {
val buildMinor = game.buildMinor ?: throw Exception("Current minor build not set")
val tokens = config.params.values.filter { TOKEN_REGEX.matches(it) }
val token = tokens.singleOrNull() ?: throw Exception("Multiple candidate tokens: $tokens")
hostname = if (environment == "beta") {
NXT_BETA_HOSTNAME
} else {
NXT_LIVE_HOSTNAME
}
hostname = NXT_HOSTNAME
val musicStreamClient = MusicStreamClient(client, byteBufBodyHandler, "http://$hostname")
NxtJs5ChannelInitializer(
NxtJs5ChannelHandler(
bootstrap,
game.scopeId,
game.id,
hostname,
PORT,
@ -87,7 +80,6 @@ public class CacheDownloader @Inject constructor(
)
)
}
else -> throw UnsupportedOperationException()
}
@ -101,8 +93,7 @@ public class CacheDownloader @Inject constructor(
private companion object {
private const val CODEBASE = "codebase"
private const val NXT_LIVE_HOSTNAME = "content.runescape.com"
private const val NXT_BETA_HOSTNAME = "content.beta.runescape.com"
private const val NXT_HOSTNAME = "content.runescape.com"
private const val PORT = 443
private val TOKEN_REGEX = Regex("[A-Za-z0-9*-]{32}")
}

@ -3,7 +3,6 @@ package org.openrs2.archive.cache
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonUnwrapped
import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufAllocator
import io.netty.buffer.Unpooled
import org.openrs2.buffer.use
@ -19,8 +18,6 @@ import org.openrs2.db.Database
import org.postgresql.util.PGobject
import java.sql.Connection
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.SortedSet
import javax.inject.Inject
import javax.inject.Singleton
@ -77,51 +74,6 @@ public class CacheExporter @Inject constructor(
public val diskStoreValid: Boolean = blocks <= DiskStore.MAX_BLOCK
}
public data class Archive(
val resolved: Boolean,
val stats: ArchiveStats?
)
public data class ArchiveStats(
val validGroups: Long,
val groups: Long,
val validKeys: Long,
val keys: Long,
val size: Long,
val blocks: Long
) {
public val allGroupsValid: Boolean = groups == validGroups
public val validGroupsFraction: Double = if (groups == 0L) {
1.0
} else {
validGroups.toDouble() / groups
}
public val allKeysValid: Boolean = keys == validKeys
public val validKeysFraction: Double = if (keys == 0L) {
1.0
} else {
validKeys.toDouble() / keys
}
}
public data class IndexStats(
val validFiles: Long,
val files: Long,
val size: Long,
val blocks: Long
) {
public val allFilesValid: Boolean = files == validFiles
public val validFilesFraction: Double = if (files == 0L) {
1.0
} else {
validFiles.toDouble() / files
}
}
public data class Build(val major: Int, val minor: Int?) : Comparable<Build> {
override fun compareTo(other: Build): Int {
return compareValuesBy(this, other, Build::major, Build::minor)
@ -157,7 +109,6 @@ public class CacheExporter @Inject constructor(
public data class CacheSummary(
val id: Int,
val scope: String,
val game: String,
val environment: String,
val language: String,
@ -173,8 +124,6 @@ public class CacheExporter @Inject constructor(
val sources: List<Source>,
val updates: List<String>,
val stats: Stats?,
val archives: List<Archive>,
val indexes: List<IndexStats>?,
val masterIndex: Js5MasterIndex?,
val checksumTable: ChecksumTable?
)
@ -199,25 +148,6 @@ public class CacheExporter @Inject constructor(
val key: XteaKey
)
public suspend fun totalSize(): Long {
return database.execute { connection ->
connection.prepareStatement(
"""
SELECT SUM(size)
FROM cache_stats
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
if (rows.next()) {
rows.getLong(1)
} else {
0
}
}
}
}
}
public suspend fun list(): List<CacheSummary> {
return database.execute { connection ->
connection.prepareStatement(
@ -227,7 +157,6 @@ public class CacheExporter @Inject constructor(
SELECT
c.id,
g.name AS game,
sc.name AS scope,
e.name AS environment,
l.iso_code AS language,
array_remove(array_agg(DISTINCT ROW(s.build_major, s.build_minor)::build ORDER BY ROW(s.build_major, s.build_minor)::build ASC), NULL) builds,
@ -245,16 +174,14 @@ public class CacheExporter @Inject constructor(
JOIN sources s ON s.cache_id = c.id
JOIN game_variants v ON v.id = s.game_id
JOIN games g ON g.id = v.game_id
JOIN scopes sc ON sc.id = g.scope_id
JOIN environments e ON e.id = v.environment_id
JOIN languages l ON l.id = v.language_id
LEFT JOIN cache_stats cs ON cs.scope_id = sc.id AND cs.cache_id = c.id
WHERE NOT c.hidden
GROUP BY sc.name, c.id, g.name, e.name, l.iso_code, cs.valid_indexes, cs.indexes, cs.valid_groups,
cs.groups, cs.valid_keys, cs.keys, cs.size, cs.blocks
LEFT JOIN cache_stats cs ON cs.cache_id = c.id
GROUP BY c.id, g.name, e.name, l.iso_code, cs.valid_indexes, cs.indexes, cs.valid_groups, cs.groups,
cs.valid_keys, cs.keys, cs.size, cs.blocks
) t
ORDER BY t.game ASC, t.environment ASC, t.language ASC, t.builds[1] ASC, t.timestamp ASC
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
val caches = mutableListOf<CacheSummary>()
@ -262,24 +189,21 @@ public class CacheExporter @Inject constructor(
while (rows.next()) {
val id = rows.getInt(1)
val game = rows.getString(2)
val scope = rows.getString(3)
val environment = rows.getString(4)
val language = rows.getString(5)
val builds = rows.getArray(6).array as Array<*>
val timestamp = rows.getTimestamp(7)?.toInstant()
@Suppress("UNCHECKED_CAST")
val sources = rows.getArray(8).array as Array<String>
val environment = rows.getString(3)
val language = rows.getString(4)
val builds = rows.getArray(5).array as Array<*>
val timestamp = rows.getTimestamp(6)?.toInstant()
@Suppress("UNCHECKED_CAST") val sources = rows.getArray(7).array as Array<String>
val validIndexes = rows.getLong(9)
val validIndexes = rows.getLong(8)
val stats = if (!rows.wasNull()) {
val indexes = rows.getLong(10)
val validGroups = rows.getLong(11)
val groups = rows.getLong(12)
val validKeys = rows.getLong(13)
val keys = rows.getLong(14)
val size = rows.getLong(15)
val blocks = rows.getLong(16)
val indexes = rows.getLong(9)
val validGroups = rows.getLong(10)
val groups = rows.getLong(11)
val validKeys = rows.getLong(12)
val keys = rows.getLong(13)
val size = rows.getLong(14)
val blocks = rows.getLong(15)
Stats(validIndexes, indexes, validGroups, groups, validKeys, keys, size, blocks)
} else {
null
@ -287,7 +211,6 @@ public class CacheExporter @Inject constructor(
caches += CacheSummary(
id,
scope,
game,
environment,
language,
@ -304,7 +227,7 @@ public class CacheExporter @Inject constructor(
}
}
public suspend fun get(scope: String, id: Int): Cache? {
public suspend fun get(id: Int): Cache? {
return database.execute { connection ->
val masterIndex: Js5MasterIndex?
val checksumTable: ChecksumTable?
@ -325,17 +248,15 @@ public class CacheExporter @Inject constructor(
cs.size,
cs.blocks
FROM caches c
CROSS JOIN scopes s
LEFT JOIN master_indexes m ON m.id = c.id
LEFT JOIN containers mc ON mc.id = m.container_id
LEFT JOIN crc_tables t ON t.id = c.id
LEFT JOIN blobs b ON b.id = t.blob_id
LEFT JOIN cache_stats cs ON cs.scope_id = s.id AND cs.cache_id = c.id
WHERE s.name = ? AND c.id = ?
""".trimIndent()
LEFT JOIN cache_stats cs ON cs.cache_id = c.id
WHERE c.id = ?
""".trimIndent()
).use { stmt ->
stmt.setString(1, scope)
stmt.setInt(2, id)
stmt.setInt(1, id)
stmt.executeQuery().use { rows ->
if (!rows.next()) {
@ -387,15 +308,13 @@ public class CacheExporter @Inject constructor(
FROM sources s
JOIN game_variants v ON v.id = s.game_id
JOIN games g ON g.id = v.game_id
JOIN scopes sc ON sc.id = g.scope_id
JOIN environments e ON e.id = v.environment_id
JOIN languages l ON l.id = v.language_id
WHERE sc.name = ? AND s.cache_id = ?
WHERE s.cache_id = ?
ORDER BY s.name ASC
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setString(1, scope)
stmt.setInt(2, id)
stmt.setInt(1, id)
stmt.executeQuery().use { rows ->
while (rows.next()) {
@ -436,7 +355,7 @@ public class CacheExporter @Inject constructor(
SELECT url
FROM updates
WHERE cache_id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, id)
@ -447,208 +366,18 @@ public class CacheExporter @Inject constructor(
}
}
val archives = mutableListOf<Archive>()
connection.prepareStatement(
"""
SELECT a.archive_id, c.id IS NOT NULL, s.valid_groups, s.groups, s.valid_keys, s.keys, s.size, s.blocks
FROM master_index_archives a
LEFT JOIN resolve_index((SELECT id FROM scopes WHERE name = ?), a.archive_id, a.crc32, a.version) c ON TRUE
LEFT JOIN index_stats s ON s.container_id = c.id
WHERE a.master_index_id = ?
UNION ALL
SELECT a.archive_id, b.id IS NOT NULL, NULL, NULL, NULL, NULL, length(b.data), group_blocks(a.archive_id, length(b.data))
FROM crc_table_archives a
LEFT JOIN resolve_archive(a.archive_id, a.crc32) b ON TRUE
WHERE a.crc_table_id = ?
ORDER BY archive_id ASC
""".trimIndent()
).use { stmt ->
stmt.setString(1, scope)
stmt.setInt(2, id)
stmt.setInt(3, id)
stmt.executeQuery().use { rows ->
while (rows.next()) {
val resolved = rows.getBoolean(2)
val size = rows.getLong(7)
val archiveStats = if (!rows.wasNull()) {
val validGroups = rows.getLong(3)
val groups = rows.getLong(4)
val validKeys = rows.getLong(5)
val keys = rows.getLong(6)
val blocks = rows.getLong(8)
ArchiveStats(validGroups, groups, validKeys, keys, size, blocks)
} else {
null
}
archives += Archive(resolved, archiveStats)
}
}
}
val indexes = if (checksumTable != null && archives[5].resolved) {
connection.prepareStatement(
"""
SELECT s.valid_files, s.files, s.size, s.blocks
FROM crc_table_archives a
JOIN resolve_archive(a.archive_id, a.crc32) b ON TRUE
JOIN version_list_stats s ON s.blob_id = b.id
WHERE a.crc_table_id = ? AND a.archive_id = 5
ORDER BY s.index_id ASC
""".trimIndent()
).use { stmt ->
stmt.setInt(1, id)
stmt.executeQuery().use { rows ->
val indexes = mutableListOf<IndexStats>()
while (rows.next()) {
val validFiles = rows.getLong(1)
val files = rows.getLong(2)
val size = rows.getLong(3)
val blocks = rows.getLong(4)
indexes += IndexStats(validFiles, files, size, blocks)
}
indexes
}
}
} else {
null
}
Cache(id, sources, updates, stats, archives, indexes, masterIndex, checksumTable)
Cache(id, sources, updates, stats, masterIndex, checksumTable)
}
}
public suspend fun getFileName(scope: String, id: Int): String? {
return database.execute { connection ->
// TODO(gpe): what if a cache is from multiple games?
connection.prepareStatement(
"""
SELECT
g.name AS game,
e.name AS environment,
l.iso_code AS language,
array_remove(array_agg(DISTINCT ROW(s.build_major, s.build_minor)::build ORDER BY ROW(s.build_major, s.build_minor)::build ASC), NULL) builds,
MIN(s.timestamp) AS timestamp
FROM sources s
JOIN game_variants v ON v.id = s.game_id
JOIN games g ON g.id = v.game_id
JOIN scopes sc ON sc.id = g.scope_id
JOIN environments e ON e.id = v.environment_id
JOIN languages l ON l.id = v.language_id
WHERE sc.name = ? AND s.cache_id = ?
GROUP BY g.name, e.name, l.iso_code
LIMIT 1
""".trimIndent()
).use { stmt ->
stmt.setString(1, scope)
stmt.setInt(2, id)
stmt.executeQuery().use { rows ->
if (!rows.next()) {
return@execute null
}
val game = rows.getString(1)
val environment = rows.getString(2)
val language = rows.getString(3)
val name = StringBuilder("$game-$environment-$language")
val builds = rows.getArray(4).array as Array<*>
for (build in builds.mapNotNull { o -> Build.fromPgObject(o as PGobject) }.toSortedSet()) {
name.append("-b")
name.append(build)
}
val timestamp = rows.getTimestamp(5)
if (!rows.wasNull()) {
name.append('-')
name.append(
timestamp.toInstant()
.atOffset(ZoneOffset.UTC)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"))
)
}
name.append("-openrs2#")
name.append(id)
name.toString()
}
}
}
}
public suspend fun exportGroup(scope: String, id: Int, archive: Int, group: Int): ByteBuf? {
return database.execute { connection ->
if (archive == Store.ARCHIVESET && group == Store.ARCHIVESET) {
connection.prepareStatement(
"""
SELECT c.data
FROM master_indexes m
JOIN containers c ON c.id = m.container_id
WHERE m.id = ?
""".trimIndent()
).use { stmt ->
stmt.setInt(1, id)
stmt.executeQuery().use { rows ->
if (rows.next()) {
val data = rows.getBytes(1)
return@execute Unpooled.wrappedBuffer(data)
}
}
}
}
connection.prepareStatement(
"""
SELECT g.data
FROM resolved_groups g
JOIN scopes s ON s.id = g.scope_id
WHERE s.name = ? AND g.master_index_id = ? AND g.archive_id = ? AND g.group_id = ?
UNION ALL
SELECT f.data
FROM resolved_files f
WHERE f.crc_table_id = ? AND f.index_id = ? AND f.file_id = ?
""".trimIndent()
).use { stmt ->
stmt.setString(1, scope)
stmt.setInt(2, id)
stmt.setInt(3, archive)
stmt.setInt(4, group)
stmt.setInt(5, id)
stmt.setInt(6, archive)
stmt.setInt(7, group)
stmt.executeQuery().use { rows ->
if (!rows.next()) {
return@execute null
}
val data = rows.getBytes(1)
return@execute Unpooled.wrappedBuffer(data)
}
}
}
}
public fun export(scope: String, id: Int, storeFactory: (Boolean) -> Store) {
public fun export(id: Int, storeFactory: (Boolean) -> Store) {
database.executeOnce { connection ->
val legacy = connection.prepareStatement(
"""
SELECT id
FROM crc_tables
WHERE id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, id)
@ -661,24 +390,22 @@ public class CacheExporter @Inject constructor(
if (legacy) {
exportLegacy(connection, id, store)
} else {
export(connection, scope, id, store)
export(connection, id, store)
}
}
}
}
private fun export(connection: Connection, scope: String, id: Int, store: Store) {
private fun export(connection: Connection, id: Int, store: Store) {
connection.prepareStatement(
"""
SELECT g.archive_id, g.group_id, g.data, g.version
FROM resolved_groups g
JOIN scopes s ON s.id = g.scope_id
WHERE s.name = ? AND g.master_index_id = ?
""".trimIndent()
SELECT archive_id, group_id, data, version
FROM resolved_groups
WHERE master_index_id = ?
""".trimIndent()
).use { stmt ->
stmt.fetchSize = BATCH_SIZE
stmt.setString(1, scope)
stmt.setInt(2, id)
stmt.setInt(1, id)
stmt.executeQuery().use { rows ->
alloc.buffer(2, 2).use { versionBuf ->
@ -716,7 +443,7 @@ public class CacheExporter @Inject constructor(
SELECT index_id, file_id, data, version
FROM resolved_files
WHERE crc_table_id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.fetchSize = BATCH_SIZE
stmt.setInt(1, id)
@ -746,20 +473,18 @@ public class CacheExporter @Inject constructor(
}
}
public suspend fun exportKeys(scope: String, id: Int): List<Key> {
public suspend fun exportKeys(id: Int): List<Key> {
return database.execute { connection ->
connection.prepareStatement(
"""
SELECT g.archive_id, g.group_id, g.name_hash, n.name, (k.key).k0, (k.key).k1, (k.key).k2, (k.key).k3
FROM resolved_groups g
JOIN scopes s ON s.id = g.scope_id
JOIN keys k ON k.id = g.key_id
LEFT JOIN names n ON n.hash = g.name_hash AND n.name ~ '^l(?:[0-9]|[1-9][0-9])_(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
WHERE s.name = ? AND g.master_index_id = ?
""".trimIndent()
WHERE g.master_index_id = ?
""".trimIndent()
).use { stmt ->
stmt.setString(1, scope)
stmt.setInt(2, id)
stmt.setInt(1, id)
stmt.executeQuery().use { rows ->
val keys = mutableListOf<Key>()

@ -105,10 +105,9 @@ public class CacheImporter @Inject constructor(
public val version: Int
) : Blob(buf)
internal enum class SourceType {
private enum class SourceType {
DISK,
JS5REMOTE,
CROSS_POLLINATION
JS5REMOTE
}
public data class MasterIndexResult(
@ -117,14 +116,9 @@ public class CacheImporter @Inject constructor(
val indexes: List<ByteBuf?>
)
private data class Game(
val id: Int,
val scopeId: Int
)
public suspend fun import(
store: Store,
gameName: String,
game: String,
environment: String,
language: String,
buildMajor: Int?,
@ -137,12 +131,12 @@ public class CacheImporter @Inject constructor(
database.execute { connection ->
prepare(connection)
val game = getGame(connection, gameName, environment, language)
val gameId = getGameId(connection, game, environment, language)
if (store is DiskStore && store.legacy) {
importLegacy(connection, store, game.id, buildMajor, buildMinor, timestamp, name, description, url)
importLegacy(connection, store, gameId, buildMajor, buildMinor, timestamp, name, description, url)
} else {
importJs5(connection, store, game, buildMajor, buildMinor, timestamp, name, description, url)
importJs5(connection, store, gameId, buildMajor, buildMinor, timestamp, name, description, url)
}
}
}
@ -150,7 +144,7 @@ public class CacheImporter @Inject constructor(
private fun importJs5(
connection: Connection,
store: Store,
game: Game,
gameId: Int,
buildMajor: Int?,
buildMinor: Int?,
timestamp: Instant?,
@ -175,7 +169,7 @@ public class CacheImporter @Inject constructor(
connection,
SourceType.DISK,
masterIndexId,
game.id,
gameId,
buildMajor,
buildMinor,
timestamp,
@ -200,7 +194,7 @@ public class CacheImporter @Inject constructor(
}
for (index in indexGroups) {
addIndex(connection, game.scopeId, sourceId, index)
addIndex(connection, sourceId, index)
}
} finally {
indexGroups.forEach(Index::release)
@ -221,7 +215,7 @@ public class CacheImporter @Inject constructor(
groups += group
if (groups.size >= BATCH_SIZE) {
addGroups(connection, game.scopeId, sourceId, groups)
addGroups(connection, sourceId, groups)
groups.forEach(Group::release)
groups.clear()
@ -230,7 +224,7 @@ public class CacheImporter @Inject constructor(
}
if (groups.isNotEmpty()) {
addGroups(connection, game.scopeId, sourceId, groups)
addGroups(connection, sourceId, groups)
}
} finally {
groups.forEach(Group::release)
@ -240,7 +234,7 @@ public class CacheImporter @Inject constructor(
public suspend fun importMasterIndex(
buf: ByteBuf,
format: MasterIndexFormat,
gameName: String,
game: String,
environment: String,
language: String,
buildMajor: Int?,
@ -260,14 +254,14 @@ public class CacheImporter @Inject constructor(
database.execute { connection ->
prepare(connection)
val game = getGame(connection, gameName, environment, language)
val gameId = getGameId(connection, game, environment, language)
val masterIndexId = addMasterIndex(connection, masterIndex)
addSource(
connection,
SourceType.DISK,
masterIndexId,
game.id,
gameId,
buildMajor,
buildMinor,
timestamp,
@ -284,7 +278,6 @@ public class CacheImporter @Inject constructor(
buf: ByteBuf,
uncompressed: ByteBuf,
gameId: Int,
scopeId: Int,
buildMajor: Int,
buildMinor: Int?,
lastId: Int?,
@ -298,7 +291,7 @@ public class CacheImporter @Inject constructor(
UPDATE game_variants
SET build_major = ?, build_minor = ?
WHERE id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, buildMajor)
stmt.setObject(2, buildMinor, Types.INTEGER)
@ -339,14 +332,13 @@ public class CacheImporter @Inject constructor(
FROM master_index_archives a
LEFT JOIN master_index_archives a2 ON a2.master_index_id = ? AND a2.archive_id = a.archive_id AND
a2.crc32 = a.crc32 AND a2.version = a.version
LEFT JOIN resolve_index(?, a2.archive_id, a2.crc32, a2.version) c ON TRUE
LEFT JOIN resolve_index(a2.archive_id, a2.crc32, a2.version) c ON TRUE
WHERE a.master_index_id = ?
ORDER BY a.archive_id ASC
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setObject(1, lastId, Types.INTEGER)
stmt.setInt(2, scopeId)
stmt.setInt(3, masterIndexId)
stmt.setInt(2, masterIndexId)
stmt.executeQuery().use { rows ->
val indexes = mutableListOf<ByteBuf?>()
@ -371,7 +363,6 @@ public class CacheImporter @Inject constructor(
}
public suspend fun importIndexAndGetMissingGroups(
scopeId: Int,
sourceId: Int,
archive: Int,
index: Js5Index,
@ -381,7 +372,7 @@ public class CacheImporter @Inject constructor(
): List<Int> {
return database.execute { connection ->
prepare(connection)
val id = addIndex(connection, scopeId, sourceId, Index(archive, index, buf, uncompressed))
val id = addIndex(connection, sourceId, Index(archive, index, buf, uncompressed))
/*
* In order to defend against (crc32, version) collisions, we only
@ -399,18 +390,17 @@ public class CacheImporter @Inject constructor(
SELECT ig.group_id
FROM index_groups ig
LEFT JOIN resolved_indexes i ON i.master_index_id = ? AND
i.archive_id = ? AND i.scope_id = ?
i.archive_id = ?
LEFT JOIN index_groups ig2 ON ig2.container_id = i.container_id AND ig2.group_id = ig.group_id AND
ig2.crc32 = ig.crc32 AND ig2.version = ig.version
LEFT JOIN resolve_group(i.scope_id, i.archive_id, ig2.group_id, ig2.crc32, ig2.version) c ON TRUE
LEFT JOIN resolve_group(i.archive_id, ig2.group_id, ig2.crc32, ig2.version) c ON TRUE
WHERE ig.container_id = ? AND c.id IS NULL
ORDER BY ig.group_id ASC
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setObject(1, lastMasterIndexId, Types.INTEGER)
stmt.setInt(2, archive)
stmt.setInt(3, scopeId)
stmt.setLong(4, id)
stmt.setLong(3, id)
stmt.executeQuery().use { rows ->
val groups = mutableListOf<Int>()
@ -425,14 +415,14 @@ public class CacheImporter @Inject constructor(
}
}
public suspend fun importGroups(scopeId: Int, sourceId: Int, groups: List<Group>) {
public suspend fun importGroups(sourceId: Int, groups: List<Group>) {
if (groups.isEmpty()) {
return
}
database.execute { connection ->
prepare(connection)
addGroups(connection, scopeId, sourceId, groups)
addGroups(connection, sourceId, groups)
}
}
@ -459,7 +449,7 @@ public class CacheImporter @Inject constructor(
SELECT id
FROM master_indexes
WHERE container_id = ? AND format = ?::master_index_format
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setLong(1, containerId)
stmt.setString(2, masterIndex.index.format.name.lowercase())
@ -478,7 +468,7 @@ public class CacheImporter @Inject constructor(
INSERT INTO caches (id)
VALUES (DEFAULT)
RETURNING id
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
check(rows.next())
@ -490,7 +480,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO master_indexes (id, container_id, format)
VALUES (?, ?, ?::master_index_format)
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, masterIndexId)
stmt.setLong(2, containerId)
@ -505,7 +495,7 @@ public class CacheImporter @Inject constructor(
master_index_id, archive_id, crc32, version, whirlpool, groups, total_uncompressed_length
)
VALUES (?, ?, ?, ?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, entry) in masterIndex.index.entries.withIndex()) {
stmt.setInt(1, masterIndexId)
@ -541,11 +531,11 @@ public class CacheImporter @Inject constructor(
return masterIndexId
}
internal fun addSource(
private fun addSource(
connection: Connection,
type: SourceType,
cacheId: Int?,
gameId: Int?,
cacheId: Int,
gameId: Int,
buildMajor: Int?,
buildMinor: Int?,
timestamp: Instant?,
@ -553,29 +543,13 @@ public class CacheImporter @Inject constructor(
description: String?,
url: String?
): Int {
if (type == SourceType.CROSS_POLLINATION) {
connection.prepareStatement(
"""
SELECT id
FROM sources
WHERE type = 'cross_pollination'
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
if (rows.next()) {
return rows.getInt(1)
}
}
}
}
if (type == SourceType.JS5REMOTE && cacheId != null && gameId != null && buildMajor != null) {
if (type == SourceType.JS5REMOTE && buildMajor != null) {
connection.prepareStatement(
"""
SELECT id
FROM sources
WHERE type = 'js5remote' AND cache_id = ? AND game_id = ? AND build_major = ? AND build_minor IS NOT DISTINCT FROM ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, cacheId)
stmt.setInt(2, gameId)
@ -595,11 +569,11 @@ public class CacheImporter @Inject constructor(
INSERT INTO sources (type, cache_id, game_id, build_major, build_minor, timestamp, name, description, url)
VALUES (?::source_type, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setString(1, type.toString().lowercase())
stmt.setObject(2, cacheId, Types.INTEGER)
stmt.setObject(3, gameId, Types.INTEGER)
stmt.setInt(2, cacheId)
stmt.setInt(3, gameId)
stmt.setObject(4, buildMajor, Types.INTEGER)
stmt.setObject(5, buildMinor, Types.INTEGER)
@ -653,23 +627,22 @@ public class CacheImporter @Inject constructor(
}
}
internal fun addGroups(connection: Connection, scopeId: Int, sourceId: Int, groups: List<Group>): List<Long> {
private fun addGroups(connection: Connection, sourceId: Int, groups: List<Group>): List<Long> {
val containerIds = addContainers(connection, groups)
connection.prepareStatement(
"""
INSERT INTO groups (scope_id, archive_id, group_id, version, version_truncated, container_id)
VALUES (?, ?, ?, ?, ?, ?)
INSERT INTO groups (archive_id, group_id, version, version_truncated, container_id)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, group) in groups.withIndex()) {
stmt.setInt(1, scopeId)
stmt.setInt(2, group.archive)
stmt.setInt(3, group.group)
stmt.setInt(4, group.version)
stmt.setBoolean(5, group.versionTruncated)
stmt.setLong(6, containerIds[i])
stmt.setInt(1, group.archive)
stmt.setInt(2, group.group)
stmt.setInt(3, group.version)
stmt.setBoolean(4, group.versionTruncated)
stmt.setLong(5, containerIds[i])
stmt.addBatch()
}
@ -681,7 +654,7 @@ public class CacheImporter @Inject constructor(
INSERT INTO source_groups (source_id, archive_id, group_id, version, version_truncated, container_id)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, group) in groups.withIndex()) {
stmt.setInt(1, sourceId)
@ -699,8 +672,8 @@ public class CacheImporter @Inject constructor(
return containerIds
}
private fun addGroup(connection: Connection, scopeId: Int, sourceId: Int, group: Group): Long {
return addGroups(connection, scopeId, sourceId, listOf(group)).single()
private fun addGroup(connection: Connection, sourceId: Int, group: Group): Long {
return addGroups(connection, sourceId, listOf(group)).single()
}
private fun readIndex(store: Store, archive: Int): Index {
@ -711,8 +684,8 @@ public class CacheImporter @Inject constructor(
}
}
private fun addIndex(connection: Connection, scopeId: Int, sourceId: Int, index: Index): Long {
val containerId = addGroup(connection, scopeId, sourceId, index)
private fun addIndex(connection: Connection, sourceId: Int, index: Index): Long {
val containerId = addGroup(connection, sourceId, index)
val savepoint = connection.setSavepoint()
connection.prepareStatement(
@ -721,7 +694,7 @@ public class CacheImporter @Inject constructor(
container_id, protocol, version, has_names, has_digests, has_lengths, has_uncompressed_checksums
)
VALUES (?, ?, ?, ?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setLong(1, containerId)
stmt.setInt(2, index.index.protocol.id)
@ -749,7 +722,7 @@ public class CacheImporter @Inject constructor(
uncompressed_crc32
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for (group in index.index) {
stmt.setLong(1, containerId)
@ -788,7 +761,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO index_files (container_id, group_id, file_id, name_hash)
VALUES (?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for (group in index.index) {
for (file in group) {
@ -812,21 +785,10 @@ public class CacheImporter @Inject constructor(
return containerId
}
internal fun prepare(connection: Connection) {
private fun prepare(connection: Connection) {
connection.prepareStatement(
"""
LOCK TABLE containers IN EXCLUSIVE MODE
""".trimIndent()
).use { stmt ->
stmt.execute()
}
connection.prepareStatement(
"""
CREATE TEMPORARY TABLE tmp_container_hashes (
index INTEGER NOT NULL,
whirlpool BYTEA NOT NULL
) ON COMMIT DROP
""".trimIndent()
).use { stmt ->
stmt.execute()
@ -844,7 +806,7 @@ public class CacheImporter @Inject constructor(
encrypted BOOLEAN NOT NULL,
empty_loc BOOLEAN NULL
) ON COMMIT DROP
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -857,7 +819,7 @@ public class CacheImporter @Inject constructor(
whirlpool BYTEA NOT NULL,
data BYTEA NOT NULL
) ON COMMIT DROP
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -870,70 +832,19 @@ public class CacheImporter @Inject constructor(
private fun addContainers(connection: Connection, containers: List<Container>): List<Long> {
connection.prepareStatement(
"""
TRUNCATE TABLE tmp_containers, tmp_container_hashes
""".trimIndent()
).use { stmt ->
stmt.execute()
}
connection.prepareStatement(
"""
INSERT INTO tmp_container_hashes (index, whirlpool)
VALUES (?, ?)
TRUNCATE TABLE tmp_containers
""".trimIndent()
).use { stmt ->
for ((i, container) in containers.withIndex()) {
stmt.setInt(1, i)
stmt.setBytes(2, container.whirlpool)
stmt.addBatch()
}
stmt.executeBatch()
}
val ids = mutableListOf<Long?>()
var count = 0
connection.prepareStatement(
"""
SELECT c.id
FROM tmp_container_hashes t
LEFT JOIN containers c ON c.whirlpool = t.whirlpool
ORDER BY t.index ASC
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
while (rows.next()) {
val id = rows.getLong(1)
if (rows.wasNull()) {
ids += null
} else {
ids += id
count++
}
}
}
}
check(ids.size == containers.size)
if (count == containers.size) {
@Suppress("UNCHECKED_CAST")
return ids as List<Long>
stmt.execute()
}
connection.prepareStatement(
"""
INSERT INTO tmp_containers (index, crc32, whirlpool, data, uncompressed_length, uncompressed_crc32, encrypted, empty_loc)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, container) in containers.withIndex()) {
if (ids[i] != null) {
continue
}
stmt.setInt(1, i)
stmt.setInt(2, container.crc32)
stmt.setBytes(3, container.whirlpool)
@ -962,34 +873,30 @@ public class CacheImporter @Inject constructor(
LEFT JOIN containers c ON c.whirlpool = t.whirlpool
WHERE c.whirlpool IS NULL
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
val ids = mutableListOf<Long>()
connection.prepareStatement(
"""
SELECT t.index, c.id
SELECT c.id
FROM tmp_containers t
JOIN containers c ON c.whirlpool = t.whirlpool
ORDER BY t.index ASC
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
while (rows.next()) {
val index = rows.getInt(1)
val id = rows.getLong(2)
ids[index] = id
count++
ids += rows.getLong(1)
}
}
}
check(count == containers.size)
@Suppress("UNCHECKED_CAST")
return ids as List<Long>
check(ids.size == containers.size)
return ids
}
private fun addBlob(connection: Connection, blob: Blob): Long {
@ -1000,7 +907,7 @@ public class CacheImporter @Inject constructor(
connection.prepareStatement(
"""
TRUNCATE TABLE tmp_blobs
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -1009,7 +916,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO tmp_blobs (index, crc32, whirlpool, data)
VALUES (?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, blob) in blobs.withIndex()) {
stmt.setInt(1, i)
@ -1031,7 +938,7 @@ public class CacheImporter @Inject constructor(
LEFT JOIN blobs b ON b.whirlpool = t.whirlpool
WHERE b.whirlpool IS NULL
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -1044,7 +951,7 @@ public class CacheImporter @Inject constructor(
FROM tmp_blobs t
JOIN blobs b ON b.whirlpool = t.whirlpool
ORDER BY t.index ASC
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
while (rows.next()) {
@ -1057,10 +964,10 @@ public class CacheImporter @Inject constructor(
return ids
}
private fun getGame(connection: Connection, name: String, environment: String, language: String): Game {
private fun getGameId(connection: Connection, name: String, environment: String, language: String): Int {
connection.prepareStatement(
"""
SELECT v.id, g.scope_id
SELECT v.id
FROM game_variants v
JOIN games g ON g.id = v.game_id
JOIN environments e ON e.id = v.environment_id
@ -1077,10 +984,7 @@ public class CacheImporter @Inject constructor(
throw Exception("Game not found")
}
val id = rows.getInt(1)
val scopeId = rows.getInt(2)
return Game(id, scopeId)
return rows.getInt(1)
}
}
}
@ -1090,7 +994,7 @@ public class CacheImporter @Inject constructor(
connection.prepareStatement(
"""
UPDATE game_variants SET last_master_index_id = ? WHERE id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, masterIndexId)
stmt.setInt(2, gameId)
@ -1139,13 +1043,8 @@ public class CacheImporter @Inject constructor(
// import archives and version list
for (id in store.list(0)) {
try {
readArchive(store, id).use { archive ->
addArchive(connection, sourceId, archive)
}
} catch (ex: StoreCorruptException) {
// see the comment in ChecksumTable::create
logger.warn(ex) { "Skipping corrupt archive ($id)" }
readArchive(store, id).use { archive ->
addArchive(connection, sourceId, archive)
}
}
@ -1197,7 +1096,7 @@ public class CacheImporter @Inject constructor(
SELECT id
FROM crc_tables
WHERE blob_id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setLong(1, blobId)
@ -1215,7 +1114,7 @@ public class CacheImporter @Inject constructor(
INSERT INTO caches (id)
VALUES (DEFAULT)
RETURNING id
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
check(rows.next())
@ -1227,7 +1126,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO crc_tables (id, blob_id)
VALUES (?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, checksumTableId)
stmt.setLong(2, blobId)
@ -1239,7 +1138,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO crc_table_archives (crc_table_id, archive_id, crc32)
VALUES (?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, entry) in checksumTable.table.entries.withIndex()) {
stmt.setInt(1, checksumTableId)
@ -1277,7 +1176,7 @@ public class CacheImporter @Inject constructor(
INSERT INTO archives (archive_id, blob_id)
VALUES (?, ?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, archive.id)
stmt.setLong(2, blobId)
@ -1290,7 +1189,7 @@ public class CacheImporter @Inject constructor(
INSERT INTO source_archives (source_id, archive_id, blob_id)
VALUES (?, ?, ?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, sourceId)
stmt.setInt(2, archive.id)
@ -1306,7 +1205,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO version_lists (blob_id)
VALUES (?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
try {
stmt.setLong(1, blobId)
@ -1325,7 +1224,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO version_list_files (blob_id, index_id, file_id, version, crc32)
VALUES (?, ?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((indexId, files) in versionList.files.withIndex()) {
for ((fileId, file) in files.withIndex()) {
@ -1350,7 +1249,7 @@ public class CacheImporter @Inject constructor(
"""
INSERT INTO version_list_maps (blob_id, map_square, map_file_id, loc_file_id, free_to_play)
VALUES (?, ?, ?, ?, ?)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((mapSquare, map) in versionList.maps) {
stmt.setLong(1, blobId)
@ -1381,7 +1280,7 @@ public class CacheImporter @Inject constructor(
}
}
internal fun addFiles(connection: Connection, sourceId: Int, files: List<File>) {
private fun addFiles(connection: Connection, sourceId: Int, files: List<File>) {
val blobIds = addBlobs(connection, files)
connection.prepareStatement(
@ -1389,7 +1288,7 @@ public class CacheImporter @Inject constructor(
INSERT INTO files (index_id, file_id, version, blob_id)
VALUES (?, ?, ?, ?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, file) in files.withIndex()) {
stmt.setInt(1, file.index)
@ -1408,7 +1307,7 @@ public class CacheImporter @Inject constructor(
INSERT INTO source_files (source_id, index_id, file_id, version, blob_id)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
for ((i, file) in files.withIndex()) {
stmt.setInt(1, sourceId)
@ -1426,27 +1325,10 @@ public class CacheImporter @Inject constructor(
public suspend fun refreshViews() {
database.execute { connection ->
connection.prepareStatement(
"""
SELECT pg_try_advisory_lock(0)
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
if (!rows.next()) {
throw IllegalStateException()
}
val locked = rows.getBoolean(1)
if (!locked) {
return@execute
}
}
}
connection.prepareStatement(
"""
REFRESH MATERIALIZED VIEW CONCURRENTLY index_stats
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -1454,7 +1336,7 @@ public class CacheImporter @Inject constructor(
connection.prepareStatement(
"""
REFRESH MATERIALIZED VIEW CONCURRENTLY master_index_stats
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -1462,7 +1344,7 @@ public class CacheImporter @Inject constructor(
connection.prepareStatement(
"""
REFRESH MATERIALIZED VIEW CONCURRENTLY version_list_stats
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -1470,7 +1352,7 @@ public class CacheImporter @Inject constructor(
connection.prepareStatement(
"""
REFRESH MATERIALIZED VIEW CONCURRENTLY crc_table_stats
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}

@ -1,16 +0,0 @@
package org.openrs2.archive.cache
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class CrossPollinateCommand : CliktCommand(name = "cross-pollinate") {
override fun run(): Unit = runBlocking {
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val crossPollinator = injector.getInstance(CrossPollinator::class.java)
crossPollinator.crossPollinate()
}
}
}

@ -1,223 +0,0 @@
package org.openrs2.archive.cache
import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufAllocator
import io.netty.buffer.ByteBufInputStream
import io.netty.buffer.Unpooled
import org.openrs2.buffer.crc32
import org.openrs2.buffer.use
import org.openrs2.cache.Js5Compression
import org.openrs2.cache.Js5CompressionType
import org.openrs2.db.Database
import java.sql.Connection
import java.util.zip.GZIPInputStream
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
public class CrossPollinator @Inject constructor(
private val database: Database,
private val alloc: ByteBufAllocator,
private val importer: CacheImporter
) {
public suspend fun crossPollinate() {
database.execute { connection ->
for ((index, archive) in OLD_TO_NEW_ENGINE) {
crossPollinate(connection, index, archive);
}
}
}
private fun crossPollinate(connection: Connection, index: Int, archive: Int) {
val scopeId: Int
connection.prepareStatement(
"""
SELECT id
FROM scopes
WHERE name = 'runescape'
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
check(rows.next())
scopeId = rows.getInt(1)
}
}
val groups = mutableListOf<CacheImporter.Group>()
val files = mutableListOf<CacheImporter.File>()
try {
connection.prepareStatement(
"""
SELECT
new.group_id AS id,
old.version AS old_version,
old.crc32 AS old_crc32,
b.data AS old_data,
new.version AS new_version,
new.crc32 AS new_crc32,
c.data AS new_data
FROM (
SELECT DISTINCT vf.index_id, vf.file_id, vf.version, vf.crc32
FROM version_list_files vf
WHERE vf.blob_id IN (
SELECT v.blob_id
FROM version_lists v
JOIN resolved_archives a ON a.blob_id = v.blob_id AND a.archive_id = 5
) AND vf.index_id = ?
) old
JOIN (
SELECT DISTINCT ig.group_id, ig.version, ig.crc32
FROM index_groups ig
WHERE ig.container_id IN (
SELECT i.container_id
FROM resolved_indexes i
WHERE i.scope_id = ? AND i.archive_id = ?
)
) new ON old.file_id = new.group_id AND old.version = new.version + 1
LEFT JOIN resolve_file(old.index_id, old.file_id, old.version, old.crc32) b ON TRUE
LEFT JOIN resolve_group(?, ?::uint1, new.group_id, new.crc32, new.version) c ON TRUE
WHERE (b.data IS NULL AND c.data IS NOT NULL) OR (b.data IS NOT NULL AND c.data IS NULL)
""".trimIndent()
).use { stmt ->
stmt.setInt(1, index)
stmt.setInt(2, scopeId)
stmt.setInt(3, archive)
stmt.setInt(4, scopeId)
stmt.setInt(5, archive)
stmt.executeQuery().use { rows ->
while (rows.next()) {
val id = rows.getInt(1)
val oldVersion = rows.getInt(2)
val oldChecksum = rows.getInt(3)
val newVersion = rows.getInt(5)
val newChecksum = rows.getInt(6)
val oldData = rows.getBytes(4)
if (oldData != null) {
Unpooled.wrappedBuffer(oldData).use { oldBuf ->
fileToGroup(oldBuf, newChecksum).use { newBuf ->
if (newBuf != null) {
val uncompressed = Js5Compression.uncompressUnlessEncrypted(newBuf.slice())
groups += CacheImporter.Group(
archive,
id,
newBuf.retain(),
uncompressed,
newVersion,
false
)
}
}
}
}
val newData = rows.getBytes(7)
if (newData != null) {
Unpooled.wrappedBuffer(newData).use { newBuf ->
val oldBuf = groupToFile(newBuf, oldChecksum)
if (oldBuf != null) {
files += CacheImporter.File(index, id, oldBuf, oldVersion)
}
}
}
}
}
}
if (groups.isEmpty() && files.isEmpty()) {
return
}
importer.prepare(connection)
val sourceId = importer.addSource(
connection,
type = CacheImporter.SourceType.CROSS_POLLINATION,
cacheId = null,
gameId = null,
buildMajor = null,
buildMinor = null,
timestamp = null,
name = null,
description = null,
url = null,
)
if (groups.isNotEmpty()) {
importer.addGroups(connection, scopeId, sourceId, groups)
}
if (files.isNotEmpty()) {
importer.addFiles(connection, sourceId, files)
}
} finally {
groups.forEach(CacheImporter.Group::release)
files.forEach(CacheImporter.File::release)
}
}
private fun getUncompressedLength(buf: ByteBuf): Int {
GZIPInputStream(ByteBufInputStream(buf)).use { input ->
var len = 0
val temp = ByteArray(4096)
while (true) {
val n = input.read(temp)
if (n == -1) {
break
}
len += n
}
return len
}
}
private fun fileToGroup(input: ByteBuf, expectedChecksum: Int): ByteBuf? {
val len = input.readableBytes()
val lenWithHeader = len + JS5_COMPRESSION_HEADER_LEN
val uncompressedLen = getUncompressedLength(input.slice())
alloc.buffer(lenWithHeader, lenWithHeader).use { output ->
output.writeByte(Js5CompressionType.GZIP.ordinal)
output.writeInt(len)
output.writeInt(uncompressedLen)
output.writeBytes(input)
return if (output.crc32() == expectedChecksum) {
output.retain()
} else {
null
}
}
}
private fun groupToFile(input: ByteBuf, expectedChecksum: Int): ByteBuf? {
val type = Js5CompressionType.fromOrdinal(input.readUnsignedByte().toInt())
if (type != Js5CompressionType.GZIP) {
return null
}
input.skipBytes(JS5_COMPRESSION_HEADER_LEN - 1)
return if (input.crc32() == expectedChecksum) {
input.retainedSlice()
} else {
null
}
}
private companion object {
private val OLD_TO_NEW_ENGINE = mapOf(
1 to 7, // MODELS
3 to 6, // MIDI_SONGS
4 to 5, // MAPS
)
private const val JS5_COMPRESSION_HEADER_LEN = 9
}
}

@ -2,8 +2,6 @@ package org.openrs2.archive.cache
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parameters.types.path
import com.google.inject.Guice
@ -13,7 +11,6 @@ import org.openrs2.cache.DiskStore
import org.openrs2.inject.CloseableInjector
public class ExportCommand : CliktCommand(name = "export") {
private val scope by option().default("runescape")
private val id by argument().int()
private val output by argument().path(
mustExist = true,
@ -26,7 +23,7 @@ public class ExportCommand : CliktCommand(name = "export") {
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val exporter = injector.getInstance(CacheExporter::class.java)
exporter.export(scope, id) { legacy ->
exporter.export(id) { legacy ->
DiskStore.create(output, legacy = legacy)
}
}

@ -25,7 +25,6 @@ import kotlin.coroutines.resumeWithException
@ChannelHandler.Sharable
public abstract class Js5ChannelHandler(
private val bootstrap: Bootstrap,
private val scopeId: Int,
private val gameId: Int,
private val hostname: String,
private val port: Int,
@ -152,7 +151,6 @@ public abstract class Js5ChannelHandler(
}
}
@Suppress("OVERRIDE_DEPRECATION")
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
releaseGroups()
@ -238,7 +236,7 @@ public abstract class Js5ChannelHandler(
if (groups.size >= CacheImporter.BATCH_SIZE || complete) {
runBlocking {
importer.importGroups(scopeId, sourceId, groups)
importer.importGroups(sourceId, groups)
}
releaseGroups()
@ -270,7 +268,6 @@ public abstract class Js5ChannelHandler(
buf,
uncompressed,
gameId,
scopeId,
buildMajor,
buildMinor,
lastMasterIndexId,
@ -318,15 +315,7 @@ public abstract class Js5ChannelHandler(
}
val groups = runBlocking {
importer.importIndexAndGetMissingGroups(
scopeId,
sourceId,
archive,
index,
buf,
uncompressed,
lastMasterIndexId
)
importer.importIndexAndGetMissingGroups(sourceId, archive, index, buf, uncompressed, lastMasterIndexId)
}
for (group in groups) {
val groupEntry = index[group]!!

@ -19,12 +19,11 @@ import org.openrs2.buffer.use
import org.openrs2.cache.MasterIndexFormat
import org.openrs2.protocol.Rs2Decoder
import org.openrs2.protocol.Rs2Encoder
import org.openrs2.protocol.js5.downstream.XorDecoder
import org.openrs2.protocol.js5.XorDecoder
import kotlin.coroutines.Continuation
public class NxtJs5ChannelHandler(
bootstrap: Bootstrap,
scopeId: Int,
gameId: Int,
hostname: String,
port: Int,
@ -39,7 +38,6 @@ public class NxtJs5ChannelHandler(
private val maxMinorBuildAttempts: Int = 5
) : Js5ChannelHandler(
bootstrap,
scopeId,
gameId,
hostname,
port,

@ -6,28 +6,26 @@ import io.netty.channel.ChannelPipeline
import org.openrs2.cache.MasterIndexFormat
import org.openrs2.protocol.Rs2Decoder
import org.openrs2.protocol.Rs2Encoder
import org.openrs2.protocol.js5.downstream.Js5LoginResponse
import org.openrs2.protocol.js5.downstream.Js5Response
import org.openrs2.protocol.js5.downstream.Js5ResponseDecoder
import org.openrs2.protocol.js5.downstream.XorDecoder
import org.openrs2.protocol.js5.upstream.Js5Request
import org.openrs2.protocol.js5.upstream.Js5RequestEncoder
import org.openrs2.protocol.login.upstream.LoginRequest
import org.openrs2.protocol.js5.Js5Request
import org.openrs2.protocol.js5.Js5RequestEncoder
import org.openrs2.protocol.js5.Js5Response
import org.openrs2.protocol.js5.Js5ResponseDecoder
import org.openrs2.protocol.js5.XorDecoder
import org.openrs2.protocol.login.LoginRequest
import org.openrs2.protocol.login.LoginResponse
import kotlin.coroutines.Continuation
public class OsrsJs5ChannelHandler(
bootstrap: Bootstrap,
scopeId: Int,
gameId: Int,
hostname: String,
port: Int,
build: Int,
lastMasterIndexId: Int?,
continuation: Continuation<Unit>,
importer: CacheImporter
importer: CacheImporter,
) : Js5ChannelHandler(
bootstrap,
scopeId,
gameId,
hostname,
port,
@ -66,9 +64,9 @@ public class OsrsJs5ChannelHandler(
override fun channelRead0(ctx: ChannelHandlerContext, msg: Any) {
when (msg) {
is Js5LoginResponse.Ok -> handleOk(ctx)
is Js5LoginResponse.ClientOutOfDate -> handleClientOutOfDate(ctx)
is Js5LoginResponse -> throw Exception("Invalid response: $msg")
is LoginResponse.Js5Ok -> handleOk(ctx)
is LoginResponse.ClientOutOfDate -> handleClientOutOfDate(ctx)
is LoginResponse -> throw Exception("Invalid response: $msg")
is Js5Response -> handleResponse(ctx, msg.prefetch, msg.archive, msg.group, msg.data)
else -> throw Exception("Unknown message type: ${msg.javaClass.name}")
}

@ -6,16 +6,13 @@ import io.netty.handler.timeout.ReadTimeoutHandler
import org.openrs2.protocol.Protocol
import org.openrs2.protocol.Rs2Decoder
import org.openrs2.protocol.Rs2Encoder
import org.openrs2.protocol.js5.downstream.Js5ClientOutOfDateCodec
import org.openrs2.protocol.js5.downstream.Js5OkCodec
import org.openrs2.protocol.login.upstream.InitJs5RemoteConnectionCodec
public class OsrsJs5ChannelInitializer(private val handler: OsrsJs5ChannelHandler) : ChannelInitializer<Channel>() {
override fun initChannel(ch: Channel) {
ch.pipeline().addLast(
ReadTimeoutHandler(30),
Rs2Encoder(Protocol(InitJs5RemoteConnectionCodec())),
Rs2Decoder(Protocol(Js5OkCodec(), Js5ClientOutOfDateCodec()))
Rs2Encoder(Protocol.LOGIN_UPSTREAM),
Rs2Decoder(Protocol.LOGIN_DOWNSTREAM)
)
ch.pipeline().addLast("handler", handler)
}

@ -1,149 +0,0 @@
package org.openrs2.archive.cache.finder
import com.github.michaelbull.logging.InlineLogger
import com.google.common.io.ByteStreams
import com.google.common.io.LittleEndianDataInputStream
import org.openrs2.util.charset.Cp1252Charset
import java.io.Closeable
import java.io.EOFException
import java.io.IOException
import java.io.InputStream
import java.io.PushbackInputStream
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributeView
import java.nio.file.attribute.FileTime
import java.time.Instant
public class CacheFinderExtractor(
input: InputStream
) : Closeable {
private val pushbackInput = PushbackInputStream(input)
private val input = LittleEndianDataInputStream(pushbackInput)
private fun readTimestamp(): FileTime {
val lo = input.readInt().toLong() and 0xFFFFFFFF
val hi = input.readInt().toLong() and 0xFFFFFFFF
val seconds = (((hi shl 32) or lo) / 10_000_000) - FILETIME_TO_UNIX_EPOCH
return FileTime.from(Instant.ofEpochSecond(seconds, lo))
}
private fun readName(): String {
val bytes = ByteArray(MAX_PATH)
input.readFully(bytes)
var len = bytes.size
for ((i, b) in bytes.withIndex()) {
if (b.toInt() == 0) {
len = i
break
}
}
return String(bytes, 0, len, Cp1252Charset)
}
private fun peekUnsignedByte(): Int {
val n = pushbackInput.read()
pushbackInput.unread(n)
return n
}
public fun extract(destination: Path) {
val newVersion = peekUnsignedByte() == 0xFE
if (newVersion) {
val signature = input.readInt()
if (signature != 0x435352FE) {
throw IOException("Invalid signature")
}
}
var readDirectoryPath = true
var number = 0
var directorySuffix: String? = null
while (true) {
if (newVersion && readDirectoryPath) {
val len = try {
input.readInt()
} catch (ex: EOFException) {
break
}
val bytes = ByteArray(len)
input.readFully(bytes)
val path = String(bytes, Cp1252Charset)
logger.info { "Extracting $path" }
readDirectoryPath = false
directorySuffix = path.substring(path.lastIndexOf('\\') + 1)
.replace(INVALID_CHARS, "_")
continue
}
if (peekUnsignedByte() == 0xFF) {
input.skipBytes(1)
readDirectoryPath = true
number++
continue
}
val attributes = try {
input.readInt()
} catch (ex: EOFException) {
break
}
val btime = readTimestamp()
val atime = readTimestamp()
val mtime = readTimestamp()
val sizeHi = input.readInt().toLong() and 0xFFFFFFFF
val sizeLo = input.readInt().toLong() and 0xFFFFFFFF
val size = (sizeHi shl 32) or sizeLo
input.skipBytes(8) // reserved
val name = readName()
input.skipBytes(14) // alternate name
input.skipBytes(2) // padding
val dir = if (directorySuffix != null) {
destination.resolve("cache${number}_$directorySuffix")
} else {
destination.resolve("cache$number")
}
Files.createDirectories(dir)
if ((attributes and FILE_ATTRIBUTE_DIRECTORY) == 0) {
val file = dir.resolve(name)
Files.newOutputStream(file).use { output ->
ByteStreams.copy(ByteStreams.limit(input, size), output)
}
val view = Files.getFileAttributeView(file, BasicFileAttributeView::class.java)
view.setTimes(mtime, atime, btime)
}
}
}
override fun close() {
input.close()
}
private companion object {
private const val FILETIME_TO_UNIX_EPOCH: Long = 11644473600
private const val MAX_PATH = 260
private const val FILE_ATTRIBUTE_DIRECTORY = 0x10
private val INVALID_CHARS = Regex("[^A-Za-z0-9-]")
private val logger = InlineLogger()
}
}

@ -1,25 +0,0 @@
package org.openrs2.archive.cache.finder
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.default
import com.github.ajalt.clikt.parameters.types.inputStream
import com.github.ajalt.clikt.parameters.types.path
import java.nio.file.Path
public class ExtractCommand : CliktCommand(name = "extract") {
private val input by argument().inputStream()
private val output by argument().path(
mustExist = false,
canBeFile = false,
canBeDir = true,
mustBeReadable = true,
mustBeWritable = true
).default(Path.of("."))
override fun run() {
CacheFinderExtractor(input).use { extractor ->
extractor.extract(output)
}
}
}

@ -4,11 +4,13 @@ import io.netty.buffer.ByteBuf
import org.openrs2.buffer.readString
import org.openrs2.buffer.writeString
import org.openrs2.crypto.StreamCipher
import org.openrs2.protocol.VariableBytePacketCodec
import org.openrs2.protocol.PacketCodec
import org.openrs2.protocol.PacketLength
public object InitJs5RemoteConnectionCodec : VariableBytePacketCodec<InitJs5RemoteConnection>(
type = InitJs5RemoteConnection::class.java,
opcode = 15
public object InitJs5RemoteConnectionCodec : PacketCodec<InitJs5RemoteConnection>(
length = PacketLength.VARIABLE_BYTE,
opcode = 15,
type = InitJs5RemoteConnection::class.java
) {
override fun decode(input: ByteBuf, cipher: StreamCipher): InitJs5RemoteConnection {
val buildMajor = input.readInt()

@ -1,8 +1,25 @@
package org.openrs2.archive.cache.nxt
import org.openrs2.protocol.EmptyPacketCodec
import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
import org.openrs2.protocol.PacketCodec
public object Js5OkCodec : EmptyPacketCodec<LoginResponse.Js5Ok>(
public object Js5OkCodec : PacketCodec<LoginResponse.Js5Ok>(
opcode = 0,
packet = LoginResponse.Js5Ok
)
length = LoginResponse.Js5Ok.LOADING_REQUIREMENTS * 4,
type = LoginResponse.Js5Ok::class.java
) {
override fun decode(input: ByteBuf, cipher: StreamCipher): LoginResponse.Js5Ok {
val loadingRequirements = mutableListOf<Int>()
for (i in 0 until LoginResponse.Js5Ok.LOADING_REQUIREMENTS) {
loadingRequirements += input.readInt()
}
return LoginResponse.Js5Ok(loadingRequirements)
}
override fun encode(input: LoginResponse.Js5Ok, output: ByteBuf, cipher: StreamCipher) {
for (requirement in input.loadingRequirements) {
output.writeInt(requirement)
}
}
}

@ -16,7 +16,6 @@ public object Js5RequestEncoder : MessageToByteEncoder<Js5Request>(Js5Request::c
out.writeShort(msg.build)
out.writeShort(0)
}
is Js5Request.Connected -> {
out.writeByte(6)
out.writeMedium(5)

@ -38,10 +38,10 @@ public class Js5ResponseDecoder : ByteToMessageDecoder() {
request = Request(prefetch, archive, group)
state = if (buffers.containsKey(request)) {
State.READ_DATA
if (buffers.containsKey(request)) {
state = State.READ_DATA
} else {
State.READ_LEN
state = State.READ_LEN
}
}

@ -3,6 +3,11 @@ package org.openrs2.archive.cache.nxt
import org.openrs2.protocol.Packet
public sealed class LoginResponse : Packet {
public object Js5Ok : LoginResponse()
public data class Js5Ok(val loadingRequirements: List<Int>) : LoginResponse() {
public companion object {
public const val LOADING_REQUIREMENTS: Int = 31
}
}
public object ClientOutOfDate : LoginResponse()
}

@ -6,6 +6,5 @@ public data class Game(
public val buildMajor: Int?,
public val buildMinor: Int?,
public val lastMasterIndexId: Int?,
public val languageId: Int,
public val scopeId: Int
public val languageId: Int
)

@ -12,13 +12,13 @@ public class GameDatabase @Inject constructor(
return database.execute { connection ->
connection.prepareStatement(
"""
SELECT v.id, v.url, v.build_major, v.build_minor, v.last_master_index_id, v.language_id, g.scope_id
SELECT v.id, v.url, v.build_major, v.build_minor, v.last_master_index_id, v.language_id
FROM game_variants v
JOIN games g ON g.id = v.game_id
JOIN environments e ON e.id = v.environment_id
JOIN languages l ON l.id = v.language_id
WHERE g.name = ? AND e.name = ? AND l.iso_code = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setString(1, name)
stmt.setString(2, environment)
@ -48,9 +48,8 @@ public class GameDatabase @Inject constructor(
}
val languageId = rows.getInt(6)
val scopeId = rows.getInt(7)
return@execute Game(id, url, buildMajor, buildMinor, lastMasterIndexId, languageId, scopeId)
return@execute Game(id, url, buildMajor, buildMinor, lastMasterIndexId, languageId)
}
}
}

@ -47,14 +47,12 @@ public data class JavConfig(
messages[parts[0]] = parts[1]
}
}
line.startsWith("param=") -> {
val parts = line.substring("param=".length).split("=", limit = 2)
if (parts.size == 2) {
params[parts[0]] = parts[1]
}
}
else -> {
val parts = line.split("=", limit = 2)
if (parts.size == 2) {

@ -1,57 +0,0 @@
package org.openrs2.archive.key
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.await
import kotlinx.coroutines.withContext
import org.openrs2.crypto.XteaKey
import org.openrs2.http.checkStatusCode
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Duration
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
public class HdosKeyDownloader @Inject constructor(
private val client: HttpClient
) : KeyDownloader(KeySource.HDOS) {
override suspend fun getMissingUrls(seenUrls: Set<String>): Set<String> {
return setOf(ENDPOINT)
}
override suspend fun download(url: String): Sequence<XteaKey> {
val request = HttpRequest.newBuilder(URI(url))
.GET()
.timeout(Duration.ofSeconds(30))
.build()
val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).await()
response.checkStatusCode()
return withContext(Dispatchers.IO) {
response.body().use { input ->
input.bufferedReader().use { reader ->
val keys = mutableSetOf<XteaKey>()
for (line in reader.lineSequence()) {
val parts = line.split(',')
if (parts.size < 3) {
continue
}
val key = XteaKey.fromHexOrNull(parts[2]) ?: continue
keys += key
}
keys.asSequence()
}
}
}
}
private companion object {
private const val ENDPOINT = "https://api.hdos.dev/keys/get"
}
}

@ -24,13 +24,11 @@ public class JsonKeyReader @Inject constructor(
keys += mapper.treeToValue<XteaKey?>(key) ?: throw IOException("Key must be non-null")
}
}
root.isObject -> {
for (entry in root.fields()) {
keys += mapper.treeToValue<XteaKey?>(entry.value) ?: throw IOException("Key must be non-null")
}
}
else -> throw IOException("Root element must be an array or object")
}

@ -36,7 +36,7 @@ public class KeyBruteForcer @Inject constructor(
connection.prepareStatement(
"""
LOCK TABLE keys IN EXCLUSIVE MODE
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -49,7 +49,7 @@ public class KeyBruteForcer @Inject constructor(
first_seen TIMESTAMPTZ NOT NULL,
last_seen TIMESTAMPTZ NOT NULL
) ON COMMIT DROP
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -60,7 +60,7 @@ public class KeyBruteForcer @Inject constructor(
SELECT key, source, first_seen, last_seen
FROM key_queue
FOR UPDATE SKIP LOCKED
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -73,7 +73,7 @@ public class KeyBruteForcer @Inject constructor(
LEFT JOIN keys k ON k.key = t.key
WHERE k.key IS NULL
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -87,7 +87,7 @@ public class KeyBruteForcer @Inject constructor(
ON CONFLICT (key_id, source) DO UPDATE SET
first_seen = LEAST(s.first_seen, EXCLUDED.first_seen),
last_seen = GREATEST(s.last_seen, EXCLUDED.last_seen)
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -97,7 +97,7 @@ public class KeyBruteForcer @Inject constructor(
DELETE FROM key_queue k
USING tmp_keys t
WHERE k.key = t.key AND k.source = t.source
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -152,7 +152,7 @@ public class KeyBruteForcer @Inject constructor(
connection.prepareStatement(
"""
LOCK TABLE containers, keys IN SHARE MODE
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.execute()
}
@ -170,7 +170,7 @@ public class KeyBruteForcer @Inject constructor(
SELECT last_container_id
FROM brute_force_iterator
FOR UPDATE
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
check(rows.next())
@ -191,7 +191,7 @@ public class KeyBruteForcer @Inject constructor(
"""
SELECT id, (key).k0, (key).k1, (key).k2, (key).k3
FROM keys
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.fetchSize = BATCH_SIZE
@ -224,7 +224,7 @@ public class KeyBruteForcer @Inject constructor(
"""
UPDATE brute_force_iterator
SET last_container_id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setObject(1, lastContainerId, Types.BIGINT)
stmt.execute()
@ -239,7 +239,7 @@ public class KeyBruteForcer @Inject constructor(
WHERE (? IS NULL OR id > ?) AND encrypted AND key_id IS NULL
ORDER BY id ASC
LIMIT 1
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setObject(1, lastContainerId, Types.BIGINT)
stmt.setObject(2, lastContainerId, Types.BIGINT)
@ -265,7 +265,7 @@ public class KeyBruteForcer @Inject constructor(
SELECT last_key_id, last_container_id
FROM brute_force_iterator
FOR UPDATE
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
check(rows.next())
@ -292,7 +292,7 @@ public class KeyBruteForcer @Inject constructor(
SELECT id, data
FROM containers
WHERE encrypted AND key_id IS NULL AND id <= ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.fetchSize = BATCH_SIZE
stmt.setLong(1, lastContainerId)
@ -319,7 +319,7 @@ public class KeyBruteForcer @Inject constructor(
"""
UPDATE brute_force_iterator
SET last_key_id = ?
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setObject(1, lastKeyId, Types.BIGINT)
stmt.execute()
@ -334,7 +334,7 @@ public class KeyBruteForcer @Inject constructor(
WHERE ? IS NULL OR id > ?
ORDER BY id ASC
LIMIT 1
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setObject(1, lastKeyId, Types.BIGINT)
stmt.setObject(2, lastKeyId, Types.BIGINT)

@ -51,7 +51,7 @@ public class KeyExporter @Inject constructor(
COUNT(*) FILTER (WHERE c.key_id IS NULL AND c.empty_loc)
FROM containers c
WHERE c.encrypted
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
check(rows.next())
@ -69,7 +69,7 @@ public class KeyExporter @Inject constructor(
COUNT(DISTINCT k.id) FILTER (WHERE c.key_id IS NOT NULL)
FROM keys k
LEFT JOIN containers c ON c.key_id = k.id
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
check(rows.next())

@ -3,7 +3,6 @@ package org.openrs2.archive.key
import com.github.michaelbull.logging.InlineLogger
import org.openrs2.crypto.XteaKey
import org.openrs2.db.Database
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.sql.Connection
@ -59,7 +58,7 @@ public class KeyImporter @Inject constructor(
connection.prepareStatement(
"""
SELECT url FROM keysets
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.executeQuery().use { rows ->
val urls = mutableSetOf<String>()
@ -75,16 +74,11 @@ public class KeyImporter @Inject constructor(
val urls = mutableSetOf<String>()
for (downloader in downloaders) {
try {
for (url in downloader.getMissingUrls(seenUrls)) {
keys += downloader.download(url).map { key ->
Key(key, downloader.source)
}
urls += url
for (url in downloader.getMissingUrls(seenUrls)) {
keys += downloader.download(url).map { key ->
Key(key, downloader.source)
}
} catch (ex: IOException) {
logger.warn(ex) { "Failed to download keys from ${downloader.source.name}" }
continue
urls += url
}
}
@ -94,7 +88,7 @@ public class KeyImporter @Inject constructor(
INSERT INTO keysets (url)
VALUES (?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
for (url in urls) {
stmt.setString(1, url)
@ -128,7 +122,7 @@ public class KeyImporter @Inject constructor(
ON CONFLICT (key, source) DO UPDATE SET
first_seen = LEAST(k.first_seen, EXCLUDED.first_seen),
last_seen = GREATEST(k.last_seen, EXCLUDED.last_seen)
""".trimIndent()
""".trimIndent()
).use { stmt ->
for (key in keys) {
if (key.key.isZero) {

@ -5,6 +5,5 @@ public enum class KeySource {
DISK,
OPENOSRS,
POLAR,
RUNELITE,
HDOS
RUNELITE
}

@ -0,0 +1,19 @@
package org.openrs2.archive.key
import java.net.http.HttpClient
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
public class OpenOsrsKeyDownloader @Inject constructor(
client: HttpClient,
jsonKeyReader: JsonKeyReader
) : JsonKeyDownloader(KeySource.OPENOSRS, client, jsonKeyReader) {
override suspend fun getMissingUrls(seenUrls: Set<String>): Set<String> {
return setOf(ENDPOINT)
}
private companion object {
private const val ENDPOINT = "https://xtea.openosrs.dev/get"
}
}

@ -0,0 +1,50 @@
package org.openrs2.archive.key
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.await
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.openrs2.http.charset
import org.openrs2.http.checkStatusCode
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Duration
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
public class PolarKeyDownloader @Inject constructor(
private val client: HttpClient,
jsonKeyReader: JsonKeyReader
) : JsonKeyDownloader(KeySource.POLAR, client, jsonKeyReader) {
override suspend fun getMissingUrls(seenUrls: Set<String>): Set<String> {
val request = HttpRequest.newBuilder(ENDPOINT)
.GET()
.timeout(Duration.ofSeconds(30))
.build()
val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).await()
response.checkStatusCode()
val document = withContext(Dispatchers.IO) {
Jsoup.parse(response.body(), response.charset?.name(), ENDPOINT.toString())
}
val urls = mutableSetOf<String>()
for (element in document.select("a")) {
val url = element.absUrl("href")
if (url.endsWith(".json") && url !in seenUrls) {
urls += url
}
}
return urls
}
private companion object {
private val ENDPOINT = URI("https://archive.runestats.com/osrs/xtea/")
}
}

@ -31,28 +31,10 @@ public class MapRenderer @Inject constructor(
val fillColor = Color(outlineColor.red, outlineColor.green, outlineColor.blue, 128)
}
public suspend fun render(scope: String, masterIndexId: Int): BufferedImage {
public suspend fun render(masterIndexId: Int): BufferedImage {
return database.execute { connection ->
val scopeId = connection.prepareStatement(
"""
SELECT id
FROM scopes
WHERE name = ?
""".trimIndent()
).use { stmt ->
stmt.setString(1, scope)
stmt.executeQuery().use { rows ->
if (!rows.next()) {
throw IllegalArgumentException("Invalid scope")
}
rows.getInt(1)
}
}
// read config index
val configIndex = readIndex(connection, scopeId, masterIndexId, Js5Archive.CONFIG)
val configIndex = readIndex(connection, masterIndexId, Js5Archive.CONFIG)
?: throw IllegalArgumentException("Config index missing")
// read FluType group
@ -61,7 +43,7 @@ public class MapRenderer @Inject constructor(
val underlayGroup = configIndex[Js5ConfigGroup.FLUTYPE]
?: throw IllegalArgumentException("FluType group missing in index")
val underlayFiles = readGroup(connection, scopeId, masterIndexId, Js5Archive.CONFIG, underlayGroup)
val underlayFiles = readGroup(connection, masterIndexId, Js5Archive.CONFIG, underlayGroup)
?: throw IllegalArgumentException("FluType group missing")
try {
for ((id, file) in underlayFiles) {
@ -77,7 +59,7 @@ public class MapRenderer @Inject constructor(
val overlayGroup = configIndex[Js5ConfigGroup.FLOTYPE]
?: throw IllegalArgumentException("FloType group missing in index")
val overlayFiles = readGroup(connection, scopeId, masterIndexId, Js5Archive.CONFIG, overlayGroup)
val overlayFiles = readGroup(connection, masterIndexId, Js5Archive.CONFIG, overlayGroup)
?: throw IllegalArgumentException("FloType group missing")
try {
for ((id, file) in overlayFiles) {
@ -89,13 +71,13 @@ public class MapRenderer @Inject constructor(
// read textures
val textures = mutableMapOf<Int, Int>()
val materialsIndex = readIndex(connection, scopeId, masterIndexId, Js5Archive.MATERIALS)
val materialsIndex = readIndex(connection, masterIndexId, Js5Archive.MATERIALS)
if (materialsIndex != null) {
val materialsGroup = materialsIndex[0]
?: throw IllegalArgumentException("Materials group missing in index")
val materialsFiles = readGroup(connection, scopeId, masterIndexId, Js5Archive.MATERIALS, materialsGroup)
val materialsFiles = readGroup(connection, masterIndexId, Js5Archive.MATERIALS, materialsGroup)
?: throw IllegalArgumentException("Materials group missing")
try {
val metadata = materialsFiles[0]
@ -141,13 +123,13 @@ public class MapRenderer @Inject constructor(
materialsFiles.values.forEach(ByteBuf::release)
}
} else {
val textureIndex = readIndex(connection, scopeId, masterIndexId, Js5Archive.TEXTURES)
val textureIndex = readIndex(connection, masterIndexId, Js5Archive.TEXTURES)
?: throw IllegalArgumentException("Textures index missing")
val textureGroup = textureIndex[0]
?: throw IllegalArgumentException("Textures group missing from index")
val textureFiles = readGroup(connection, scopeId, masterIndexId, Js5Archive.TEXTURES, textureGroup)
val textureFiles = readGroup(connection, masterIndexId, Js5Archive.TEXTURES, textureGroup)
?: throw IllegalArgumentException("Textures group missing")
try {
for ((id, file) in textureFiles) {
@ -173,12 +155,11 @@ public class MapRenderer @Inject constructor(
SELECT n.name, g.encrypted, g.empty_loc, g.key_id
FROM resolved_groups g
JOIN names n ON n.hash = g.name_hash
WHERE g.scope_id = ? AND g.master_index_id = ? AND g.archive_id = ${Js5Archive.MAPS} AND
WHERE g.master_index_id = ? AND g.archive_id = ${Js5Archive.MAPS} AND
n.name ~ '^[lm](?:[0-9]|[1-9][0-9])_(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, scopeId)
stmt.setInt(2, masterIndexId)
stmt.setInt(1, masterIndexId)
stmt.executeQuery().use { rows ->
while (rows.next()) {
@ -226,12 +207,11 @@ public class MapRenderer @Inject constructor(
SELECT n.name, g.data
FROM resolved_groups g
JOIN names n ON n.hash = g.name_hash
WHERE g.scope_id = ? AND g.master_index_id = ? AND g.archive_id = ${Js5Archive.MAPS} AND
WHERE g.master_index_id = ? AND g.archive_id = ${Js5Archive.MAPS} AND
n.name ~ '^m(?:[0-9]|[1-9][0-9])_(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
""".trimIndent()
""".trimIndent()
).use { stmt ->
stmt.setInt(1, scopeId)
stmt.setInt(2, masterIndexId)
stmt.setInt(1, masterIndexId)
stmt.executeQuery().use { rows ->
while (rows.next()) {
@ -266,17 +246,16 @@ public class MapRenderer @Inject constructor(
}
}
private fun readIndex(connection: Connection, scopeId: Int, masterIndexId: Int, archiveId: Int): Js5Index? {
private fun readIndex(connection: Connection, masterIndexId: Int, archiveId: Int): Js5Index? {
connection.prepareStatement(
"""
SELECT data
FROM resolved_indexes
WHERE scope_id = ? AND master_index_id = ? AND archive_id = ?
""".trimIndent()
WHERE master_index_id = ? AND archive_id = ?
""".trimIndent()
).use { stmt ->
stmt.setInt(1, scopeId)
stmt.setInt(2, masterIndexId)
stmt.setInt(3, archiveId)
stmt.setInt(1, masterIndexId)
stmt.setInt(2, archiveId)
stmt.executeQuery().use { rows ->
if (!rows.next()) {
@ -296,7 +275,6 @@ public class MapRenderer @Inject constructor(
private fun readGroup(
connection: Connection,
scopeId: Int,
masterIndexId: Int,
archiveId: Int,
group: Js5Index.Group<*>
@ -305,13 +283,12 @@ public class MapRenderer @Inject constructor(
"""
SELECT data
FROM resolved_groups
WHERE scope_id = ? AND master_index_id = ? AND archive_id = ? AND group_id = ?
""".trimIndent()
WHERE master_index_id = ? AND archive_id = ? AND group_id = ?
""".trimIndent()
).use { stmt ->
stmt.setInt(1, scopeId)
stmt.setInt(2, masterIndexId)
stmt.setInt(3, archiveId)
stmt.setInt(4, group.id)
stmt.setInt(1, masterIndexId)
stmt.setInt(2, archiveId)
stmt.setInt(3, group.id)
stmt.executeQuery().use { rows ->
if (!rows.next()) {
@ -344,40 +321,6 @@ public class MapRenderer @Inject constructor(
}
}
private fun isShortCode(buf: ByteBuf): Boolean {
for (plane in 0 until LEVELS) {
for (dx in 0 until MAP_SIZE) {
for (dz in 0 until MAP_SIZE) {
while (true) {
if (buf.readableBytes() < 2) {
return false
}
val code = buf.readUnsignedShort()
if (code == 0) {
break
} else if (code == 1) {
if (!buf.isReadable) {
return false
}
buf.skipBytes(1)
break
} else if (code <= 49) {
if (buf.readableBytes() < 2) {
return false
}
buf.skipBytes(2)
}
}
}
}
}
return !buf.isReadable
}
private fun renderMap(
image: BufferedImage,
x: Int,
@ -386,12 +329,6 @@ public class MapRenderer @Inject constructor(
underlayColors: Map<Int, Int>,
overlayColors: Map<Int, Int>
) {
val readCode = if (isShortCode(buf.slice())) {
buf::readUnsignedShort
} else {
{ buf.readUnsignedByte().toInt() }
}
for (plane in 0 until LEVELS) {
for (dx in 0 until MAP_SIZE) {
for (dz in 0 until MAP_SIZE) {
@ -400,14 +337,14 @@ public class MapRenderer @Inject constructor(
var underlay = 0
while (true) {
val code = readCode()
val code = buf.readUnsignedByte().toInt()
if (code == 0) {
break
} else if (code == 1) {
buf.skipBytes(1)
break
} else if (code <= 49) {
overlay = readCode()
overlay = buf.readUnsignedByte().toInt()
shape = (code - 2) shr 2
} else if (code <= 81) {
// empty

@ -25,7 +25,7 @@ public class NameImporter @Inject constructor(
INSERT INTO names (hash, name)
VALUES (?, ?)
ON CONFLICT DO NOTHING
""".trimIndent()
""".trimIndent()
).use { stmt ->
for (name in names) {
stmt.setInt(1, name.krHashCode())

@ -27,8 +27,6 @@ public class RuneStarNameDownloader @Inject constructor(
names += readTsv(endpoint, 0)
}
names += readTsv(LEANBOW_NAMES_ENDPOINT, 1)
return names.asSequence()
}
@ -52,18 +50,12 @@ public class RuneStarNameDownloader @Inject constructor(
private companion object {
private val NAMES_ENDPOINTS = listOf(
URI("https://raw.githubusercontent.com/Joshua-F/cache-names/master/names.tsv"),
URI("https://raw.githubusercontent.com/Pazaz/RT4-Data/main/names.tsv"),
URI("https://raw.githubusercontent.com/Pazaz/RT4-Data/main/osrs.tsv"),
URI("https://raw.githubusercontent.com/Pazaz/RT4-Data/main/walied.tsv"),
URI("https://raw.githubusercontent.com/RuneStar/cache-names/master/names.tsv"),
URI("https://raw.githubusercontent.com/Joshua-F/cache-names/master/names.tsv"),
)
private val INDIVIDUAL_NAMES_ENDPOINTS = listOf(
URI("https://raw.githubusercontent.com/Joshua-F/cache-names/master/individual-names.tsv"),
URI("https://raw.githubusercontent.com/Pazaz/RT4-Data/main/walied.individual.components.tsv"),
URI("https://raw.githubusercontent.com/Pazaz/RT4-Data/main/walied.individual.tsv"),
URI("https://raw.githubusercontent.com/RuneStar/cache-names/master/individual-names.tsv"),
URI("https://raw.githubusercontent.com/Joshua-F/cache-names/master/individual-names.tsv"),
)
private val LEANBOW_NAMES_ENDPOINT = URI("https://raw.githubusercontent.com/Pazaz/RT4-Data/main/leanbow.tsv")
}
}

@ -1,38 +1,25 @@
package org.openrs2.archive.web
import io.ktor.http.CacheControl
import io.ktor.application.ApplicationCall
import io.ktor.http.ContentDisposition
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.EntityTagVersion
import io.ktor.http.content.caching
import io.ktor.http.content.versions
import io.ktor.server.application.ApplicationCall
import io.ktor.server.http.content.CachingOptions
import io.ktor.server.plugins.cachingheaders.caching
import io.ktor.server.response.header
import io.ktor.server.response.respond
import io.ktor.server.response.respondBytes
import io.ktor.server.response.respondOutputStream
import io.ktor.server.thymeleaf.ThymeleafContent
import io.ktor.response.header
import io.ktor.response.respond
import io.ktor.response.respondOutputStream
import io.ktor.thymeleaf.ThymeleafContent
import io.netty.buffer.ByteBufAllocator
import io.netty.buffer.ByteBufUtil
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
import org.openrs2.archive.cache.CacheExporter
import org.openrs2.archive.map.MapRenderer
import org.openrs2.buffer.use
import org.openrs2.cache.DiskStoreZipWriter
import org.openrs2.cache.FlatFileStoreTarWriter
import org.openrs2.compress.gzip.GzipLevelOutputStream
import org.openrs2.crypto.whirlpool
import java.nio.file.attribute.FileTime
import java.time.Instant
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.Base64
import java.util.zip.Deflater
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
@ -50,186 +37,93 @@ public class CachesController @Inject constructor(
public suspend fun index(call: ApplicationCall) {
val caches = exporter.list()
val totalSize = exporter.totalSize()
call.respond(
ThymeleafContent(
"caches/index.html", mapOf(
"caches" to caches,
"totalSize" to totalSize
)
)
)
call.respond(ThymeleafContent("caches/index.html", mapOf("caches" to caches)))
}
public suspend fun indexJson(call: ApplicationCall) {
val caches = exporter.list()
call.caching = CachingOptions(
cacheControl = CacheControl.MaxAge(
maxAgeSeconds = 900,
visibility = CacheControl.Visibility.Public
),
expires = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(900)
)
call.respond(caches)
}
public suspend fun show(call: ApplicationCall) {
val scope = call.parameters["scope"]!!
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return
}
val cache = exporter.get(scope, id)
val cache = exporter.get(id)
if (cache == null) {
call.respond(HttpStatusCode.NotFound)
return
}
call.respond(
ThymeleafContent(
"caches/show.html", mapOf(
"cache" to cache,
"scope" to scope
)
)
)
}
public suspend fun exportGroup(call: ApplicationCall) {
val scope = call.parameters["scope"]!!
val id = call.parameters["id"]?.toIntOrNull()
val archiveId = call.parameters["archive"]?.toIntOrNull()
val groupId = call.parameters["group"]?.toIntOrNull()
if (id == null || archiveId == null || groupId == null) {
call.respond(HttpStatusCode.NotFound)
return
}
exporter.exportGroup(scope, id, archiveId, groupId).use { buf ->
if (buf == null) {
call.respond(HttpStatusCode.NotFound)
return
}
val etag = Base64.getEncoder().encodeToString(buf.whirlpool().sliceArray(0 until 16))
val bytes = ByteBufUtil.getBytes(buf, 0, buf.readableBytes(), false)
call.respondBytes(bytes, contentType = ContentType.Application.OctetStream) {
caching = CachingOptions(
cacheControl = CacheControl.MaxAge(
maxAgeSeconds = 86400,
visibility = CacheControl.Visibility.Public
),
expires = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(86400)
)
versions = listOf(
EntityTagVersion(etag, weak = false)
)
}
}
call.respond(ThymeleafContent("caches/show.html", mapOf("cache" to cache)))
}
public suspend fun exportDisk(call: ApplicationCall) {
val scope = call.parameters["scope"]!!
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return
}
val name = exporter.getFileName(scope, id)
if (name == null) {
call.respond(HttpStatusCode.NotFound)
return
}
call.response.header(
HttpHeaders.ContentDisposition,
ContentDisposition.Attachment
.withParameter(ContentDisposition.Parameters.FileName, "cache-$name.zip")
.withParameter(ContentDisposition.Parameters.FileName, "cache.zip")
.toString()
)
call.respondOutputStream(contentType = ContentType.Application.Zip) {
exporter.export(scope, id) { legacy ->
exporter.export(id) { legacy ->
DiskStoreZipWriter(ZipOutputStream(this), alloc = alloc, legacy = legacy)
}
}
}
public suspend fun exportFlatFile(call: ApplicationCall) {
val scope = call.parameters["scope"]!!
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return
}
val name = exporter.getFileName(scope, id)
if (name == null) {
call.respond(HttpStatusCode.NotFound)
return
}
call.response.header(
HttpHeaders.ContentDisposition,
ContentDisposition.Attachment
.withParameter(ContentDisposition.Parameters.FileName, "cache-$name.tar.gz")
.withParameter(ContentDisposition.Parameters.FileName, "cache.tar.gz")
.toString()
)
call.respondOutputStream(contentType = ContentType.Application.GZip) {
exporter.export(scope, id) {
exporter.export(id) {
FlatFileStoreTarWriter(TarArchiveOutputStream(GzipLevelOutputStream(this, Deflater.BEST_COMPRESSION)))
}
}
}
public suspend fun exportKeysJson(call: ApplicationCall) {
val scope = call.parameters["scope"]!!
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return
}
val name = exporter.getFileName(scope, id)
if (name == null) {
call.respond(HttpStatusCode.NotFound)
return
}
call.response.header(
HttpHeaders.ContentDisposition,
ContentDisposition.Inline
.withParameter(ContentDisposition.Parameters.FileName, "keys-$name.json")
.toString()
)
call.respond(exporter.exportKeys(scope, id))
call.respond(exporter.exportKeys(id))
}
public suspend fun exportKeysZip(call: ApplicationCall) {
val scope = call.parameters["scope"]!!
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return
}
val name = exporter.getFileName(scope, id)
if (name == null) {
call.respond(HttpStatusCode.NotFound)
return
}
call.response.header(
HttpHeaders.ContentDisposition,
ContentDisposition.Attachment
.withParameter(ContentDisposition.Parameters.FileName, "keys-$name.zip")
.withParameter(ContentDisposition.Parameters.FileName, "keys.zip")
.toString()
)
@ -240,7 +134,7 @@ public class CachesController @Inject constructor(
val timestamp = FileTime.from(Instant.EPOCH)
for (key in exporter.exportKeys(scope, id)) {
for (key in exporter.exportKeys(id)) {
if (key.mapSquare == null) {
continue
}
@ -272,33 +166,19 @@ public class CachesController @Inject constructor(
}
public suspend fun renderMap(call: ApplicationCall) {
val scope = call.parameters["scope"]!!
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return
}
val name = exporter.getFileName(scope, id)
if (name == null) {
call.respond(HttpStatusCode.NotFound)
return
}
call.response.header(
HttpHeaders.ContentDisposition,
ContentDisposition.Inline
.withParameter(ContentDisposition.Parameters.FileName, "map-$name.png")
.toString()
)
/*
* The temporary BufferedImages used by the MapRenderer use a large
* amount of heap space. We limit the number of renders that can be
* performed in parallel to prevent OOMs.
*/
renderSemaphore.withPermit {
val image = renderer.render(scope, id)
val image = renderer.render(id)
call.respondOutputStream(contentType = ContentType.Image.PNG) {
ImageIO.write(image, "PNG", this)

@ -1,10 +1,10 @@
package org.openrs2.archive.web
import io.ktor.application.ApplicationCall
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.thymeleaf.ThymeleafContent
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.thymeleaf.ThymeleafContent
import org.openrs2.archive.key.KeyExporter
import org.openrs2.archive.key.KeyImporter
import org.openrs2.archive.key.KeySource

@ -1,32 +1,25 @@
package org.openrs2.archive.web
import com.fasterxml.jackson.databind.ObjectMapper
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.ContentNegotiation
import io.ktor.features.XForwardedHeaderSupport
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.serialization.jackson.JacksonConverter
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.application.install
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.resources
import io.ktor.http.content.static
import io.ktor.jackson.JacksonConverter
import io.ktor.response.respond
import io.ktor.response.respondRedirect
import io.ktor.routing.get
import io.ktor.routing.post
import io.ktor.routing.routing
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import io.ktor.server.http.content.resources
import io.ktor.server.http.content.static
import io.ktor.server.jetty.Jetty
import io.ktor.server.plugins.autohead.AutoHeadResponse
import io.ktor.server.plugins.cachingheaders.CachingHeaders
import io.ktor.server.plugins.conditionalheaders.ConditionalHeaders
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.defaultheaders.DefaultHeaders
import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders
import io.ktor.server.response.header
import io.ktor.server.response.respond
import io.ktor.server.response.respondRedirect
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.routing
import io.ktor.server.thymeleaf.Thymeleaf
import io.ktor.server.thymeleaf.ThymeleafContent
import io.ktor.server.webjars.Webjars
import io.ktor.thymeleaf.Thymeleaf
import io.ktor.thymeleaf.ThymeleafContent
import io.ktor.webjars.Webjars
import org.openrs2.json.Json
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect
import org.thymeleaf.templatemode.TemplateMode
@ -41,25 +34,11 @@ public class WebServer @Inject constructor(
@Json private val mapper: ObjectMapper
) {
public fun start(address: String, port: Int) {
embeddedServer(Jetty, host = address, port = port) {
install(AutoHeadResponse)
install(CachingHeaders)
install(ConditionalHeaders)
install(createApplicationPlugin(name = "CORS") {
onCall { call ->
call.response.header(HttpHeaders.AccessControlAllowOrigin, "*")
}
})
embeddedServer(CIO, host = address, port = port) {
install(ContentNegotiation) {
ignoreType<ThymeleafContent>()
register(ContentType.Application.Json, JacksonConverter(mapper))
}
install(DefaultHeaders)
install(Thymeleaf) {
addDialect(ByteUnitsDialect)
addDialect(Java8TimeDialect())
@ -70,52 +49,47 @@ public class WebServer @Inject constructor(
})
}
install(XForwardedHeaders)
install(XForwardedHeaderSupport)
install(Webjars)
routing {
get("/") { call.respond(ThymeleafContent("index.html", emptyMap())) }
get("/api") { call.respond(ThymeleafContent("api/index.html", mapOf("active" to "api"))) }
get("/caches") { cachesController.index(call) }
get("/caches.json") { cachesController.indexJson(call) }
get("/caches/{scope}/{id}") { cachesController.show(call) }
get("/caches/{scope}/{id}/archives/{archive}/groups/{group}.dat") { cachesController.exportGroup(call) }
get("/caches/{scope}/{id}/disk.zip") { cachesController.exportDisk(call) }
get("/caches/{scope}/{id}/flat-file.tar.gz") { cachesController.exportFlatFile(call) }
get("/caches/{scope}/{id}/keys.json") { cachesController.exportKeysJson(call) }
get("/caches/{scope}/{id}/keys.zip") { cachesController.exportKeysZip(call) }
get("/caches/{scope}/{id}/map.png") { cachesController.renderMap(call) }
get("/caches/{id}") { cachesController.show(call) }
get("/caches/{id}.zip") {
val id = call.parameters["id"]
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return@get
}
call.respondRedirect(permanent = true) {
path("caches", id, "disk.zip")
}
}
get("/caches/{id}.json") {
val id = call.parameters["id"]
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return@get
}
call.respondRedirect(permanent = true) {
path("caches", id, "keys.json")
}
}
get("/caches/{id}/disk.zip") { cachesController.exportDisk(call) }
get("/caches/{id}/flat-file.tar.gz") { cachesController.exportFlatFile(call) }
get("/caches/{id}/keys.json") { cachesController.exportKeysJson(call) }
get("/caches/{id}/keys.zip") { cachesController.exportKeysZip(call) }
get("/caches/{id}/map.png") { cachesController.renderMap(call) }
get("/keys") { keysController.index(call) }
post("/keys") { keysController.import(call) }
get("/keys/all.json") { keysController.exportAll(call) }
get("/keys/valid.json") { keysController.exportValid(call) }
static("/static") { resources("/org/openrs2/archive/static") }
// compatibility redirects
get("/caches/{id}") { redirect(call, permanent = true, "/caches/runescape/{id}") }
get("/caches/{id}.json") { redirect(call, permanent = true, "/caches/runescape/{id}/keys.json") }
get("/caches/{id}.zip") { redirect(call, permanent = true, "/caches/runescape/{id}/disk.zip") }
get("/caches/{id}/disk.zip") { redirect(call, permanent = true, "/caches/runescape/{id}/disk.zip") }
get("/caches/{id}/flat-file.tar.gz") {
redirect(call, permanent = true, "/caches/runescape/{id}/flat-file.tar.gz")
}
get("/caches/{id}/keys.json") { redirect(call, permanent = true, "/caches/runescape/{id}/keys.json") }
get("/caches/{id}/keys.zip") { redirect(call, permanent = true, "/caches/runescape/{id}/keys.zip") }
get("/caches/{id}/map.png") { redirect(call, permanent = true, "/caches/runescape/{id}/map.png") }
}
}.start(wait = true)
}
private suspend fun redirect(call: ApplicationCall, permanent: Boolean, path: String) {
val destination = path.replace(PARAMETER) { match ->
val (name) = match.destructured
call.parameters[name] ?: throw IllegalArgumentException()
}
call.respondRedirect(destination, permanent)
}
private companion object {
private val PARAMETER = Regex("\\{([^}]*)}")
}
}

@ -1,176 +0,0 @@
-- @formatter:off
CREATE TABLE scopes (
id SERIAL PRIMARY KEY NOT NULL,
name TEXT UNIQUE NOT NULL
);
INSERT INTO scopes (name) VALUES ('runescape');
ALTER TABLE games
ADD COLUMN scope_id INTEGER DEFAULT 1 NOT NULL REFERENCES scopes (id);
ALTER TABLE games
ALTER COLUMN scope_id DROP DEFAULT;
-- XXX(gpe): I don't think we can easily replace this as the source_groups
-- table doesn't contain a scope_id directly - only indirectly via the sources
-- and games tables.
ALTER TABLE source_groups
DROP CONSTRAINT source_groups_archive_id_group_id_version_version_truncate_fkey;
ALTER TABLE groups
ADD COLUMN scope_id INTEGER DEFAULT 1 NOT NULL REFERENCES scopes (id),
DROP CONSTRAINT groups_pkey,
ADD PRIMARY KEY (scope_id, archive_id, group_id, version, version_truncated, container_id);
ALTER TABLE groups
ALTER COLUMN scope_id DROP DEFAULT;
CREATE FUNCTION resolve_index(_scope_id INTEGER, _archive_id uint1, _crc32 INTEGER, _version INTEGER) RETURNS SETOF containers AS $$
SELECT c.*
FROM groups g
JOIN containers c ON c.id = g.container_id
JOIN indexes i ON i.container_id = c.id
WHERE g.scope_id = _scope_id AND g.archive_id = 255 AND g.group_id = _archive_id::INTEGER AND c.crc32 = _crc32 AND
g.version = _version AND NOT g.version_truncated AND i.version = _version
ORDER BY c.id ASC
LIMIT 1;
$$ LANGUAGE SQL STABLE PARALLEL SAFE ROWS 1;
CREATE FUNCTION resolve_group(_scope_id INTEGER, _archive_id uint1, _group_id INTEGER, _crc32 INTEGER, _version INTEGER) RETURNS SETOF containers AS $$
SELECT c.*
FROM groups g
JOIN containers c ON c.id = g.container_id
WHERE g.scope_id = _scope_id AND g.archive_id = _archive_id AND g.group_id = _group_id AND c.crc32 = _crc32 AND (
(g.version = _version AND NOT g.version_truncated) OR
(g.version = _version & 65535 AND g.version_truncated)
)
ORDER BY g.version_truncated ASC, c.id ASC
LIMIT 1;
$$ LANGUAGE SQL STABLE PARALLEL SAFE ROWS 1;
DROP VIEW resolved_groups;
DROP VIEW resolved_indexes;
CREATE VIEW resolved_indexes AS
SELECT s.id AS scope_id, m.id AS master_index_id, a.archive_id, c.data, c.id AS container_id
FROM scopes s
CROSS JOIN master_indexes m
JOIN master_index_archives a ON a.master_index_id = m.id
JOIN resolve_index(s.id, a.archive_id, a.crc32, a.version) c ON TRUE;
CREATE VIEW resolved_groups (scope_id, master_index_id, archive_id, group_id, name_hash, version, data, encrypted, empty_loc, key_id) AS
WITH i AS NOT MATERIALIZED (
SELECT scope_id, master_index_id, archive_id, data, container_id
FROM resolved_indexes
)
SELECT i.scope_id, i.master_index_id, 255::uint1, i.archive_id::INTEGER, NULL, NULL, i.data, FALSE, FALSE, NULL
FROM i
UNION ALL
SELECT i.scope_id, i.master_index_id, i.archive_id, ig.group_id, ig.name_hash, ig.version, c.data, c.encrypted, c.empty_loc, c.key_id
FROM i
JOIN index_groups ig ON ig.container_id = i.container_id
JOIN resolve_group(i.scope_id, i.archive_id, ig.group_id, ig.crc32, ig.version) c ON TRUE;
DROP VIEW colliding_groups;
CREATE VIEW colliding_groups (scope_id, archive_id, group_id, crc32, truncated_version, versions, containers) AS
SELECT
g.scope_id,
g.archive_id,
g.group_id,
c.crc32,
g.version & 65535 AS truncated_version,
array_agg(DISTINCT g.version ORDER BY g.version ASC),
array_agg(DISTINCT c.id ORDER BY c.id ASC)
FROM groups g
JOIN containers c ON c.id = g.container_id
GROUP BY g.scope_id, g.archive_id, g.group_id, c.crc32, truncated_version
HAVING COUNT(DISTINCT c.id) > 1;
DROP VIEW cache_stats;
DROP MATERIALIZED VIEW master_index_stats;
DROP MATERIALIZED VIEW index_stats;
CREATE MATERIALIZED VIEW index_stats (
scope_id,
archive_id,
container_id,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
s.id AS scope_id,
g.group_id AS archive_id,
i.container_id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL) AS valid_groups,
COUNT(*) AS groups,
COUNT(*) FILTER (WHERE c.encrypted AND (c.key_id IS NOT NULL OR c.empty_loc)) AS valid_keys,
COUNT(*) FILTER (WHERE c.encrypted) AS keys,
SUM(length(c.data) + 2) FILTER (WHERE c.id IS NOT NULL) AS size,
SUM(group_blocks(ig.group_id, length(c.data) + 2)) FILTER (WHERE c.id IS NOT NULL) AS blocks
FROM scopes s
CROSS JOIN indexes i
JOIN groups g ON g.container_id = i.container_id AND g.archive_id = 255 AND NOT g.version_truncated AND
g.version = i.version
JOIN index_groups ig ON ig.container_id = i.container_id
LEFT JOIN resolve_group(s.id, g.group_id::uint1, ig.group_id, ig.crc32, ig.version) c ON TRUE
GROUP BY s.id, g.group_id, i.container_id;
CREATE UNIQUE INDEX ON index_stats (scope_id, archive_id, container_id);
CREATE MATERIALIZED VIEW master_index_stats (
scope_id,
master_index_id,
valid_indexes,
indexes,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
sc.id,
m.id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL OR (a.version = 0 AND a.crc32 = 0)) AS valid_indexes,
COUNT(*) FILTER (WHERE a.master_index_id IS NOT NULL) AS indexes,
SUM(COALESCE(s.valid_groups, 0)) AS valid_groups,
SUM(COALESCE(s.groups, 0)) AS groups,
SUM(COALESCE(s.valid_keys, 0)) AS valid_keys,
SUM(COALESCE(s.keys, 0)) AS keys,
SUM(COALESCE(s.size, 0)) + SUM(COALESCE(length(c.data), 0)) AS size,
SUM(COALESCE(s.blocks, 0)) + SUM(COALESCE(group_blocks(a.archive_id, length(c.data)), 0)) AS blocks
FROM scopes sc
CROSS JOIN master_indexes m
LEFT JOIN master_index_archives a ON a.master_index_id = m.id
LEFT JOIN resolve_index(sc.id, a.archive_id, a.crc32, a.version) c ON TRUE
LEFT JOIN index_stats s ON s.scope_id = sc.id AND s.archive_id = a.archive_id AND s.container_id = c.id
GROUP BY sc.id, m.id;
CREATE UNIQUE INDEX ON master_index_stats (scope_id, master_index_id);
CREATE VIEW cache_stats AS
SELECT
s.id AS scope_id,
c.id AS cache_id,
COALESCE(ms.valid_indexes, cs.valid_archives) AS valid_indexes,
COALESCE(ms.indexes, cs.archives) AS indexes,
COALESCE(ms.valid_groups, cs.valid_files) AS valid_groups,
COALESCE(ms.groups, cs.files) AS groups,
COALESCE(ms.valid_keys, 0) AS valid_keys,
COALESCE(ms.keys, 0) AS keys,
COALESCE(ms.size, cs.size) AS size,
COALESCE(ms.blocks, cs.blocks) AS blocks
FROM scopes s
CROSS JOIN caches c
LEFT JOIN master_index_stats ms ON ms.scope_id = s.id AND ms.master_index_id = c.id
LEFT JOIN crc_table_stats cs ON s.name = 'runescape' AND cs.crc_table_id = c.id;
DROP FUNCTION resolve_group(_archive_id uint1, _group_id INTEGER, _crc32 INTEGER, _version INTEGER);
DROP FUNCTION resolve_index(_archive_id uint1, _crc32 INTEGER, _version INTEGER);

@ -1,2 +0,0 @@
-- @formatter:off
ALTER TABLE caches ADD COLUMN hidden BOOLEAN NOT NULL DEFAULT FALSE;

@ -1,95 +0,0 @@
-- @formatter:off
CREATE MATERIALIZED VIEW index_stats_new (
scope_id,
archive_id,
container_id,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
s.id AS scope_id,
g.group_id AS archive_id,
i.container_id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL) AS valid_groups,
COUNT(*) AS groups,
COUNT(*) FILTER (WHERE c.encrypted AND (c.key_id IS NOT NULL OR c.empty_loc)) AS valid_keys,
COUNT(*) FILTER (WHERE c.encrypted) AS keys,
SUM(length(c.data) + 2) FILTER (WHERE c.id IS NOT NULL) AS size,
SUM(group_blocks(ig.group_id, length(c.data) + 2)) FILTER (WHERE c.id IS NOT NULL) AS blocks
FROM scopes s
CROSS JOIN indexes i
JOIN groups g ON g.scope_id = s.id AND g.container_id = i.container_id AND g.archive_id = 255 AND
NOT g.version_truncated AND g.version = i.version
JOIN index_groups ig ON ig.container_id = i.container_id
LEFT JOIN resolve_group(s.id, g.group_id::uint1, ig.group_id, ig.crc32, ig.version) c ON TRUE
GROUP BY s.id, g.group_id, i.container_id;
CREATE UNIQUE INDEX ON index_stats_new (scope_id, archive_id, container_id);
ALTER MATERIALIZED VIEW index_stats RENAME TO index_stats_old;
ALTER INDEX index_stats_scope_id_archive_id_container_id_idx RENAME TO index_stats_old_scope_id_archive_id_container_id_idx;
ALTER MATERIALIZED VIEW index_stats_new RENAME TO index_stats;
ALTER INDEX index_stats_new_scope_id_archive_id_container_id_idx RENAME TO index_stats_scope_id_archive_id_container_id_idx;
CREATE MATERIALIZED VIEW master_index_stats_new (
scope_id,
master_index_id,
valid_indexes,
indexes,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
sc.id,
m.id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL OR (a.version = 0 AND a.crc32 = 0)) AS valid_indexes,
COUNT(*) FILTER (WHERE a.master_index_id IS NOT NULL) AS indexes,
SUM(COALESCE(s.valid_groups, 0)) AS valid_groups,
SUM(COALESCE(s.groups, 0)) AS groups,
SUM(COALESCE(s.valid_keys, 0)) AS valid_keys,
SUM(COALESCE(s.keys, 0)) AS keys,
SUM(COALESCE(s.size, 0)) + SUM(COALESCE(length(c.data), 0)) AS size,
SUM(COALESCE(s.blocks, 0)) + SUM(COALESCE(group_blocks(a.archive_id, length(c.data)), 0)) AS blocks
FROM scopes sc
CROSS JOIN master_indexes m
LEFT JOIN master_index_archives a ON a.master_index_id = m.id
LEFT JOIN resolve_index(sc.id, a.archive_id, a.crc32, a.version) c ON TRUE
LEFT JOIN index_stats s ON s.scope_id = sc.id AND s.archive_id = a.archive_id AND s.container_id = c.id
GROUP BY sc.id, m.id;
CREATE UNIQUE INDEX ON master_index_stats_new (scope_id, master_index_id);
ALTER MATERIALIZED VIEW master_index_stats RENAME TO master_index_stats_old;
ALTER INDEX master_index_stats_scope_id_master_index_id_idx RENAME TO master_index_stats_old_scope_id_master_index_id_idx;
ALTER MATERIALIZED VIEW master_index_stats_new RENAME TO master_index_stats;
ALTER INDEX master_index_stats_new_scope_id_master_index_id_idx RENAME TO master_index_stats_scope_id_master_index_id_idx;
CREATE OR REPLACE VIEW cache_stats AS
SELECT
s.id AS scope_id,
c.id AS cache_id,
COALESCE(ms.valid_indexes, cs.valid_archives) AS valid_indexes,
COALESCE(ms.indexes, cs.archives) AS indexes,
COALESCE(ms.valid_groups, cs.valid_files) AS valid_groups,
COALESCE(ms.groups, cs.files) AS groups,
COALESCE(ms.valid_keys, 0) AS valid_keys,
COALESCE(ms.keys, 0) AS keys,
COALESCE(ms.size, cs.size) AS size,
COALESCE(ms.blocks, cs.blocks) AS blocks
FROM scopes s
CROSS JOIN caches c
LEFT JOIN master_index_stats ms ON ms.scope_id = s.id AND ms.master_index_id = c.id
LEFT JOIN crc_table_stats cs ON s.name = 'runescape' AND cs.crc_table_id = c.id;
DROP MATERIALIZED VIEW master_index_stats_old;
DROP MATERIALIZED VIEW index_stats_old;

@ -1,95 +0,0 @@
-- @formatter:off
CREATE MATERIALIZED VIEW index_stats_new (
scope_id,
archive_id,
container_id,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
s.id AS scope_id,
g.group_id AS archive_id,
i.container_id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL) AS valid_groups,
COUNT(*) FILTER (WHERE ig.container_id IS NOT NULL) AS groups,
COUNT(*) FILTER (WHERE c.encrypted AND (c.key_id IS NOT NULL OR c.empty_loc)) AS valid_keys,
COUNT(*) FILTER (WHERE c.encrypted) AS keys,
SUM(length(c.data) + 2) FILTER (WHERE c.id IS NOT NULL) AS size,
SUM(group_blocks(ig.group_id, length(c.data) + 2)) FILTER (WHERE c.id IS NOT NULL) AS blocks
FROM scopes s
CROSS JOIN indexes i
JOIN groups g ON g.scope_id = s.id AND g.container_id = i.container_id AND g.archive_id = 255 AND
NOT g.version_truncated AND g.version = i.version
LEFT JOIN index_groups ig ON ig.container_id = i.container_id
LEFT JOIN resolve_group(s.id, g.group_id::uint1, ig.group_id, ig.crc32, ig.version) c ON TRUE
GROUP BY s.id, g.group_id, i.container_id;
CREATE UNIQUE INDEX ON index_stats_new (scope_id, archive_id, container_id);
ALTER MATERIALIZED VIEW index_stats RENAME TO index_stats_old;
ALTER INDEX index_stats_scope_id_archive_id_container_id_idx RENAME TO index_stats_old_scope_id_archive_id_container_id_idx;
ALTER MATERIALIZED VIEW index_stats_new RENAME TO index_stats;
ALTER INDEX index_stats_new_scope_id_archive_id_container_id_idx RENAME TO index_stats_scope_id_archive_id_container_id_idx;
CREATE MATERIALIZED VIEW master_index_stats_new (
scope_id,
master_index_id,
valid_indexes,
indexes,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
sc.id,
m.id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL OR (a.version = 0 AND a.crc32 = 0)) AS valid_indexes,
COUNT(*) FILTER (WHERE a.master_index_id IS NOT NULL) AS indexes,
SUM(COALESCE(s.valid_groups, 0)) AS valid_groups,
SUM(COALESCE(s.groups, 0)) AS groups,
SUM(COALESCE(s.valid_keys, 0)) AS valid_keys,
SUM(COALESCE(s.keys, 0)) AS keys,
SUM(COALESCE(s.size, 0)) + SUM(COALESCE(length(c.data), 0)) AS size,
SUM(COALESCE(s.blocks, 0)) + SUM(COALESCE(group_blocks(a.archive_id, length(c.data)), 0)) AS blocks
FROM scopes sc
CROSS JOIN master_indexes m
LEFT JOIN master_index_archives a ON a.master_index_id = m.id
LEFT JOIN resolve_index(sc.id, a.archive_id, a.crc32, a.version) c ON TRUE
LEFT JOIN index_stats s ON s.scope_id = sc.id AND s.archive_id = a.archive_id AND s.container_id = c.id
GROUP BY sc.id, m.id;
CREATE UNIQUE INDEX ON master_index_stats_new (scope_id, master_index_id);
ALTER MATERIALIZED VIEW master_index_stats RENAME TO master_index_stats_old;
ALTER INDEX master_index_stats_scope_id_master_index_id_idx RENAME TO master_index_stats_old_scope_id_master_index_id_idx;
ALTER MATERIALIZED VIEW master_index_stats_new RENAME TO master_index_stats;
ALTER INDEX master_index_stats_new_scope_id_master_index_id_idx RENAME TO master_index_stats_scope_id_master_index_id_idx;
CREATE OR REPLACE VIEW cache_stats AS
SELECT
s.id AS scope_id,
c.id AS cache_id,
COALESCE(ms.valid_indexes, cs.valid_archives) AS valid_indexes,
COALESCE(ms.indexes, cs.archives) AS indexes,
COALESCE(ms.valid_groups, cs.valid_files) AS valid_groups,
COALESCE(ms.groups, cs.files) AS groups,
COALESCE(ms.valid_keys, 0) AS valid_keys,
COALESCE(ms.keys, 0) AS keys,
COALESCE(ms.size, cs.size) AS size,
COALESCE(ms.blocks, cs.blocks) AS blocks
FROM scopes s
CROSS JOIN caches c
LEFT JOIN master_index_stats ms ON ms.scope_id = s.id AND ms.master_index_id = c.id
LEFT JOIN crc_table_stats cs ON s.name = 'runescape' AND cs.crc_table_id = c.id;
DROP MATERIALIZED VIEW master_index_stats_old;
DROP MATERIALIZED VIEW index_stats_old;

@ -1,95 +0,0 @@
-- @formatter:off
CREATE MATERIALIZED VIEW index_stats_new (
scope_id,
archive_id,
container_id,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
s.id AS scope_id,
g.group_id AS archive_id,
i.container_id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL) AS valid_groups,
COUNT(*) FILTER (WHERE ig.container_id IS NOT NULL) AS groups,
COUNT(*) FILTER (WHERE c.encrypted AND (c.key_id IS NOT NULL OR c.empty_loc)) AS valid_keys,
COUNT(*) FILTER (WHERE c.encrypted) AS keys,
COALESCE(SUM(length(c.data) + 2) FILTER (WHERE c.id IS NOT NULL), 0) AS size,
COALESCE(SUM(group_blocks(ig.group_id, length(c.data) + 2)) FILTER (WHERE c.id IS NOT NULL), 0) AS blocks
FROM scopes s
CROSS JOIN indexes i
JOIN groups g ON g.scope_id = s.id AND g.container_id = i.container_id AND g.archive_id = 255 AND
NOT g.version_truncated AND g.version = i.version
LEFT JOIN index_groups ig ON ig.container_id = i.container_id
LEFT JOIN resolve_group(s.id, g.group_id::uint1, ig.group_id, ig.crc32, ig.version) c ON TRUE
GROUP BY s.id, g.group_id, i.container_id;
CREATE UNIQUE INDEX ON index_stats_new (scope_id, archive_id, container_id);
ALTER MATERIALIZED VIEW index_stats RENAME TO index_stats_old;
ALTER INDEX index_stats_scope_id_archive_id_container_id_idx RENAME TO index_stats_old_scope_id_archive_id_container_id_idx;
ALTER MATERIALIZED VIEW index_stats_new RENAME TO index_stats;
ALTER INDEX index_stats_new_scope_id_archive_id_container_id_idx RENAME TO index_stats_scope_id_archive_id_container_id_idx;
CREATE MATERIALIZED VIEW master_index_stats_new (
scope_id,
master_index_id,
valid_indexes,
indexes,
valid_groups,
groups,
valid_keys,
keys,
size,
blocks
) AS
SELECT
sc.id,
m.id,
COUNT(*) FILTER (WHERE c.id IS NOT NULL OR (a.version = 0 AND a.crc32 = 0)) AS valid_indexes,
COUNT(*) FILTER (WHERE a.master_index_id IS NOT NULL) AS indexes,
SUM(COALESCE(s.valid_groups, 0)) AS valid_groups,
SUM(COALESCE(s.groups, 0)) AS groups,
SUM(COALESCE(s.valid_keys, 0)) AS valid_keys,
SUM(COALESCE(s.keys, 0)) AS keys,
SUM(COALESCE(s.size, 0)) + SUM(COALESCE(length(c.data), 0)) AS size,
SUM(COALESCE(s.blocks, 0)) + SUM(COALESCE(group_blocks(a.archive_id, length(c.data)), 0)) AS blocks
FROM scopes sc
CROSS JOIN master_indexes m
LEFT JOIN master_index_archives a ON a.master_index_id = m.id
LEFT JOIN resolve_index(sc.id, a.archive_id, a.crc32, a.version) c ON TRUE
LEFT JOIN index_stats s ON s.scope_id = sc.id AND s.archive_id = a.archive_id AND s.container_id = c.id
GROUP BY sc.id, m.id;
CREATE UNIQUE INDEX ON master_index_stats_new (scope_id, master_index_id);
ALTER MATERIALIZED VIEW master_index_stats RENAME TO master_index_stats_old;
ALTER INDEX master_index_stats_scope_id_master_index_id_idx RENAME TO master_index_stats_old_scope_id_master_index_id_idx;
ALTER MATERIALIZED VIEW master_index_stats_new RENAME TO master_index_stats;
ALTER INDEX master_index_stats_new_scope_id_master_index_id_idx RENAME TO master_index_stats_scope_id_master_index_id_idx;
CREATE OR REPLACE VIEW cache_stats AS
SELECT
s.id AS scope_id,
c.id AS cache_id,
COALESCE(ms.valid_indexes, cs.valid_archives) AS valid_indexes,
COALESCE(ms.indexes, cs.archives) AS indexes,
COALESCE(ms.valid_groups, cs.valid_files) AS valid_groups,
COALESCE(ms.groups, cs.files) AS groups,
COALESCE(ms.valid_keys, 0) AS valid_keys,
COALESCE(ms.keys, 0) AS keys,
COALESCE(ms.size, cs.size) AS size,
COALESCE(ms.blocks, cs.blocks) AS blocks
FROM scopes s
CROSS JOIN caches c
LEFT JOIN master_index_stats ms ON ms.scope_id = s.id AND ms.master_index_id = c.id
LEFT JOIN crc_table_stats cs ON s.name = 'runescape' AND cs.crc_table_id = c.id;
DROP MATERIALIZED VIEW master_index_stats_old;
DROP MATERIALIZED VIEW index_stats_old;

@ -1,53 +0,0 @@
-- @formatter:off
DROP VIEW cache_stats;
DROP MATERIALIZED VIEW crc_table_stats;
DROP MATERIALIZED VIEW version_list_stats;
CREATE MATERIALIZED VIEW version_list_stats AS
SELECT
v.blob_id,
vf.index_id,
COUNT(*) FILTER (WHERE b.id IS NOT NULL) AS valid_files,
COUNT(*) AS files,
SUM(length(b.data) + 2) FILTER (WHERE b.id IS NOT NULL) AS size,
SUM(group_blocks(vf.file_id, length(b.data) + 2)) AS blocks
FROM version_lists v
JOIN version_list_files vf ON vf.blob_id = v.blob_id
LEFT JOIN resolve_file(vf.index_id, vf.file_id, vf.version, vf.crc32) b ON TRUE
GROUP BY v.blob_id, vf.index_id;
CREATE UNIQUE INDEX ON version_list_stats (blob_id, index_id);
CREATE MATERIALIZED VIEW crc_table_stats AS
SELECT
c.id AS crc_table_id,
COUNT(*) FILTER (WHERE b.id IS NOT NULL AND a.crc32 <> 0) AS valid_archives,
COUNT(*) FILTER (WHERE a.crc32 <> 0) AS archives,
SUM(COALESCE(s.valid_files, 0)) AS valid_files,
SUM(COALESCE(s.files, 0)) AS files,
SUM(COALESCE(s.size, 0)) + SUM(COALESCE(length(b.data), 0)) AS size,
SUM(COALESCE(s.blocks, 0)) + SUM(COALESCE(group_blocks(a.archive_id, length(b.data)), 0)) AS blocks
FROM crc_tables c
LEFT JOIN crc_table_archives a ON a.crc_table_id = c.id
LEFT JOIN resolve_archive(a.archive_id, a.crc32) b ON TRUE
LEFT JOIN version_list_stats s ON s.blob_id = b.id
GROUP BY c.id;
CREATE UNIQUE INDEX ON crc_table_stats (crc_table_id);
CREATE VIEW cache_stats AS
SELECT
s.id AS scope_id,
c.id AS cache_id,
COALESCE(ms.valid_indexes, cs.valid_archives) AS valid_indexes,
COALESCE(ms.indexes, cs.archives) AS indexes,
COALESCE(ms.valid_groups, cs.valid_files) AS valid_groups,
COALESCE(ms.groups, cs.files) AS groups,
COALESCE(ms.valid_keys, 0) AS valid_keys,
COALESCE(ms.keys, 0) AS keys,
COALESCE(ms.size, cs.size) AS size,
COALESCE(ms.blocks, cs.blocks) AS blocks
FROM scopes s
CROSS JOIN caches c
LEFT JOIN master_index_stats ms ON ms.scope_id = s.id AND ms.master_index_id = c.id
LEFT JOIN crc_table_stats cs ON s.name = 'runescape' AND cs.crc_table_id = c.id;

@ -1,2 +0,0 @@
-- @formatter:off
ALTER TYPE key_source ADD VALUE 'hdos';

@ -1,3 +0,0 @@
-- @formatter:off
ALTER TYPE source_type ADD VALUE 'cross_pollination';

@ -1,7 +0,0 @@
-- @formatter:off
ALTER TABLE sources
ALTER COLUMN cache_id DROP NOT NULL,
ALTER COLUMN game_id DROP NOT NULL;
CREATE UNIQUE INDEX ON sources (type) WHERE type = 'cross_pollination';

@ -1,4 +1,3 @@
-- @formatter:off
ALTER TABLE games
DROP COLUMN hostname,
DROP COLUMN port,

@ -1,3 +1,2 @@
-- @formatter:off
ALTER TABLE games
DROP COLUMN key;

@ -1,43 +0,0 @@
var buildRegex = new RegExp('>([0-9]+)(?:[.]([0-9]+))?<');
function customSort(name, order, data) {
order = order === 'asc' ? 1 : -1;
data.sort(function (a, b) {
a = a[name];
b = b[name];
if (!a) {
return 1;
} else if (!b) {
return -1;
}
if (name === 'builds') {
return buildSort(a, b) * order;
} else {
if (a < b) {
return -order;
} else if (a === b) {
return 0;
} else {
return order;
}
}
});
}
function buildSort(a, b) {
a = buildRegex.exec(a);
b = buildRegex.exec(b);
var aMajor = parseInt(a[1]);
var bMajor = parseInt(b[1]);
if (aMajor !== bMajor) {
return aMajor - bMajor;
}
var aMinor = a[2] ? parseInt(a[2]) : 0;
var bMinor = b[2] ? parseInt(b[2]) : 0;
return aMinor - bMinor;
}

@ -1,285 +0,0 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:replace="layout.html :: head">
<title>API - OpenRS2 Archive</title>
<link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="/static/css/openrs2.css" />
<script src="/webjars/jquery/jquery.min.js" defer></script>
<script src="/webjars/bootstrap/js/bootstrap.bundle.min.js" defer></script>
</head>
<body>
<nav th:replace="layout.html :: nav"></nav>
<main class="container">
<h1>API</h1>
<p>All endpoints accept requests from any origin. Range requests are not supported by any endpoint.</p>
<h2><code>GET /caches.json</code></h2>
<p>
Returns a list of all caches, including all data available on the main <a href="/caches">caches</a>
page, in JSON format:
</p>
<pre><code>[
{
// The cache's internal ID.
"id": 1,
// A scope is a group of related games. Missing groups are only located
// from caches for games in the same scope.
//
// Currently the "runescape" scope is used for the "runescape" and
// "oldschool" games. Each FunOrb game has its own scope.
//
// Your code must be prepared for new scopes to be added in the future.
"scope": "runescape",
// The game's name. Your code must be prepared for new games to be
// added in the future.
"game": "runescape",
// Currently either "live" or "beta", but your code must be prepared
// for new environments to be added in the future.
"environment": "live",
// The language's ISO-639-1 code. Currently either "en", "de", "fr" or
// "pt", but your code must be prepared for new languages to be added
// in the future.
"language": "en",
// A list of build numbers the cache is associated with, which may be
// empty if the build number(s) are not known.
"builds": [
{
// The major number is always set.
"major": 549,
// The minor number may be null.
"minor": null
},
{
"major": 550,
"minor": null
}
],
// The earliest timestamp the cache was available to users, in ISO 8601
// format. May be null if not known.
"timestamp": "2009-06-12T14:55:58Z",
// A list of users who provided a copy of this cache.
//
// May be empty if the users wished to remain anonymous.
//
// The value "Jagex" indicates the cache was directly downloaded from
// Jagex's servers by the OpenRS2 project, so we are completely certain
// it is genuine. This value will never be used for a cache obtained
// from a third party.
"sources": [
"Erand",
"Hlwys",
"Jagex",
"K4rn4ge",
"Nathan",
"Rune-Wars"
],
// In old engine caches, the number of valid .jag archives that are not
// missing.
//
// In new engine caches, the number of valid JS5 indexes that are not
// missing.
//
// May be null if the cache is still being processed.
"valid_indexes": 29,
// In old engine caches, the total number of .jag archives that should
// exist, based on the cache's CRC table.
//
// In new engine caches, the total number of JS5 indexes that should
// exist, based on the JS5 master index.
//
// May be null if the cache is still being processed.
"indexes": 29,
// The number of valid files (old engine) or valid groups (new engine)
// that are not missing. May be null if the cache is still being processed.
"valid_groups": 71002,
// In old engine caches, the total number of files that should exist,
// based on the cache's versionlist.jag archive.
//
// In new engine caches, the total number of groups that should exist,
// based on the JS5 indexes that are available.
//
// May be null if the cache is still being processed.
"groups": 71146,
// The number of encrypted groups for which a valid key is available.
// May be null if the cache is still being processed.
"valid_keys": 1203,
// The total number of encrypted groups in the cache. May be null if
// the cache is still being processed.
"keys": 1240,
// The total size of all groups in the cache in bytes. May be null if
// the cache is still being processed.
"size": 74970573,
// The number of 520-byte blocks required to store the cache's data in
// a .dat2 file. May be null if the cache is still being processed.
"blocks": 185273,
// A boolean flag indicating if the cache is small enough to be
// downloaded in .dat2/.idx format. May be null if the cache is still
// being processed.
"disk_store_valid": true
},
...
]</code></pre>
<h2><code>GET /caches/&lt;scope&gt;/&lt;id&gt;/disk.zip</code></h2>
<p>
Returns a cache as a ZIP archive of <code>.dat/.idx</code>
(old engine) or <code>.dat2/.idx</code> (new engine) files. All
files are stored underneath a <code>cache</code> subdirectory
in the zip archive.
</p>
<h2><code>GET /caches/&lt;scope&gt;/&lt;id&gt;/flat-file.tar.gz</code></h2>
<p>
Returns a cache as a gzipped tarball of files, where each
file in the tarball holds a single file from the cache (old
engine) or single group (new engine).
</p>
<p>
The paths within the archive all have a format of
<code>cache/&lt;index&gt;/&lt;file&gt;.dat</code> (old engine)
or <code>cache/&lt;archive&gt;/&lt;group&gt;.dat</code> (new
engine).
</p>
<p>The two byte version trailers are included.</p>
<h2><code>GET /caches/&lt;scope&gt;/&lt;id&gt;/keys.json</code></h2>
<p>Returns a list of valid XTEA keys for the cache in JSON format:</p>
<pre><code>[
{
// The ID of the archive containing the group the key is used for.
// Typically this is 5 (maps), but do note that RuneScape 3 does
// support encrypting interfaces, though the functionality has not yet
// been used, and some FunOrb games also have encrypted groups.
"archive": 5,
// The ID of the group the key is used for.
"group": 1,
// The group's name hash, or null if the group has no name.
"name_hash": -1153472937,
// The name of the group, if available, or null if the group has no
// name or if the name is not known.
"name": "l40_55",
// The ID of the map square, if the group is an encrypted loc group
// (has a name of lX_Z). The map square ID is ((X &lt;&lt; 8) | Z).
// null if the group is not an encrypted loc group.
"mapsquare": 10295,
// The XTEA key, represented as four 32-bit integers.
"key": [
-1920480496,
-1423914110,
951774544,
-1419269290
]
},
...
]</code></pre>
<h2><code>GET /caches/&lt;scope&gt;/&lt;id&gt;/keys.zip</code></h2>
<p>
Returns a zip archive file of valid XTEA keys for loc groups.
Each key is stored in a text file containing four lines, with
each line containing a 32-bit component of the key as a decimal
string. The paths within the archive all have a format of
<code>keys/&lt;mapsquare&gt;.txt</code>.
</p>
<h2><code>GET /caches/&lt;scope&gt;/&lt;id&gt;/map.png</code></h2>
<p>
Renders the map squares in the cache, with a coloured outline
representing whether we have a valid key for each map square or
not:
</p>
<ul>
<li><strong>Valid key:</strong> green outline.</li>
<li><strong>Loc group is not encrypted:</strong> green outline.</li>
<li><strong>Empty loc group:</strong> grey outline.</li>
<li><strong>Key unknown:</strong> red outline.</li>
</ul>
<p>
Empty loc groups may be replaced with an unencrypted equivalent
with a cache editor.
</p>
<h2><code>GET /caches/&lt;scope&gt;/&lt;id&gt;/archives/&lt;archive&gt;/groups/&lt;group&gt;.dat</code></h2>
<p>
Returns a single file (old engine) or group (new engine) in
binary format. The response contains a <code>.jag</code>
archive (index 0 of an old engine cache), a GZIP-compressed
file (the remaining indexes of an old engine cache) or
JS5-compressed data (new engine cache, also known as a
container). The two byte version trailer is not included.
</p>
<h2><code>GET /keys/all.json</code></h2>
<p>
Returns a list of all XTEA keys in the database, including
candidate keys that have not been validated against any cache.
</p>
<pre><code>[
// The XTEA key, represented as four 32-bit integers.
[
-2147135705,
1113423446,
1294100345,
946019601
],
...
]</code></pre>
<h2><code>GET /keys/valid.json</code></h2>
<p>
Returns a list of XTEA keys in the database, only including
keys validated against at least one cache.
</p>
<pre><code>[
// The XTEA key, represented as four 32-bit integers.
[
-2147135705,
1113423446,
1294100345,
946019601
],
...
]</code></pre>
</main>
</body>
</html>

@ -12,7 +12,7 @@
<main class="container">
<h1>Caches</h1>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" data-toggle="table" data-filter-control="true" data-sticky-header="true" data-custom-sort="customSort">
<table class="table table-striped table-bordered table-hover" data-toggle="table" data-filter-control="true" data-sticky-header="true">
<thead class="table-dark">
<tr>
<th data-field="game" data-filter-control="select">Game</th>
@ -34,22 +34,20 @@
<td th:text="${cache.game}">runescape</td>
<td th:text="${cache.environment}">live</td>
<td th:text="${cache.language}">en</td>
<td class="text-end">
<td class="text-right">
<span th:each="build, it : ${cache.builds}" th:remove="tag">
<span th:text="${build}">550</span>
<br th:remove="${it.last}? 'all' : 'none'" />
</span>
</td>
<td>
<span th:if="${cache.timestamp}" th:remove="tag">
<span th:text="${#temporals.format(cache.timestamp, 'yyyy-MM-dd')}"></span>
<br />
<span th:text="${#temporals.format(cache.timestamp, 'HH:mm:ss')}"></span>
</span>
<span th:text="${#temporals.format(cache.timestamp, 'yyyy-MM-dd')}"></span>
<br />
<span th:text="${#temporals.format(cache.timestamp, 'HH:mm:ss')}"></span>
</td>
<td th:text="${#strings.setJoin(cache.sources, ', ')}"></td>
<td th:classappend="${cache.stats}? (${cache.stats.allIndexesValid}? 'table-success' : 'table-danger')"
class="text-end">
class="text-right">
<span
th:text="${cache.stats}? ${cache.stats.validIndexes} + '&nbsp;/&nbsp;' + ${cache.stats.indexes} : 'Calculating...'"></span>
<br />
@ -57,7 +55,7 @@
th:text="${cache.stats}? '(' + ${#numbers.formatPercent(cache.stats.validIndexesFraction, 1, 2)} + ')'"></span>
</td>
<td th:classappend="${cache.stats}? (${cache.stats.allGroupsValid}? 'table-success' : 'table-warning')"
class="text-end">
class="text-right">
<span
th:text="${cache.stats}? ${#numbers.formatInteger(cache.stats.validGroups, 1, 'COMMA')} + '&nbsp;/&nbsp;' + ${#numbers.formatInteger(cache.stats.groups, 1, 'COMMA')} : 'Calculating...'"></span>
<br />
@ -65,7 +63,7 @@
th:text="${cache.stats}? '(' + ${#numbers.formatPercent(cache.stats.validGroupsFraction, 1, 2)} + ')'"></span>
</td>
<td th:classappend="${cache.stats}? (${cache.stats.allKeysValid}? 'table-success' : 'table-warning')"
class="text-end">
class="text-right">
<span
th:text="${cache.stats}? ${#numbers.formatInteger(cache.stats.validKeys, 1, 'COMMA')} + '&nbsp;/&nbsp;' + ${#numbers.formatInteger(cache.stats.keys, 1, 'COMMA')} : 'Calculating...'"></span>
<br />
@ -74,7 +72,7 @@
</td>
<!--/*@thymesVar id="#byteunits" type="org.openrs2.archive.web.ByteUnits"*/-->
<td th:text="${cache.stats}? ${#byteunits.format(cache.stats.size)} : 'Calculating...'"
class="text-end">Calculating...
class="text-right">Calculating...
</td>
<td>
<div class="btn-group">
@ -86,25 +84,25 @@
</button>
<ul class="dropdown-menu">
<li th:if="${cache.stats != null and cache.stats.diskStoreValid}"><a
th:href="${'/caches/' + cache.scope + '/' + cache.id + '/disk.zip'}"
th:href="${'/caches/' + cache.id + '/disk.zip'}"
class="dropdown-item">Cache (.dat2/.idx)</a></li>
<li><a th:href="${'/caches/' + cache.scope + '/' + cache.id + '/flat-file.tar.gz'}"
<li><a th:href="${'/caches/' + cache.id + '/flat-file.tar.gz'}"
class="dropdown-item">Cache (Flat file)</a></li>
<li>
<hr class="dropdown-divider" />
</li>
<li><a th:href="${'/caches/' + cache.scope + '/' + cache.id + '/keys.json'}"
<li><a th:href="${'/caches/' + cache.id + '/keys.json'}"
class="dropdown-item">Keys (JSON)</a></li>
<li><a th:href="${'/caches/' + cache.scope + '/' + cache.id + '/keys.zip'}"
<li><a th:href="${'/caches/' + cache.id + '/keys.zip'}"
class="dropdown-item">Keys (Text)</a></li>
<li>
<hr class="dropdown-divider" />
</li>
<li><a th:href="${'/caches/' + cache.scope + '/' + cache.id + '/map.png'}"
<li><a th:href="${'/caches/' + cache.id + '/map.png'}"
class="dropdown-item">Map</a></li>
</ul>
</div>
<a th:href="${'/caches/' + cache.scope + '/' + cache.id}"
<a th:href="${'/caches/' + cache.id}"
class="btn btn-secondary btn-sm">More</a>
</div>
</td>
@ -112,10 +110,6 @@
</tbody>
</table>
</div>
<p>
The total size of all caches in the archive is
<strong th:text="${#byteunits.format(totalSize)}">0 B</strong>.
</p>
<p>
<sup id="empty-locs">1</sup> Map squares in the middle of the
sea are unreachable by normal players, making it impossible to

@ -52,19 +52,19 @@
<div class="btn-toolbar">
<div class="btn-group me-2">
<a th:if="${cache.stats != null and cache.stats.diskStoreValid}"
th:href="${'/caches/' + scope + '/' + cache.id + '/disk.zip'}"
th:href="${'/caches/' + cache.id + '/disk.zip'}"
class="btn btn-primary btn-sm">Cache (.dat2/.idx)</a>
<a th:href="${'/caches/' + scope + '/' + cache.id + '/flat-file.tar.gz'}"
<a th:href="${'/caches/' + cache.id + '/flat-file.tar.gz'}"
class="btn btn-primary btn-sm">Cache (Flat file)</a>
</div>
<div class="btn-group me-2">
<a th:href="${'/caches/' + scope + '/' + cache.id + '/keys.json'}"
<a th:href="${'/caches/' + cache.id + '/keys.json'}"
class="btn btn-primary btn-sm">Keys (JSON)</a>
<a th:href="${'/caches/' + scope + '/' + cache.id + '/keys.zip'}"
<a th:href="${'/caches/' + cache.id + '/keys.zip'}"
class="btn btn-primary btn-sm">Keys (Text)</a>
</div>
<div class="btn-group">
<a th:href="${'/caches/' + scope + '/' + cache.id + '/map.png'}"
<a th:href="${'/caches/' + cache.id + '/map.png'}"
class="btn btn-primary btn-sm">Map</a>
</div>
</div>
@ -102,7 +102,7 @@
<td th:text="${source.game}">runescape</td>
<td th:text="${source.environment}">live</td>
<td th:text="${source.language}">en</td>
<td th:text="${source.build}" class="text-end">550</td>
<td th:text="${source.build}" class="text-right">550</td>
<td th:text="${#temporals.format(source.timestamp, 'yyyy-MM-dd HH:mm:ss')}"></td>
<td th:text="${source.name}"></td>
<td th:text="${source.description}"></td>
@ -124,43 +124,37 @@
<th>Archive</th>
<th>Version</th>
<th>Checksum</th>
<th>Digest</th>
<th>Groups</th>
<th>Keys<sup><a href="/caches#empty-locs">1</a></sup></th>
<th>Size<sup><a href="/caches#size">2</a></sup></th>
<th>Total uncompressed length</th>
</tr>
</thead>
<tbody>
<tr th:each="entry, it : ${cache.masterIndex.entries}" th:with="archive=${cache.archives[it.index]}">
<td th:text="${it.index}" class="text-end">0</td>
<td th:text="${#numbers.formatInteger(entry.version, 1, 'COMMA')}" class="text-end">0</td>
<td class="text-end">
<tr th:each="entry, it : ${cache.masterIndex.entries}">
<td th:text="${it.index}" class="text-right">0</td>
<td th:text="${#numbers.formatInteger(entry.version, 1, 'COMMA')}" class="text-right">0</td>
<td class="text-right">
<code th:text="${entry.checksum}">0</code>
</td>
<div th:switch="true" th:remove="tag">
<div th:case="${archive.stats != null}" th:remove="tag">
<td th:classappend="${archive.stats.allGroupsValid}? 'table-success' : 'table-warning'" class="text-end">
<span th:text="${#numbers.formatInteger(archive.stats.validGroups, 1, 'COMMA')} + '&nbsp;/&nbsp;' + ${#numbers.formatInteger(archive.stats.groups, 1, 'COMMA')}"></span>
<br />
<span th:text="'(' + ${#numbers.formatPercent(archive.stats.validGroupsFraction, 1, 2)} + ')'"></span>
</td>
<td th:classappend="${archive.stats.allKeysValid}? 'table-success' : 'table-warning'" class="text-end">
<span th:text="${#numbers.formatInteger(archive.stats.validKeys, 1, 'COMMA')} + '&nbsp;/&nbsp;' + ${#numbers.formatInteger(archive.stats.keys, 1, 'COMMA')}"></span>
<br />
<span th:text="'(' + ${#numbers.formatPercent(archive.stats.validKeysFraction, 1, 2)} + ')'"></span>
</td>
<!--/*@thymesVar id="#byteunits" type="org.openrs2.archive.web.ByteUnits"*/-->
<td th:text="${#byteunits.format(archive.stats.size)}" class="text-end">0 B</td>
</div>
<div th:case="${archive.resolved}" th:remove="tag">
<td class="text-center" colspan="3">Calculating...</td>
</div>
<div th:case="${entry.checksum != 0 || entry.version != 0}" th:remove="tag">
<td class="text-center table-danger" colspan="3">Index missing</td>
</div>
<div th:case="true" th:remove="tag">
<td class="text-center text-muted" colspan="3">N/A</td>
</div>
</div>
<td>
<code
th:if="${cache.masterIndex.format >= @org.openrs2.cache.MasterIndexFormat@DIGESTS}"><span
th:remove="tag"
th:text="${@io.netty.buffer.ByteBufUtil@hexDump(entry.digest).substring(0, 64)}"></span>&ZeroWidthSpace;<span
th:remove="tag"
th:text="${@io.netty.buffer.ByteBufUtil@hexDump(entry.digest).substring(64)}"></span></code>
</td>
<td class="text-right">
<span
th:if="${cache.masterIndex.format >= @org.openrs2.cache.MasterIndexFormat@LENGTHS}"
th:text="${#numbers.formatInteger(entry.groups, 1, 'COMMA')}"></span>
</td>
<td class="text-right">
<!--/*@thymesVar id="#byteunits" type="org.openrs2.archive.web.ByteUnits"*/-->
<span
th:if="${cache.masterIndex.format >= @org.openrs2.cache.MasterIndexFormat@LENGTHS}"
th:text="${#byteunits.format(@java.lang.Integer@toUnsignedLong(entry.totalUncompressedLength))}"></span>
</td>
</tr>
</tbody>
</table>
@ -176,54 +170,18 @@
<tr>
<th>Archive</th>
<th>Checksum</th>
<th>Size<sup><a href="/caches#size">2</a></sup></th>
</tr>
</thead>
<tbody>
<tr th:each="entry, it : ${cache.checksumTable.entries}" th:with="archive=${cache.archives[it.index]}">
<td th:text="${it.index}" class="text-end">0</td>
<td class="text-end">
<tr th:each="entry, it : ${cache.checksumTable.entries}">
<td th:text="${it.index}" class="text-right">0</td>
<td class="text-right">
<code th:text="${entry}">0</code>
</td>
<div th:switch="true" th:remove="tag">
<!--/*@thymesVar id="#byteunits" type="org.openrs2.archive.web.ByteUnits"*/-->
<td th:case="${archive.stats != null}" th:text="${#byteunits.format(archive.stats.size)}" class="text-end">0 B</td>
<td th:case="${archive.resolved}" class="text-center">Calculating...</td>
<td th:case="${entry != 0}" class="text-center table-danger">Missing</td>
<td th:case="true" class="text-center text-muted">N/A</td>
</div>
</tr>
</tbody>
</table>
</div>
<div th:if="${cache.indexes}" th:remove="tag">
<h2>Version list</h2>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead class="table-dark">
<tr>
<th>Index</th>
<th>Files</th>
<th>Size<sup><a href="/caches#size">2</a></sup></th>
</tr>
</thead>
<tbody>
<tr th:each="index, it : ${cache.indexes}">
<td th:text="${it.index + 1}" class="text-end">0</td>
<td th:classappend="${index.allFilesValid}? 'table-success' : 'table-warning'" class="text-end">
<span th:text="${#numbers.formatInteger(index.validFiles, 1, 'COMMA')} + '&nbsp;/&nbsp;' + ${#numbers.formatInteger(index.files, 1, 'COMMA')}"></span>
<br />
<span th:text="'(' + ${#numbers.formatPercent(index.validFilesFraction, 1, 2)} + ')'"></span>
</td>
<!--/*@thymesVar id="#byteunits" type="org.openrs2.archive.web.ByteUnits"*/-->
<td th:text="${#byteunits.format(index.size)}" class="text-end">0 B</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</main>
</body>

@ -52,7 +52,7 @@
<ul>
<li><a href="https://displee.com/archive/">Displee's archive</a></li>
<li><a href="https://gregs.world/archive/">Greg's archive</a></li>
<li><a href="https://www.hdos.dev/">HDOS</a></li>
<li><a href="https://openosrs.com/">OpenOSRS</a></li>
<li><a href="https://archive.runestats.com/">Polar's archive</a></li>
<!-- We don't use Moparisthebest's or RS-Hacking's -->
<!-- data yet, but we will once we start archiving clients. -->
@ -60,7 +60,6 @@
<!-- <li><a href="https://rs-hacking.com/">RS-Hacking</a></li> -->
<li><a href="https://runearchive.org/">RuneArchive</a></li>
<li><a href="https://runelite.net/">RuneLite</a></li>
<li><a href="https://rs-archive.github.io/">The RuneScape Archive</a></li>
<li><a href="https://runescape.wiki/w/User:Manpaint55/RPU">The RuneScape Preservation Unit</a></li>
<li><a href="http://runestar.org/">RuneStar</a></li>
<li><a href="https://www.runewiki.org/">RuneWiki</a></li>

@ -13,7 +13,6 @@
<script src="/webjars/bootstrap-table/dist/bootstrap-table.min.js" defer></script>
<script src="/webjars/bootstrap-table/dist/extensions/filter-control/bootstrap-table-filter-control.min.js" defer></script>
<script src="/webjars/bootstrap-table/dist/extensions/sticky-header/bootstrap-table-sticky-header.min.js" defer></script>
<script src="/static/js/openrs2.js" defer></script>
</head>
<body>
<nav class="navbar navbar-dark navbar-expand bg-dark mb-4" th:fragment="nav">
@ -27,9 +26,6 @@
<li class="nav-item">
<a class="nav-link" th:classappend="${active == 'keys'}? 'active'" href="/keys">Keys</a>
</li>
<li class="nav-item">
<a class="nav-link" th:classappend="${active == 'api'}? 'active'" href="/api">API</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/pub">Other</a>
</li>

@ -6,7 +6,7 @@ plugins {
dependencies {
api(projects.util)
api(libs.bundles.asm)
api(libs.bundles.guice)
api(libs.guice)
api(libs.jackson.databind)
api(libs.netty.buffer)

@ -109,14 +109,12 @@ public fun AbstractInsnNode.remap(remapper: ExtendedRemapper) {
name = remapper.mapFieldName(originalOwner, name, desc)
desc = remapper.mapDesc(desc)
}
is MethodInsnNode -> {
val originalOwner = owner
owner = remapper.mapMethodOwner(originalOwner, name, desc)
name = remapper.mapMethodName(originalOwner, name, desc)
desc = remapper.mapDesc(desc)
}
is InvokeDynamicInsnNode -> throw UnsupportedOperationException()
is TypeInsnNode -> desc = remapper.mapType(desc)
is LdcInsnNode -> cst = remapper.mapValue(cst)

@ -245,7 +245,6 @@ public val AbstractInsnNode.intConstant: Int?
null
}
}
is LdcInsnNode -> {
val cst = cst
if (cst is Int) {
@ -254,7 +253,6 @@ public val AbstractInsnNode.intConstant: Int?
null
}
}
else -> when (opcode) {
Opcodes.ICONST_M1 -> -1
Opcodes.ICONST_0 -> 0

@ -99,7 +99,6 @@ public fun MethodNode.removeArgument(argIndex: Int) {
newLocalIndexUsed = true
}
}
is IincInsnNode -> {
insn.`var` = remap(insn.`var`, argType, localIndex, newLocalIndex)
@ -107,7 +106,6 @@ public fun MethodNode.removeArgument(argIndex: Int) {
newLocalIndexUsed = true
}
}
is FrameNode -> throw UnsupportedOperationException("SKIP_FRAMES and COMPUTE_FRAMES must be used")
}
}

@ -188,7 +188,6 @@ public val AbstractInsnNode.stackMetadata: StackMetadata
} else {
PUSH1
}
is FieldInsnNode -> {
val fieldSize = Type.getType(desc).size
var pushes = 0
@ -203,7 +202,6 @@ public val AbstractInsnNode.stackMetadata: StackMetadata
}
StackMetadata(pops, pushes)
}
is MethodInsnNode -> {
val argumentsAndReturnSizes = Type.getArgumentsAndReturnSizes(desc)
val pushes = argumentsAndReturnSizes and 0x3
@ -213,7 +211,6 @@ public val AbstractInsnNode.stackMetadata: StackMetadata
}
StackMetadata(pops, pushes)
}
is InvokeDynamicInsnNode -> throw UnsupportedOperationException()
is MultiANewArrayInsnNode -> StackMetadata(dims, 1)
else -> SIMPLE_OPCODES[opcode] ?: throw IllegalArgumentException()

@ -35,7 +35,6 @@ public object Glob {
} else {
regex.append(".*")
}
else -> regex.append(Regex.escape(ch.toString()))
}
}

@ -94,7 +94,6 @@ public class ConstantPool private constructor(
addMethodRef(methodRef)
}
}
is FieldInsnNode -> addFieldRef(MemberRef(insn.owner, insn.name, insn.desc))
is TypeInsnNode -> strings += insn.desc
}
@ -156,7 +155,6 @@ public class ConstantPool private constructor(
throw IllegalArgumentException("Unsupported constant type: ${value.sort}")
}
}
else -> throw IllegalArgumentException("Unsupported constant type: ${value.javaClass.name}")
}
}

@ -42,7 +42,7 @@ public object PackClass {
Opcodes.V1_3,
Opcodes.V1_4,
Opcodes.V1_5,
Opcodes.V1_6
Opcodes.V1_6,
)
private const val INT_DESCRIPTOR = "I"
@ -503,13 +503,11 @@ public object PackClass {
branchLen += (cases + 2) * 4
sipushAndSwitchLen += 4
}
Opcodes.LOOKUPSWITCH -> {
val cases = buf.readVarInt()
branchLen += (cases + 1) * 4
sipushAndSwitchLen += cases * 4
}
Opcodes.INVOKEINTERFACE -> interfaceMethodRefLen += 2
Opcodes.NEWARRAY -> newArrayLen++
Opcodes.MULTIANEWARRAY -> multiNewArrayLen++
@ -1157,12 +1155,10 @@ public object PackClass {
)
}
}
insn.`var` < 256 -> {
buf.writeByte(insn.opcode)
localVarBuf.writeByte(insn.`var`)
}
else -> {
buf.writeByte(WIDE)
buf.writeByte(insn.opcode)
@ -1170,34 +1166,28 @@ public object PackClass {
}
}
}
is LdcInsnNode -> {
when (val value = insn.cst) {
is Int -> {
buf.writeByte(LDC_INT)
constantPool.writeInt(constantBuf, value)
}
is Long -> {
buf.writeByte(LDC_LONG)
constantPool.writeLong(wideConstantBuf, value)
}
is Float -> {
buf.writeByte(LDC_FLOAT)
constantPool.writeFloat(constantBuf, value)
}
is Double -> {
buf.writeByte(LDC_DOUBLE)
constantPool.writeDouble(wideConstantBuf, value)
}
is String -> {
buf.writeByte(LDC_STRING)
constantPool.writeString(constantBuf, value)
}
is Type -> {
if (value.sort == Type.OBJECT) {
buf.writeByte(LDC_CLASS)
@ -1208,23 +1198,19 @@ public object PackClass {
)
}
}
else -> throw IllegalArgumentException(
"Unsupported constant type: ${value.javaClass.name}"
)
}
}
is TypeInsnNode -> {
buf.writeByte(insn.opcode)
constantPool.writeString(classBuf, insn.desc)
}
is FieldInsnNode -> {
buf.writeByte(insn.opcode)
constantPool.writeFieldRef(fieldRefBuf, MemberRef(insn.owner, insn.name, insn.desc))
}
is MethodInsnNode -> {
buf.writeByte(insn.opcode)
@ -1235,7 +1221,6 @@ public object PackClass {
constantPool.writeMethodRef(methodRefBuf, methodRef)
}
}
is JumpInsnNode -> {
val targetPc = insns[i].indexOf(insn.label.nextReal)
val delta = targetPc - pc
@ -1254,7 +1239,6 @@ public object PackClass {
branchBuf.writeInt(delta)
}
}
is IntInsnNode -> {
buf.writeByte(insn.opcode)
@ -1265,7 +1249,6 @@ public object PackClass {
else -> throw IllegalArgumentException("Unsupported IntInsnNode opcode: ${insn.opcode}")
}
}
is IincInsnNode -> {
if (insn.`var` < 256 && (insn.incr >= -128 && insn.incr <= 127)) {
buf.writeByte(insn.opcode)
@ -1278,7 +1261,6 @@ public object PackClass {
wideIincBuf.writeShort(insn.incr)
}
}
is TableSwitchInsnNode -> {
buf.writeByte(insn.opcode)
@ -1293,7 +1275,6 @@ public object PackClass {
branchBuf.writeInt(targetPc - pc)
}
}
is LookupSwitchInsnNode -> {
buf.writeByte(insn.opcode)
@ -1314,13 +1295,11 @@ public object PackClass {
branchBuf.writeInt(targetPc - pc)
}
}
is MultiANewArrayInsnNode -> {
buf.writeByte(insn.opcode)
constantPool.writeString(classBuf, insn.desc)
multiNewArrayBuf.writeByte(insn.dims)
}
is InsnNode -> buf.writeByte(insn.opcode)
else -> throw IllegalArgumentException("Unsupported instruction type: ${insn.javaClass.name}")
}
@ -1573,7 +1552,6 @@ public object PackClass {
throw IllegalArgumentException("Unsupported constant type: ${value.sort}")
}
}
else -> throw IllegalArgumentException("Unsupported constant type: ${value.javaClass.name}")
}
}

@ -5,7 +5,7 @@ plugins {
}
application {
mainClass.set("org.openrs2.buffer.generator.GenerateBufferCommandKt")
mainClass.set("org.openrs2.buffer.generator.GenerateBufferCommand")
}
dependencies {

@ -9,7 +9,7 @@ public class ByteBufExtensionGenerator {
public fun generate(): String {
val builder = FileSpec.builder("org.openrs2.buffer", "GeneratedByteBufExtensions")
builder.indent(" ")
builder.addFileComment("This file is generated automatically. DO NOT EDIT.")
builder.addComment("This file is generated automatically. DO NOT EDIT.")
for (type in IntType.values()) {
for (order in ByteOrder.values()) {

@ -19,7 +19,6 @@ public enum class ByteOrder(public val suffix: String) {
else -> 8
}
}
ALT3_REVERSE -> {
require(width == 4)
when (i) {

@ -4,7 +4,7 @@ plugins {
}
dependencies {
api(libs.bundles.guice)
api(libs.guice)
api(libs.netty.buffer)
implementation(projects.util)

@ -1,5 +1,6 @@
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jmailen.gradle.kotlinter.KotlinterExtension
import org.jmailen.gradle.kotlinter.KotlinterPlugin
@ -52,7 +53,7 @@ allprojects {
plugins.withType<KotlinterPlugin> {
configure<KotlinterExtension> {
// see https://github.com/pinterest/ktlint/issues/764
disabledRules = arrayOf("argument-list-wrapping", "parameter-list-wrapping", "wrapping")
disabledRules = arrayOf("indent", "parameter-list-wrapping")
}
}
@ -125,6 +126,15 @@ configure(subprojects.filter { it.isFree }) {
apply(plugin = "org.jmailen.kotlinter")
dependencies {
val api by configurations
for (module in listOf("stdlib", "stdlib-common", "stdlib-jdk7", "stdlib-jdk8")) {
api("org.jetbrains.kotlin:kotlin-$module") {
version {
strictly(project.getKotlinPluginVersion())
}
}
}
val implementation by configurations
implementation(kotlin("reflect"))
implementation(libs.inlineLogger)

@ -5,7 +5,7 @@ plugins {
dependencies {
api(projects.cache)
api(libs.bundles.guice)
api(libs.guice)
implementation(projects.buffer)
implementation(projects.util)

@ -37,7 +37,6 @@ public class EnumType(id: Int) : ConfigType(id) {
this.strings = strings
}
6 -> {
val size = buf.readUnsignedShort()
val ints = Int2IntOpenHashMap()
@ -49,7 +48,6 @@ public class EnumType(id: Int) : ConfigType(id) {
this.ints = ints
}
else -> throw IllegalArgumentException("Unsupported config code: $code")
}
}

@ -26,7 +26,6 @@ public class StructType(id: Int) : ConfigType(id) {
}
}
}
else -> throw IllegalArgumentException("Unsupported config code: $code")
}
}
@ -43,13 +42,11 @@ public class StructType(id: Int) : ConfigType(id) {
buf.writeMedium(id)
buf.writeString(value)
}
is Int -> {
buf.writeBoolean(false)
buf.writeMedium(id)
buf.writeInt(value)
}
else -> throw IllegalStateException()
}
}

@ -15,7 +15,6 @@ public class VarbitType(id: Int) : ConfigType(id) {
startBit = buf.readUnsignedByte().toInt()
endBit = buf.readUnsignedByte().toInt()
}
else -> throw IllegalArgumentException("Unsupported config code: $code")
}
}

@ -196,13 +196,11 @@ public object Song {
onVelocity = (onVelocity + onVelocityBuf.readUnsignedByte().toInt()) and 0x7F
ShortMessage(ShortMessage.NOTE_ON, channel, key, onVelocity)
}
NOTE_OFF -> {
key = (key + keyBuf.readUnsignedByte().toInt()) and 0x7F
offVelocity = (offVelocity + offVelocityBuf.readUnsignedByte().toInt()) and 0x7F
ShortMessage(ShortMessage.NOTE_OFF, channel, key, offVelocity)
}
CONTROL_CHANGE -> {
controller = (controller + controllerBuf.readUnsignedByte()) and 0x7F
@ -220,7 +218,6 @@ public object Song {
REGISTERED_LSB -> registeredLsbBuf.readUnsignedByte().toInt()
DAMPER, PORTAMENTO, ALL_SOUND_OFF, RESET_CONTROLLERS, ALL_NOTES_OFF ->
otherKnownControllerBuf.readUnsignedByte().toInt()
else -> unknownControllerBuf.readUnsignedByte().toInt()
}
@ -228,30 +225,25 @@ public object Song {
values[controller] = value
ShortMessage(ShortMessage.CONTROL_CHANGE, channel, controller, value and 0x7F)
}
PITCH_WHEEL_CHANGE -> {
pitchWheel += pitchWheelLsbBuf.readUnsignedByte().toInt()
pitchWheel += (pitchWheelMsbBuf.readUnsignedByte().toInt() shl 7)
pitchWheel = pitchWheel and 0x3FFF
ShortMessage(ShortMessage.PITCH_BEND, channel, pitchWheel and 0x7F, pitchWheel shr 7)
}
CHANNEL_PRESSURE_CHANGE -> {
channelPressure = (channelPressure + channelPressureBuf.readUnsignedByte().toInt()) and 0x7F
ShortMessage(ShortMessage.CHANNEL_PRESSURE, channel, channelPressure, 0)
}
KEY_PRESSURE_CHANGE -> {
key = (key + keyBuf.readUnsignedByte().toInt()) and 0x7F
keyPressure = (keyPressure + keyPressureBuf.readUnsignedByte().toInt()) and 0x7F
ShortMessage(ShortMessage.POLY_PRESSURE, channel, key, keyPressure)
}
PROGRAM_CHANGE -> {
val bankSelect = bankSelectBuf.readUnsignedByte().toInt()
ShortMessage(ShortMessage.PROGRAM_CHANGE, channel, bankSelect, 0)
}
else -> throw IllegalStateException()
}
@ -325,11 +317,9 @@ public object Song {
require(message.data.size == 3)
tempoBuf.writeBytes(message.data)
}
else -> throw IllegalArgumentException("Unsupported meta type: $type")
}
}
is ShortMessage -> {
val command = message.status and 0xF0
val channel = message.status and 0xF
@ -364,13 +354,11 @@ public object Song {
onVelocityBuf.writeByte((onVelocity - prevOnVelocity) and 0x7F)
prevOnVelocity = onVelocity
}
ShortMessage.NOTE_OFF -> {
val offVelocity = message.data2
offVelocityBuf.writeByte((offVelocity - prevOffVelocity) and 0x7F)
prevOffVelocity = offVelocity
}
ShortMessage.CONTROL_CHANGE -> {
val controller = message.data1
controllerBuf.writeByte((controller - prevController) and 0x7F)
@ -393,13 +381,11 @@ public object Song {
REGISTERED_LSB -> registeredLsbBuf.writeByte(valueDelta)
DAMPER, PORTAMENTO, ALL_SOUND_OFF, RESET_CONTROLLERS, ALL_NOTES_OFF ->
otherKnownControllerBuf.writeByte(valueDelta)
else -> unknownControllerBuf.writeByte(valueDelta)
}
prevValues[controller] = value
}
ShortMessage.PITCH_BEND -> {
val pitchWheel = message.data1 or (message.data2 shl 7)
val pitchWheelDelta = (pitchWheel - prevPitchWheel) and 0x3FFF
@ -407,27 +393,22 @@ public object Song {
pitchWheelMsbBuf.writeByte(pitchWheelDelta shr 7)
prevPitchWheel = pitchWheel
}
ShortMessage.CHANNEL_PRESSURE -> {
val channelPressure = message.data1
channelPressureBuf.writeByte((channelPressure - prevChannelPressure) and 0x7F)
prevChannelPressure = channelPressure
}
ShortMessage.POLY_PRESSURE -> {
val keyPressure = message.data2
keyPressureBuf.writeByte((keyPressure - prevKeyPressure) and 0x7F)
prevKeyPressure = keyPressure
}
ShortMessage.PROGRAM_CHANGE -> {
bankSelectBuf.writeByte(message.data1)
}
else -> throw IllegalStateException()
}
}
else -> throw IllegalArgumentException("Unsupported message type: ${message.javaClass.name}")
}
}

@ -14,7 +14,7 @@ public class Sprite private constructor(
val xOffset: Int,
val yOffset: Int,
val innerWidth: Int,
val innerHeight: Int
val innerHeight: Int,
) {
val pixels = ByteArray(innerWidth * innerHeight)
var alpha: ByteArray? = null

@ -1,15 +0,0 @@
package org.openrs2.cache.cli
import com.github.ajalt.clikt.core.NoOpCliktCommand
import com.github.ajalt.clikt.core.subcommands
public fun main(args: Array<String>): Unit = CacheCommand().main(args)
public class CacheCommand : NoOpCliktCommand(name = "cache") {
init {
subcommands(
OpenNxtUnpackCommand(),
RuneLiteUnpackCommand()
)
}
}

@ -1,26 +0,0 @@
package org.openrs2.cache.cli
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.types.path
import com.google.inject.Guice
import io.netty.buffer.ByteBufAllocator
import org.openrs2.cache.CacheModule
import org.openrs2.cache.OpenNxtStore
import org.openrs2.cache.Store
import org.openrs2.inject.CloseableInjector
public class OpenNxtUnpackCommand : CliktCommand(name = "unpack-opennxt") {
private val input by argument().path(mustExist = true, canBeFile = false, mustBeReadable = true)
private val output by argument().path(canBeFile = false, mustBeReadable = true, mustBeWritable = true)
override fun run() {
CloseableInjector(Guice.createInjector(CacheModule)).use { injector ->
val alloc = injector.getInstance(ByteBufAllocator::class.java)
Store.open(output, alloc).use { store ->
OpenNxtStore.unpack(input, store)
}
}
}
}

@ -1,27 +0,0 @@
package org.openrs2.cache.cli
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.types.path
import com.google.inject.Guice
import io.netty.buffer.ByteBufAllocator
import org.openrs2.cache.CacheModule
import org.openrs2.cache.RuneLiteStore
import org.openrs2.cache.Store
import org.openrs2.inject.CloseableInjector
public class RuneLiteUnpackCommand : CliktCommand(name = "unpack-runelite") {
private val input by argument().path(mustExist = true, canBeFile = false, mustBeReadable = true)
private val output by argument().path(canBeFile = false, mustBeReadable = true, mustBeWritable = true)
override fun run() {
CloseableInjector(Guice.createInjector(CacheModule)).use { injector ->
val alloc = injector.getInstance(ByteBufAllocator::class.java)
val runeLiteStore = injector.getInstance(RuneLiteStore::class.java)
Store.open(output, alloc).use { store ->
runeLiteStore.unpack(input, store)
}
}
}
}

@ -5,15 +5,14 @@ plugins {
dependencies {
api(projects.crypto)
api(libs.bundles.guice)
api(libs.commons.compress)
api(libs.fastutil)
api(libs.guice)
api(libs.netty.buffer)
implementation(projects.buffer)
implementation(projects.compress)
implementation(projects.util)
implementation(libs.sqlite)
testImplementation(libs.jimfs)
}

@ -161,17 +161,6 @@ public abstract class Archive internal constructor(
return existsNamed(group.krHashCode(), file.krHashCode())
}
public fun existsNamedGroup(groupNameHash: Int, file: Int): Boolean {
require(file >= 0)
val entry = index.getNamed(groupNameHash) ?: return false
return entry.contains(file)
}
public fun exists(group: String, file: Int): Boolean {
return existsNamedGroup(group.krHashCode(), file)
}
public fun list(): Iterator<Js5Index.Group<*>> {
return index.iterator()
}
@ -192,7 +181,6 @@ public abstract class Archive internal constructor(
return listNamed(group.krHashCode())
}
@JvmOverloads
public fun read(group: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
require(group >= 0 && file >= 0)
@ -201,33 +189,16 @@ public abstract class Archive internal constructor(
return unpacked.read(file)
}
@JvmOverloads
public fun readNamed(groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
val entry = index.getNamed(groupNameHash) ?: throw FileNotFoundException()
val unpacked = getUnpacked(entry, key)
return unpacked.readNamed(fileNameHash)
}
@JvmOverloads
public fun read(group: String, file: String, key: XteaKey = XteaKey.ZERO): ByteBuf {
return readNamed(group.krHashCode(), file.krHashCode(), key)
}
@JvmOverloads
public fun readNamedGroup(groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
require(file >= 0)
val entry = index.getNamed(groupNameHash) ?: throw FileNotFoundException()
val unpacked = getUnpacked(entry, key)
return unpacked.read(file)
}
@JvmOverloads
public fun read(group: String, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
return readNamedGroup(group.krHashCode(), file, key)
}
@JvmOverloads
public fun write(group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
require(group >= 0 && file >= 0)
@ -238,7 +209,6 @@ public abstract class Archive internal constructor(
dirty = true
}
@JvmOverloads
public fun writeNamed(groupNameHash: Int, fileNameHash: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
val entry = index.createOrGetNamed(groupNameHash)
val unpacked = createOrGetUnpacked(entry, key, isOverwritingNamed(entry, fileNameHash))
@ -248,28 +218,10 @@ public abstract class Archive internal constructor(
index.hasNames = true
}
@JvmOverloads
public fun write(group: String, file: String, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
return writeNamed(group.krHashCode(), file.krHashCode(), buf, key)
}
@JvmOverloads
public fun writeNamedGroup(groupNameHash: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
require(file >= 0)
val entry = index.createOrGetNamed(groupNameHash)
val unpacked = createOrGetUnpacked(entry, key, isOverwriting(entry, file))
unpacked.write(file, buf)
dirty = true
index.hasNames = true
}
@JvmOverloads
public fun write(group: String, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
return writeNamedGroup(group.krHashCode(), file, buf, key)
}
public fun remove(group: Int) {
require(group >= 0)
@ -292,7 +244,6 @@ public abstract class Archive internal constructor(
return removeNamed(group.krHashCode())
}
@JvmOverloads
public fun remove(group: Int, file: Int, key: XteaKey = XteaKey.ZERO) {
require(group >= 0 && file >= 0)
@ -309,7 +260,6 @@ public abstract class Archive internal constructor(
dirty = true
}
@JvmOverloads
public fun removeNamed(groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO) {
val entry = index.getNamed(groupNameHash) ?: return
@ -324,33 +274,10 @@ public abstract class Archive internal constructor(
dirty = true
}
@JvmOverloads
public fun remove(group: String, file: String, key: XteaKey = XteaKey.ZERO) {
return removeNamed(group.krHashCode(), file.krHashCode(), key)
}
@JvmOverloads
public fun removeNamedGroup(groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO) {
require(file >= 0)
val entry = index.getNamed(groupNameHash) ?: return
if (isOverwriting(entry, file)) {
removeNamed(groupNameHash)
return
}
val unpacked = getUnpacked(entry, key)
unpacked.remove(file)
dirty = true
}
@JvmOverloads
public fun remove(group: String, file: Int, key: XteaKey = XteaKey.ZERO) {
removeNamedGroup(group.krHashCode(), file, key)
}
public override fun flush() {
if (!dirty) {
return

@ -106,15 +106,6 @@ public class Cache private constructor(
return existsNamed(archive, group.krHashCode(), file.krHashCode())
}
public fun existsNamedGroup(archive: Int, groupNameHash: Int, file: Int): Boolean {
checkArchive(archive)
return archives[archive]?.existsNamedGroup(groupNameHash, file) ?: false
}
public fun exists(archive: Int, group: String, file: Int): Boolean {
return existsNamedGroup(archive, group.krHashCode(), file)
}
public fun list(): Iterator<Int> {
return archives.withIndex()
.filter { it.value != null }
@ -141,41 +132,25 @@ public class Cache private constructor(
return listNamed(archive, group.krHashCode())
}
@JvmOverloads
public fun read(archive: Int, group: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
checkArchive(archive)
return archives[archive]?.read(group, file, key) ?: throw FileNotFoundException()
}
@JvmOverloads
public fun readNamed(archive: Int, groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
checkArchive(archive)
return archives[archive]?.readNamed(groupNameHash, fileNameHash, key) ?: throw FileNotFoundException()
}
@JvmOverloads
public fun read(archive: Int, group: String, file: String, key: XteaKey = XteaKey.ZERO): ByteBuf {
return readNamed(archive, group.krHashCode(), file.krHashCode(), key)
}
@JvmOverloads
public fun readNamedGroup(archive: Int, groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
checkArchive(archive)
return archives[archive]?.readNamedGroup(groupNameHash, file, key) ?: throw FileNotFoundException()
}
@JvmOverloads
public fun read(archive: Int, group: String, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf {
return readNamedGroup(archive, group.krHashCode(), file, key)
}
@JvmOverloads
public fun write(archive: Int, group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
checkArchive(archive)
createOrGetArchive(archive).write(group, file, buf, key)
}
@JvmOverloads
public fun writeNamed(
archive: Int,
groupNameHash: Int,
@ -187,22 +162,10 @@ public class Cache private constructor(
createOrGetArchive(archive).writeNamed(groupNameHash, fileNameHash, buf, key)
}
@JvmOverloads
public fun write(archive: Int, group: String, file: String, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
writeNamed(archive, group.krHashCode(), file.krHashCode(), buf, key)
}
@JvmOverloads
public fun writeNamedGroup(archive: Int, groupNameHash: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
checkArchive(archive)
createOrGetArchive(archive).writeNamedGroup(groupNameHash, file, buf, key)
}
@JvmOverloads
public fun write(archive: Int, group: String, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) {
writeNamedGroup(archive, group.krHashCode(), file, buf, key)
}
public fun remove(archive: Int) {
checkArchive(archive)
@ -232,34 +195,20 @@ public class Cache private constructor(
return removeNamed(archive, group.krHashCode())
}
@JvmOverloads
public fun remove(archive: Int, group: Int, file: Int, key: XteaKey = XteaKey.ZERO) {
checkArchive(archive)
archives[archive]?.remove(group, file, key)
}
@JvmOverloads
public fun removeNamed(archive: Int, groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO) {
checkArchive(archive)
archives[archive]?.removeNamed(groupNameHash, fileNameHash, key)
}
@JvmOverloads
public fun remove(archive: Int, group: String, file: String, key: XteaKey = XteaKey.ZERO) {
return removeNamed(archive, group.krHashCode(), file.krHashCode(), key)
}
@JvmOverloads
public fun removeNamedGroup(archive: Int, groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO) {
checkArchive(archive)
archives[archive]?.removeNamedGroup(groupNameHash, file, key)
}
@JvmOverloads
public fun remove(archive: Int, group: String, file: Int, key: XteaKey = XteaKey.ZERO) {
removeNamedGroup(archive, group.krHashCode(), file, key)
}
/**
* Writes pending changes back to the underlying [Store].
*/
@ -269,8 +218,6 @@ public class Cache private constructor(
for (archive in archives) {
archive?.flush()
}
store.flush()
}
/**
@ -293,8 +240,6 @@ public class Cache private constructor(
public companion object {
public const val MAX_ARCHIVE: Int = 254
@JvmOverloads
@JvmStatic
public fun open(
root: Path,
alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,
@ -303,8 +248,6 @@ public class Cache private constructor(
return open(Store.open(root, alloc), alloc, unpackedCacheSize)
}
@JvmOverloads
@JvmStatic
public fun open(
store: Store,
alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,

@ -21,19 +21,13 @@ public class ChecksumTable(
}
public companion object {
@JvmStatic
public fun create(store: Store): ChecksumTable {
val table = ChecksumTable()
var nextArchive = 0
for (archive in store.list(0)) {
val entry = try {
store.read(0, archive).use { buf ->
buf.crc32()
}
} catch (ex: StoreCorruptException) {
// see the equivalent comment in Js5MasterIndex::create
continue
val entry = store.read(0, archive).use { buf ->
buf.crc32()
}
for (i in nextArchive until archive) {
@ -47,7 +41,6 @@ public class ChecksumTable(
return table
}
@JvmStatic
public fun read(buf: ByteBuf): ChecksumTable {
val table = ChecksumTable()

@ -450,7 +450,7 @@ public class DiskStore private constructor(
override fun flush() {
data.flush()
musicData?.flush()
musicData?.close()
for (index in indexes) {
index?.flush()
@ -500,8 +500,6 @@ public class DiskStore private constructor(
return root.resolve("main_file_cache.idx$archive")
}
@JvmOverloads
@JvmStatic
public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store {
val js5DataPath = dataPath(root)
val legacyDataPath = legacyDataPath(root)
@ -550,8 +548,6 @@ public class DiskStore private constructor(
return DiskStore(root, data, musicData, archives, alloc, legacy)
}
@JvmOverloads
@JvmStatic
public fun create(
root: Path,
alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,

@ -92,7 +92,7 @@ public class FlatFileStore private constructor(
val path = groupPath(archive, group)
Files.createDirectories(path.parent)
path.useAtomicOutputStream(sync = false) { output ->
path.useAtomicOutputStream { output ->
buf.readBytes(output, buf.readableBytes())
}
}
@ -128,8 +128,6 @@ public class FlatFileStore private constructor(
private val GROUP_NAME = Regex("[0-9]+[.]dat")
private const val GROUP_EXTENSION = ".dat"
@JvmOverloads
@JvmStatic
public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store {
if (!Files.isDirectory(root)) {
throw FileNotFoundException()
@ -138,8 +136,6 @@ public class FlatFileStore private constructor(
return FlatFileStore(root, alloc)
}
@JvmOverloads
@JvmStatic
public fun create(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store {
Files.createDirectories(root)
return FlatFileStore(root, alloc)

@ -129,7 +129,6 @@ public class JagArchive : Closeable {
* @param alloc the allocator.
* @return the compressed archive.
*/
@JvmOverloads
public fun pack(compressedArchive: Boolean, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): ByteBuf {
alloc.buffer().use { output ->
alloc.buffer().use { uncompressedArchiveBuf ->
@ -190,7 +189,6 @@ public class JagArchive : Closeable {
* @param alloc the allocator.
* @return the compressed archive.
*/
@JvmOverloads
public fun packBest(alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): ByteBuf {
pack(true, alloc).use { compressedArchive ->
pack(false, alloc).use { compressedEntries ->
@ -230,7 +228,6 @@ public class JagArchive : Closeable {
* @param buf the compressed archive.
* @return the unpacked archive.
*/
@JvmStatic
public fun unpack(buf: ByteBuf): JagArchive {
val archive = JagArchive()

@ -18,7 +18,6 @@ public object Js5Compression {
private const val LZMA_PB_MAX = 4
private const val LZMA_PRESET_DICT_SIZE_MAX = 1 shl 26
@JvmOverloads
public fun compress(input: ByteBuf, type: Js5CompressionType, key: XteaKey = XteaKey.ZERO): ByteBuf {
input.alloc().buffer().use { output ->
output.writeByte(type.ordinal)
@ -108,12 +107,7 @@ public object Js5Compression {
}
}
@JvmOverloads
public fun uncompress(input: ByteBuf, key: XteaKey = XteaKey.ZERO): ByteBuf {
if (input.readableBytes() < 5) {
throw IOException("Missing header")
}
val typeId = input.readUnsignedByte().toInt()
val type = Js5CompressionType.fromOrdinal(typeId)
?: throw IOException("Invalid compression type: $typeId")
@ -147,8 +141,8 @@ public object Js5Compression {
throw IOException("Uncompressed length is negative: $uncompressedLen")
}
type.createInputStream(ByteBufInputStream(plaintext, len), uncompressedLen).use { inputStream ->
plaintext.alloc().buffer(uncompressedLen, uncompressedLen).use { output ->
plaintext.alloc().buffer(uncompressedLen, uncompressedLen).use { output ->
type.createInputStream(ByteBufInputStream(plaintext, len), uncompressedLen).use { inputStream ->
var remaining = uncompressedLen
while (remaining > 0) {
val n = output.writeBytes(inputStream, remaining)
@ -161,9 +155,9 @@ public object Js5Compression {
if (inputStream.read() != -1) {
throw IOException("Uncompressed data overflow")
}
return output.retain()
}
return output.retain()
}
}
}
@ -173,10 +167,6 @@ public object Js5Compression {
}
public fun uncompressIfKeyValid(input: ByteBuf, key: XteaKey): ByteBuf? {
if (input.readableBytes() < 5) {
throw IOException("Missing header")
}
val typeId = input.readUnsignedByte().toInt()
val type = Js5CompressionType.fromOrdinal(typeId)
?: throw IOException("Invalid compression type: $typeId")
@ -255,7 +245,6 @@ public object Js5Compression {
return null
}
}
Js5CompressionType.GZIP -> {
val magic = plaintext.readUnsignedShort()
if (magic != GZIP_MAGIC) {
@ -270,7 +259,6 @@ public object Js5Compression {
return null
}
}
Js5CompressionType.LZMA -> {
val properties = plaintext.readUnsignedByte()
@ -319,21 +307,21 @@ public object Js5Compression {
val uncompressedLen = plaintext.readInt()
check(uncompressedLen >= 0)
try {
type.createInputStream(ByteBufInputStream(plaintext, len), uncompressedLen).use { inputStream ->
/**
* We don't pass uncompressedLen to the buffer here: in some cases,
* an incorrect key can produce a valid header (particularly for
* LZMA, which has no magic number). If we're unlucky,
* uncompressedLen will be a huge number (e.g. 1 or 2 gigabytes),
* which might OOM some environments if allocated up front.
*
* However, if the key is incorrect it's likely that actually
* attempting to uncompress the data will quickly produce an error,
* long before we need to actually read 1 or 2 gigabytes of data.
* We therefore allow the buffer to grow dynamically.
*/
plaintext.alloc().buffer().use { output ->
/**
* We don't pass uncompressedLen to the buffer here: in some cases,
* an incorrect key can produce a valid header (particularly for
* LZMA, which has no magic number). If we're unlucky,
* uncompressedLen will be a huge number (e.g. 1 or 2 gigabytes),
* which might OOM some environments if allocated up front.
*
* However, if the key is incorrect it's likely that actually
* attempting to uncompress the data will quickly produce an error,
* long before we need to actually read 1 or 2 gigabytes of data.
* We therefore allow the buffer to grow dynamically.
*/
plaintext.alloc().buffer().use { output ->
try {
type.createInputStream(ByteBufInputStream(plaintext, len), uncompressedLen).use { inputStream ->
var remaining = uncompressedLen
while (remaining > 0) {
val n = output.writeBytes(inputStream, remaining)
@ -348,12 +336,12 @@ public object Js5Compression {
// uncompressed data overflow
return null
}
return output.retain()
}
} catch (ex: IOException) {
return null
}
} catch (ex: IOException) {
return null
return output.retain()
}
}
}
@ -370,10 +358,6 @@ public object Js5Compression {
}
public fun isEmptyLoc(buf: ByteBuf): Boolean {
if (buf.readableBytes() < 5) {
throw IOException("Missing header")
}
val typeId = buf.readUnsignedByte().toInt()
val type = Js5CompressionType.fromOrdinal(typeId)
?: throw IOException("Invalid compression type: $typeId")

@ -41,7 +41,6 @@ public enum class Js5CompressionType {
public companion object {
private val values = values()
@JvmStatic
public fun fromOrdinal(ordinal: Int): Js5CompressionType? {
return if (ordinal >= 0 && ordinal < values.size) {
values[ordinal]

@ -102,7 +102,7 @@ public class Js5Index(
public class MutableFile internal constructor(
parent: MutableNamedEntryCollection<MutableFile>,
override val id: Int
override val id: Int,
) : MutableNamedEntry, File {
private var parent: MutableNamedEntryCollection<MutableFile>? = parent
@ -280,7 +280,6 @@ public class Js5Index(
private const val FLAG_LENGTHS = 0x04
private const val FLAG_UNCOMPRESSED_CHECKSUMS = 0x08
@JvmStatic
public fun read(buf: ByteBuf): Js5Index {
val number = buf.readUnsignedByte().toInt()
val protocol = Js5Protocol.fromId(number)

@ -66,7 +66,6 @@ public data class Js5MasterIndex(
}
}
@JvmOverloads
public fun write(buf: ByteBuf, key: RSAKeyParameters? = null) {
val start = buf.writerIndex()
@ -118,7 +117,6 @@ public data class Js5MasterIndex(
public companion object {
private const val SIGNATURE_LENGTH = Whirlpool.DIGESTBYTES + 1
@JvmStatic
public fun create(store: Store): Js5MasterIndex {
val masterIndex = Js5MasterIndex(MasterIndexFormat.ORIGINAL)
@ -181,13 +179,10 @@ public data class Js5MasterIndex(
return masterIndex
}
@JvmOverloads
@JvmStatic
public fun read(buf: ByteBuf, format: MasterIndexFormat, key: RSAKeyParameters? = null): Js5MasterIndex {
return read(buf, format, key, true)
}
@JvmStatic
public fun readUnverified(buf: ByteBuf, format: MasterIndexFormat): Js5MasterIndex {
return read(buf, format, null, false)
}
@ -210,14 +205,12 @@ public data class Js5MasterIndex(
}
len / 4
}
MasterIndexFormat.VERSIONED -> {
require(len % 8 == 0) {
"Length is not a multiple of 8 bytes"
}
len / 8
}
else -> {
buf.readUnsignedByte().toInt()
}

@ -23,7 +23,7 @@ public class Js5Pack private constructor(
index: Js5Index,
unpackedCacheSize: Int,
private var packedIndex: ByteBuf,
private val packed: Int2ObjectSortedMap<ByteBuf>
private val packed: Int2ObjectSortedMap<ByteBuf>,
) : Archive(alloc, index, 0, UnpackedCache(unpackedCacheSize)), Closeable {
override fun packedExists(group: Int): Boolean {
return packed.containsKey(group)
@ -91,8 +91,6 @@ public class Js5Pack private constructor(
}
public companion object {
@JvmOverloads
@JvmStatic
public fun create(
alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,
unpackedCacheSize: Int = UnpackedCache.DEFAULT_CAPACITY
@ -109,8 +107,6 @@ public class Js5Pack private constructor(
}
}
@JvmOverloads
@JvmStatic
public fun read(
path: Path,
alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,
@ -121,8 +117,6 @@ public class Js5Pack private constructor(
}
}
@JvmOverloads
@JvmStatic
public fun read(
input: InputStream,
alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save