cssc: Check identifier against regex

This commit is contained in:
Bixilon 2020-12-21 18:41:52 +01:00
parent 7d6588378a
commit 096279efd9
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 84 additions and 8 deletions

View File

@ -1,6 +0,0 @@
version: "2" # required to adjust maintainability checks
plugins:
sonar-java:
enabled: true
config:
sonar.java.source: "14"

View File

@ -49,7 +49,7 @@ object CommandParsers {
// swizzle // swizzle
// team // team
// item_slot // item_slot
// resource_location ModIdentifier("resource_location") to IdentifierParser.IDENTIFIER_PARSER,
ModIdentifier("mob_effect") to IdentifierListParser.MOB_EFFECT_PARSER, ModIdentifier("mob_effect") to IdentifierListParser.MOB_EFFECT_PARSER,
// function // function
// entity_anchor // entity_anchor

View File

@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.commands.parser
import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException
import de.bixilon.minosoft.data.commands.parser.exceptions.EnchantmentNotFoundCommandParseException import de.bixilon.minosoft.data.commands.parser.exceptions.EnchantmentNotFoundCommandParseException
import de.bixilon.minosoft.data.commands.parser.exceptions.InvalidIdentifierCommandParseException
import de.bixilon.minosoft.data.commands.parser.exceptions.MobEffectNotFoundCommandParseException import de.bixilon.minosoft.data.commands.parser.exceptions.MobEffectNotFoundCommandParseException
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties
import de.bixilon.minosoft.data.mappings.ModIdentifier import de.bixilon.minosoft.data.mappings.ModIdentifier
@ -26,7 +27,13 @@ class IdentifierListParser : CommandParser() {
override fun isParsable(connection: Connection, properties: ParserProperties?, stringReader: ImprovedStringReader) { override fun isParsable(connection: Connection, properties: ParserProperties?, stringReader: ImprovedStringReader) {
val argument = stringReader.readUntilNextCommandArgument() val argument = stringReader.readUntilNextCommandArgument()
val identifier = ModIdentifier(argument)
val identifier: ModIdentifier
try {
identifier = ModIdentifier.getIdentifier(argument)
} catch (exception: IllegalArgumentException) {
throw InvalidIdentifierCommandParseException(stringReader, argument)
}
if (this == ENCHANTMENT_PARSER) { if (this == ENCHANTMENT_PARSER) {
if (!connection.mapping.doesEnchantmentExist(identifier)) { if (!connection.mapping.doesEnchantmentExist(identifier)) {

View File

@ -0,0 +1,38 @@
/*
* Minosoft
* Copyright (C) 2020 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.commands.parser
import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException
import de.bixilon.minosoft.data.commands.parser.exceptions.InvalidIdentifierCommandParseException
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties
import de.bixilon.minosoft.data.mappings.ModIdentifier
import de.bixilon.minosoft.protocol.network.Connection
import de.bixilon.minosoft.util.buffers.ImprovedStringReader
class IdentifierParser : CommandParser() {
@Throws(CommandParseException::class)
override fun isParsable(connection: Connection, properties: ParserProperties?, stringReader: ImprovedStringReader) {
val argument = stringReader.readUntilNextCommandArgument()
try {
ModIdentifier.getIdentifier(argument)
} catch (exception: IllegalArgumentException) {
throw InvalidIdentifierCommandParseException(stringReader, argument)
}
}
companion object {
val IDENTIFIER_PARSER = IdentifierParser()
}
}

View File

@ -0,0 +1,25 @@
/*
* Minosoft
* Copyright (C) 2020 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.commands.parser.exceptions
import de.bixilon.minosoft.util.buffers.ImprovedStringReader
class InvalidIdentifierCommandParseException : CommandParseException {
constructor(command: ImprovedStringReader, currentArgument: String) : super(ERROR_MESSAGE, command, currentArgument)
constructor(command: ImprovedStringReader, currentArgument: String, cause: Throwable) : super(ERROR_MESSAGE, command, currentArgument, cause)
companion object {
private const val ERROR_MESSAGE = "Identifier is invalid!"
}
}

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.mappings;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition; import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Matcher;
public class ModIdentifier { public class ModIdentifier {
protected final String mod; protected final String mod;
@ -42,6 +43,16 @@ public class ModIdentifier {
this.identifier = identifier.getIdentifier(); this.identifier = identifier.getIdentifier();
} }
public static ModIdentifier getIdentifier(String identifier) throws IllegalArgumentException {
Matcher matcher = ProtocolDefinition.IDENTIFIER_PATTERN.matcher(identifier);
if (!matcher.find() || !matcher.group().equals(identifier)) {
throw new IllegalArgumentException(String.format("%s in not a valid identifier!", identifier));
}
return new ModIdentifier(identifier);
}
public String getMod() { public String getMod() {
return this.mod; return this.mod;
} }

View File

@ -48,6 +48,7 @@ public final class ProtocolDefinition {
public static final String COMMAND_SEPARATOR = " "; public static final String COMMAND_SEPARATOR = " ";
public static final Pattern MINECRAFT_NAME_VALIDATOR = Pattern.compile("\\w{3,16}"); public static final Pattern MINECRAFT_NAME_VALIDATOR = Pattern.compile("\\w{3,16}");
public static final Pattern IDENTIFIER_PATTERN = Pattern.compile("([a-z_]+:)?[a-z_]+");
public static final int SECTION_WIDTH_X = 16; public static final int SECTION_WIDTH_X = 16;
public static final int SECTION_WIDTH_Z = 16; public static final int SECTION_WIDTH_Z = 16;