|
@@ -1,77 +1,99 @@
|
|
|
package me.hammerle.code;
|
|
|
|
|
|
-import java.awt.Color;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
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.Collection;
|
|
|
import java.util.Collections;
|
|
|
import java.util.GregorianCalendar;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.function.BiFunction;
|
|
|
import java.util.function.BiConsumer;
|
|
|
+import java.util.function.Consumer;
|
|
|
import java.util.stream.Collectors;
|
|
|
-import me.hammerle.console.ConsoleUtils;
|
|
|
-import me.hammerle.graphics.*;
|
|
|
-import me.hammerle.scheduler.SnuviScheduler;
|
|
|
+import me.hammerle.exceptions.PrescriptException;
|
|
|
+import me.hammerle.exceptions.SnuviException;
|
|
|
|
|
|
-public class CodeParser
|
|
|
+public class SnuviParser
|
|
|
{
|
|
|
- private final static HashMap<String, BiFunction<Object[], Script, Object>> METHODS = new HashMap<>();
|
|
|
+ protected final ISnuviLogger logger;
|
|
|
+ private final ISnuviScheduler scheduler;
|
|
|
+ private final HashMap<String, BiFunction<Object[], Script, Object>> functions;
|
|
|
+ private boolean printStack;
|
|
|
|
|
|
- private static void registerFunction(String s, BiFunction<Object[], Script, Object> f)
|
|
|
+ private int idCounter;
|
|
|
+ private final HashMap<Integer, Script> scripts;
|
|
|
+ private final ArrayList<Integer> termQueue;
|
|
|
+
|
|
|
+ public SnuviParser(ISnuviLogger logger, ISnuviScheduler scheduler)
|
|
|
+ {
|
|
|
+ this.logger = logger;
|
|
|
+ this.scheduler = scheduler;
|
|
|
+ functions = new HashMap<>();
|
|
|
+ registerStandardLibraries();
|
|
|
+
|
|
|
+ printStack = false;
|
|
|
+
|
|
|
+ scripts = new HashMap<>();
|
|
|
+ termQueue = new ArrayList<>();
|
|
|
+ idCounter = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+ // function registry
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+
|
|
|
+ public boolean isRegisteredFunction(String s)
|
|
|
{
|
|
|
- METHODS.put(s, f);
|
|
|
+ return functions.containsKey(s);
|
|
|
}
|
|
|
|
|
|
- private static void registerConsumer(String s, BiConsumer<Object[], Script> f)
|
|
|
+ public void registerFunction(String s, BiFunction<Object[], Script, Object> f)
|
|
|
{
|
|
|
- METHODS.put(s, (BiFunction<Object[], Script, Object>) (args, sc) ->
|
|
|
+ functions.put(s, f);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void registerConsumer(String s, BiConsumer<Object[], Script> f)
|
|
|
+ {
|
|
|
+ functions.put(s, (BiFunction<Object[], Script, Object>) (args, sc) ->
|
|
|
{
|
|
|
f.accept(args, sc);
|
|
|
return Void.TYPE;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private static void registerAlias(String s, String original)
|
|
|
+ public void registerAlias(String alias, String original)
|
|
|
{
|
|
|
- registerFunction(s, METHODS.get(original));
|
|
|
+ BiFunction<Object[], Script, Object> f = functions.get(original);
|
|
|
+ if(f == null)
|
|
|
+ {
|
|
|
+ throw new IllegalArgumentException("registering alias for non existent function");
|
|
|
+ }
|
|
|
+ registerFunction(alias, functions.get(original));
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static void initCodeParser()
|
|
|
+ private void registerStandardLibraries()
|
|
|
{
|
|
|
- registerConsumer("test", (args, sc) ->
|
|
|
- test(args, sc));
|
|
|
registerFunction("nothing", (args, sc) ->
|
|
|
0);
|
|
|
registerFunction("", (args, sc) ->
|
|
|
args[0]);
|
|
|
registerConsumer("error", (args, sc) ->
|
|
|
- printStack = !printStack);
|
|
|
+ printStack = !printStack);
|
|
|
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Script-Consolen-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
- registerConsumer("script.list", (args, sc) ->
|
|
|
- { ScriptUtils.print("The following scripts are active:"); ScriptControl.getScripts().forEach(s -> ScriptUtils.print(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
|
|
|
- // -------------------------------------------------------------
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
+ // event
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
registerConsumer("event.load", (args, sc) ->
|
|
|
sc.loadEvent(args[0].toString()));
|
|
|
registerConsumer("event.unload", (args, sc) ->
|
|
@@ -79,9 +101,9 @@ public class CodeParser
|
|
|
registerFunction("event.isloaded", (args, sc) ->
|
|
|
sc.isLoadedEvent(args[0].toString()));
|
|
|
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Mathe-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
+ // math
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
registerFunction("math.mod", (args, sc) ->
|
|
|
((double) args[0]) % ((double) args[1]));
|
|
|
registerFunction("math.abs", (args, sc) ->
|
|
@@ -121,9 +143,9 @@ public class CodeParser
|
|
|
registerFunction("math.roundcomma", (args, sc) ->
|
|
|
new BigDecimal(((double) args[0])).setScale(getInt(args[1]), RoundingMode.HALF_UP).doubleValue());
|
|
|
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Listen-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
+ // lists
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
registerConsumer("list.new", (args, sc) ->
|
|
|
sc.setVar(args[0].toString(), new ArrayList<>()));
|
|
|
registerFunction("list.exists", (args, sc) ->
|
|
@@ -153,13 +175,13 @@ public class CodeParser
|
|
|
registerConsumer("list.shuffle", (args, sc) ->
|
|
|
Collections.shuffle((List<Object>) args[0]));
|
|
|
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Map-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
+ // maps
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
registerConsumer("map.new", (args, sc) ->
|
|
|
sc.setVar(args[0].toString(), new HashMap<>()));
|
|
|
registerFunction("map.exists", (args, sc) ->
|
|
|
- args[0] instanceof HashMap);
|
|
|
+ args[0] instanceof Map);
|
|
|
registerConsumer("map.add", (args, sc) ->
|
|
|
((HashMap) args[0]).put(args[1], args[2]));
|
|
|
registerConsumer("map.remove", (args, sc) ->
|
|
@@ -173,13 +195,13 @@ public class CodeParser
|
|
|
registerConsumer("map.clear", (args, sc) ->
|
|
|
((HashMap) args[0]).clear());
|
|
|
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Set-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
+ // sets
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
registerConsumer("set.new", (args, sc) ->
|
|
|
sc.setVar(args[0].toString(), new HashSet<>()));
|
|
|
registerFunction("set.exists", (args, sc) ->
|
|
|
- args[0] instanceof HashSet);
|
|
|
+ args[0] instanceof Set);
|
|
|
registerConsumer("set.add", (args, sc) ->
|
|
|
((HashSet) args[0]).add(args[1]));
|
|
|
registerConsumer("set.remove", (args, sc) ->
|
|
@@ -189,9 +211,9 @@ public class CodeParser
|
|
|
registerFunction("set.getsize", (args, sc) ->
|
|
|
((HashSet) args[0]).size());
|
|
|
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Time-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
+ // time
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
registerFunction("time.get", (args, sc) ->
|
|
|
(double) System.currentTimeMillis());
|
|
|
registerFunction("time.nextday", (args, sc) ->
|
|
@@ -208,48 +230,10 @@ public class CodeParser
|
|
|
(double) getMinute(args));
|
|
|
registerFunction("time.getsecond", (args, sc) ->
|
|
|
(double) getSecond(args));
|
|
|
-
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Graphic-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
- registerConsumer("g.new", (args, sc) ->
|
|
|
- sc.newFrame(getInt(args[0]), getInt(args[1])));
|
|
|
- registerFunction("g.get", (args, sc) ->
|
|
|
- sc.getGraphicPanel().getGraphic(getInt(args[0])));
|
|
|
- registerConsumer("g.repaint", (args, sc) ->
|
|
|
- sc.getGraphicPanel().repaint());
|
|
|
- registerConsumer("g.newpoint", (args, sc) ->
|
|
|
- newPoint(args, sc));
|
|
|
- registerConsumer("g.newline", (args, sc) ->
|
|
|
- newLine(args, sc));
|
|
|
- registerConsumer("g.newoval", (args, sc) ->
|
|
|
- newOval(args, sc));
|
|
|
- registerConsumer("g.newrectangle", (args, sc) ->
|
|
|
- newRectangle(args, sc));
|
|
|
- registerConsumer("g.translate", (args, sc) ->
|
|
|
- ((Graphic) args[0]).translate(getInt(args[1]), getInt(args[2])));
|
|
|
- registerConsumer("g.setcolor", (args, sc) ->
|
|
|
- ((Colorable) args[0]).setColor((Color) args[1]));
|
|
|
- registerConsumer("g.setfilled", (args, sc) ->
|
|
|
- ((Colorable) args[0]).setFilled((boolean) args[1]));
|
|
|
- registerConsumer("g.setx1", (args, sc) ->
|
|
|
- ((Line) args[0]).setFirstX(getInt(args[1])));
|
|
|
- registerConsumer("g.setx2", (args, sc) ->
|
|
|
- ((Line) args[0]).setSecondX(getInt(args[1])));
|
|
|
- registerConsumer("g.sety1", (args, sc) ->
|
|
|
- ((Line) args[0]).setFirstY(getInt(args[1])));
|
|
|
- registerConsumer("g.sety2", (args, sc) ->
|
|
|
- ((Line) args[0]).setSecondY(getInt(args[1])));
|
|
|
-
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Read-Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
- registerFunction("read.color", (args, sc) ->
|
|
|
- getColor(args, sc));
|
|
|
-
|
|
|
- // -------------------------------------------------------------
|
|
|
- // Ohne Bibliothek
|
|
|
- // -------------------------------------------------------------
|
|
|
+
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
+ // commands without library
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
registerFunction("add", (args, sc) ->
|
|
|
Arrays.stream(args).mapToDouble(s -> (double) s).sum());
|
|
|
registerFunction("sub", (args, sc) ->
|
|
@@ -268,10 +252,6 @@ public class CodeParser
|
|
|
sc.setVar(args[0].toString(), args[1]));
|
|
|
registerConsumer("removevar", (args, sc) ->
|
|
|
sc.removeVar(args[0].toString()));
|
|
|
- registerConsumer("debug", (args, sc) ->
|
|
|
- ScriptUtils.printDebug(ScriptUtils.connect(args, 0)));
|
|
|
- registerConsumer("print", (args, sc) ->
|
|
|
- print(args, sc));
|
|
|
registerConsumer("reset", (args, sc) ->
|
|
|
sc.resetLoopCounter());
|
|
|
registerConsumer("wait", (args, sc) ->
|
|
@@ -327,28 +307,23 @@ public class CodeParser
|
|
|
ScriptUtils.connect(args, 0).toUpperCase());
|
|
|
registerConsumer("waitfor", (args, sc) ->
|
|
|
waitFor(args, sc));
|
|
|
+ registerConsumer("term", (args, sc) ->
|
|
|
+ { termSafe(sc); throw new HoldCodeException(); });
|
|
|
}
|
|
|
|
|
|
- public static boolean printStack = false;
|
|
|
-
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static Object parseFunction(Script sc, String function, Object[] args) throws HoldCodeException
|
|
|
+ protected 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);
|
|
|
+ return functions.get(function).apply(args, sc);
|
|
|
+ }
|
|
|
+ catch(HoldCodeException ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
}
|
|
|
catch(Exception ex)
|
|
|
{
|
|
|
- if(ex instanceof HoldCodeException)
|
|
|
- {
|
|
|
- throw ex;
|
|
|
- }
|
|
|
if(printStack)
|
|
|
{
|
|
|
ex.printStackTrace();
|
|
@@ -360,85 +335,161 @@ public class CodeParser
|
|
|
sc.resetTryJumpLine();
|
|
|
return Void.TYPE;
|
|
|
}
|
|
|
- printQuestException(sc, ex, function + "(" + Arrays.stream(args).map(o -> String.valueOf(o)).collect(Collectors.joining(", ")) + ")");
|
|
|
+ if(ex instanceof SnuviException)
|
|
|
+ {
|
|
|
+ logger.printException((SnuviException) ex);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ logger.printException(new SnuviException(ex, sc));
|
|
|
+ }
|
|
|
+ sc.resetLoopCounter();
|
|
|
throw new HoldCodeException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void printQuestException(Script sc, Exception ex, String line)
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+ // script controller
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+
|
|
|
+ public Script getScript(int id)
|
|
|
{
|
|
|
- sc.resetLoopCounter();
|
|
|
- ScriptUtils.printError("Error in");
|
|
|
- ScriptUtils.printErrorList("script:", sc.getName());
|
|
|
- ScriptUtils.printErrorList("line:", line);
|
|
|
- ScriptUtils.printErrorList("line number:", sc.getActiveCodeLine());
|
|
|
- if(ex.getLocalizedMessage() == null)
|
|
|
+ return scripts.get(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void termSafe(Script sc)
|
|
|
+ {
|
|
|
+ if(sc == null)
|
|
|
{
|
|
|
- ScriptUtils.printErrorList("exception:", ex.getClass().getSimpleName());
|
|
|
+ return;
|
|
|
}
|
|
|
- else
|
|
|
+ sc.setInvalid();
|
|
|
+ termQueue.add(sc.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void term()
|
|
|
+ {
|
|
|
+ if(!termQueue.isEmpty())
|
|
|
{
|
|
|
- ScriptUtils.printErrorList("exception:", ex.getClass().getSimpleName() + " - " + ex.getLocalizedMessage());
|
|
|
+ termQueue.forEach(i -> scripts.remove(i));
|
|
|
+ termQueue.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void termAllUnsafe()
|
|
|
+ {
|
|
|
+ scripts.values().forEach(sc -> sc.setInvalid());
|
|
|
+ scripts.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Collection<Script> getScripts()
|
|
|
+ {
|
|
|
+ return scripts.values();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Script startScript(Script script, Object... o)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ script.initExpansion(o);
|
|
|
+ scripts.put(idCounter, script);
|
|
|
+ idCounter++;
|
|
|
+ script.runCode();
|
|
|
+ term();
|
|
|
+ return script;
|
|
|
}
|
|
|
- if(ex instanceof IllegalStringException)
|
|
|
+ catch(PrescriptException ex)
|
|
|
{
|
|
|
- ScriptUtils.printErrorList("illegal value:", ((IllegalStringException) ex).getBadString());
|
|
|
+ logger.printException(ex);
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // -----------------------------------------------------------------------------------
|
|
|
- // Functions
|
|
|
- // -----------------------------------------------------------------------------------
|
|
|
+ public Script startScript(String scriptName, String code)
|
|
|
+ {
|
|
|
+ return startScript(new Script(this, idCounter, scriptName, code));
|
|
|
+ }
|
|
|
|
|
|
- private static void termScript(Object[] args, Script sc)
|
|
|
- {
|
|
|
- if(args.length == 0)
|
|
|
+ public Script startScript(Class<? extends Script> c, String scriptName, String code, boolean b, Object... o)
|
|
|
+ {
|
|
|
+ try
|
|
|
{
|
|
|
- ScriptControl.term(sc);
|
|
|
- throw new HoldCodeException();
|
|
|
+ return startScript(c.getDeclaredConstructor(
|
|
|
+ SnuviParser.class, int.class, String.class, String.class, boolean.class)
|
|
|
+ .newInstance(this, idCounter, scriptName, code, b), o);
|
|
|
}
|
|
|
- else if(args[0].toString().equals("all"))
|
|
|
+ catch(IllegalAccessException | IllegalArgumentException | InstantiationException |
|
|
|
+ InvocationTargetException | NoSuchMethodException | SecurityException ex)
|
|
|
{
|
|
|
- ScriptControl.termScripts();
|
|
|
- throw new HoldCodeException();
|
|
|
+ return null;
|
|
|
}
|
|
|
- else
|
|
|
+ }
|
|
|
+
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+ // event
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+
|
|
|
+ public void callEvent(String name, Consumer<Script> before, Consumer<Script> after)
|
|
|
+ {
|
|
|
+ scripts.values().stream()
|
|
|
+ .filter(script -> script.receiveEventBroadcast)
|
|
|
+ .filter(script -> script.isLoadedEvent(name))
|
|
|
+ .forEach(sc ->
|
|
|
+ {
|
|
|
+ sc.setVar("event", name);
|
|
|
+ before.accept(sc);
|
|
|
+ sc.runCode();
|
|
|
+ after.accept(sc);
|
|
|
+ sc.eventVars.forEach(s -> sc.removeVar(s));
|
|
|
+ sc.eventVars.clear();
|
|
|
+ });
|
|
|
+ term();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callEvent(String name, Script sc, Consumer<Script> before, Consumer<Script> after)
|
|
|
+ {
|
|
|
+ if(sc.isLoadedEvent(name))
|
|
|
{
|
|
|
- int i = getInt(args[0]);
|
|
|
- ScriptControl.term(ScriptControl.getScript(i));
|
|
|
- if(sc.getId() == i)
|
|
|
- {
|
|
|
- throw new HoldCodeException();
|
|
|
- }
|
|
|
+ sc.setVar("event", name);
|
|
|
+ before.accept(sc);
|
|
|
+ sc.runCode();
|
|
|
+ after.accept(sc);
|
|
|
+ sc.eventVars.forEach(s -> sc.removeVar(s));
|
|
|
+ sc.eventVars.clear();
|
|
|
+ term();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static void ifFunction(Object[] args, Script sc)
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+ // functions
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+
|
|
|
+ private void ifFunction(Object[] args, Script sc)
|
|
|
{
|
|
|
- if(Arrays.stream(args).anyMatch(s -> (boolean) s == false))
|
|
|
+ if(Arrays.stream(args).anyMatch(s -> !((boolean) s)))
|
|
|
{
|
|
|
sc.gotoSpecialJumpLine();
|
|
|
sc.jumpNextIfElse();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static void whileFunction(Object[] args, Script sc)
|
|
|
+ private void whileFunction(Object[] args, Script sc)
|
|
|
{
|
|
|
- if(Arrays.stream(args).anyMatch(s -> (boolean) s == false))
|
|
|
+ if(Arrays.stream(args).anyMatch(s -> !((boolean) s)))
|
|
|
{
|
|
|
sc.gotoSpecialJumpLine(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @SuppressWarnings(value = "unchecked")
|
|
|
- private static void sortList(List<Object> args, Script sc)
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private void sortList(List<Object> args, Script sc)
|
|
|
{
|
|
|
Collections.sort(args, (Object o1, Object o2) -> ((Comparable) o1).compareTo(o2));
|
|
|
}
|
|
|
|
|
|
- private static void scheduleGoto(Object[] args, Script sc)
|
|
|
+ private void scheduleGoto(Object[] args, Script sc)
|
|
|
{
|
|
|
- SnuviScheduler.doScheduledTask(() ->
|
|
|
+ scheduler.scheduleTask(() ->
|
|
|
{
|
|
|
if(!sc.isValid())
|
|
|
{
|
|
@@ -452,21 +503,22 @@ public class CodeParser
|
|
|
}
|
|
|
catch(Exception ex)
|
|
|
{
|
|
|
- printQuestException(sc, ex, "(Scheduled Goto)");
|
|
|
+ logger.printException(new SnuviException(ex, sc.getName(), "scheduled goto"));
|
|
|
}
|
|
|
}, getInt(args[0]));
|
|
|
}
|
|
|
|
|
|
- private static void waitFor(Object[] args, Script sc) throws UnsupportedOperationException
|
|
|
+ @SuppressWarnings("")
|
|
|
+ private void waitFor(Object[] args, Script sc) throws UnsupportedOperationException
|
|
|
{
|
|
|
sc.resetLoopCounter();
|
|
|
- int i = getInt(args[0]);
|
|
|
- if(i < 1)
|
|
|
+ long l = getLong(args[0]);
|
|
|
+ if(l < 0)
|
|
|
{
|
|
|
- throw new UnsupportedOperationException();
|
|
|
+ throw new IllegalArgumentException("time units can't be negative");
|
|
|
}
|
|
|
sc.setHalt(true);
|
|
|
- SnuviScheduler.doScheduledTask(() ->
|
|
|
+ scheduler.scheduleTask(() ->
|
|
|
{
|
|
|
if(sc == null || !sc.isValid())
|
|
|
{
|
|
@@ -474,10 +526,10 @@ public class CodeParser
|
|
|
}
|
|
|
sc.setHalt(false);
|
|
|
sc.runCode();
|
|
|
- }, i);
|
|
|
+ }, l);
|
|
|
}
|
|
|
|
|
|
- private static Number increaseVar(Object var, Script sc, int value)
|
|
|
+ private Number increaseVar(Object var, Script sc, int value)
|
|
|
{
|
|
|
double n = ((double) sc.getVar(var.toString())) + value;
|
|
|
sc.setVar(var.toString(), n);
|
|
@@ -492,8 +544,9 @@ public class CodeParser
|
|
|
}
|
|
|
else if(args[1] == null)
|
|
|
{
|
|
|
- return args[0] == null;
|
|
|
+ return false;
|
|
|
}
|
|
|
+ // this is needed for comparing different number types (e.g. Integer and Double)
|
|
|
else if(args[1] instanceof Number && args[0] instanceof Number)
|
|
|
{
|
|
|
return ((Number) args[0]).doubleValue() == ((Number) args[1]).doubleValue();
|
|
@@ -501,108 +554,32 @@ public class CodeParser
|
|
|
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);
|
|
|
- }
|
|
|
-
|
|
|
- private static void print(Object[] args, Script sc)
|
|
|
+ private void split(Object[] args, Script sc)
|
|
|
{
|
|
|
- if(args.length == 0)
|
|
|
- {
|
|
|
- ScriptUtils.print("");
|
|
|
- }
|
|
|
- else if(args[0] instanceof Color)
|
|
|
- {
|
|
|
- ConsoleUtils.sendMessage(ScriptUtils.connect(args, 1), (Color) args[0]);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- ScriptUtils.print(ScriptUtils.connect(args, 0));
|
|
|
- }
|
|
|
+ sc.setVar(args[0].toString(),
|
|
|
+ Arrays.stream(ScriptUtils.connect(args, 2).split(args[1].toString()))
|
|
|
+ .map(s -> Code.convertInput(s)).collect(Collectors.toList()));
|
|
|
}
|
|
|
|
|
|
- // -------------------------------------------------------------------------
|
|
|
- // Read-Handler
|
|
|
- // -------------------------------------------------------------------------
|
|
|
-
|
|
|
- private static Color getColor(Object[] args, Script sc)
|
|
|
- {
|
|
|
- int r = getInt(args[0]);
|
|
|
- int g = getInt(args[1]);
|
|
|
- int b = getInt(args[2]);
|
|
|
- int a;
|
|
|
- if(args.length >= 4)
|
|
|
- {
|
|
|
- a = getInt(args[3]);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- a = 255;
|
|
|
- }
|
|
|
- return new Color(r, g, b, a);
|
|
|
- }
|
|
|
-
|
|
|
- // -------------------------------------------------------------------------
|
|
|
- // Graphics
|
|
|
- // -------------------------------------------------------------------------
|
|
|
-
|
|
|
- private static void newLine(Object[] args, Script sc)
|
|
|
- {
|
|
|
- sc.getGraphicPanel().addGraphic(getInt(args[0]), new Line((Color) args[1], getInt(args[2]), getInt(args[3]), getInt(args[4]), getInt(args[5])));
|
|
|
- }
|
|
|
-
|
|
|
- private static void newPoint(Object[] args, Script sc)
|
|
|
- {
|
|
|
- sc.getGraphicPanel().addGraphic(getInt(args[0]), new Point((Color) args[1], getInt(args[2]), getInt(args[3])));
|
|
|
- }
|
|
|
-
|
|
|
- private static void newOval(Object[] args, Script sc)
|
|
|
- {
|
|
|
- Oval oval = new Oval((Color) args[1], getInt(args[2]), getInt(args[3]), getInt(args[4]), getInt(args[5]));
|
|
|
- if(args.length >= 7)
|
|
|
- {
|
|
|
- oval.setFilled((boolean) args[6]);
|
|
|
- }
|
|
|
- sc.getGraphicPanel().addGraphic(getInt(args[0]), oval);
|
|
|
- }
|
|
|
-
|
|
|
- private static void newRectangle(Object[] args, Script sc)
|
|
|
- {
|
|
|
- Rectangle rec = new Rectangle((Color) args[1], getInt(args[2]), getInt(args[3]), getInt(args[4]), getInt(args[5]));
|
|
|
- if(args.length >= 7)
|
|
|
- {
|
|
|
- rec.setFilled((boolean) args[6]);
|
|
|
- }
|
|
|
- sc.getGraphicPanel().addGraphic(getInt(args[0]), rec);
|
|
|
- }
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+ // number handlers, cause primitive types transform into their classes all the time
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
|
|
|
- private static void test(Object[] args, Script sc)
|
|
|
+ private int getInt(Object o)
|
|
|
{
|
|
|
- //sc.getGraphicPanel().objects.clear();
|
|
|
- //sc.getGraphicPanel().getGraphics().drawLine(20, 20, 30, 30);
|
|
|
+ return ((Number) o).intValue();
|
|
|
}
|
|
|
|
|
|
- // -------------------------------------------------------------------------
|
|
|
- // Int-Handler, weil die Objekte double immer zu Double konvertieren
|
|
|
- // -------------------------------------------------------------------------
|
|
|
-
|
|
|
- private static int getInt(Object o)
|
|
|
+ private long getLong(Object o)
|
|
|
{
|
|
|
- return ((Number) o).intValue();
|
|
|
+ return ((Number) o).longValue();
|
|
|
}
|
|
|
|
|
|
- // -------------------------------------------------------------------------
|
|
|
- // Zeit-Handler
|
|
|
- // -------------------------------------------------------------------------
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
+ // time handler
|
|
|
+ // -----------------------------------------------------------------------------------
|
|
|
|
|
|
- private static long getNextDay(Object[] args)
|
|
|
+ private long getNextDay(Object[] args)
|
|
|
{
|
|
|
GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
cal.setTimeInMillis((long) args[0]);
|
|
@@ -614,42 +591,42 @@ public class CodeParser
|
|
|
return cal.getTimeInMillis();
|
|
|
}
|
|
|
|
|
|
- private static int getYear(Object[] args)
|
|
|
+ private 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)
|
|
|
+ private 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)
|
|
|
+ private 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)
|
|
|
+ private 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)
|
|
|
+ private 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)
|
|
|
+ private int getSecond(Object[] args)
|
|
|
{
|
|
|
GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
|
|
|
cal.setTimeInMillis((long) args[0]);
|