restructure some things in ResourceLocation

This commit is contained in:
Bixilon 2023-01-04 23:57:15 +01:00
parent 75e262e292
commit 951bf6e339
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
12 changed files with 69 additions and 70 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.
*
@ -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 {

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.
*
@ -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 <T> readResult(reader: StringReader.() -> T): ReadResult<T> {

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.
*
@ -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<Language> = 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?
}
}

View File

@ -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 <a href="https://minecraft.fandom.com/wiki/Resource_location">Resource location</a>
*/
open class ResourceLocation private constructor(
open class ResourceLocation(
val namespace: String = ProtocolDefinition.DEFAULT_NAMESPACE,
val path: String
) : Comparable<ResourceLocation>, 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])
}

View File

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

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.
*
@ -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()
}

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.
*
@ -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()}")
}
}
}

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.
*
@ -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)

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

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.
*
@ -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;

View File

@ -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> T.synchronizedDeepCopy(): T {

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