tcr/java/src/game/command/ScriptEnvironment.java
2025-03-19 21:54:09 +01:00

195 lines
5.7 KiB
Java

package game.command;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import game.Server;
import game.color.TextColor;
import game.command.commands.CommandSpawn;
import game.log.Log;
public class ScriptEnvironment {
private final Server server;
private final Map<String, CachedExecutable> executables = Maps.newTreeMap();
private final List<PatternReplacer> replacers = Lists.newArrayList();
private ScriptExecutor currentExecutor;
private Object previousOutput;
public ScriptEnvironment(Server server) {
this.server = server;
this.registerDefaults();
}
public void registerExecutable(Executable executable) {
CachedExecutable cached = CachedExecutable.cacheExecutable(executable);
this.executables.put(cached.getName(), cached);
this.registerReplacer(cached.getName(), new Function<String, String>() {
public String apply(String str) {
Object o;
try {
o = ScriptEnvironment.this.execute(str, false);
}
catch(Throwable e) {
throw new ScriptException(e, "Variable konnte nicht ersetzt werden: '%s' konnte nicht ausgeführt werden", str);
}
if(o == null)
throw new ScriptException("Variable konnte nicht ersetzt werden: null von '%s' zurückgegeben", str);
return o.toString();
}
});
}
public void registerReplacer(String var, Function<String, String> function) {
this.replacers.add(new PatternReplacer(var, true, function));
}
public void registerVariable(String var, Variable variable) {
this.replacers.add(new PatternReplacer(var, false, new Function<String, String>() {
public String apply(String ign) {
return variable.get();
}
}));
}
public Server getServer() {
return this.server;
}
public ScriptExecutor getExecutor() {
return this.currentExecutor;
}
private String substitute(String str) {
StringBuffer sb = new StringBuffer(str);
for(PatternReplacer replacer : this.replacers) {
replacer.replaceVar(sb);
}
return sb.toString();
}
private Object execute(String cmd, boolean setPrev) {
String line = this.substitute(cmd);
String[][] cmds = ArgumentParser.splitString(line);
Object o = null;
for(String[] argv : cmds) {
CachedExecutable cached = this.executables.get(argv[0]);
if(cached == null)
throw new ScriptException("Skript '%s' existiert nicht", argv[0]);
ScriptArgs args = ScriptArgs.parseArgs(this, cmd, argv, cached);
List<Object> params = Lists.newArrayList(this, this.currentExecutor);
for(Parameter param : cached.getExecutable().getParamList()) {
ArgCombiner combiner = param.getCombiner();
if(combiner != null) {
Object[] data = (Object[])Array.newInstance(combiner.getInputClass(), param.getParsers().size());
for(int z = 0; z < data.length; z++) {
data[z] = args.getUnchecked(param.getName(), param.getParsers().get(z).getName());
if(data[z] == null) {
data = null;
break;
}
}
params.add(data == null ? null : combiner.combine(data));
continue;
}
if(param.getParsers().isEmpty()) {
params.add(args.hasArg(param.getName()));
continue;
}
for(ArgumentParser parser : param.getParsers()) {
params.add(args.getUnchecked(param.getName(), parser.getName()));
}
}
try {
o = cached.getMethod().invoke(cached.getExecutable(), params.toArray(new Object[params.size()]));
}
catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
// o = cached.getExecutable().exec(this, args);
if(setPrev)
this.previousOutput = o;
}
return o;
}
public void execute(String cmd, ScriptExecutor exec) {
this.currentExecutor = exec;
try {
this.execute(cmd, true);
}
catch(ScriptException e) {
Throwable cause = e;
do {
exec.logConsole(TextColor.RED + cause.getMessage());
cause = cause.getCause();
}
while(cause != null);
}
catch(Throwable t) {
exec.logConsole(TextColor.RED + "Fehler: %s", t.getMessage());
Log.CONSOLE.error(t, "Fehler beim Ausführen von Befehl '%s'", cmd);
}
finally {
this.currentExecutor = null;
this.previousOutput = null;
}
}
public List<String> complete(String cmd, ScriptExecutor exec) {
List<String> list = Lists.newArrayList();
try {
String[][] cmds = ArgumentParser.splitString(cmd.endsWith(" ") ? cmd + "END" : cmd);
if(cmds.length == 0)
return list;
String[] argv = cmds[cmds.length - 1];
if(argv.length == 0)
return list;
String param = cmd.endsWith(" ") ? "" : argv[argv.length - 1];
String[] comp;
if(argv.length > 1) {
CachedExecutable cached = this.executables.get(argv[0]);
if(cached == null)
return list;
comp = ScriptArgs.parseComplete(this, argv, cached, param);
}
else {
comp = this.executables.keySet().toArray(new String[this.executables.keySet().size()]);
}
for(String cmp : comp) {
if(cmp.regionMatches(true, 0, param, 0, param.length()))
list.add(cmp);
}
}
catch(Throwable t) {
list.clear();
}
return list;
}
public void registerDefaults() {
this.registerVariable("id", new Variable() {
public String get() {
return ScriptEnvironment.this.currentExecutor.getExecId();
}
});
this.registerVariable("name", new Variable() {
public String get() {
return ScriptEnvironment.this.currentExecutor.getExecName();
}
});
this.registerVariable("chain", new Variable() {
public String get() {
return ScriptEnvironment.this.previousOutput == null ? null : ScriptEnvironment.this.previousOutput.toString();
}
});
this.registerExecutable(new CommandSpawn());
}
}