|
@@ -1,380 +1,532 @@
|
|
|
package me.hammerle.code;
|
|
|
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
+import me.hammerle.exceptions.HoldCodeException;
|
|
|
import me.hammerle.exceptions.IllegalStringException;
|
|
|
+import me.hammerle.exceptions.NoSuchMethodException;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.time.ZonedDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Calendar;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.GregorianCalendar;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
-import java.awt.Font;
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import me.hammerle.exceptions.IllegalColorException;
|
|
|
-import me.hammerle.graphics.*;
|
|
|
-import me.hammerle.snuviscript.SnuviScript;
|
|
|
+import java.util.function.BiFunction;
|
|
|
+import java.util.function.BiConsumer;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import me.hammerle.console.ConsoleUtils;
|
|
|
+import me.hammerle.scheduler.SnuviScheduler;
|
|
|
|
|
|
public class CodeParser
|
|
|
-{
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static Object parseFunction(Script sd, String function, Object[] args, int jump)
|
|
|
+{
|
|
|
+ private final static HashMap<String, BiFunction<Object[], Script, Object>> METHODS = new HashMap<>();
|
|
|
+
|
|
|
+ private static void registerFunction(String s, BiFunction<Object[], Script, Object> f)
|
|
|
{
|
|
|
- try
|
|
|
+ METHODS.put(s, f);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void registerConsumer(String s, BiConsumer<Object[], Script> f)
|
|
|
+ {
|
|
|
+ METHODS.put(s, (BiFunction<Object[], Script, Object>) (args, sc) ->
|
|
|
{
|
|
|
- switch(function.toLowerCase())
|
|
|
- {
|
|
|
- // Rechenoperationen
|
|
|
- /*case "add":
|
|
|
- return args.stream().mapToDouble(s -> ScriptUtils.getDouble(s)).sum();
|
|
|
- case "sub":
|
|
|
- return ScriptUtils.getDouble(args.get(0)) - ScriptUtils.getDouble(args.get(1));
|
|
|
- case "mul":
|
|
|
- return ScriptUtils.getDouble(args.get(0)) * ScriptUtils.getDouble(args.get(1));
|
|
|
- case "div":
|
|
|
- return ScriptUtils.getDouble(args.get(0)) / ScriptUtils.getDouble(args.get(1));
|
|
|
- case "math.mod":
|
|
|
- return ScriptUtils.getDouble(args.get(0)) % ScriptUtils.getDouble(args.get(1));
|
|
|
- case "math.abs":
|
|
|
- return Math.abs(ScriptUtils.getDouble(args.get(0)));
|
|
|
- case "math.pow":
|
|
|
- return Math.pow(ScriptUtils.getDouble(args.get(0)), ScriptUtils.getDouble(args.get(1)));
|
|
|
- case "math.root":
|
|
|
- return Math.pow(ScriptUtils.getDouble(args.get(0)), 1d / ScriptUtils.getDouble(args.get(1)));
|
|
|
- case "math.sin":
|
|
|
- return Math.sin(ScriptUtils.getDouble(args.get(0)));
|
|
|
- case "math.cos":
|
|
|
- return Math.cos(ScriptUtils.getDouble(args.get(0)));
|
|
|
- case "math.e":
|
|
|
- return Math.E;
|
|
|
- case "math.pi":
|
|
|
- return Math.PI;
|
|
|
- case "math.ln":
|
|
|
- return Math.log(ScriptUtils.getDouble(args.get(0)));
|
|
|
- case "math.log":
|
|
|
- return Math.log10(ScriptUtils.getDouble(args.get(0)));
|
|
|
- case "math.random":
|
|
|
- return ScriptUtils.randomInt(ScriptUtils.getInteger(args.get(0)), ScriptUtils.getInteger(args.get(1)));
|
|
|
- case "math.round":
|
|
|
- return ((Long) Math.round(ScriptUtils.getDouble(args.get(0)))).intValue();
|
|
|
- case "math.rounddown":
|
|
|
- return Math.floor(ScriptUtils.getDouble(args.get(0)));
|
|
|
- case "math.roundup":
|
|
|
- return Math.ceil(ScriptUtils.getDouble(args.get(0)));
|
|
|
- case "math.roundcomma":
|
|
|
- return new BigDecimal(ScriptUtils.getDouble(args.get(0))).setScale(ScriptUtils.getInteger(args.get(1)), RoundingMode.HALF_UP);
|
|
|
-
|
|
|
- // Variablen
|
|
|
- case "getvar":
|
|
|
- return sd.getVar(args.get(0));
|
|
|
- case "setvar":
|
|
|
- sd.setVar(args.get(0), args.get(1)); break;
|
|
|
- case "removevar":
|
|
|
- sd.removeVar(args.get(0)); break;
|
|
|
-
|
|
|
- // Listen - Befehle
|
|
|
- case "list.new":
|
|
|
- sd.setVar(args.get(0), new ArrayList<>()); break;
|
|
|
- case "list.exists":
|
|
|
- return sd.getVar(args.get(0)) instanceof List;
|
|
|
- case "list.add":
|
|
|
- ((List) sd.getVar(args.get(0))).add(args.get(1)); break;
|
|
|
- case "list.remove":
|
|
|
- ((List) sd.getVar(args.get(0))).remove(args.get(1)); break;
|
|
|
- case "list.removeindex":
|
|
|
- ((List) sd.getVar(args.get(0))).remove((int) args.get(1)); break;
|
|
|
- case "list.contains":
|
|
|
- return ((List) sd.getVar(args.get(0))).contains(args.get(1));
|
|
|
- case "list.size":
|
|
|
- return ((List) sd.getVar(args.get(0))).size();
|
|
|
- case "list.get":
|
|
|
- return ((List) sd.getVar(args.get(0))).get((int) args.get(1));
|
|
|
- case "list.set":
|
|
|
- ((List) sd.getVar(args.get(0))).set((int) args.get(1), args.get(2)); break;
|
|
|
- case "list.indexof":
|
|
|
- return ((List) sd.getVar(args.get(0))).indexOf(args.get(1));
|
|
|
- case "list.sort":
|
|
|
- sortList(args, sd); break;
|
|
|
- case "list.reverse":
|
|
|
- Collections.reverse((List<Object>) sd.getVar(args.get(0))); break;
|
|
|
- case "list.shuffle":
|
|
|
- Collections.shuffle((List<Object>) sd.getVar(args.get(0))); break;
|
|
|
-
|
|
|
- // Maps - Befehle
|
|
|
- case "map.new":
|
|
|
- sd.setVar(args.get(0), new HashMap<>()); break;
|
|
|
- case "map.exists":
|
|
|
- return sd.getVar(args.get(0)) instanceof HashMap;
|
|
|
- case "map.add":
|
|
|
- ((HashMap) sd.getVar(args.get(0))).put(args.get(1), args.get(2)); break;
|
|
|
- case "map.remove":
|
|
|
- ((HashMap) sd.getVar(args.get(0))).remove(args.get(1)); break;
|
|
|
- case "map.contains":
|
|
|
- return ((HashMap) sd.getVar(args.get(0))).containsKey(args.get(1));
|
|
|
- case "map.get":
|
|
|
- return ((HashMap) sd.getVar(args.get(0))).get(args.get(1));
|
|
|
-
|
|
|
- // Grafik
|
|
|
- case "repaint":
|
|
|
- SnuviScript.screen.graphics.repaint(); break;
|
|
|
- case "drawline":
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new Line(ScriptUtils.getColor(args.get(1)),
|
|
|
- ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3)),
|
|
|
- ScriptUtils.getInteger(args.get(4)), ScriptUtils.getInteger(args.get(5)))); break;
|
|
|
- case "drawoval":
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new Oval(ScriptUtils.getColor(args.get(1)),
|
|
|
- ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3)),
|
|
|
- ScriptUtils.getInteger(args.get(4)), ScriptUtils.getInteger(args.get(5)))); break;
|
|
|
- case "drawrec":
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new Rectangle(ScriptUtils.getColor(args.get(1)),
|
|
|
- ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3)),
|
|
|
- ScriptUtils.getInteger(args.get(4)), ScriptUtils.getInteger(args.get(5)))); break;
|
|
|
- case "drawfulloval":
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new FullOval(ScriptUtils.getColor(args.get(1)),
|
|
|
- ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3)),
|
|
|
- ScriptUtils.getInteger(args.get(4)), ScriptUtils.getInteger(args.get(5)))); break;
|
|
|
- case "drawfullrec":
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new FullRectangle(ScriptUtils.getColor(args.get(1)),
|
|
|
- ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3)),
|
|
|
- ScriptUtils.getInteger(args.get(4)), ScriptUtils.getInteger(args.get(5)))); break;
|
|
|
- case "drawimage":
|
|
|
- drawImage(args); break;
|
|
|
- case "drawstring":
|
|
|
- drawString(args); break;
|
|
|
- case "removegraphic":
|
|
|
- SnuviScript.screen.graphics.removeGraphicObject(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "buffergraphic":
|
|
|
- SnuviScript.screen.graphics.loadGraphicObjectIntoBuffer(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "setcolor":
|
|
|
- SnuviScript.screen.graphics.getBuffer().setColor(ScriptUtils.getColor(args.get(0))); break;
|
|
|
- case "setx":
|
|
|
- ((StartPoint) SnuviScript.screen.graphics.getBuffer()).setX(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "sety":
|
|
|
- ((StartPoint) SnuviScript.screen.graphics.getBuffer()).setY(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "setx1":
|
|
|
- ((Line) SnuviScript.screen.graphics.getBuffer()).setFirstX(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "sety1":
|
|
|
- ((Line) SnuviScript.screen.graphics.getBuffer()).setFirstY(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "setx2":
|
|
|
- ((Line) SnuviScript.screen.graphics.getBuffer()).setSecondX(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "sety2":
|
|
|
- ((Line) SnuviScript.screen.graphics.getBuffer()).setSecondY(ScriptUtils.getInteger(args.get(0))); break;
|
|
|
- case "setscreensize":
|
|
|
- SnuviScript.screen.setSize(ScriptUtils.getInteger(args.get(0)), ScriptUtils.getInteger(args.get(1)));
|
|
|
- SnuviScript.screen.graphics.setSize(SnuviScript.screen.getWidth(), SnuviScript.screen.getHeight() - 22); break;
|
|
|
- case "getscreenx":
|
|
|
- return SnuviScript.screen.graphics.getWidth();
|
|
|
- case "getscreeny":
|
|
|
- return SnuviScript.screen.graphics.getHeight();
|
|
|
+ f.accept(args, sc);
|
|
|
+ return Void.TYPE;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void registerAlias(String s, String original)
|
|
|
+ {
|
|
|
+ registerFunction(s, METHODS.get(original));
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static void initCodeParser()
|
|
|
+ {
|
|
|
+ registerFunction("nothing", (args, sc) ->
|
|
|
+ 0);
|
|
|
+
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Script-Consolen-Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerConsumer("script.list", (args, sc) ->
|
|
|
+ { ScriptUtils.printDebug("The following scripts are active:"); ScriptControl.getScripts().forEach(s -> ScriptUtils.printDebug(s.getId() + " | " + s.getName()));});
|
|
|
+ registerConsumer("script.load", (args, sc) ->
|
|
|
+ ScriptControl.startScriptFromFile(args[0].toString()));
|
|
|
+ registerConsumer("clear", (args, sc) ->
|
|
|
+ ConsoleUtils.clear());
|
|
|
+ registerConsumer("script.term", (args, sc) ->
|
|
|
+ termScript(args, sc));
|
|
|
+
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Event-Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerConsumer("event.load", (args, sc) ->
|
|
|
+ sc.loadEvent(args[0].toString()));
|
|
|
+ registerConsumer("event.unload", (args, sc) ->
|
|
|
+ sc.unloadEvent(args[0].toString()));
|
|
|
+ registerFunction("event.isloaded", (args, sc) ->
|
|
|
+ sc.isLoadedEvent(args[0].toString()));
|
|
|
|
|
|
- // Allgemeines
|
|
|
- case "debug":
|
|
|
- System.out.println(args.get(0));
|
|
|
- break;
|
|
|
- case "loadlib":
|
|
|
- sd.loadLibrary(args.get(0)); break;
|
|
|
- case "lib":
|
|
|
- SnuviScript.scriptController.startLibrary(qd, args); return false;
|
|
|
- case "reset":
|
|
|
- sd.resetOverflowProtection(); break;
|
|
|
- case "wait":
|
|
|
- sd.resetOverflowProtection(); return false;
|
|
|
- case "term":
|
|
|
- SnuviScript.scriptController.term(sd); return false;
|
|
|
- case "exit":
|
|
|
- System.exit(0); return false;
|
|
|
- case "loadevent":
|
|
|
- sd.loadEvent(args.get(0)); break;
|
|
|
- case "unloadevent":
|
|
|
- sd.unloadEvent(args.get(0)); break;
|
|
|
- case "goto":
|
|
|
- sd.gotoLabel(args.get(0).toString()); break;
|
|
|
- case "sgoto":
|
|
|
- scheduleGoto(args, sd); break;
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Mathe-Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerFunction("math.mod", (args, sc) ->
|
|
|
+ ((double) args[0]) % ((double) args[1]));
|
|
|
+ registerFunction("math.abs", (args, sc) ->
|
|
|
+ Math.abs((double) args[0]));
|
|
|
+ registerFunction("math.pow", (args, sc) ->
|
|
|
+ Math.pow((double) args[0], (double) args[1]));
|
|
|
+ registerFunction("math.root", (args, sc) ->
|
|
|
+ Math.pow((double) args[0], 1d / ((double) args[1])));
|
|
|
+ registerFunction("math.sin", (args, sc) ->
|
|
|
+ Math.sin((double) args[0]));
|
|
|
+ registerFunction("math.cos", (args, sc) ->
|
|
|
+ Math.cos((double) args[0]));
|
|
|
+ registerFunction("math.tan", (args, sc) ->
|
|
|
+ Math.tan((double) args[0]));
|
|
|
+ registerFunction("math.asin", (args, sc) ->
|
|
|
+ Math.asin((double) args[0]));
|
|
|
+ registerFunction("math.acos", (args, sc) ->
|
|
|
+ Math.acos((double) args[0]));
|
|
|
+ registerFunction("math.atan", (args, sc) ->
|
|
|
+ Math.atan((double) args[0]));
|
|
|
+ registerFunction("math.e", (args, sc) ->
|
|
|
+ Math.E);
|
|
|
+ registerFunction("math.pi", (args, sc) ->
|
|
|
+ Math.PI);
|
|
|
+ registerFunction("math.ln", (args, sc) ->
|
|
|
+ Math.log((double) args[0]));
|
|
|
+ registerFunction("math.log", (args, sc) ->
|
|
|
+ Math.log10((double) args[0]));
|
|
|
+ registerFunction("math.random", (args, sc) ->
|
|
|
+ ScriptUtils.randomInt(getInt(args[0]), getInt(args[1])));
|
|
|
+ registerFunction("math.round", (args, sc) ->
|
|
|
+ Math.round((double) args[0]));
|
|
|
+ registerFunction("math.rounddown", (args, sc) ->
|
|
|
+ Math.floor((double) args[0]));
|
|
|
+ registerFunction("math.roundup", (args, sc) ->
|
|
|
+ Math.ceil((double) args[0]));
|
|
|
+ registerFunction("math.roundcomma", (args, sc) ->
|
|
|
+ new BigDecimal(((double) args[0])).setScale(getInt(args[1]), RoundingMode.HALF_UP).doubleValue());
|
|
|
+
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Listen-Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerConsumer("list.new", (args, sc) ->
|
|
|
+ sc.setVar(args[0].toString(), new ArrayList<>()));
|
|
|
+ registerFunction("list.exists", (args, sc) ->
|
|
|
+ args[0] instanceof List);
|
|
|
+ registerConsumer("list.add", (args, sc) ->
|
|
|
+ ((List) args[0]).add(args[1]));
|
|
|
+ registerConsumer("list.remove", (args, sc) ->
|
|
|
+ ((List) args[0]).remove(args[1]));
|
|
|
+ registerConsumer("list.removeindex", (args, sc) ->
|
|
|
+ ((List) args[0]).remove(getInt(args[1])));
|
|
|
+ registerFunction("list.contains", (args, sc) ->
|
|
|
+ ((List) args[0]).contains(args[1]));
|
|
|
+ registerFunction("list.getsize", (args, sc) ->
|
|
|
+ ((List) args[0]).size());
|
|
|
+ registerFunction("list.getindex", (args, sc) ->
|
|
|
+ ((List) args[0]).get(getInt(args[1])));
|
|
|
+ registerConsumer("list.setindex", (args, sc) ->
|
|
|
+ ((List) args[0]).set(getInt(args[1]), args[2]));
|
|
|
+ registerConsumer("list.clear", (args, sc) ->
|
|
|
+ ((List) args[0]).clear());
|
|
|
+ registerFunction("list.getindexof", (args, sc) ->
|
|
|
+ ((List) args[0]).indexOf(args[1]));
|
|
|
+ registerConsumer("list.sort", (args, sc) ->
|
|
|
+ sortList((List<Object>) args[0], sc));
|
|
|
+ registerConsumer("list.reverse", (args, sc) ->
|
|
|
+ Collections.reverse((List<Object>) args[0]));
|
|
|
+ registerConsumer("list.shuffle", (args, sc) ->
|
|
|
+ Collections.shuffle((List<Object>) args[0]));
|
|
|
|
|
|
- // Conditions
|
|
|
- case "if":
|
|
|
- ifFunction(args, sd); break;
|
|
|
- case "equal":
|
|
|
- return ((Comparable) args.get(0)).compareTo(args.get(1)) == 0;
|
|
|
- case "less":
|
|
|
- return ((Comparable) args.get(0)).compareTo(args.get(1)) == -1;
|
|
|
- case "greater":
|
|
|
- return ((Comparable) args.get(0)).compareTo(args.get(1)) == 1;
|
|
|
- case "notequal":
|
|
|
- return ((Comparable) args.get(0)).compareTo(args.get(1)) != 0;
|
|
|
- case "lessequal":
|
|
|
- return ((Comparable) args.get(0)).compareTo(args.get(1)) <= 0;
|
|
|
- case "greaterequal":
|
|
|
- return ((Comparable) args.get(0)).compareTo(args.get(1)) >= 0;
|
|
|
- case "invert":
|
|
|
- return args.stream().allMatch(s -> s.equals(true));
|
|
|
- case "and":
|
|
|
- return args.stream().allMatch(s -> s.equals(true));
|
|
|
- case "or":
|
|
|
- return args.stream().anyMatch(s -> s.equals(true));
|
|
|
-
|
|
|
- // Sonstiges
|
|
|
- case "concatlist":
|
|
|
- return String.join("", ((List) sd.getVar(args.get(0))).subList(ScriptUtils.getInteger(args.get(1)), ScriptUtils.getInteger(args.get(2))));
|
|
|
- case "concatlistspace":
|
|
|
- return String.join(" ", ((List) sd.getVar(args.get(0))).subList(ScriptUtils.getInteger(args.get(1)), ScriptUtils.getInteger(args.get(2))));
|
|
|
- case "split":
|
|
|
- sd.setVar(args.get(0), new ArrayList<>(Arrays.asList(args.get(2).toString().split(args.get(1).toString())))); break;
|
|
|
- case "concat":
|
|
|
- return args.stream().map(o -> o.toString()).collect(Collectors.joining());
|
|
|
- case "concatspace":
|
|
|
- return args.stream().map(o -> o.toString()).collect(Collectors.joining(" "));
|
|
|
- case "tolowercase":
|
|
|
- return args.get(0).toString().toLowerCase();
|
|
|
- case "touppercase":
|
|
|
- return args.get(0).toString().toUpperCase();
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Map-Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerConsumer("map.new", (args, sc) ->
|
|
|
+ sc.setVar(args[0].toString(), new HashMap<>()));
|
|
|
+ registerFunction("map.exists", (args, sc) ->
|
|
|
+ args[0] instanceof HashMap);
|
|
|
+ registerConsumer("map.add", (args, sc) ->
|
|
|
+ ((HashMap) args[0]).put(args[1], args[2]));
|
|
|
+ registerConsumer("map.remove", (args, sc) ->
|
|
|
+ ((HashMap) args[0]).remove(args[1]));
|
|
|
+ registerFunction("map.contains", (args, sc) ->
|
|
|
+ ((HashMap) args[0]).containsKey(args[1]));
|
|
|
+ registerFunction("map.getsize", (args, sc) ->
|
|
|
+ ((HashMap) args[0]).size());
|
|
|
+ registerFunction("map.get", (args, sc) ->
|
|
|
+ ((HashMap) args[0]).get(args[1]));
|
|
|
+ registerConsumer("map.clear", (args, sc) ->
|
|
|
+ ((HashMap) args[0]).clear());
|
|
|
+
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Set-Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerConsumer("set.new", (args, sc) ->
|
|
|
+ sc.setVar(args[0].toString(), new HashSet<>()));
|
|
|
+ registerFunction("set.exists", (args, sc) ->
|
|
|
+ args[0] instanceof HashSet);
|
|
|
+ registerConsumer("set.add", (args, sc) ->
|
|
|
+ ((HashSet) args[0]).add(args[1]));
|
|
|
+ registerConsumer("set.remove", (args, sc) ->
|
|
|
+ ((HashSet) args[0]).remove(args[1]));
|
|
|
+ registerFunction("set.contains", (args, sc) ->
|
|
|
+ ((HashSet) args[0]).contains(args[1]));
|
|
|
+ registerFunction("set.getsize", (args, sc) ->
|
|
|
+ ((HashSet) args[0]).size());
|
|
|
|
|
|
- case "gettime":
|
|
|
- return System.currentTimeMillis();
|
|
|
- case "nextday":
|
|
|
- return getNextDay(args, sd);
|
|
|
- case "waitfor":
|
|
|
- return waitFor(args, sd);
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Time-Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerFunction("time.get", (args, sc) ->
|
|
|
+ System.currentTimeMillis());
|
|
|
+ registerFunction("time.nextday", (args, sc) ->
|
|
|
+ getNextDay(args));
|
|
|
+ registerFunction("time.getyear", (args, sc) ->
|
|
|
+ getYear(args));
|
|
|
+ registerFunction("time.getmonth", (args, sc) ->
|
|
|
+ getMonth(args));
|
|
|
+ registerFunction("time.getday", (args, sc) ->
|
|
|
+ getDay(args));
|
|
|
+ registerFunction("time.gethour", (args, sc) ->
|
|
|
+ getHour(args));
|
|
|
+ registerFunction("time.getminute", (args, sc) ->
|
|
|
+ getMinute(args));
|
|
|
+ registerFunction("time.getsecond", (args, sc) ->
|
|
|
+ getSecond(args));
|
|
|
|
|
|
- // Is Valid Checker
|
|
|
- case "isvalidnumber":
|
|
|
- return generalValidChecker(() -> ScriptUtils.getDouble(args.get(0)));*/
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ // Ohne Bibliothek
|
|
|
+ // -------------------------------------------------------------
|
|
|
+ registerFunction("add", (args, sc) ->
|
|
|
+ Arrays.stream(args).mapToDouble(s -> (double) s).sum());
|
|
|
+ registerFunction("sub", (args, sc) ->
|
|
|
+ ((double) args[0]) - ((double) args[1]));
|
|
|
+ registerFunction("inc", (args, sc) ->
|
|
|
+ increaseVar(args[0], sc, 1));
|
|
|
+ registerFunction("dec", (args, sc) ->
|
|
|
+ increaseVar(args[0], sc, -1));
|
|
|
+ registerFunction("mul", (args, sc) ->
|
|
|
+ ((double) args[0]) * ((double) args[1]));
|
|
|
+ registerFunction("div", (args, sc) ->
|
|
|
+ ((double) args[0]) / ((double) args[1]));
|
|
|
+ registerFunction("getvar", (args, sc) ->
|
|
|
+ sc.getVar(args[0].toString()));
|
|
|
+ registerConsumer("setvar", (args, sc) ->
|
|
|
+ sc.setVar(args[0].toString(), args[1]));
|
|
|
+ registerConsumer("removevar", (args, sc) ->
|
|
|
+ sc.removeVar(args[0].toString()));
|
|
|
+ registerConsumer("debug", (args, sc) ->
|
|
|
+ ConsoleUtils.sendMessage(ScriptUtils.connect(args, 0), null));
|
|
|
+ registerConsumer("reset", (args, sc) ->
|
|
|
+ sc.resetLoopCounter());
|
|
|
+ registerConsumer("wait", (args, sc) ->
|
|
|
+ { sc.resetLoopCounter(); throw new HoldCodeException(); });
|
|
|
+ registerConsumer("goto", (args, sc) ->
|
|
|
+ sc.gotoLabel(args[0].toString()));
|
|
|
+ registerConsumer("sgoto", (args, sc) ->
|
|
|
+ scheduleGoto(args, sc));
|
|
|
+ registerConsumer("gosub", (args, sc) ->
|
|
|
+ sc.gotoLabelWithReturn(args[0].toString()));
|
|
|
+ registerConsumer("return", (args, sc) ->
|
|
|
+ sc.doReturn());
|
|
|
+ registerConsumer("try", (args, sc) ->
|
|
|
+ sc.saveTryJumpLine());
|
|
|
+ registerConsumer("catch", (args, sc) ->
|
|
|
+ sc.gotoSpecialJumpLine());
|
|
|
+ registerConsumer("if", (args, sc) ->
|
|
|
+ ifFunction(args, sc));
|
|
|
+ registerConsumer("else", (args, sc) ->
|
|
|
+ sc.gotoSpecialJumpLine());
|
|
|
+ registerConsumer("while", (args, sc) ->
|
|
|
+ whileFunction(args, sc));
|
|
|
+ registerFunction("equal", (args, sc) ->
|
|
|
+ isEqual(args));
|
|
|
+ registerAlias("equals", "equal");
|
|
|
+ registerFunction("less", (args, sc) ->
|
|
|
+ (double) args[0] < (double) args[1]);
|
|
|
+ registerFunction("greater", (args, sc) ->
|
|
|
+ (double) args[0] > (double) args[1]);
|
|
|
+ registerFunction("notequal", (args, sc) ->
|
|
|
+ !isEqual(args));
|
|
|
+ registerFunction("lessequal", (args, sc) ->
|
|
|
+ (double) args[0] <= (double) args[1]);
|
|
|
+ registerFunction("greaterequal", (args, sc) ->
|
|
|
+ (double) args[0] >= (double) args[1]);
|
|
|
+ registerFunction("invert", (args, sc) ->
|
|
|
+ !((boolean) args[0]));
|
|
|
+ registerFunction("and", (args, sc) ->
|
|
|
+ Arrays.stream(args).allMatch(s -> s.equals(true)));
|
|
|
+ registerFunction("or", (args, sc) ->
|
|
|
+ Arrays.stream(args).anyMatch(s -> s.equals(true)));
|
|
|
+ registerFunction("concatlist", (args, sc) ->
|
|
|
+ ((List<Object>) args[0]).stream().limit(getInt(args[3]) + 1).skip(getInt(args[2])).map(o -> o.toString()).collect(Collectors.joining(args[1].toString())));
|
|
|
+ registerConsumer("split", (args, sc) ->
|
|
|
+ split(args, sc));
|
|
|
+ registerFunction("concat", (args, sc) ->
|
|
|
+ ScriptUtils.connect(args, 0));
|
|
|
+ registerFunction("tolowercase", (args, sc) ->
|
|
|
+ ScriptUtils.connect(args, 0).toLowerCase());
|
|
|
+ registerFunction("touppercase", (args, sc) ->
|
|
|
+ ScriptUtils.connect(args, 0).toUpperCase());
|
|
|
+ registerConsumer("waitfor", (args, sc) ->
|
|
|
+ waitFor(args, sc));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean printStack = false;
|
|
|
|
|
|
- default:
|
|
|
- throw new NoSuchMethodException();
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static Object parseFunction(Script sc, String function, Object[] args) throws HoldCodeException
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ BiFunction<Object[], Script, Object> f = METHODS.get(function);
|
|
|
+ if(f == null)
|
|
|
+ {
|
|
|
+ throw new NoSuchMethodException(function);
|
|
|
}
|
|
|
+ return METHODS.get(function).apply(args, sc);
|
|
|
}
|
|
|
catch(Exception ex)
|
|
|
{
|
|
|
- //ex.printStackTrace();
|
|
|
- printQuestException(sd, ex, function);
|
|
|
- return null;
|
|
|
+ if(ex instanceof HoldCodeException)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ if(printStack)
|
|
|
+ {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ if(sc.getTryJumpLine() != -1)
|
|
|
+ {
|
|
|
+ sc.setVar("error", ex.getClass().getSimpleName());
|
|
|
+ sc.gotoTryJumpLine();
|
|
|
+ sc.resetTryJumpLine();
|
|
|
+ return Void.TYPE;
|
|
|
+ }
|
|
|
+ printQuestException(sc, ex, function + "(" + Arrays.stream(args).map(o -> String.valueOf(o)).collect(Collectors.joining(", ")) + ")");
|
|
|
+ throw new HoldCodeException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void printQuestException(Script sd, Exception ex, String line)
|
|
|
+ public static void printQuestException(Script sc, Exception ex, String line)
|
|
|
{
|
|
|
- sd.resetLoopCounter();
|
|
|
- ScriptUtils.printDebug("Error in");
|
|
|
- ScriptUtils.printDebugList("Script: " + sd.getName());
|
|
|
- ScriptUtils.printDebugList("Zeile: " + line);
|
|
|
- ScriptUtils.printDebugList("Exception: " + ex.getClass().getSimpleName());
|
|
|
+ sc.resetLoopCounter();
|
|
|
+ ScriptUtils.printError("Error in");
|
|
|
+ ScriptUtils.printErrorList("Script", sc.getName());
|
|
|
+ ScriptUtils.printErrorList("Zeile", line);
|
|
|
+ ScriptUtils.printErrorList("Zeilennummer", sc.getActiveCodeLine());
|
|
|
+ if(ex.getLocalizedMessage() == null)
|
|
|
+ {
|
|
|
+ ScriptUtils.printErrorList("Exception", ex.getClass().getSimpleName());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ScriptUtils.printErrorList("Exception", ex.getClass().getSimpleName() + " - " + ex.getLocalizedMessage());
|
|
|
+ }
|
|
|
if(ex instanceof IllegalStringException)
|
|
|
{
|
|
|
- ScriptUtils.printDebugList("Ungültiger Wert: " + ((IllegalStringException) ex).getBadString());
|
|
|
+ ScriptUtils.printErrorList("Ungültiger Wert", ((IllegalStringException) ex).getBadString());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // -----------------------
|
|
|
- // Script Befehle
|
|
|
- // -----------------------
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+ // Functions
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
|
|
|
- private static void drawImage(List<Object> args) throws IOException
|
|
|
+ private static void termScript(Object[] args, Script sc)
|
|
|
{
|
|
|
- BufferedImage image = ImageIO.read(new File(args.get(1).toString()));
|
|
|
- if(args.size() >= 5)
|
|
|
+ if(args.length == 0)
|
|
|
+ {
|
|
|
+ ScriptControl.term(sc);
|
|
|
+ throw new HoldCodeException();
|
|
|
+ }
|
|
|
+ else if(args[0].toString().equals("all"))
|
|
|
{
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new Image(image, ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3)),
|
|
|
- ScriptUtils.getInteger(args.get(4)), ScriptUtils.getInteger(args.get(5)),
|
|
|
- ScriptUtils.getInteger(args.get(6)), ScriptUtils.getInteger(args.get(7))));
|
|
|
- return;
|
|
|
- }
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new Image(image, ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3))));
|
|
|
+ ScriptControl.termScripts();
|
|
|
+ throw new HoldCodeException();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ int i = getInt(args[0]);
|
|
|
+ ScriptControl.term(ScriptControl.getScript(i));
|
|
|
+ if(sc.getId() == i)
|
|
|
+ {
|
|
|
+ throw new HoldCodeException();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static void drawString(List<Object> args) throws IllegalColorException
|
|
|
+ private static void ifFunction(Object[] args, Script sc)
|
|
|
{
|
|
|
- SnuviScript.screen.graphics.addGraphicObject(ScriptUtils.getInteger(args.get(0)),
|
|
|
- new Text(ScriptUtils.getColor(args.get(1)),
|
|
|
- ScriptUtils.getInteger(args.get(2)), ScriptUtils.getInteger(args.get(3)),
|
|
|
- args.get(4).toString(), new Font(args.get(5).toString(), ScriptUtils.getInteger(args.get(6)), ScriptUtils.getInteger(args.get(7)))));
|
|
|
- }
|
|
|
-
|
|
|
- private static void ifFunction(List<Object> args, Script sd)
|
|
|
+ if(Arrays.stream(args).anyMatch(s -> (boolean) s == false))
|
|
|
+ {
|
|
|
+ sc.gotoSpecialJumpLine();
|
|
|
+ sc.jumpNextIfElse();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void whileFunction(Object[] args, Script sc)
|
|
|
{
|
|
|
- if(args.stream().allMatch(s -> s.equals(true)))
|
|
|
+ if(Arrays.stream(args).anyMatch(s -> (boolean) s == false))
|
|
|
{
|
|
|
- //sd.goDeeper();
|
|
|
+ sc.gotoSpecialJumpLine();
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- private static void sortList(List<Object> args, Script sd)
|
|
|
- {
|
|
|
- //List<Object> list = (List<Object>) sd.getVar(args.get(0));
|
|
|
- //sd.setVar(args.get(0), list.stream().sorted().collect(Collectors.toList()));
|
|
|
+ @SuppressWarnings(value = "unchecked")
|
|
|
+ private static void sortList(List<Object> args, Script sc)
|
|
|
+ {
|
|
|
+ Collections.sort(args, (Object o1, Object o2) -> ((Comparable) o1).compareTo(o2));
|
|
|
}
|
|
|
-
|
|
|
- private static void scheduleGoto(List<Object> args, Script qd)
|
|
|
+
|
|
|
+ private static void scheduleGoto(Object[] args, Script sc)
|
|
|
{
|
|
|
- /*SnuviScheduler.doScheduledTask(() ->
|
|
|
+ SnuviScheduler.doScheduledTask(() ->
|
|
|
{
|
|
|
- if(!sd.isValid())
|
|
|
+ if(!sc.isValid())
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
try
|
|
|
{
|
|
|
- sd.setCodeLine(sd.getGoto(args.get(0)));
|
|
|
- continueCode(qd, null);
|
|
|
+ sc.gotoLabel(args[1].toString());
|
|
|
+ sc.setHalt(true);
|
|
|
+ sc.runCode();
|
|
|
}
|
|
|
- catch(GotoLabelNotFoundException ex)
|
|
|
+ catch(Exception ex)
|
|
|
{
|
|
|
- ScriptUtilities.printDebugMessage("Das Scheduled-Goto '" + args.get(0) + "' wurde nicht gefunden.");
|
|
|
+ printQuestException(sc, ex, "(Scheduled Goto)");
|
|
|
}
|
|
|
- }, Integer.parseInt(args.get(1)));*/
|
|
|
+ }, getInt(args[0]));
|
|
|
}
|
|
|
-
|
|
|
- private static String getNextDay(List<Object> args, Script qd)
|
|
|
- {
|
|
|
- GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
- cal.setTimeInMillis(Long.parseLong(args.get(0).toString()));
|
|
|
- cal.set(Calendar.HOUR, 0);
|
|
|
- cal.set(Calendar.SECOND, 0);
|
|
|
- cal.set(Calendar.MINUTE, 0);
|
|
|
- cal.set(Calendar.MILLISECOND, 0);
|
|
|
- cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
- return String.valueOf(cal.getTimeInMillis());
|
|
|
- }
|
|
|
|
|
|
- private static boolean waitFor(List<Object> args, Script qd) throws UnsupportedOperationException
|
|
|
+ private static void waitFor(Object[] args, Script sc) throws UnsupportedOperationException
|
|
|
{
|
|
|
- /*SnuviScript.scriptController.resetOverflowProtection();
|
|
|
- int i = Integer.parseInt(args.get(0));
|
|
|
+ sc.resetLoopCounter();
|
|
|
+ int i = getInt(args[0]);
|
|
|
if(i < 1)
|
|
|
{
|
|
|
throw new UnsupportedOperationException();
|
|
|
}
|
|
|
+ sc.setHalt(true);
|
|
|
SnuviScheduler.doScheduledTask(() ->
|
|
|
{
|
|
|
- if(qd == null || !sd.isValid())
|
|
|
+ if(sc == null || !sc.isValid())
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
- sd.setWaiting(false);
|
|
|
- sd.nextCodeLine();
|
|
|
- continueCode(qd, null);
|
|
|
+ sc.setHalt(false);
|
|
|
+ sc.runCode();
|
|
|
}, i);
|
|
|
- sd.setWaiting(true);*/
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean generalValidChecker(Runnable r)
|
|
|
+ throw new HoldCodeException();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Number increaseVar(Object var, Script sc, int value)
|
|
|
{
|
|
|
- try
|
|
|
+ double n = ((double) sc.getVar(var.toString())) + value;
|
|
|
+ sc.setVar(var.toString(), n);
|
|
|
+ return n;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isEqual(Object[] args)
|
|
|
+ {
|
|
|
+ if(args[0] == null)
|
|
|
{
|
|
|
- r.run();
|
|
|
- return true;
|
|
|
+ return args[1] == null;
|
|
|
}
|
|
|
- catch(Exception ex)
|
|
|
+ else if(args[1] == null)
|
|
|
{
|
|
|
- return false;
|
|
|
+ return args[0] == null;
|
|
|
}
|
|
|
+ else if(args[1] instanceof Number && args[0] instanceof Number)
|
|
|
+ {
|
|
|
+ return ((Number) args[0]).doubleValue() == ((Number) args[1]).doubleValue();
|
|
|
+ }
|
|
|
+ return args[0].equals(args[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void split(Object[] args, Script sc)
|
|
|
+ {
|
|
|
+ String[] parts = ScriptUtils.connect(args, 2).split(args[1].toString());
|
|
|
+ ArrayList<Object> list = new ArrayList<>();
|
|
|
+ for(String s : parts)
|
|
|
+ {
|
|
|
+ list.add(Code.convertInput(s));
|
|
|
+ }
|
|
|
+ sc.setVar(args[0].toString(), list);
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+ // Int-Handler, weil die Objekte double immer zu Double konvertieren
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+
|
|
|
+ private static int getInt(Object o)
|
|
|
+ {
|
|
|
+ return ((Double) o).intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+ // Zeit-Handler
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+
|
|
|
+ private static long getNextDay(Object[] args)
|
|
|
+ {
|
|
|
+ GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
+ cal.setTimeInMillis((long) args[0]);
|
|
|
+ cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ cal.set(Calendar.HOUR, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ return cal.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getYear(Object[] args)
|
|
|
+ {
|
|
|
+ GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
+ cal.setTimeInMillis((long) args[0]);
|
|
|
+ return cal.get(Calendar.YEAR);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getMonth(Object[] args)
|
|
|
+ {
|
|
|
+ GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
+ cal.setTimeInMillis((long) args[0]);
|
|
|
+ return cal.get(Calendar.MONTH) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getDay(Object[] args)
|
|
|
+ {
|
|
|
+ GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
+ cal.setTimeInMillis((long) args[0]);
|
|
|
+ return cal.get(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getHour(Object[] args)
|
|
|
+ {
|
|
|
+ GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
+ cal.setTimeInMillis((long) args[0]);
|
|
|
+ return cal.get(Calendar.HOUR_OF_DAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getMinute(Object[] args)
|
|
|
+ {
|
|
|
+ GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
+ cal.setTimeInMillis((long) args[0]);
|
|
|
+ return cal.get(Calendar.MINUTE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getSecond(Object[] args)
|
|
|
+ {
|
|
|
+ GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
+ cal.setTimeInMillis((long) args[0]);
|
|
|
+ return cal.get(Calendar.SECOND);
|
|
|
+ }
|
|
|
+}
|