assets: allow different assets prefix, overwrite assets manager

This commit is contained in:
Bixilon 2022-04-19 17:18:37 +02:00
parent c031b241f9
commit d3a0ff5745
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 28 additions and 16 deletions

View File

@ -58,6 +58,7 @@ import de.bixilon.minosoft.util.task.worker.StartupTasks
object Minosoft {
val MAIN_THREAD: Thread = Thread.currentThread()
val MINOSOFT_ASSETS_MANAGER = ResourcesAssetsUtil.create(Minosoft::class.java, canUnload = false)
val OVERWRITE_ASSETS_MANAGER = ResourcesAssetsUtil.create(Minosoft::class.java, canUnload = false, prefix = "assets_overwrite")
val LANGUAGE_MANAGER = MultiLanguageManager()
val START_UP_LATCH = CountUpAndDownLatch(1)
@ -66,6 +67,7 @@ object Minosoft {
CommandLineArguments.parse(args)
KUtil.initUtilClasses()
MINOSOFT_ASSETS_MANAGER.load(CountUpAndDownLatch(0))
OVERWRITE_ASSETS_MANAGER.load(CountUpAndDownLatch(0)) // ToDo: async
Log.log(LogMessageType.OTHER, LogLevels.INFO) { "Starting minosoft..." }
if (OSUtil.OS == OSUtil.OSs.MAC && !RunConfiguration.X_START_ON_FIRST_THREAD_SET && !RunConfiguration.DISABLE_RENDERING) {

View File

@ -28,7 +28,8 @@ object AssetsLoader {
fun create(profile: ResourcesProfile, version: Version, latch: CountUpAndDownLatch, property: AssetsVersionProperty = AssetsVersionProperties[version] ?: throw IllegalAccessException("$version has no assets!")): AssetsManager {
val assetsManager = PriorityAssetsManager()
for (resourcePack in profile.assets.resourcePacks) {
assetsManager += Minosoft.OVERWRITE_ASSETS_MANAGER
for (resourcePack in profile.assets.resourcePacks.reversed()) {
resourcePack.type.creator(resourcePack).let {
it.load(latch)
assetsManager += it

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
* Copyright (C) 2020-2022 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.
*
@ -60,4 +60,8 @@ interface AssetsManager {
* Deletes all assets from memory
*/
fun unload()
companion object {
const val DEFAULT_ASSETS_PREFIX = "assets"
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
* Copyright (C) 2020-2022 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.
*
@ -31,6 +31,7 @@ import java.io.InputStream
class DirectoryAssetsManager(
basePath: String,
private val canUnload: Boolean = true,
val prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX,
) : AssetsManager {
private val basePath = File(basePath).slashPath
override val namespaces: MutableSet<String> = mutableSetOf()
@ -47,7 +48,7 @@ class DirectoryAssetsManager(
scanDirectory(file)
continue
}
val path = file.slashPath.removePrefix(basePath).removePrefix("/").toAssetName(false) ?: continue
val path = file.slashPath.removePrefix(basePath).removePrefix("/").toAssetName(false, prefix) ?: continue
assets += path
namespaces += path.namespace
}

View File

@ -22,16 +22,16 @@ import java.nio.charset.StandardCharsets
object ResourcesAssetsUtil {
fun create(clazz: Class<*>, canUnload: Boolean = true): AssetsManager {
fun create(clazz: Class<*>, canUnload: Boolean = true, prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX): AssetsManager {
val rootResources = clazz.classLoader.getResource("assets") ?: throw FileNotFoundException("Can not find assets folder in $clazz")
return when (rootResources.protocol) {
"file" -> DirectoryAssetsManager(rootResources.path, canUnload) // Read them directly from the folder
"file" -> DirectoryAssetsManager(rootResources.path, canUnload, prefix) // Read them directly from the folder
"jar" -> {
val path: String = rootResources.path
val jarPath = path.substring(5, path.indexOf("!"))
val zip = URLDecoder.decode(jarPath, StandardCharsets.UTF_8)
ZipAssetsManager(zip, canUnload = canUnload)
ZipAssetsManager(zip, canUnload = canUnload, prefix = prefix)
}
else -> throw IllegalStateException("Can not read resources: $rootResources")
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
* Copyright (C) 2020-2022 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.
*
@ -14,6 +14,7 @@
package de.bixilon.minosoft.assets.file
import de.bixilon.kutil.latch.CountUpAndDownLatch
import de.bixilon.minosoft.assets.AssetsManager
import de.bixilon.minosoft.assets.util.FileAssetsUtil.toAssetName
import de.bixilon.minosoft.assets.util.FileUtil.readJson
import java.io.File
@ -27,6 +28,7 @@ import java.util.zip.ZipInputStream
class ZipAssetsManager(
private val inputStream: ZipInputStream,
canUnload: Boolean = true,
val prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX,
) : FileAssetsManager(canUnload) {
override fun load(latch: CountUpAndDownLatch) {
@ -42,7 +44,7 @@ class ZipAssetsManager(
"pack.png" -> image = inputStream.readAllBytes()
"pack.mcmeta" -> properties = inputStream.readJson(false)
else -> {
val resourceLocation = name.toAssetName() ?: continue
val resourceLocation = name.toAssetName(prefix = prefix) ?: continue
namespaces += resourceLocation.namespace
assets[resourceLocation] = inputStream.readAllBytes()
}
@ -54,6 +56,6 @@ class ZipAssetsManager(
loaded = true
}
constructor(file: File, canUnload: Boolean = true) : this(ZipInputStream(FileInputStream(file)), canUnload)
constructor(path: String, canUnload: Boolean = true) : this(File(path), canUnload)
constructor(file: File, canUnload: Boolean = true, prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX) : this(ZipInputStream(FileInputStream(file)), canUnload, prefix)
constructor(path: String, canUnload: Boolean = true, prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX) : this(File(path), canUnload, prefix)
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
* Copyright (C) 2020-2022 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.
*
@ -18,6 +18,7 @@ import com.github.luben.zstd.ZstdOutputStream
import de.bixilon.kutil.array.ByteArrayUtil.toHex
import de.bixilon.kutil.hex.HexUtil.isHexString
import de.bixilon.kutil.random.RandomStringUtil.randomString
import de.bixilon.minosoft.assets.AssetsManager
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.terminal.RunConfiguration
@ -115,11 +116,11 @@ object FileAssetsUtil {
return saveAndGet(ByteArrayInputStream(data), compress, false, hashType)
}
fun String.toAssetName(verifyPrefix: Boolean = true): ResourceLocation? {
if (verifyPrefix && !startsWith("assets/")) {
fun String.toAssetName(verifyPrefix: Boolean = true, prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX): ResourceLocation? {
if (verifyPrefix && !startsWith("$prefix/")) {
return null
}
val split = removePrefix("assets/").split("/", limit = 2)
val split = removePrefix("$prefix/").split("/", limit = 2)
if (split.size != 2) {
return null
}

View File

@ -28,9 +28,10 @@ class SignBlockEntityRenderer(
) : MeshedBlockEntityRenderer<SignBlockEntity> {
override fun singleRender(position: Vec3i, mesh: WorldMesh, random: Random, blockState: BlockState, neighbours: Array<BlockState?>, light: ByteArray, ambientLight: FloatArray, tints: IntArray?): Boolean {
val model = this.blockState.block.nullCast<SignBlock>() ?: return false
val model = this.blockState.block.nullCast<SignBlock>()?.model ?: return false
println("Rendering sign at $position")
// ToDo
return true