From 4e6d9a7675c495c1db8dfe9bedb2ff7d6ed5f080 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 25 Jun 2023 13:34:07 +0200 Subject: [PATCH] resource assets stream May be a workaround for graal vm (#115) or Android (#71) --- .../assets/file/ResourcesAssetsUtil.kt | 14 ++++- .../assets/multi/PriorityAssetsManager.kt | 2 +- .../assets/resource/ResourceAssetsManager.kt | 57 +++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/assets/resource/ResourceAssetsManager.kt diff --git a/src/main/java/de/bixilon/minosoft/assets/file/ResourcesAssetsUtil.kt b/src/main/java/de/bixilon/minosoft/assets/file/ResourcesAssetsUtil.kt index 7c525b56d..bfb791ea8 100644 --- a/src/main/java/de/bixilon/minosoft/assets/file/ResourcesAssetsUtil.kt +++ b/src/main/java/de/bixilon/minosoft/assets/file/ResourcesAssetsUtil.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -15,6 +15,10 @@ package de.bixilon.minosoft.assets.file import de.bixilon.minosoft.assets.AssetsManager import de.bixilon.minosoft.assets.directory.DirectoryAssetsManager +import de.bixilon.minosoft.assets.resource.ResourceAssetsManager +import de.bixilon.minosoft.util.logging.Log +import de.bixilon.minosoft.util.logging.LogLevels +import de.bixilon.minosoft.util.logging.LogMessageType import java.io.FileNotFoundException import java.net.URLDecoder import java.nio.charset.StandardCharsets @@ -23,7 +27,7 @@ import java.nio.charset.StandardCharsets object ResourcesAssetsUtil { fun create(clazz: Class<*>, canUnload: Boolean = true, prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX): AssetsManager { - val rootResources = clazz.classLoader.getResource(prefix) ?: throw FileNotFoundException("Can not find assets folder in $clazz") + val rootResources = clazz.classLoader.getResource(prefix) ?: throw FileNotFoundException("Can not find assets root for $clazz") return when (rootResources.protocol) { "file" -> DirectoryAssetsManager(rootResources.path.removeSuffix("/").removeSuffix(prefix), canUnload, prefix) // Read them directly from the folder @@ -34,7 +38,11 @@ object ResourcesAssetsUtil { ZipAssetsManager(zip, canUnload = canUnload, prefix = prefix) } - else -> throw IllegalStateException("Can not read resources: $rootResources") + else -> { + Log.log(LogMessageType.ASSETS, LogLevels.WARN) { "Can not find resource manager for $rootResources" } + + ResourceAssetsManager(clazz, prefix) + } } } } diff --git a/src/main/java/de/bixilon/minosoft/assets/multi/PriorityAssetsManager.kt b/src/main/java/de/bixilon/minosoft/assets/multi/PriorityAssetsManager.kt index 8f36a5546..b63493db0 100644 --- a/src/main/java/de/bixilon/minosoft/assets/multi/PriorityAssetsManager.kt +++ b/src/main/java/de/bixilon/minosoft/assets/multi/PriorityAssetsManager.kt @@ -56,7 +56,7 @@ class PriorityAssetsManager( operator fun plusAssign(manager: AssetsManager) = add(manager) override fun get(path: ResourceLocation): InputStream { - return getOrNull(path) ?: throw FileNotFoundException("Can not find assets-manager for $path") + return getOrNull(path) ?: throw FileNotFoundException("Can not find asset $path") } override fun getOrNull(path: ResourceLocation): InputStream? { diff --git a/src/main/java/de/bixilon/minosoft/assets/resource/ResourceAssetsManager.kt b/src/main/java/de/bixilon/minosoft/assets/resource/ResourceAssetsManager.kt new file mode 100644 index 000000000..c647a593c --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/assets/resource/ResourceAssetsManager.kt @@ -0,0 +1,57 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.assets.resource + +import de.bixilon.kutil.latch.AbstractLatch +import de.bixilon.minosoft.assets.AssetsManager +import de.bixilon.minosoft.data.registries.identified.ResourceLocation +import de.bixilon.minosoft.util.logging.Log +import de.bixilon.minosoft.util.logging.LogLevels +import de.bixilon.minosoft.util.logging.LogMessageType +import java.io.FileNotFoundException +import java.io.InputStream + +@Deprecated("Super slow") +class ResourceAssetsManager( + val clazz: Class<*>, + val prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX, +) : AssetsManager { + override var loaded: Boolean = false + + override fun load(latch: AbstractLatch?) { + Log.log(LogMessageType.ASSETS, LogLevels.WARN) { "Loaded resource assets manager for class $clazz. Performance will be impacted!" } + } + + override fun unload() = Unit + + private fun open(path: ResourceLocation): InputStream? { + val url = "/$prefix/${path.namespace}/${path.path}" + + return clazz.getResourceAsStream(url) + } + + override fun contains(path: ResourceLocation): Boolean { + val stream = open(path) ?: return false + stream.close() + return true + } + + override fun get(path: ResourceLocation): InputStream { + return open(path) ?: throw FileNotFoundException("Can not find asset $path") + } + + override fun getOrNull(path: ResourceLocation): InputStream? { + return open(path) + } +}