From 364b6947e8ee6285ea68d67c7a02b2d9ed9e39b1 Mon Sep 17 00:00:00 2001 From: Scyu_ Date: Mon, 28 Oct 2024 00:05:49 +0000 Subject: [PATCH] chore: make jar resource more generic --- .../geary/prefabs/PrefabsDSLExtensions.kt | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/addons/geary-prefabs/src/jvmMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSLExtensions.kt b/addons/geary-prefabs/src/jvmMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSLExtensions.kt index 7b6ca85f..f0464e33 100644 --- a/addons/geary-prefabs/src/jvmMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSLExtensions.kt +++ b/addons/geary-prefabs/src/jvmMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSLExtensions.kt @@ -61,9 +61,9 @@ object PrefabsDSLExtensions { val entry = entries.nextElement() if (entry.name.startsWith(directory) && !entry.isDirectory) { yield(JarResource( - nameWithoutExt = entry.name.substringAfterLast("/").substringBeforeLast("."), - ext = entry.name.substringAfterLast("."), - stream = jarFile.getInputStream(entry) + classLoader, + path = entry.name.substringAfter(directory).removePrefix("/"), + resource = entry.name )) } } @@ -71,9 +71,9 @@ object PrefabsDSLExtensions { val directoryPath = File(classLoader.getResource(directory)?.toURI() ?: return@sequence).toPath() yieldAll(directoryPath.walk().filter { it.isRegularFile() }.map { JarResource( - nameWithoutExt = it.nameWithoutExtension, - ext = it.extension, - stream = it.inputStream() + classLoader, + path = it.toString().substringAfter(directoryPath.toString()).removePrefix("/"), + resource = it.toString() ) }) } @@ -83,9 +83,9 @@ object PrefabsDSLExtensions { fun getResource(classLoader: ClassLoader, path: String): JarResource? { return classLoader.getResourceAsStream(path)?.let { stream -> JarResource( - nameWithoutExt = path.substringAfterLast("/").substringBeforeLast("."), - ext = path.substringAfterLast("."), - stream = stream + classLoader = classLoader, + path = path, + resource = path ) } } @@ -97,8 +97,12 @@ object PrefabsDSLExtensions { ) data class JarResource( - val nameWithoutExt: String, - val ext: String, - val stream: InputStream, - ) + val classLoader: ClassLoader, + val path: String, + val resource: String + ) { + val nameWithoutExt = path.substringAfterLast("/").substringBeforeLast(".") + val ext = path.substringAfterLast(".") + val stream = classLoader.getResourceAsStream(resource)!! + } }