assets: normalize path, tests

This tests the recent regression on windows with backslashes in a path
This commit is contained in:
Moritz Zwerger 2024-01-19 07:47:20 +01:00
parent abab3e83b7
commit 75ed3fdb2d
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 56 additions and 4 deletions

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.assets.directory
import de.bixilon.kutil.latch.AbstractLatch
import de.bixilon.minosoft.assets.AssetsManager
import de.bixilon.minosoft.assets.properties.manager.AssetsManagerProperties
import de.bixilon.minosoft.assets.util.FileAssetsUtil.normalizePath
import de.bixilon.minosoft.assets.util.FileAssetsUtil.toAssetName
import de.bixilon.minosoft.assets.util.FileUtil
import de.bixilon.minosoft.assets.util.InputStreamUtil.readAll
@ -58,7 +59,7 @@ class DirectoryAssetsManager(
continue
}
if (root) continue // root path just allows folders
val path = file.toPath().relativeTo(basePath).toString().replace("\\", "/").removePrefix("/").toAssetName(false, prefix) ?: continue
val path = file.toPath().relativeTo(basePath).toString().normalizePath().toAssetName(false, prefix) ?: continue
assets += path
}
}

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.assets.file
import de.bixilon.kutil.latch.AbstractLatch
import de.bixilon.minosoft.assets.AssetsManager
import de.bixilon.minosoft.assets.util.FileAssetsUtil.normalizePath
import de.bixilon.minosoft.assets.util.FileAssetsUtil.toAssetName
import de.bixilon.minosoft.assets.util.InputStreamUtil.readAll
import de.bixilon.minosoft.assets.util.InputStreamUtil.readJson
@ -56,7 +57,7 @@ class ZipAssetsManager(
"pack.png" -> image = stream.readAll(false)
"pack.mcmeta" -> properties = stream.readJson(false)
else -> {
val resourceLocation = name.toAssetName(prefix = prefix) ?: return
val resourceLocation = name.normalizePath().toAssetName(prefix = prefix) ?: return
assets[resourceLocation] = stream.readAll(false)
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -17,6 +17,7 @@ import de.bixilon.minosoft.terminal.RunConfiguration
object AssetsOptions {
const val MAX_FILE_CHECKING = 10
const val SEPARATOR = '/'
var PATH = RunConfiguration.HOME_DIRECTORY.resolve("assets/")
var COMPRESS_ASSETS = true
}

View File

@ -136,6 +136,18 @@ object FileAssetsUtil {
}
fun String.normalizePath(): String {
var string = this.replace('\\', AssetsOptions.SEPARATOR)
if (File.separatorChar != AssetsOptions.SEPARATOR) {
string = string.replace(File.separatorChar, AssetsOptions.SEPARATOR)
}
while (string.startsWith(AssetsOptions.SEPARATOR)) {
string = string.substring(1)
}
return string
}
fun String.toAssetName(verifyPrefix: Boolean = true, prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX): ResourceLocation? {
if (verifyPrefix && !startsWith("$prefix/")) {
return null

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.modding.loader
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.reflection.ReflectionUtil.getFieldOrNull
import de.bixilon.minosoft.assets.util.FileAssetsUtil.normalizePath
import de.bixilon.minosoft.assets.util.InputStreamUtil.readAll
import org.xeustechnologies.jcl.JarClassLoader
import org.xeustechnologies.jcl.JarResources
@ -40,7 +41,7 @@ object LoaderUtil {
}
fun JarClassLoader.load(name: String, data: ByteArray) {
val fixed = name.replace('\\', '/').replace(" ", "").replace(":", "") // class loaders just work with /
val fixed = name.normalizePath().replace(" ", "").replace(":", "") // class loaders just work with /
val content = this.classpathResources.contents
val entry = JclJarEntry()
entry.baseUrl = null

View File

@ -0,0 +1,36 @@
/*
* Minosoft
* Copyright (C) 2020-2024 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.util
import de.bixilon.minosoft.assets.util.FileAssetsUtil.normalizePath
import de.bixilon.minosoft.assets.util.FileAssetsUtil.toAssetName
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class FileAssetsUtilTest {
@Test
fun `windows path with backslashes`() {
val path = "\\\\test\\folder\\with\\some\\file\\in\\it.abc"
assertEquals(path.normalizePath(), "test/folder/with/some/file/in/it.abc")
}
@Test
fun `windows get asset name`() {
val path = "assets\\minecraft\\folder\\file.png"
assertEquals(path.normalizePath().toAssetName(), ResourceLocation("minecraft", "folder/file.png"))
}
}