From c5bd04457406778df92adbf886d8bd0041e828f3 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 20 Feb 2021 13:43:25 +0000 Subject: [PATCH] Add CloseableInjector I'm going to try to minimise use of this (as per https://github.com/google/guice/wiki/Avoid-Injecting-Closable-Resources). For example, I'm going to inject a pooling DataSource rather than Connection objects, as per the advice at the end of the page. However, HikariCP's DataSource implementation is itself Closeable. Signed-off-by: Graham --- archive/build.gradle.kts | 1 + .../openrs2/archive/cache/DownloadCommand.kt | 8 ++++--- .../openrs2/archive/cache/ExportCommand.kt | 10 ++++---- .../openrs2/archive/cache/ImportCommand.kt | 10 ++++---- .../archive/cache/ImportMasterIndexCommand.kt | 10 ++++---- .../openrs2/archive/key/BruteForceCommand.kt | 9 ++++---- .../openrs2/archive/key/DownloadCommand.kt | 8 ++++--- .../org/openrs2/archive/key/ImportCommand.kt | 9 ++++---- .../openrs2/archive/name/DownloadCommand.kt | 8 ++++--- .../openrs2/archive/name/GenerateCommand.kt | 20 ++++++++-------- .../org/openrs2/archive/name/ImportCommand.kt | 10 ++++---- .../org/openrs2/archive/web/WebCommand.kt | 8 ++++--- deob-ast/build.gradle.kts | 1 + .../openrs2/deob/ast/DeobfuscateAstCommand.kt | 8 ++++--- deob-bytecode/build.gradle.kts | 1 + .../bytecode/DeobfuscateBytecodeCommand.kt | 14 ++++++----- deob-processor/build.gradle.kts | 1 + .../deob/processor/NameMapProcessor.kt | 11 ++++----- deob/build.gradle.kts | 1 + .../org/openrs2/deob/DeobfuscateCommand.kt | 8 ++++--- game/build.gradle.kts | 2 +- .../kotlin/org/openrs2/game/GameCommand.kt | 8 ++++--- inject/build.gradle.kts | 10 ++------ .../org/openrs2/inject/CloseableInjector.kt | 23 +++++++++++++++++++ patcher/build.gradle.kts | 1 + .../org/openrs2/patcher/PatchCommand.kt | 16 +++++++------ settings.gradle.kts | 1 + 27 files changed, 135 insertions(+), 82 deletions(-) create mode 100644 inject/src/main/kotlin/org/openrs2/inject/CloseableInjector.kt diff --git a/archive/build.gradle.kts b/archive/build.gradle.kts index 6c0325fb..f4d5afe5 100644 --- a/archive/build.gradle.kts +++ b/archive/build.gradle.kts @@ -16,6 +16,7 @@ dependencies { implementation(project(":cli")) implementation(project(":db")) implementation(project(":http")) + implementation(project(":inject")) implementation(project(":json")) implementation(project(":net")) implementation(project(":protocol")) diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/DownloadCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/DownloadCommand.kt index 5a719670..9b01ffcb 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/DownloadCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/DownloadCommand.kt @@ -6,13 +6,15 @@ import com.github.ajalt.clikt.parameters.arguments.default import com.google.inject.Guice import kotlinx.coroutines.runBlocking import org.openrs2.archive.ArchiveModule +import org.openrs2.inject.CloseableInjector public class DownloadCommand : CliktCommand(name = "download") { private val game by argument().default("oldschool") override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val downloader = injector.getInstance(CacheDownloader::class.java) - downloader.download(game) + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val downloader = injector.getInstance(CacheDownloader::class.java) + downloader.download(game) + } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/ExportCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/ExportCommand.kt index efb7f98c..100100cd 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/ExportCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/ExportCommand.kt @@ -8,6 +8,7 @@ import com.google.inject.Guice import kotlinx.coroutines.runBlocking import org.openrs2.archive.ArchiveModule import org.openrs2.cache.DiskStore +import org.openrs2.inject.CloseableInjector public class ExportCommand : CliktCommand(name = "export") { private val id by argument().int() @@ -19,11 +20,12 @@ public class ExportCommand : CliktCommand(name = "export") { ) override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val exporter = injector.getInstance(CacheExporter::class.java) + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val exporter = injector.getInstance(CacheExporter::class.java) - DiskStore.create(output).use { store -> - exporter.export(id, store) + DiskStore.create(output).use { store -> + exporter.export(id, store) + } } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/ImportCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/ImportCommand.kt index dacd90b2..67e496e0 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/ImportCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/ImportCommand.kt @@ -10,6 +10,7 @@ import kotlinx.coroutines.runBlocking import org.openrs2.archive.ArchiveModule import org.openrs2.cache.Store import org.openrs2.cli.instant +import org.openrs2.inject.CloseableInjector public class ImportCommand : CliktCommand(name = "import") { private val build by option().int() @@ -25,11 +26,12 @@ public class ImportCommand : CliktCommand(name = "import") { ) override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val importer = injector.getInstance(CacheImporter::class.java) + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val importer = injector.getInstance(CacheImporter::class.java) - Store.open(input).use { store -> - importer.import(store, game, build, timestamp, name, description) + Store.open(input).use { store -> + importer.import(store, game, build, timestamp, name, description) + } } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/ImportMasterIndexCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/ImportMasterIndexCommand.kt index ef866fd6..6ee05bed 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/ImportMasterIndexCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/ImportMasterIndexCommand.kt @@ -13,6 +13,7 @@ import org.openrs2.archive.ArchiveModule import org.openrs2.buffer.use import org.openrs2.cache.MasterIndexFormat import org.openrs2.cli.instant +import org.openrs2.inject.CloseableInjector import java.nio.file.Files public class ImportMasterIndexCommand : CliktCommand(name = "import-master-index") { @@ -30,11 +31,12 @@ public class ImportMasterIndexCommand : CliktCommand(name = "import-master-index ) override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val importer = injector.getInstance(CacheImporter::class.java) + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val importer = injector.getInstance(CacheImporter::class.java) - Unpooled.wrappedBuffer(Files.readAllBytes(input)).use { buf -> - importer.importMasterIndex(buf, format, game, build, timestamp, name, description) + Unpooled.wrappedBuffer(Files.readAllBytes(input)).use { buf -> + importer.importMasterIndex(buf, format, game, build, timestamp, name, description) + } } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/BruteForceCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/key/BruteForceCommand.kt index 2b1ec828..64dec0ef 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/BruteForceCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/BruteForceCommand.kt @@ -4,12 +4,13 @@ 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 BruteForceCommand : CliktCommand(name = "brute-force") { override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val bruteForcer = injector.getInstance(KeyBruteForcer::class.java) - - bruteForcer.bruteForce() + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val bruteForcer = injector.getInstance(KeyBruteForcer::class.java) + bruteForcer.bruteForce() + } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/DownloadCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/key/DownloadCommand.kt index ee022e08..23cf3629 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/DownloadCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/DownloadCommand.kt @@ -4,11 +4,13 @@ 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 DownloadCommand : CliktCommand(name = "download") { override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val importer = injector.getInstance(KeyImporter::class.java) - importer.download() + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val importer = injector.getInstance(KeyImporter::class.java) + importer.download() + } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/ImportCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/key/ImportCommand.kt index e433596a..62a3a74d 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/ImportCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/ImportCommand.kt @@ -6,6 +6,7 @@ import com.github.ajalt.clikt.parameters.types.path import com.google.inject.Guice import kotlinx.coroutines.runBlocking import org.openrs2.archive.ArchiveModule +import org.openrs2.inject.CloseableInjector public class ImportCommand : CliktCommand(name = "import") { private val input by argument().path( @@ -14,9 +15,9 @@ public class ImportCommand : CliktCommand(name = "import") { ) override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val importer = injector.getInstance(KeyImporter::class.java) - - importer.import(input) + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val importer = injector.getInstance(KeyImporter::class.java) + importer.import(input) + } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/name/DownloadCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/name/DownloadCommand.kt index b76ce64d..fa83d095 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/name/DownloadCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/name/DownloadCommand.kt @@ -4,11 +4,13 @@ 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 DownloadCommand : CliktCommand(name = "download") { override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val importer = injector.getInstance(NameImporter::class.java) - importer.download() + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val importer = injector.getInstance(NameImporter::class.java) + importer.download() + } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/name/GenerateCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/name/GenerateCommand.kt index 44c2f1c5..8373bafb 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/name/GenerateCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/name/GenerateCommand.kt @@ -4,23 +4,25 @@ 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 GenerateCommand : CliktCommand(name = "generate") { override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val importer = injector.getInstance(NameImporter::class.java) + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val importer = injector.getInstance(NameImporter::class.java) - val names = mutableSetOf() + val names = mutableSetOf() - for (x in 0..99) { - for (z in 0..255) { - for (prefix in PREFIXES) { - names += "$prefix${x}_$z" + for (x in 0..99) { + for (z in 0..255) { + for (prefix in PREFIXES) { + names += "$prefix${x}_$z" + } } } - } - importer.import(names) + importer.import(names) + } } private companion object { diff --git a/archive/src/main/kotlin/org/openrs2/archive/name/ImportCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/name/ImportCommand.kt index 8437f977..80a8fa5b 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/name/ImportCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/name/ImportCommand.kt @@ -7,16 +7,18 @@ import com.github.ajalt.clikt.parameters.types.inputStream import com.google.inject.Guice import kotlinx.coroutines.runBlocking import org.openrs2.archive.ArchiveModule +import org.openrs2.inject.CloseableInjector public class ImportCommand : CliktCommand(name = "import") { private val input by option().inputStream().defaultStdin() override fun run(): Unit = runBlocking { - val injector = Guice.createInjector(ArchiveModule) - val importer = injector.getInstance(NameImporter::class.java) + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val importer = injector.getInstance(NameImporter::class.java) - input.bufferedReader().useLines { lines -> - importer.import(lines.toSet()) + input.bufferedReader().useLines { lines -> + importer.import(lines.toSet()) + } } } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/WebCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/web/WebCommand.kt index 84f2b593..1ab5cdd7 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/WebCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/WebCommand.kt @@ -3,11 +3,13 @@ package org.openrs2.archive.web import com.github.ajalt.clikt.core.CliktCommand import com.google.inject.Guice import org.openrs2.archive.ArchiveModule +import org.openrs2.inject.CloseableInjector public class WebCommand : CliktCommand(name = "web") { override fun run() { - val injector = Guice.createInjector(ArchiveModule) - val server = injector.getInstance(WebServer::class.java) - server.start() + CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> + val server = injector.getInstance(WebServer::class.java) + server.start() + } } } diff --git a/deob-ast/build.gradle.kts b/deob-ast/build.gradle.kts index bf1c5e56..e617b337 100644 --- a/deob-ast/build.gradle.kts +++ b/deob-ast/build.gradle.kts @@ -13,6 +13,7 @@ dependencies { api("com.github.ajalt.clikt:clikt:${Versions.clikt}") api("com.google.inject:guice:${Versions.guice}") + implementation(project(":inject")) implementation(project(":util")) implementation("com.github.javaparser:javaparser-symbol-solver-core:${Versions.javaParser}") implementation("com.google.guava:guava:${Versions.guava}") diff --git a/deob-ast/src/main/kotlin/org/openrs2/deob/ast/DeobfuscateAstCommand.kt b/deob-ast/src/main/kotlin/org/openrs2/deob/ast/DeobfuscateAstCommand.kt index 468df7a1..c974aa96 100644 --- a/deob-ast/src/main/kotlin/org/openrs2/deob/ast/DeobfuscateAstCommand.kt +++ b/deob-ast/src/main/kotlin/org/openrs2/deob/ast/DeobfuscateAstCommand.kt @@ -3,13 +3,15 @@ package org.openrs2.deob.ast import com.github.ajalt.clikt.core.CliktCommand import com.google.inject.Guice import org.openrs2.deob.util.Module +import org.openrs2.inject.CloseableInjector public fun main(args: Array): Unit = DeobfuscateAstCommand().main(args) public class DeobfuscateAstCommand : CliktCommand(name = "ast") { override fun run() { - val injector = Guice.createInjector(AstDeobfuscatorModule) - val deobfuscator = injector.getInstance(AstDeobfuscator::class.java) - deobfuscator.run(Module.ALL) + CloseableInjector(Guice.createInjector(AstDeobfuscatorModule)).use { injector -> + val deobfuscator = injector.getInstance(AstDeobfuscator::class.java) + deobfuscator.run(Module.ALL) + } } } diff --git a/deob-bytecode/build.gradle.kts b/deob-bytecode/build.gradle.kts index 6a3ac624..4e9ff64f 100644 --- a/deob-bytecode/build.gradle.kts +++ b/deob-bytecode/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { implementation(project(":deob-annotations")) implementation(project(":deob-util")) + implementation(project(":inject")) implementation(project(":patcher")) implementation(project(":yaml")) implementation("com.google.guava:guava:${Versions.guava}") diff --git a/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/DeobfuscateBytecodeCommand.kt b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/DeobfuscateBytecodeCommand.kt index 5dc66001..0e022aa5 100644 --- a/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/DeobfuscateBytecodeCommand.kt +++ b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/DeobfuscateBytecodeCommand.kt @@ -2,17 +2,19 @@ package org.openrs2.deob.bytecode import com.github.ajalt.clikt.core.CliktCommand import com.google.inject.Guice +import org.openrs2.inject.CloseableInjector import java.nio.file.Path public fun main(args: Array): Unit = DeobfuscateBytecodeCommand().main(args) public class DeobfuscateBytecodeCommand : CliktCommand(name = "bytecode") { override fun run() { - val injector = Guice.createInjector(BytecodeDeobfuscatorModule) - val deobfuscator = injector.getInstance(BytecodeDeobfuscator::class.java) - deobfuscator.run( - input = Path.of("nonfree/lib"), - output = Path.of("nonfree/var/cache/deob") - ) + CloseableInjector(Guice.createInjector(BytecodeDeobfuscatorModule)).use { injector -> + val deobfuscator = injector.getInstance(BytecodeDeobfuscator::class.java) + deobfuscator.run( + input = Path.of("nonfree/lib"), + output = Path.of("nonfree/var/cache/deob") + ) + } } } diff --git a/deob-processor/build.gradle.kts b/deob-processor/build.gradle.kts index be111760..86803038 100644 --- a/deob-processor/build.gradle.kts +++ b/deob-processor/build.gradle.kts @@ -6,6 +6,7 @@ plugins { dependencies { implementation(project(":deob-annotations")) implementation(project(":deob-util")) + implementation(project(":inject")) implementation(project(":yaml")) } diff --git a/deob-processor/src/main/kotlin/org/openrs2/deob/processor/NameMapProcessor.kt b/deob-processor/src/main/kotlin/org/openrs2/deob/processor/NameMapProcessor.kt index 8630ef4e..73315469 100644 --- a/deob-processor/src/main/kotlin/org/openrs2/deob/processor/NameMapProcessor.kt +++ b/deob-processor/src/main/kotlin/org/openrs2/deob/processor/NameMapProcessor.kt @@ -12,6 +12,7 @@ import org.openrs2.deob.annotation.OriginalMember import org.openrs2.deob.util.map.Field import org.openrs2.deob.util.map.Method import org.openrs2.deob.util.map.NameMap +import org.openrs2.inject.CloseableInjector import org.openrs2.util.io.useAtomicBufferedWriter import org.openrs2.yaml.Yaml import java.lang.reflect.Proxy @@ -40,16 +41,12 @@ import javax.lang.model.element.VariableElement "map" ) public class NameMapProcessor : AbstractProcessor() { + private val injector = CloseableInjector(Guice.createInjector(DeobfuscatorProcessorModule)) private val map = NameMap() - private val mapper: ObjectMapper + private val mapper = injector.getInstance(Key.get(ObjectMapper::class.java, Yaml::class.java)) private lateinit var trees: Trees private lateinit var localScanner: LocalVariableScanner - init { - val injector = Guice.createInjector(DeobfuscatorProcessorModule) - mapper = injector.getInstance(Key.get(ObjectMapper::class.java, Yaml::class.java)) - } - override fun init(env: ProcessingEnvironment) { super.init(env) trees = Trees.instance(unwrap(env)) @@ -124,6 +121,8 @@ public class NameMapProcessor : AbstractProcessor() { mapPath.useAtomicBufferedWriter { writer -> mapper.writeValue(writer, combinedMap) } + + injector.close() } return true diff --git a/deob/build.gradle.kts b/deob/build.gradle.kts index c16240ac..2478b0ad 100644 --- a/deob/build.gradle.kts +++ b/deob/build.gradle.kts @@ -15,6 +15,7 @@ dependencies { implementation(project(":decompiler")) implementation(project(":deob-ast")) implementation(project(":deob-bytecode")) + implementation(project(":inject")) } publishing { diff --git a/deob/src/main/kotlin/org/openrs2/deob/DeobfuscateCommand.kt b/deob/src/main/kotlin/org/openrs2/deob/DeobfuscateCommand.kt index 336d8a22..9a7cb336 100644 --- a/deob/src/main/kotlin/org/openrs2/deob/DeobfuscateCommand.kt +++ b/deob/src/main/kotlin/org/openrs2/deob/DeobfuscateCommand.kt @@ -2,13 +2,15 @@ package org.openrs2.deob import com.github.ajalt.clikt.core.CliktCommand import com.google.inject.Guice +import org.openrs2.inject.CloseableInjector public fun main(args: Array): Unit = DeobfuscateCommand().main(args) public class DeobfuscateCommand : CliktCommand(name = "deob") { override fun run() { - val injector = Guice.createInjector(DeobfuscatorModule) - val deobfuscator = injector.getInstance(Deobfuscator::class.java) - deobfuscator.run() + CloseableInjector(Guice.createInjector(DeobfuscatorModule)).use { injector -> + val deobfuscator = injector.getInstance(Deobfuscator::class.java) + deobfuscator.run() + } } } diff --git a/game/build.gradle.kts b/game/build.gradle.kts index d045642f..b15c2b0a 100644 --- a/game/build.gradle.kts +++ b/game/build.gradle.kts @@ -11,7 +11,7 @@ application { dependencies { api("com.github.ajalt.clikt:clikt:${Versions.clikt}") - implementation("com.google.inject:guice:${Versions.guice}") + implementation(project(":inject")) } publishing { diff --git a/game/src/main/kotlin/org/openrs2/game/GameCommand.kt b/game/src/main/kotlin/org/openrs2/game/GameCommand.kt index 6831915b..1b273825 100644 --- a/game/src/main/kotlin/org/openrs2/game/GameCommand.kt +++ b/game/src/main/kotlin/org/openrs2/game/GameCommand.kt @@ -2,13 +2,15 @@ package org.openrs2.game import com.github.ajalt.clikt.core.CliktCommand import com.google.inject.Guice +import org.openrs2.inject.CloseableInjector public fun main(args: Array): Unit = GameCommand().main(args) public class GameCommand : CliktCommand(name = "game") { override fun run() { - val injector = Guice.createInjector(GameModule) - val server = injector.getInstance(GameServer::class.java) - server.run() + CloseableInjector(Guice.createInjector(GameModule)).use { injector -> + val server = injector.getInstance(GameServer::class.java) + server.run() + } } } diff --git a/inject/build.gradle.kts b/inject/build.gradle.kts index f433eed6..88cede5f 100644 --- a/inject/build.gradle.kts +++ b/inject/build.gradle.kts @@ -5,9 +5,6 @@ plugins { dependencies { api("com.google.inject:guice:${Versions.guice}") - - implementation("com.google.guava:guava:${Versions.guava}") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}") } publishing { @@ -16,13 +13,10 @@ publishing { pom { packaging = "jar" - name.set("OpenRS2 HTTP") + name.set("OpenRS2 Inject") description.set( """ - Guava module for creating a HTTP client with .netrc and - redirection support. The I/O dispatcher is used to run - asynchronous requests, as code using coroutines will likely use - the I/O dispatcher to read the response body. + Guice extension for closing AutoCloseable singletons. """.trimIndent() ) } diff --git a/inject/src/main/kotlin/org/openrs2/inject/CloseableInjector.kt b/inject/src/main/kotlin/org/openrs2/inject/CloseableInjector.kt new file mode 100644 index 00000000..3bd0fcbb --- /dev/null +++ b/inject/src/main/kotlin/org/openrs2/inject/CloseableInjector.kt @@ -0,0 +1,23 @@ +package org.openrs2.inject + +import com.google.inject.Injector +import com.google.inject.Scopes + +public class CloseableInjector( + private val injector: Injector +) : Injector by injector, AutoCloseable { + override fun close() { + for (binding in allBindings.values) { + if (!Scopes.isSingleton(binding)) { + continue + } + + val o = binding.provider.get() + if (o !is AutoCloseable) { + continue + } + + o.close() + } + } +} diff --git a/patcher/build.gradle.kts b/patcher/build.gradle.kts index 13256f6e..cc09500b 100644 --- a/patcher/build.gradle.kts +++ b/patcher/build.gradle.kts @@ -15,6 +15,7 @@ dependencies { implementation(project(":conf")) implementation(project(":crypto")) + implementation(project(":inject")) implementation("org.openrs2:openrs2-natives-all:${Versions.openrs2Natives}") } diff --git a/patcher/src/main/kotlin/org/openrs2/patcher/PatchCommand.kt b/patcher/src/main/kotlin/org/openrs2/patcher/PatchCommand.kt index 8ffeb0f7..b94a35e2 100644 --- a/patcher/src/main/kotlin/org/openrs2/patcher/PatchCommand.kt +++ b/patcher/src/main/kotlin/org/openrs2/patcher/PatchCommand.kt @@ -2,18 +2,20 @@ package org.openrs2.patcher import com.github.ajalt.clikt.core.CliktCommand import com.google.inject.Guice +import org.openrs2.inject.CloseableInjector import java.nio.file.Path public fun main(args: Array): Unit = PatchCommand().main(args) public class PatchCommand : CliktCommand(name = "patch") { override fun run() { - val injector = Guice.createInjector(PatcherModule) - val patcher = injector.getInstance(Patcher::class.java) - patcher.run( - input = Path.of("nonfree/lib"), - output = Path.of("nonfree/var/cache/client"), - keyStorePath = Path.of("etc/loader.p12") - ) + CloseableInjector(Guice.createInjector(PatcherModule)).use { injector -> + val patcher = injector.getInstance(Patcher::class.java) + patcher.run( + input = Path.of("nonfree/lib"), + output = Path.of("nonfree/var/cache/client"), + keyStorePath = Path.of("etc/loader.p12") + ) + } } } diff --git a/settings.gradle.kts b/settings.gradle.kts index e06d43b4..0b0c439b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -32,6 +32,7 @@ include( "deob-util", "game", "http", + "inject", "json", "net", "nonfree",