From 951bf6e339778a02d9bd07a7dcd4062797da8733 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 4 Jan 2023 23:57:15 +0100 Subject: [PATCH] restructure some things in ResourceLocation --- .../minosoft/assets/util/FileAssetsUtil.kt | 4 +- .../minosoft/commands/util/StringReader.kt | 4 +- .../minosoft/data/language/LanguageUtil.kt | 6 +-- .../data/registries/ResourceLocation.kt | 45 +++++++++--------- .../gui/rendering/models/ModelLoader.kt | 6 +-- .../model/textures/SkeletalTexture.kt | 4 +- .../system/base/shader/NativeShader.kt | 4 +- .../base/shader/code/glsl/GLSLShaderCode.kt | 4 +- .../skin/vanilla/DefaultSkinProvider.kt | 6 +-- .../protocol/protocol/ProtocolDefinition.java | 5 +- .../java/de/bixilon/minosoft/util/KUtil.kt | 4 +- .../protocol/ProtocolDefinitionTest.kt | 47 ++++++++++--------- 12 files changed, 69 insertions(+), 70 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/assets/util/FileAssetsUtil.kt b/src/main/java/de/bixilon/minosoft/assets/util/FileAssetsUtil.kt index e831de071..9f11ef5a3 100644 --- a/src/main/java/de/bixilon/minosoft/assets/util/FileAssetsUtil.kt +++ b/src/main/java/de/bixilon/minosoft/assets/util/FileAssetsUtil.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. * @@ -133,7 +133,7 @@ object FileAssetsUtil { if (split.size != 2) { return null } - return ResourceLocation.of(split[0], split[1]) + return ResourceLocation(split[0], split[1]) } fun verifyAsset(hash: String, file: File = File(getPath(hash)), verify: Boolean, hashType: HashTypes = HashTypes.SHA256, compress: Boolean = true): Boolean { diff --git a/src/main/java/de/bixilon/minosoft/commands/util/StringReader.kt b/src/main/java/de/bixilon/minosoft/commands/util/StringReader.kt index 4760b12d7..288db63bd 100644 --- a/src/main/java/de/bixilon/minosoft/commands/util/StringReader.kt +++ b/src/main/java/de/bixilon/minosoft/commands/util/StringReader.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. * @@ -298,7 +298,7 @@ open class StringReader(val string: String) { read() val path = readWord() ?: return null - return ResourceLocation.of(namespace, path) + return ResourceLocation(namespace, path) } fun readResult(reader: StringReader.() -> T): ReadResult { diff --git a/src/main/java/de/bixilon/minosoft/data/language/LanguageUtil.kt b/src/main/java/de/bixilon/minosoft/data/language/LanguageUtil.kt index 2f9a5a38e..446317d32 100644 --- a/src/main/java/de/bixilon/minosoft/data/language/LanguageUtil.kt +++ b/src/main/java/de/bixilon/minosoft/data/language/LanguageUtil.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. * @@ -83,7 +83,7 @@ object LanguageUtil { fun loadLanguage(language: String, assetsManager: AssetsManager, json: Boolean, path: ResourceLocation): Translator { val assets = assetsManager.getAll( - ResourceLocation.of(path.namespace, path.path + language + if (json) ".json" else ".lang") + ResourceLocation(path.namespace, path.path + language + if (json) ".json" else ".lang") ) val languages: MutableList = mutableListOf() @@ -121,6 +121,6 @@ object LanguageUtil { fun ResourceLocation.translation(name: String): ResourceLocation { - return ResourceLocation.of(this.namespace, "item.$namespace.$path") + return ResourceLocation(this.namespace, "item.$namespace.$path") // TODO: use name? } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt b/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt index 70e88bf46..9f61373d9 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt @@ -13,8 +13,9 @@ package de.bixilon.minosoft.data.registries import de.bixilon.minosoft.data.language.translate.Translatable +import de.bixilon.minosoft.data.registries.ResourceLocation.Companion.ALLOWED_NAMESPACE_PATTERN +import de.bixilon.minosoft.data.registries.ResourceLocation.Companion.ALLOWED_PATH_PATTERN import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition -import de.bixilon.minosoft.util.KUtil.length import java.util.* /** @@ -24,22 +25,23 @@ import java.util.* * @param namespace The namespace of the resource location * @param path The path of the resource location * - * @throws IllegalArgumentException If the namespace or path does not match the allowed pattern. See [ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN] and [ProtocolDefinition.ALLOWED_PATH_PATTERN] + * @throws IllegalArgumentException If the namespace or path does not match the allowed pattern. See [ALLOWED_NAMESPACE_PATTERN] and [ALLOWED_PATH_PATTERN] * * @see Resource location */ -open class ResourceLocation private constructor( +open class ResourceLocation( val namespace: String = ProtocolDefinition.DEFAULT_NAMESPACE, - val path: String -) : Comparable, Translatable { // compare is for moshi + val path: String, +) : Translatable { private val hashCode = Objects.hash(namespace, path) override val translationKey: ResourceLocation get() = this init { - if (!ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches(namespace) && namespace != "") - throw IllegalArgumentException("Namespace '$namespace' is not allowed!") + if (namespace.isBlank() || !ALLOWED_NAMESPACE_PATTERN.matches(namespace)) { + throw IllegalArgumentException("Invalid namespace: $namespace") + } //if (!ProtocolDefinition.ALLOWED_PATH_PATTERN.matches(path) && path != "") // throw IllegalArgumentException("Path '$path' is not allowed!") } @@ -57,8 +59,8 @@ open class ResourceLocation private constructor( * @return If the namespace is "minecraft", the path is returned. Otherwise, the full string is returned. */ fun toMinifiedString(): String { - return if (namespace == ProtocolDefinition.DEFAULT_NAMESPACE) path - else toString() + return if (namespace == ProtocolDefinition.DEFAULT_NAMESPACE) path + else toString() } override fun toString(): String { @@ -69,10 +71,6 @@ open class ResourceLocation private constructor( return hashCode } - override fun compareTo(other: ResourceLocation): Int { - return hashCode() - other.hashCode() - } - override fun equals(other: Any?): Boolean { if (other === this) { return true @@ -87,19 +85,22 @@ open class ResourceLocation private constructor( } companion object { - fun of(resourceLocation: String): ResourceLocation { - var split = resourceLocation.split(':', limit = 2) - if (split.length == 1) - split = arrayOf(ProtocolDefinition.DEFAULT_NAMESPACE, split[0]).toList() + val ALLOWED_NAMESPACE_PATTERN = Regex("[a-z0-9_.\\-]+") + val ALLOWED_PATH_PATTERN = Regex("(?!.*//)[a-z0-9_./\\-]+") + + fun of(string: String): ResourceLocation { + val split = string.split(':', limit = 2) + if (split.size == 1) { + return ResourceLocation(ProtocolDefinition.DEFAULT_NAMESPACE, string) + } return ResourceLocation(split[0], split[1]) } - fun of(namespace: String, path: String): ResourceLocation { - return ResourceLocation(namespace, path) - } - + @Deprecated("Use case??") fun ofPath(path: String): ResourceLocation { - if (path.contains(':') || !path.contains('/')) return of(path) + if (path.contains(':') || !path.contains('/')) { + return of(path) + } val split = path.split('/', limit = 2) return ResourceLocation(split[0], split[1]) } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/models/ModelLoader.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/models/ModelLoader.kt index 20e9cff20..1807d7f5e 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/models/ModelLoader.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/models/ModelLoader.kt @@ -159,15 +159,15 @@ class ModelLoader( companion object { fun ResourceLocation.model(): ResourceLocation { - return ResourceLocation.of(this.namespace, "models/" + this.path + ".json") + return ResourceLocation(this.namespace, "models/" + this.path + ".json") } fun ResourceLocation.blockState(): ResourceLocation { - return ResourceLocation.of(this.namespace, "blockstates/" + this.path + ".json") + return ResourceLocation(this.namespace, "blockstates/" + this.path + ".json") } fun ResourceLocation.bbModel(): ResourceLocation { - return ResourceLocation.of(this.namespace, "models/" + this.path + ".bbmodel") + return ResourceLocation(this.namespace, "models/" + this.path + ".bbmodel") } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/textures/SkeletalTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/textures/SkeletalTexture.kt index 172c82b22..bf992301a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/textures/SkeletalTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/textures/SkeletalTexture.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. * @@ -26,5 +26,5 @@ data class SkeletalTexture( val id: Int, val uuid: UUID, ) { - val resourceLocation = ResourceLocation.of(namespace, path).texture() + val resourceLocation = ResourceLocation(namespace, path).texture() } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/NativeShader.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/NativeShader.kt index 00185d309..3d9e31daa 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/NativeShader.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/NativeShader.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. * @@ -91,7 +91,7 @@ interface NativeShader { ) fun ResourceLocation.shader(): ResourceLocation { - return ResourceLocation.of(namespace, "rendering/shader/${path.replace("(\\w+)\\.\\w+".toRegex(), "$1")}/${path.split("/").last()}") + return ResourceLocation(namespace, "rendering/shader/${path.replace("(\\w+)\\.\\w+".toRegex(), "$1")}/${path.split("/").last()}") } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/code/glsl/GLSLShaderCode.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/code/glsl/GLSLShaderCode.kt index 0ad187da0..a287aa9f7 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/code/glsl/GLSLShaderCode.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/code/glsl/GLSLShaderCode.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. * @@ -54,7 +54,7 @@ class GLSLShaderCode( val include = ResourceLocation.of(reader.readString()!!) - val includeCode = GLSLShaderCode(context, context.connection.assetsManager[ResourceLocation.of(include.namespace, "rendering/shader/includes/${include.path}.glsl")].readAsString()) + val includeCode = GLSLShaderCode(context, context.connection.assetsManager[ResourceLocation(include.namespace, "rendering/shader/includes/${include.path}.glsl")].readAsString()) code.append('\n') code.append(includeCode.code) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/vanilla/DefaultSkinProvider.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/vanilla/DefaultSkinProvider.kt index fab4e6715..5b20eca95 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/vanilla/DefaultSkinProvider.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/vanilla/DefaultSkinProvider.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. * @@ -55,7 +55,7 @@ class DefaultSkinProvider( } private fun loadLegacy(skin: DefaultLegacySkin) { - val path = ResourceLocation.of(skin.name.namespace, "entity/${skin.name.path}").texture() + val path = ResourceLocation(skin.name.namespace, "entity/${skin.name.path}").texture() val texture = load(path) ?: return this[skin.model][skin.name] = texture @@ -72,7 +72,7 @@ class DefaultSkinProvider( } private fun ResourceLocation.skin(prefix: String): ResourceLocation { - return ResourceLocation.of(namespace, "entity/player/$prefix/$path") + return ResourceLocation(namespace, "entity/player/$prefix/$path") } operator fun get(name: ResourceLocation, slim: Boolean): DynamicTexture? { diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java index c26e94f41..1ed42c873 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java @@ -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. * @@ -16,7 +16,6 @@ package de.bixilon.minosoft.protocol.protocol; import de.bixilon.kotlinglm.vec3.Vec3i; import de.bixilon.minosoft.data.text.formatting.color.ChatColors; import de.bixilon.minosoft.data.text.formatting.color.RGBColor; -import kotlin.text.Regex; import java.util.regex.Pattern; @@ -41,8 +40,6 @@ public final class ProtocolDefinition { public static final String DEFAULT_NAMESPACE = "minecraft"; public static final String MINOSOFT_NAMESPACE = "minosoft"; - public static final Regex ALLOWED_NAMESPACE_PATTERN = new Regex("[a-z0-9_.\\-]+"); - public static final Regex ALLOWED_PATH_PATTERN = new Regex("(?!.*//)[a-z0-9_./\\-]+"); public static final char TEXT_COMPONENT_SPECIAL_PREFIX_CHAR = '\u00A7'; public static final int DEFAULT_BUFFER_SIZE = 4096; diff --git a/src/main/java/de/bixilon/minosoft/util/KUtil.kt b/src/main/java/de/bixilon/minosoft/util/KUtil.kt index 89fc6f2ed..48e5876ff 100644 --- a/src/main/java/de/bixilon/minosoft/util/KUtil.kt +++ b/src/main/java/de/bixilon/minosoft/util/KUtil.kt @@ -77,11 +77,11 @@ object KUtil { } fun minecraft(path: String): ResourceLocation { - return ResourceLocation.of(ProtocolDefinition.DEFAULT_NAMESPACE, path) + return ResourceLocation(ProtocolDefinition.DEFAULT_NAMESPACE, path) // TODO: Use MINECRAFT_NAMESPACE } fun minosoft(path: String): ResourceLocation { - return ResourceLocation.of(ProtocolDefinition.MINOSOFT_NAMESPACE, path) + return ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, path) } fun T.synchronizedDeepCopy(): T { diff --git a/src/test/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinitionTest.kt b/src/test/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinitionTest.kt index a2b8546ed..82ba5c743 100644 --- a/src/test/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinitionTest.kt +++ b/src/test/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinitionTest.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. * @@ -12,6 +12,7 @@ */ package de.bixilon.minosoft.protocol.protocol +import de.bixilon.minosoft.data.registries.ResourceLocation import org.junit.jupiter.api.Test import kotlin.test.assertEquals @@ -43,17 +44,17 @@ internal class ProtocolDefinitionTest { @Test fun testAllowedNamespaces() { // Should Pass - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("minecraft"), true) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("min1234567890craft"), true) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("mine-craft"), true) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("mine_craft"), true) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("mine.craft"), true) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("minecraft"), true) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("min1234567890craft"), true) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine-craft"), true) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine_craft"), true) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine.craft"), true) // Should Fail - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("MineCraft"), false) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("mine craft"), false) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("minecraft!"), false) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("^minecraft"), false) - assertEquals(ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches("mine/craft"), false) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("MineCraft"), false) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine craft"), false) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("minecraft!"), false) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("^minecraft"), false) + assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine/craft"), false) } /** @@ -62,19 +63,19 @@ internal class ProtocolDefinitionTest { @Test fun testAllowedResourceLocationPaths() { // Should Pass - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("minecraft"), true) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("min1234567890craft"), true) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("mine-craft"), true) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("mine_craft"), true) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("mine.craft"), true) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("mine/craft"), true) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("minecraft"), true) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("min1234567890craft"), true) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("mine-craft"), true) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("mine_craft"), true) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("mine.craft"), true) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("mine/craft"), true) // Should Fail - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("MineCraft"), false) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("mine craft"), false) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("minecraft!"), false) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("^minecraft"), false) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("mine//craft"), false) - assertEquals(ProtocolDefinition.ALLOWED_PATH_PATTERN.matches("mine///craft"), false) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("MineCraft"), false) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("mine craft"), false) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("minecraft!"), false) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("^minecraft"), false) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("mine//craft"), false) + assertEquals(ResourceLocation.ALLOWED_PATH_PATTERN.matches("mine///craft"), false) } }