Skip to content

Commit

Permalink
simplify autoExecute annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 23, 2022
1 parent 60f8af3 commit e6c48a2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 46 deletions.
Expand Up @@ -2,8 +2,9 @@

import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.commands.generator.BooleanArg;
import com.denizenscript.denizencore.scripts.commands.generator.PrefixedArg;
import com.denizenscript.denizencore.scripts.commands.generator.ArgName;
import com.denizenscript.denizencore.scripts.commands.generator.ArgPrefixed;
import com.denizenscript.denizencore.scripts.commands.generator.RequiredPrefixedArg;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
Expand Down Expand Up @@ -57,9 +58,10 @@ public FileCopyCommand() {
//
// -->

public static void autoExecute(final ScriptEntry scriptEntry, @PrefixedArg(prefix = "origin") final ElementTag origin,
@PrefixedArg(prefix = "destination") final ElementTag destination,
@BooleanArg(name = "overwrite") final boolean overwrite) {
public static void autoExecute(final ScriptEntry scriptEntry,
@ArgPrefixed @ArgName("origin") final ElementTag origin,
@ArgPrefixed @ArgName("destination") final ElementTag destination,
@ArgName("overwrite") final boolean overwrite) {
if (!CoreConfiguration.allowFileCopy) {
Debug.echoError(scriptEntry, "File copy disabled by server administrator (refer to command documentation).");
scriptEntry.addObject("success", new ElementTag("false"));
Expand Down
Expand Up @@ -6,7 +6,9 @@
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.commands.generator.PrefixedArg;
import com.denizenscript.denizencore.scripts.commands.generator.ArgName;
import com.denizenscript.denizencore.scripts.commands.generator.ArgPrefixed;
import com.denizenscript.denizencore.scripts.commands.generator.RequiredPrefixedArg;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.scheduling.AsyncSchedulable;
Expand Down Expand Up @@ -52,7 +54,8 @@ public FileReadCommand() {
//
// -->

public static void autoExecute(final ScriptEntry scriptEntry, @PrefixedArg(prefix = "path") final ElementTag path) {
public static void autoExecute(final ScriptEntry scriptEntry,
@ArgPrefixed @ArgName("path") final ElementTag path) {
if (!CoreConfiguration.allowFileRead) {
Debug.echoError(scriptEntry, "FileRead disabled in Denizen/config.yml (refer to command documentation).");
scriptEntry.setFinished(true);
Expand Down
Expand Up @@ -6,7 +6,9 @@
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.commands.generator.PrefixedArg;
import com.denizenscript.denizencore.scripts.commands.generator.ArgName;
import com.denizenscript.denizencore.scripts.commands.generator.ArgPrefixed;
import com.denizenscript.denizencore.scripts.commands.generator.RequiredPrefixedArg;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.scheduling.AsyncSchedulable;
Expand Down Expand Up @@ -55,7 +57,9 @@ public FileWriteCommand() {
//
// -->

public static void autoExecute(final ScriptEntry scriptEntry, @PrefixedArg(prefix = "path") final ElementTag path, @PrefixedArg(prefix = "data") BinaryTag data) {
public static void autoExecute(final ScriptEntry scriptEntry,
@ArgPrefixed @ArgName("path") final ElementTag path,
@ArgPrefixed @ArgName("data") BinaryTag data) {
if (!CoreConfiguration.allowFileWrite) {
Debug.echoError(scriptEntry, "FileWrite disabled in Denizen/config.yml (refer to command documentation).");
scriptEntry.setFinished(true);
Expand Down
Expand Up @@ -7,6 +7,6 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface BooleanArg {
String name();
public @interface ArgName {
String value();
}
Expand Up @@ -7,8 +7,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface PrefixedArg {
String prefix();
public @interface ArgPrefixed {
boolean required() default true;
boolean throwTypeError() default true;
}
Expand Up @@ -101,24 +101,27 @@ public static CommandExecutor generateExecutorFor(Class<? extends AbstractComman
hasScriptEntry = true;
continue;
}
PrefixedArg prefixArg = param.getAnnotation(PrefixedArg.class);
if (prefixArg != null) {
cmd.setPrefixesHandled(prefixArg.prefix());
ArgName argName = param.getAnnotation(ArgName.class);
ArgPrefixed argPrefixed = param.getAnnotation(ArgPrefixed.class);
LinearArg linearArg = param.getAnnotation(LinearArg.class);
if (argName == null) {
Debug.echoError("Cannot generate executor for command '" + cmdClass.getName() + "': autoExecute method has param '" + param.getName() + "' which lacks a proper naming parameter.");
return null;
}
MethodGenerator.Local argLocal = gen.addLocal("arg_" + args.size() + "_" + CodeGenUtil.cleanName(argName.value()), paramType);
Method argMethod = null;
boolean doCast = false;
ArgData argData = null;
if (argPrefixed != null) {
cmd.setPrefixesHandled(argName.value());
if (ObjectTag.class.isAssignableFrom(paramType)) {
MethodGenerator.Local argLocal = gen.addLocal("argPrefix_" + args.size() + "_" + CodeGenUtil.cleanName(prefixArg.prefix()), paramType);
gen.loadLocal(scriptEntryLocal);
gen.loadStaticField(className, argLocal.name, PrefixArgData.class);
gen.invokeStatic(HELPER_PREFIX_ENTRY_ARG_METHOD);
gen.cast(paramType);
gen.storeLocal(argLocal);
PrefixArgData argData = new PrefixArgData();
argData.prefix = prefixArg.prefix();
argData.required = prefixArg.required();
argData.throwTypeError = prefixArg.throwTypeError();
argData.type = paramType;
argLocals.add(argLocal);
args.add(argData);
continue;
argMethod = HELPER_PREFIX_ENTRY_ARG_METHOD;
doCast = true;
PrefixArgData prefixArgData = new PrefixArgData();
prefixArgData.required = argPrefixed.required();
prefixArgData.throwTypeError = argPrefixed.throwTypeError();
prefixArgData.type = paramType;
argData = prefixArgData;
}
else if (paramType == boolean.class) {
// TODO
Expand All @@ -127,26 +130,28 @@ else if (paramType == boolean.class) {
// TODO
}
}
BooleanArg booleanArg = param.getAnnotation(BooleanArg.class);
if (booleanArg != null && paramType == boolean.class) {
cmd.setBooleansHandled(booleanArg.name());
MethodGenerator.Local argLocal = gen.addLocal("argBool_" + args.size() + "_" + CodeGenUtil.cleanName(booleanArg.name()), boolean.class);
gen.loadLocal(scriptEntryLocal);
gen.loadStaticField(className, argLocal.name, BooleanArgData.class);
gen.invokeStatic(HELPER_BOOLEAN_ARG_METHOD);
gen.storeLocal(argLocal);
BooleanArgData argData = new BooleanArgData();
argData.prefix = booleanArg.name();
argLocals.add(argLocal);
args.add(argData);
continue;
else if (paramType == boolean.class) {
cmd.setBooleansHandled(argName.value());
argMethod = HELPER_BOOLEAN_ARG_METHOD;
argData = new BooleanArgData();
}
LinearArg linearArg = param.getAnnotation(LinearArg.class);
if (linearArg != null) {
// TODO
}
Debug.echoError("Cannot generate executor for command '" + cmdClass.getName() + "': autoExecute method has param '" + param.getName() + "' of type '" + paramType.getName() + "' which is not supported.");
return null;
if (argMethod == null) {
Debug.echoError("Cannot generate executor for command '" + cmdClass.getName() + "': autoExecute method has param '" + argName.value() + "' of type '" + paramType.getName() + "' which is not supported.");
return null;
}
gen.loadLocal(scriptEntryLocal);
gen.loadStaticField(className, argLocal.name, argData.getClass());
gen.invokeStatic(argMethod);
if (doCast) {
gen.cast(paramType);
}
gen.storeLocal(argLocal);
argData.prefix = argName.value();
argLocals.add(argLocal);
args.add(argData);
}
gen.advanceAndLabel();
Label afterDebugLabel = new Label();
Expand Down
@@ -0,0 +1,12 @@
package com.denizenscript.denizencore.scripts.commands.generator;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface RequiredPrefixedArg {
String value();
}

0 comments on commit e6c48a2

Please sign in to comment.