Implement plugin state (closes #603) (#622)

This commit is contained in:
Simon
2025-07-11 20:23:39 +02:00
committed by GitHub
parent a407471814
commit c4c39a8dd3
7 changed files with 92 additions and 28 deletions
+1
View File
@@ -21,6 +21,7 @@ subprojects {
apply(plugin = "org.jetbrains.kotlin.jvm")
dependencies {
compileOnly(kotlin("stdlib"))
compileOnly(project(":plugin-api"))
}
@@ -33,6 +33,8 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
private lateinit var communicationManager: CommunicationManager
private lateinit var plugin: TorrentDownloadPlugin
private lateinit var state: TorrentDownloadPluginState
}
init {
@@ -70,14 +72,8 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
)
)
@OptIn(ExperimentalPathApi::class)
override fun start() {
// Currently Gameyfin does not support storing plugin state
// and since we can't associate the torrent files with a game path after a restart
// we just delete the directory on startup.
if (Files.exists(TORRENT_FILE_DIRECTORY)) {
TORRENT_FILE_DIRECTORY.deleteRecursively()
}
Files.createDirectories(TORRENT_FILE_DIRECTORY)
tracker = Tracker(config("trackerPort"), getTrackerUri().toString())
@@ -95,6 +91,27 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
SelectorFactoryImpl(),
FirstAvailableChannel(clientPort, clientPort)
)
state = loadState<TorrentDownloadPluginState>() ?: TorrentDownloadPluginState()
state.torrentFilesMetadata.forEach {
// Check if the torrent and game files exist and
// that the game files have not been modified since the torrent file was created
if (Files.exists(it.torrentFile) && Files.exists(it.gameFile) &&
it.gameFile.getLastModifiedTime().toInstant().isBefore(it.torrentFile.getLastModifiedTime().toInstant())
) {
tracker.announce(TrackedTorrent.load(it.torrentFile.toFile()))
communicationManager.addTorrent(
it.torrentFile.toString(),
getRootPath(it.gameFile).toString(),
FullyPieceStorageFactory.INSTANCE
)
} else {
state.torrentFilesMetadata.remove(it)
}
}
saveState(state)
}
override fun stop() {
@@ -146,6 +163,14 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
)
}
private fun getRootPath(gameFilesPath: Path): Path {
return if (gameFilesPath.isDirectory()) {
gameFilesPath
} else {
gameFilesPath.parent
}
}
@Extension(ordinal = 2)
class TorrentDownloadProvider : DownloadProvider {
private val log = LoggerFactory.getLogger(TorrentDownloadProvider::class.java)
@@ -180,10 +205,19 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
tracker.announce(TrackedTorrent.load(torrentFile.toFile()))
communicationManager.addTorrent(
torrentFile.toString(),
getRootPath(gameFilesPath).toString(),
plugin.getRootPath(gameFilesPath).toString(),
FullyPieceStorageFactory.INSTANCE
)
state.torrentFilesMetadata.add(
TorrentFileMetadata(
torrentFile = torrentFile,
gameFile = gameFilesPath
)
)
plugin.saveState(state)
return torrentFile
}
@@ -192,18 +226,10 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
.numHashingThreads(Runtime.getRuntime().availableProcessors() * 2)
.createdBy(plugin.javaClass.name)
.addFile(gameFilesPath)
.rootPath(getRootPath(gameFilesPath))
.rootPath(plugin.getRootPath(gameFilesPath))
.announce(plugin.getTrackerUri().toString())
.privateFlag(plugin.config("privateMode"))
.build()
}
private fun getRootPath(gameFilesPath: Path): Path {
return if (gameFilesPath.isDirectory()) {
gameFilesPath
} else {
gameFilesPath.parent
}
}
}
}
@@ -0,0 +1,12 @@
package org.gameyfin.plugins.download.torrent
import java.nio.file.Path
data class TorrentDownloadPluginState(
val torrentFilesMetadata: MutableList<TorrentFileMetadata> = mutableListOf()
)
data class TorrentFileMetadata(
val torrentFile: Path,
val gameFile: Path
)
@@ -1,4 +1,4 @@
Plugin-Version: 1.0.0.beta1
Plugin-Version: 1.0.0.beta2
Plugin-Class: org.gameyfin.plugins.download.torrent.TorrentDownloadPlugin
Plugin-Id: org.gameyfin.plugins.download.torrent
Plugin-Name: Torrent Download