mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 19:05:02 -04:00
resource location: improve namespace validation performance, improve validation
This commit is contained in:
parent
c6b26796bf
commit
078dbdc800
@ -12,9 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.bixilon.minosoft.data.registries.identified
|
package de.bixilon.minosoft.data.registries.identified
|
||||||
|
|
||||||
import de.bixilon.minosoft.config.StaticConfiguration
|
|
||||||
import de.bixilon.minosoft.data.language.translate.Translatable
|
import de.bixilon.minosoft.data.language.translate.Translatable
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation.Companion.ALLOWED_NAMESPACE_PATTERN
|
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation.Companion.ALLOWED_PATH_PATTERN
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocation.Companion.ALLOWED_PATH_PATTERN
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@ -26,7 +24,7 @@ import java.util.*
|
|||||||
* @param namespace The namespace of the resource location
|
* @param namespace The namespace of the resource location
|
||||||
* @param path The path 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 [ALLOWED_NAMESPACE_PATTERN] and [ALLOWED_PATH_PATTERN]
|
* @throws IllegalArgumentException If the namespace or path does not match the allowed pattern. and [ALLOWED_PATH_PATTERN]
|
||||||
*
|
*
|
||||||
* @see <a href="https://minecraft.fandom.com/wiki/Resource_location">Resource location</a>
|
* @see <a href="https://minecraft.fandom.com/wiki/Resource_location">Resource location</a>
|
||||||
*/
|
*/
|
||||||
@ -40,9 +38,7 @@ open class ResourceLocation(
|
|||||||
get() = this
|
get() = this
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (StaticConfiguration.VALIDATE_RESOURCE_LOCATION && (namespace.isBlank() || !ALLOWED_NAMESPACE_PATTERN.matches(namespace))) {
|
ResourceLocationUtil.validateNamespace(namespace)
|
||||||
throw IllegalArgumentException("Invalid namespace: $namespace")
|
|
||||||
}
|
|
||||||
// TODO: Figure out a way to implement this but have backwards compatibility with pre flattening versions
|
// TODO: Figure out a way to implement this but have backwards compatibility with pre flattening versions
|
||||||
// if (!ProtocolDefinition.ALLOWED_PATH_PATTERN.matches(path) && path != "")
|
// if (!ProtocolDefinition.ALLOWED_PATH_PATTERN.matches(path) && path != "")
|
||||||
// throw IllegalArgumentException("Path '$path' is not allowed!")
|
// throw IllegalArgumentException("Path '$path' is not allowed!")
|
||||||
@ -84,7 +80,6 @@ open class ResourceLocation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val ALLOWED_NAMESPACE_PATTERN = Regex("[a-z0-9_.\\-]+")
|
|
||||||
val ALLOWED_PATH_PATTERN = Regex("(?!.*//)[a-z0-9_./\\-]+")
|
val ALLOWED_PATH_PATTERN = Regex("(?!.*//)[a-z0-9_./\\-]+")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Minosoft
|
||||||
|
* 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 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.data.registries.identified
|
||||||
|
|
||||||
|
import de.bixilon.minosoft.config.StaticConfiguration
|
||||||
|
|
||||||
|
object ResourceLocationUtil {
|
||||||
|
|
||||||
|
fun validateNamespace(namespace: String) {
|
||||||
|
if (!StaticConfiguration.VALIDATE_RESOURCE_LOCATION) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (namespace.isBlank()) {
|
||||||
|
throw IllegalArgumentException("Namespace is blank!")
|
||||||
|
}
|
||||||
|
for (code in namespace.codePoints()) {
|
||||||
|
if (
|
||||||
|
code in 'a'.code..'z'.code ||
|
||||||
|
code in '0'.code..'9'.code ||
|
||||||
|
code == '_'.code ||
|
||||||
|
code == '-'.code ||
|
||||||
|
code == '.'.code
|
||||||
|
) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
throw IllegalArgumentException("Namespace is illegal: $namespace")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -13,9 +13,12 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.data.registries
|
package de.bixilon.minosoft.data.registries
|
||||||
|
|
||||||
|
import de.bixilon.minosoft.data.registries.identified.Namespaces
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||||
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocationUtil
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.assertDoesNotThrow
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
import kotlin.test.assertNotEquals
|
import kotlin.test.assertNotEquals
|
||||||
|
|
||||||
@ -66,23 +69,31 @@ class ResourceLocationTest {
|
|||||||
assertThrows<IllegalArgumentException> { ResourceLocation("in valid", "path") }
|
assertThrows<IllegalArgumentException> { ResourceLocation("in valid", "path") }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun integratedNamespaces() {
|
||||||
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace(Namespaces.MINECRAFT) }
|
||||||
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace(Namespaces.MINOSOFT) }
|
||||||
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace(Namespaces.DEFAULT) }
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see [de.bixilon.minosoft.data.registries.ResourceLocation]
|
* @see [de.bixilon.minosoft.data.registries.ResourceLocation]
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun testAllowedNamespaces() {
|
fun testAllowedNamespaces() {
|
||||||
// Should Pass
|
// Should Pass
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("minecraft"), true)
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace("minecraft") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("min1234567890craft"), true)
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace("min1234567890craft") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine-craft"), true)
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace("mine-craft") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine_craft"), true)
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace("mine_craft") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine.craft"), true)
|
assertDoesNotThrow { ResourceLocationUtil.validateNamespace("mine.craft") }
|
||||||
|
|
||||||
// Should Fail
|
// Should Fail
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("MineCraft"), false)
|
assertThrows<IllegalArgumentException> { ResourceLocationUtil.validateNamespace("MineCraft") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine craft"), false)
|
assertThrows<IllegalArgumentException> { ResourceLocationUtil.validateNamespace("mine craft") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("minecraft!"), false)
|
assertThrows<IllegalArgumentException> { ResourceLocationUtil.validateNamespace("minecraft!") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("^minecraft"), false)
|
assertThrows<IllegalArgumentException> { ResourceLocationUtil.validateNamespace("^minecraft") }
|
||||||
kotlin.test.assertEquals(ResourceLocation.ALLOWED_NAMESPACE_PATTERN.matches("mine/craft"), false)
|
assertThrows<IllegalArgumentException> { ResourceLocationUtil.validateNamespace("mine/craft") }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user