directory assets manager: use java path instead of string with slashes

This commit is contained in:
Moritz Zwerger 2023-12-22 23:24:09 +01:00
parent d77bb34162
commit 0cbd90b598
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 16 additions and 15 deletions

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.assets.directory package de.bixilon.minosoft.assets.directory
import de.bixilon.kutil.file.FileUtil.slashPath
import de.bixilon.kutil.latch.AbstractLatch import de.bixilon.kutil.latch.AbstractLatch
import de.bixilon.minosoft.assets.AssetsManager import de.bixilon.minosoft.assets.AssetsManager
import de.bixilon.minosoft.assets.properties.manager.AssetsManagerProperties import de.bixilon.minosoft.assets.properties.manager.AssetsManagerProperties
@ -27,6 +26,7 @@ import java.io.FileInputStream
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.InputStream import java.io.InputStream
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.relativeTo
/** /**
@ -34,11 +34,11 @@ import java.nio.file.Path
*/ */
class DirectoryAssetsManager( class DirectoryAssetsManager(
private val rootPath: String, private val rootPath: Path,
private val canUnload: Boolean = true, private val canUnload: Boolean = true,
val prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX, val prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX,
) : AssetsManager { ) : AssetsManager {
private val basePath = File(rootPath).slashPath + "/" + prefix private val basePath = rootPath.resolve(prefix)
private var assets: MutableSet<ResourceLocation> = ObjectOpenHashSet() private var assets: MutableSet<ResourceLocation> = ObjectOpenHashSet()
override var loaded: Boolean = false override var loaded: Boolean = false
private set private set
@ -48,22 +48,23 @@ class DirectoryAssetsManager(
private set private set
private val ResourceLocation.filePath: Path private val ResourceLocation.filePath: Path
get() = Path.of(basePath, namespace, path) get() = basePath.resolve(namespace).resolve(path)
private fun scanDirectory(directory: File) { private fun scanDirectory(root: Boolean, directory: File) {
for (file in directory.listFiles() ?: return) { for (file in directory.listFiles() ?: return) {
if (file.isDirectory) { if (file.isDirectory) {
scanDirectory(file) scanDirectory(false, file)
continue continue
} }
val path = file.slashPath.removePrefix(basePath).removePrefix(File.separator).toAssetName(false, prefix) ?: continue if (root) continue // root path just allows folders
val path = file.toPath().relativeTo(basePath).toString().removePrefix(File.separator).toAssetName(false, prefix) ?: continue
assets += path assets += path
} }
} }
override fun load(latch: AbstractLatch?) { override fun load(latch: AbstractLatch?) {
check(!loaded) { "Already loaded!" } check(!loaded) { "Already loaded!" }
scanDirectory(File(basePath)) scanDirectory(true, basePath.toFile())
File("$rootPath/pack.png").let { if (it.exists() && it.isFile) image = FileInputStream(it).readAllBytes() } File("$rootPath/pack.png").let { if (it.exists() && it.isFile) image = FileInputStream(it).readAllBytes() }
File("$rootPath/pack.mcmeta").let { if (it.exists() && it.isFile) properties = FileInputStream(it).readJson() } File("$rootPath/pack.mcmeta").let { if (it.exists() && it.isFile) properties = FileInputStream(it).readJson() }
loaded = true loaded = true

View File

@ -19,10 +19,9 @@ import de.bixilon.minosoft.assets.resource.ResourceAssetsManager
import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType import de.bixilon.minosoft.util.logging.LogMessageType
import java.io.File
import java.net.URLDecoder import java.net.URLDecoder
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.nio.file.Path
object ResourcesAssetsUtil { object ResourcesAssetsUtil {
@ -34,7 +33,7 @@ object ResourcesAssetsUtil {
} }
return when (rootResources.protocol) { return when (rootResources.protocol) {
"file" -> DirectoryAssetsManager(rootResources.path.removeSuffix(File.separator).removeSuffix(prefix), canUnload, prefix) // Read them directly from the folder "file" -> DirectoryAssetsManager(Path.of(rootResources.path).parent, canUnload, prefix) // Read them directly from the folder
"jar" -> { "jar" -> {
val path: String = rootResources.path val path: String = rootResources.path
val jarPath = path.substring(5, path.indexOf("!")) val jarPath = path.substring(5, path.indexOf("!"))

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * 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. * 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.
* *
@ -16,9 +16,10 @@ package de.bixilon.minosoft.config.profile.profiles.resources.assets.packs
import de.bixilon.minosoft.assets.AssetsManager import de.bixilon.minosoft.assets.AssetsManager
import de.bixilon.minosoft.assets.directory.DirectoryAssetsManager import de.bixilon.minosoft.assets.directory.DirectoryAssetsManager
import de.bixilon.minosoft.assets.file.ZipAssetsManager import de.bixilon.minosoft.assets.file.ZipAssetsManager
import java.nio.file.Path
enum class ResourcePackType(val creator: (ResourcePack) -> AssetsManager) { enum class ResourcePackType(val creator: (ResourcePack) -> AssetsManager) {
ZIP({ ZipAssetsManager(it.path) }), ZIP({ ZipAssetsManager(it.path) }),
DIRECTORY({ DirectoryAssetsManager(it.path) }), DIRECTORY({ DirectoryAssetsManager(Path.of(it.path)) }),
; ;
} }

View File

@ -35,7 +35,7 @@ class DirectorySource(
override fun process(mod: MinosoftMod) { override fun process(mod: MinosoftMod) {
val files = directory.listFiles()!! val files = directory.listFiles()!!
val assets = DirectoryAssetsManager(directory.path) val assets = DirectoryAssetsManager(directory.toPath())
assets.load() assets.load()
for (sub in files) { for (sub in files) {

View File

@ -49,7 +49,7 @@ class SplitDirectorySource(
} }
private fun processResources(mod: MinosoftMod) { private fun processResources(mod: MinosoftMod) {
val assets = DirectoryAssetsManager(resources.path) val assets = DirectoryAssetsManager(resources.toPath())
assets.load(ParentLatch(0, mod.latch)) assets.load(ParentLatch(0, mod.latch))
mod.assetsManager = assets mod.assetsManager = assets