resource assets stream

May be a workaround for graal vm (#115) or Android (#71)
This commit is contained in:
Bixilon 2023-06-25 13:34:07 +02:00
parent f3bbf64e21
commit 4e6d9a7675
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 69 additions and 4 deletions

View File

@ -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)
}
}
}
}

View File

@ -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? {

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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)
}
}