Small refactoring

This commit is contained in:
grimsi
2024-10-31 09:07:46 +01:00
parent f89086a06d
commit 5ebd074d3b
8 changed files with 34 additions and 40 deletions
@@ -9,7 +9,7 @@ import java.nio.file.Path
/**
* @see https://stackoverflow.com/questions/73654174/my-application-cant-find-the-extension-with-pf4j
*/
class SpringDevtoolsDevelopmentPluginLoader(
class GameyfinPluginLoader(
pluginManager: PluginManager,
private val parentClassLoader: ClassLoader
) : DevelopmentPluginLoader(pluginManager) {
@@ -18,7 +18,7 @@ import java.nio.file.Path
/**
* @see https://stackoverflow.com/questions/73654174/my-application-cant-find-the-extension-with-pf4j
*/
class SpringDevtoolsPluginManager(
class GameyfinPluginManager(
path: Path,
private val pluginConfigRepository: PluginConfigRepository
) : DefaultPluginManager(path) {
@@ -34,7 +34,7 @@ class SpringDevtoolsPluginManager(
override fun createPluginLoader(): PluginLoader {
val compoundPluginLoader = CompoundPluginLoader()
val developmentPluginLoader = SpringDevtoolsDevelopmentPluginLoader(this, javaClass.classLoader)
val developmentPluginLoader = GameyfinPluginLoader(this, javaClass.classLoader)
val jarPluginLoader = JarPluginLoader(this)
val defaultPluginLoader = DefaultPluginLoader(this)
@@ -7,12 +7,12 @@ import org.springframework.stereotype.Service
@Service
class PluginConfigService(
private val pluginConfigRepository: PluginConfigRepository,
private val pluginManager: SpringDevtoolsPluginManager
private val pluginManager: GameyfinPluginManager
) {
fun getConfigMetadata(pluginId: String): List<PluginConfigElement> {
val plugin = pluginManager.getPlugin(pluginId).plugin as GameyfinPlugin
return plugin.getConfigMetadata()
return plugin.configMetadata
}
fun getConfig(pluginId: String): Map<String, String?> {
@@ -15,7 +15,7 @@ class PluginManagerConfig(
private val pluginPath = System.getProperty("pf4j.pluginsDir", "plugins")
@Bean
fun pluginManager() = SpringDevtoolsPluginManager(Path.of(pluginPath), pluginConfigRepository)
fun pluginManager() = GameyfinPluginManager(Path.of(pluginPath), pluginConfigRepository)
@EventListener(ApplicationReadyEvent::class)
fun loadPlugins() {
+2
View File
@@ -13,6 +13,8 @@ dependencies {
api("org.pf4j:pf4j:${rootProject.extra["pf4jVersion"]}") {
exclude(group = "org.slf4j")
}
api("org.slf4j:slf4j-api:2.0.16")
implementation(kotlin("stdlib"))
@@ -1,7 +1,18 @@
package de.grimsi.gameyfin.pluginapi.core
interface GameyfinPlugin {
fun getConfigMetadata(): List<PluginConfigElement>
fun getCurrentConfig(): Map<String, String?>
fun loadConfig(config: Map<String, String?>)
import org.pf4j.Plugin
import org.pf4j.PluginWrapper
abstract class GameyfinPlugin(wrapper: PluginWrapper) : Plugin(wrapper) {
abstract val configMetadata: List<PluginConfigElement>
protected open var config: Map<String, String?> = emptyMap()
open fun getCurrentConfig(): Map<String, String?> {
return config
}
open fun loadConfig(config: Map<String, String?>) {
this.config = config
}
}
-1
View File
@@ -7,7 +7,6 @@ subprojects {
dependencies {
compileOnly(project(":plugin-api"))
implementation("io.github.oshai:kotlin-logging-jvm:7.0.0")
}
tasks.jar {
@@ -11,66 +11,48 @@ import de.grimsi.gameyfin.pluginapi.gamemetadata.GameMetadata
import de.grimsi.gameyfin.pluginapi.gamemetadata.GameMetadataFetcher
import de.grimsi.gameyfin.pluginapi.gamemetadata.Genre
import de.grimsi.gameyfin.pluginapi.gamemetadata.Theme
import io.github.oshai.kotlinlogging.KotlinLogging
import org.pf4j.Extension
import org.pf4j.Plugin
import org.pf4j.PluginWrapper
import org.slf4j.LoggerFactory
import java.time.Instant
import kotlin.collections.filter
class IgdbPlugin(wrapper: PluginWrapper) : Plugin(wrapper), GameyfinPlugin {
class IgdbPlugin(wrapper: PluginWrapper) : GameyfinPlugin(wrapper) {
private val log = KotlinLogging.logger {}
private val configMetadata: List<PluginConfigElement> = listOf(
override val configMetadata: List<PluginConfigElement> = listOf(
PluginConfigElement("clientId", "Twitch client ID", "Your Twitch Client ID"),
PluginConfigElement("clientSecret", "Twitch client secret", "Your Twitch Client Secret")
)
private var config: Map<String, String?> = configMetadata.associate { it.key to null }
override fun getConfigMetadata(): List<PluginConfigElement> {
return configMetadata
}
override fun getCurrentConfig(): Map<String, String?> {
return config
}
override fun loadConfig(config: Map<String, String?>) {
this.config = config
}
override fun start() {
try {
authenticate()
} catch (e: PluginConfigError) {
log.error { e.message }
log.error(e.message)
}
}
override fun stop() {
log.debug { "IgdbPlugin.stop()" }
log.debug("IgdbPlugin.stop()")
}
private fun authenticate() {
log.debug { "Authenticating on Twitch API..." }
log.debug("Authenticating on Twitch API...")
val clientId: String = config["clientId"] ?: throw PluginConfigError("Twitch Client ID not set")
val clientSecret: String = config["clientSecret"] ?: throw PluginConfigError("Twitch Client Secret not set")
val token = TwitchAuthenticator.requestTwitchToken(clientId, clientSecret)
?: throw PluginConfigError("Failed to authenticate on Twitch API")
?: throw PluginConfigError("Failed to authenticate on Twitch API with provided credentials")
IGDBWrapper.setCredentials(clientId, token.access_token)
log.debug { "Authentication successful" }
log.debug("Authentication successful")
}
@Extension
class IgdbMetadataFetcher : GameMetadataFetcher {
private val log = KotlinLogging.logger {}
private val log = LoggerFactory.getLogger(javaClass)
override fun fetchMetadata(gameId: String): GameMetadata {
val findGameByName = APICalypse()
@@ -124,7 +106,7 @@ class IgdbPlugin(wrapper: PluginWrapper) : Plugin(wrapper), GameyfinPlugin {
"hack-and-slash-beat-em-up" -> Genre.HACK_AND_SLASH_BEAT_EM_UP
"quiz-trivia" -> Genre.QUIZ_TRIVIA
else -> {
log.warn { "Unknown genre: ${genre.slug}" }
log.warn("Unknown genre: {}", genre.slug)
Genre.UNKNOWN
}
}
@@ -155,7 +137,7 @@ class IgdbPlugin(wrapper: PluginWrapper) : Plugin(wrapper), GameyfinPlugin {
"erotic" -> Theme.EROTIC
"romance" -> Theme.ROMANCE
else -> {
log.warn { "Unknown theme: ${theme.slug}" }
log.warn("Unknown theme: {}", theme.slug)
Theme.UNKNOWN
}
}