Dieser Commit ist enthalten in:
Ursprung
d71896979e
Commit
3fa992b5c9
@ -1,29 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command;
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface AbstractGuardChecker<T> {
|
|
||||||
/**
|
|
||||||
* While guarding the first parameter of the command the parameter s of this method is {@code null}
|
|
||||||
*/
|
|
||||||
GuardResult guard(T t, GuardCheckType guardCheckType, String[] previousArguments, String s);
|
|
||||||
}
|
|
@ -38,7 +38,6 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
protected final List<SubCommand<T>> commandHelpList = new ArrayList<>();
|
protected final List<SubCommand<T>> commandHelpList = new ArrayList<>();
|
||||||
|
|
||||||
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
|
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
|
||||||
private final Map<String, AbstractGuardChecker<T>> localGuardChecker = new HashMap<>();
|
|
||||||
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
|
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
|
||||||
|
|
||||||
protected AbstractSWCommand(Class<T> clazz, String command) {
|
protected AbstractSWCommand(Class<T> clazz, String command) {
|
||||||
@ -126,20 +125,6 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
SWCommandUtils.getMAPPER_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), typeMapper);
|
SWCommandUtils.getMAPPER_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), typeMapper);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
addGuard(Guard.class, method, i -> i == 0, false, AbstractGuardChecker.class, (anno, guardChecker) -> {
|
|
||||||
if (anno.local()) {
|
|
||||||
localGuardChecker.putIfAbsent(anno.value(), (AbstractGuardChecker<T>) guardChecker);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value(), guardChecker);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
addGuard(ClassGuard.class, method, i -> i == 0, false, AbstractGuardChecker.class, (anno, guardChecker) -> {
|
|
||||||
if (anno.local()) {
|
|
||||||
localGuardChecker.putIfAbsent(anno.value().getTypeName(), (AbstractGuardChecker<T>) guardChecker);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
addValidator(Validator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
|
addValidator(Validator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
|
||||||
if (anno.local()) {
|
if (anno.local()) {
|
||||||
localValidators.putIfAbsent(anno.value(), (AbstractValidator<T, ?>) validator);
|
localValidators.putIfAbsent(anno.value(), (AbstractValidator<T, ?>) validator);
|
||||||
@ -168,7 +153,7 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
|
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
commandHelpList.add(new SubCommand<>(this, method, anno.value(), new HashMap<>(), new HashMap<>(), localGuardChecker, true, null, anno.noTabComplete()));
|
commandHelpList.add(new SubCommand<>(this, method, anno.value(), new HashMap<>(), new HashMap<>(), true, null, anno.noTabComplete()));
|
||||||
});
|
});
|
||||||
|
|
||||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
||||||
@ -189,7 +174,7 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
commandList.add(new SubCommand<>(this, method, anno.value(), localTypeMapper, localValidators, localGuardChecker, false, anno.description(), anno.noTabComplete()));
|
commandList.add(new SubCommand<>(this, method, anno.value(), localTypeMapper, localValidators, false, anno.description(), anno.noTabComplete()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,18 +240,6 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
private <T extends Annotation> void addGuard(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, AbstractGuardChecker<?>> consumer) {
|
|
||||||
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
|
||||||
try {
|
|
||||||
method.setAccessible(true);
|
|
||||||
consumer.accept(anno, (AbstractGuardChecker<T>) method.invoke(this));
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Implement this when Message System is ready
|
// TODO: Implement this when Message System is ready
|
||||||
/*
|
/*
|
||||||
public void addDefaultHelpMessage(String message) {
|
public void addDefaultHelpMessage(String message) {
|
||||||
@ -327,24 +300,6 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
boolean global() default false;
|
boolean global() default false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
|
||||||
protected @interface Guard {
|
|
||||||
String value() default "";
|
|
||||||
|
|
||||||
boolean local() default false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.METHOD})
|
|
||||||
protected @interface ClassGuard {
|
|
||||||
Class<?> value();
|
|
||||||
|
|
||||||
boolean local() default false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
protected @interface Validator {
|
protected @interface Validator {
|
||||||
|
@ -42,24 +42,20 @@ class CommandPart<T> {
|
|||||||
private AbstractSWCommand<T> command;
|
private AbstractSWCommand<T> command;
|
||||||
private AbstractTypeMapper<T, ?> typeMapper;
|
private AbstractTypeMapper<T, ?> typeMapper;
|
||||||
private AbstractValidator<T, Object> validator;
|
private AbstractValidator<T, Object> validator;
|
||||||
private AbstractGuardChecker<T> guardChecker;
|
|
||||||
private Class<?> varArgType;
|
private Class<?> varArgType;
|
||||||
private String optional;
|
private String optional;
|
||||||
private GuardCheckType guardCheckType;
|
|
||||||
|
|
||||||
private CommandPart<T> next = null;
|
private CommandPart<T> next = null;
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
private boolean ignoreAsArgument = false;
|
private boolean ignoreAsArgument = false;
|
||||||
|
|
||||||
public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, AbstractValidator<T, Object> validator, AbstractGuardChecker<T> guardChecker, Class<?> varArgType, String optional, GuardCheckType guardCheckType) {
|
public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, AbstractValidator<T, Object> validator, Class<?> varArgType, String optional) {
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.typeMapper = typeMapper;
|
this.typeMapper = typeMapper;
|
||||||
this.validator = validator;
|
this.validator = validator;
|
||||||
this.guardChecker = guardChecker;
|
|
||||||
this.varArgType = varArgType;
|
this.varArgType = varArgType;
|
||||||
this.optional = optional;
|
this.optional = optional;
|
||||||
this.guardCheckType = guardCheckType;
|
|
||||||
|
|
||||||
validatePart();
|
validatePart();
|
||||||
}
|
}
|
||||||
@ -72,9 +68,6 @@ class CommandPart<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void validatePart() {
|
private void validatePart() {
|
||||||
if (guardCheckType == GuardCheckType.TAB_COMPLETE) {
|
|
||||||
throw new IllegalArgumentException("Tab complete is not allowed as a guard check type!");
|
|
||||||
}
|
|
||||||
if (optional != null && varArgType != null) {
|
if (optional != null && varArgType != null) {
|
||||||
throw new IllegalArgumentException("A vararg part can't have an optional part!");
|
throw new IllegalArgumentException("A vararg part can't have an optional part!");
|
||||||
}
|
}
|
||||||
@ -92,7 +85,7 @@ class CommandPart<T> {
|
|||||||
if (varArgType != null) {
|
if (varArgType != null) {
|
||||||
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
||||||
for (int i = startIndex; i < args.length; i++) {
|
for (int i = startIndex; i < args.length; i++) {
|
||||||
CheckArgumentResult validArgument = checkArgument(null, null, sender, args, i);
|
CheckArgumentResult validArgument = checkArgument(null, sender, args, i);
|
||||||
if (!validArgument.success) {
|
if (!validArgument.success) {
|
||||||
throw new CommandParseException();
|
throw new CommandParseException();
|
||||||
}
|
}
|
||||||
@ -107,7 +100,7 @@ class CommandPart<T> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckArgumentResult validArgument = checkArgument(errors, null, sender, args, startIndex);
|
CheckArgumentResult validArgument = checkArgument(errors, sender, args, startIndex);
|
||||||
if (!validArgument.success && optional == null) {
|
if (!validArgument.success && optional == null) {
|
||||||
throw new CommandParseException();
|
throw new CommandParseException();
|
||||||
}
|
}
|
||||||
@ -128,43 +121,10 @@ class CommandPart<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean guardCheck(T sender, String[] args, int startIndex) {
|
|
||||||
if (varArgType != null) {
|
|
||||||
for (int i = startIndex; i < args.length; i++) {
|
|
||||||
GuardResult guardResult = checkGuard(guardCheckType, sender, args, i);
|
|
||||||
if (guardResult == GuardResult.DENIED) {
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
}
|
|
||||||
if (guardResult == GuardResult.DENIED_WITH_HELP) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
GuardResult guardResult = checkGuard(guardCheckType, sender, args, startIndex);
|
|
||||||
if (guardResult == GuardResult.DENIED) {
|
|
||||||
if (optional != null && next != null) {
|
|
||||||
return next.guardCheck(sender, args, startIndex);
|
|
||||||
}
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
}
|
|
||||||
if (guardResult == GuardResult.DENIED_WITH_HELP) {
|
|
||||||
if (optional != null && next != null) {
|
|
||||||
return next.guardCheck(sender, args, startIndex);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (next != null) {
|
|
||||||
return next.guardCheck(sender, args, startIndex + 1);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
|
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
|
||||||
if (varArgType != null) {
|
if (varArgType != null) {
|
||||||
for (int i = startIndex; i < args.length - 1; i++) {
|
for (int i = startIndex; i < args.length - 1; i++) {
|
||||||
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, GuardCheckType.TAB_COMPLETE, sender, args, i);
|
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, i);
|
||||||
if (!validArgument.success) {
|
if (!validArgument.success) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -177,7 +137,7 @@ class CommandPart<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length - 1 > startIndex) {
|
if (args.length - 1 > startIndex) {
|
||||||
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, GuardCheckType.TAB_COMPLETE, sender, args, startIndex);
|
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, sender, args, startIndex);
|
||||||
if (checkArgumentResult.success && next != null) {
|
if (checkArgumentResult.success && next != null) {
|
||||||
next.generateTabComplete(current, sender, args, startIndex + 1);
|
next.generateTabComplete(current, sender, args, startIndex + 1);
|
||||||
return;
|
return;
|
||||||
@ -203,7 +163,7 @@ class CommandPart<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private CheckArgumentResult checkArgument(Consumer<Runnable> errors, GuardCheckType guardCheckType, T sender, String[] args, int index) {
|
private CheckArgumentResult checkArgument(Consumer<Runnable> errors, T sender, String[] args, int index) {
|
||||||
try {
|
try {
|
||||||
Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]);
|
Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]);
|
||||||
if (validator != null && errors != null) {
|
if (validator != null && errors != null) {
|
||||||
@ -216,29 +176,8 @@ class CommandPart<T> {
|
|||||||
}
|
}
|
||||||
return new CheckArgumentResult(value != null, value);
|
return new CheckArgumentResult(value != null, value);
|
||||||
}
|
}
|
||||||
if (value == null) {
|
|
||||||
return new CheckArgumentResult(false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
GuardResult guardResult = checkGuard(guardCheckType, sender, args, index);
|
|
||||||
switch (guardResult) {
|
|
||||||
case ALLOWED:
|
|
||||||
return new CheckArgumentResult(true, value);
|
|
||||||
case DENIED:
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
case DENIED_WITH_HELP:
|
|
||||||
default:
|
|
||||||
return new CheckArgumentResult(false, null);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
return new CheckArgumentResult(false, null);
|
return new CheckArgumentResult(false, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private GuardResult checkGuard(GuardCheckType guardCheckType, T sender, String[] args, int index) {
|
|
||||||
if (guardChecker != null && guardCheckType != null) {
|
|
||||||
return guardChecker.guard(sender, guardCheckType, Arrays.copyOf(args, index), args[index]);
|
|
||||||
}
|
|
||||||
return GuardResult.ALLOWED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command;
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public enum GuardCheckType {
|
|
||||||
COMMAND,
|
|
||||||
HELP_COMMAND,
|
|
||||||
TAB_COMPLETE
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command;
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public enum GuardResult {
|
|
||||||
ALLOWED,
|
|
||||||
DENIED_WITH_HELP,
|
|
||||||
DENIED
|
|
||||||
}
|
|
@ -37,10 +37,6 @@ public class SWCommandUtils {
|
|||||||
@Getter
|
@Getter
|
||||||
private final Map<String, AbstractTypeMapper<?, ?>> MAPPER_FUNCTIONS = new HashMap<>();
|
private final Map<String, AbstractTypeMapper<?, ?>> MAPPER_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Deprecated
|
|
||||||
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final Map<String, AbstractValidator<?, ?>> VALIDATOR_FUNCTIONS = new HashMap<>();
|
private final Map<String, AbstractValidator<?, ?>> VALIDATOR_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
@ -78,11 +74,11 @@ public class SWCommandUtils {
|
|||||||
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
|
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T> CommandPart<T> generateCommandPart(AbstractSWCommand<T> command, boolean help, String[] subCommand, Parameter[] parameters, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
|
static <T> CommandPart<T> generateCommandPart(AbstractSWCommand<T> command, boolean help, String[] subCommand, Parameter[] parameters, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator) {
|
||||||
CommandPart<T> first = null;
|
CommandPart<T> first = null;
|
||||||
CommandPart<T> current = null;
|
CommandPart<T> current = null;
|
||||||
for (String s : subCommand) {
|
for (String s : subCommand) {
|
||||||
CommandPart commandPart = new CommandPart(command, createMapper(s), null, null, null, null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND);
|
CommandPart commandPart = new CommandPart(command, createMapper(s), null, null, null);
|
||||||
commandPart.setIgnoreAsArgument(true);
|
commandPart.setIgnoreAsArgument(true);
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
current.setNext(commandPart);
|
current.setNext(commandPart);
|
||||||
@ -96,11 +92,10 @@ public class SWCommandUtils {
|
|||||||
Parameter parameter = parameters[i];
|
Parameter parameter = parameters[i];
|
||||||
AbstractTypeMapper<T, ?> typeMapper = getTypeMapper(parameter, localTypeMapper);
|
AbstractTypeMapper<T, ?> typeMapper = getTypeMapper(parameter, localTypeMapper);
|
||||||
AbstractValidator<T, Object> validator = (AbstractValidator<T, Object>) getValidator(parameter, localValidator);
|
AbstractValidator<T, Object> validator = (AbstractValidator<T, Object>) getValidator(parameter, localValidator);
|
||||||
AbstractGuardChecker<T> guardChecker = getGuardChecker(parameter, localGuardChecker);
|
|
||||||
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
|
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
|
||||||
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
|
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
|
||||||
|
|
||||||
CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, guardChecker, varArgType, optionalValue != null ? optionalValue.value() : null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND);
|
CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null);
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
current.setNext(commandPart);
|
current.setNext(commandPart);
|
||||||
}
|
}
|
||||||
@ -203,40 +198,6 @@ public class SWCommandUtils {
|
|||||||
return validator;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static <T> AbstractGuardChecker<T> getGuardChecker(Parameter parameter, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
|
|
||||||
Class<?> clazz = parameter.getType();
|
|
||||||
if (parameter.isVarArgs()) {
|
|
||||||
clazz = clazz.getComponentType();
|
|
||||||
}
|
|
||||||
|
|
||||||
AbstractSWCommand.ClassGuard classGuard = parameter.getAnnotation(AbstractSWCommand.ClassGuard.class);
|
|
||||||
if (classGuard != null) {
|
|
||||||
if (classGuard.value() != null) {
|
|
||||||
return getGuardChecker(classGuard.value().getTypeName(), localGuardChecker);
|
|
||||||
}
|
|
||||||
return getGuardChecker(clazz.getTypeName(), localGuardChecker);
|
|
||||||
}
|
|
||||||
|
|
||||||
AbstractSWCommand.Guard guard = parameter.getAnnotation(AbstractSWCommand.Guard.class);
|
|
||||||
if (guard != null) {
|
|
||||||
if (guard.value() != null && !guard.value().isEmpty()) {
|
|
||||||
return getGuardChecker(guard.value(), localGuardChecker);
|
|
||||||
}
|
|
||||||
return getGuardChecker(clazz.getTypeName(), localGuardChecker);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
private static <T> AbstractGuardChecker<T> getGuardChecker(String s, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
|
|
||||||
AbstractGuardChecker<T> guardChecker = localGuardChecker.getOrDefault(s, (AbstractGuardChecker<T>) GUARD_FUNCTIONS.getOrDefault(s, null));
|
|
||||||
if (guardChecker == null) {
|
|
||||||
throw new IllegalArgumentException("No guard found for " + s);
|
|
||||||
}
|
|
||||||
return guardChecker;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, T> void addMapper(Class<T> clazz, AbstractTypeMapper<K, T> mapper) {
|
public static <K, T> void addMapper(Class<T> clazz, AbstractTypeMapper<K, T> mapper) {
|
||||||
addMapper(clazz.getTypeName(), mapper);
|
addMapper(clazz.getTypeName(), mapper);
|
||||||
}
|
}
|
||||||
@ -253,16 +214,6 @@ public class SWCommandUtils {
|
|||||||
VALIDATOR_FUNCTIONS.putIfAbsent(name, validator);
|
VALIDATOR_FUNCTIONS.putIfAbsent(name, validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static <T> void addGuard(Class<?> clazz, AbstractGuardChecker<T> guardChecker) {
|
|
||||||
addGuard(clazz.getTypeName(), guardChecker);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static <T> void addGuard(String name, AbstractGuardChecker<T> guardChecker) {
|
|
||||||
GUARD_FUNCTIONS.putIfAbsent(name, guardChecker);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T extends AbstractTypeMapper<K, String>, K> T createMapper(String... values) {
|
public static <T extends AbstractTypeMapper<K, String>, K> T createMapper(String... values) {
|
||||||
List<String> strings = Arrays.stream(values).map(String::toLowerCase).collect(Collectors.toList());
|
List<String> strings = Arrays.stream(values).map(String::toLowerCase).collect(Collectors.toList());
|
||||||
List<String> tabCompletes = Arrays.asList(values);
|
List<String> tabCompletes = Arrays.asList(values);
|
||||||
|
@ -38,13 +38,12 @@ public class SubCommand<T> {
|
|||||||
private Predicate<T> senderPredicate;
|
private Predicate<T> senderPredicate;
|
||||||
private Function<T, ?> senderFunction;
|
private Function<T, ?> senderFunction;
|
||||||
AbstractValidator<T, T> validator;
|
AbstractValidator<T, T> validator;
|
||||||
AbstractGuardChecker<T> guardChecker;
|
|
||||||
boolean noTabComplete;
|
boolean noTabComplete;
|
||||||
int comparableValue;
|
int comparableValue;
|
||||||
|
|
||||||
private CommandPart<T> commandPart;
|
private CommandPart<T> commandPart;
|
||||||
|
|
||||||
SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, Map<String, AbstractGuardChecker<T>> localGuardChecker, boolean help, String[] description, boolean noTabComplete) {
|
SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, boolean help, String[] description, boolean noTabComplete) {
|
||||||
this.abstractSWCommand = abstractSWCommand;
|
this.abstractSWCommand = abstractSWCommand;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.subCommand = subCommand;
|
this.subCommand = subCommand;
|
||||||
@ -55,9 +54,8 @@ public class SubCommand<T> {
|
|||||||
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
|
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
|
||||||
|
|
||||||
validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(parameters[0], localValidator);
|
validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(parameters[0], localValidator);
|
||||||
guardChecker = SWCommandUtils.getGuardChecker(parameters[0], localGuardChecker);
|
|
||||||
|
|
||||||
commandPart = SWCommandUtils.generateCommandPart(abstractSWCommand, help, subCommand, parameters, localTypeMapper, localValidator, localGuardChecker);
|
commandPart = SWCommandUtils.generateCommandPart(abstractSWCommand, help, subCommand, parameters, localTypeMapper, localValidator);
|
||||||
|
|
||||||
senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass());
|
senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass());
|
||||||
senderFunction = t -> parameters[0].getType().cast(t);
|
senderFunction = t -> parameters[0].getType().cast(t);
|
||||||
@ -79,17 +77,6 @@ public class SubCommand<T> {
|
|||||||
})) {
|
})) {
|
||||||
throw new CommandNoHelpException();
|
throw new CommandNoHelpException();
|
||||||
}
|
}
|
||||||
} else if (guardChecker != null) {
|
|
||||||
GuardResult guardResult = guardChecker.guard(sender, GuardCheckType.COMMAND, new String[0], null);
|
|
||||||
switch (guardResult) {
|
|
||||||
case ALLOWED:
|
|
||||||
break;
|
|
||||||
case DENIED:
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
case DENIED_WITH_HELP:
|
|
||||||
default:
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(abstractSWCommand, senderFunction.apply(sender));
|
method.invoke(abstractSWCommand, senderFunction.apply(sender));
|
||||||
@ -102,19 +89,7 @@ public class SubCommand<T> {
|
|||||||
})) {
|
})) {
|
||||||
throw new CommandNoHelpException();
|
throw new CommandNoHelpException();
|
||||||
}
|
}
|
||||||
} else if (guardChecker != null) {
|
|
||||||
GuardResult guardResult = guardChecker.guard(sender, GuardCheckType.COMMAND, new String[0], null);
|
|
||||||
switch (guardResult) {
|
|
||||||
case ALLOWED:
|
|
||||||
break;
|
|
||||||
case DENIED:
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
case DENIED_WITH_HELP:
|
|
||||||
default:
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
commandPart.guardCheck(sender, args, 0);
|
|
||||||
objects.add(0, senderFunction.apply(sender));
|
objects.add(0, senderFunction.apply(sender));
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(abstractSWCommand, objects.toArray());
|
method.invoke(abstractSWCommand, objects.toArray());
|
||||||
@ -138,8 +113,6 @@ public class SubCommand<T> {
|
|||||||
})) {
|
})) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} else if (guardChecker != null && guardChecker.guard(sender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
if (commandPart == null) {
|
if (commandPart == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command;
|
|
||||||
|
|
||||||
import de.steamwar.command.dto.ExecutionIdentifier;
|
|
||||||
import de.steamwar.command.dto.TestGuardChecker;
|
|
||||||
import de.steamwar.command.dto.TestSWCommand;
|
|
||||||
|
|
||||||
public class GuardCommand extends TestSWCommand {
|
|
||||||
|
|
||||||
public GuardCommand() {
|
|
||||||
super("typemapper");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Register
|
|
||||||
public void test(@Guard String sender) {
|
|
||||||
throw new ExecutionIdentifier("RunTypeMapper");
|
|
||||||
}
|
|
||||||
|
|
||||||
@ClassGuard(value = String.class, local = true)
|
|
||||||
public AbstractGuardChecker<String> getGuardChecker() {
|
|
||||||
return (TestGuardChecker) (s, guardCheckType, previousArguments, s2) -> {
|
|
||||||
throw new ExecutionIdentifier("GuardChecker " + guardCheckType);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command;
|
|
||||||
|
|
||||||
import de.steamwar.command.dto.ExecutionIdentifier;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static de.steamwar.AssertionUtils.assertCMDFramework;
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.instanceOf;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
|
|
||||||
public class GuardCommandTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() {
|
|
||||||
GuardCommand cmd = new GuardCommand();
|
|
||||||
try {
|
|
||||||
cmd.execute("test", "", new String[0]);
|
|
||||||
} catch (Exception e) {
|
|
||||||
assertThat(e.getMessage(), is("GuardChecker COMMAND"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testTabComplete() {
|
|
||||||
GuardCommand cmd = new GuardCommand();
|
|
||||||
try {
|
|
||||||
cmd.tabComplete("test", "", new String[]{""});
|
|
||||||
} catch (Exception e) {
|
|
||||||
assertThat(e, is(instanceOf(ExecutionIdentifier.class)));
|
|
||||||
assertThat(e.getMessage(), is("GuardChecker TAB_COMPLETE"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.command.dto;
|
|
||||||
|
|
||||||
import de.steamwar.command.AbstractGuardChecker;
|
|
||||||
|
|
||||||
public interface TestGuardChecker extends AbstractGuardChecker<String> {
|
|
||||||
}
|
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren