FunctionRegistry.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package me.hammerle.snuviscript.code;
  2. import me.hammerle.snuviscript.inputprovider.InputProvider;
  3. import java.io.File;
  4. import java.nio.charset.StandardCharsets;
  5. import java.lang.reflect.Array;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.time.ZonedDateTime;
  9. import java.util.List;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Collections;
  13. import java.util.Calendar;
  14. import java.util.GregorianCalendar;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.HashSet;
  18. import java.util.Iterator;
  19. import java.util.Objects;
  20. import java.util.Set;
  21. import java.util.stream.Collectors;
  22. import me.hammerle.snuviscript.config.SnuviConfig;
  23. import me.hammerle.snuviscript.inputprovider.Variable;
  24. public class FunctionRegistry {
  25. private static final HashMap<String, Object> GLOBAL_VARS = new HashMap<>();
  26. private static final HashMap<String, NamedFunction> FUNCTIONS = new HashMap<>();
  27. protected static void registerFunction(String name, ExceptionBiFunction<Script, InputProvider[], Object> f) {
  28. FUNCTIONS.put(name, new NamedFunction(name, f));
  29. }
  30. protected static void registerConsumer(String name, ExceptionBiConsumer<Script, InputProvider[]> f) {
  31. FUNCTIONS.put(name, new NamedFunction(name, (sc, in) -> {
  32. f.apply(sc, in);
  33. return Void.TYPE;
  34. }));
  35. }
  36. protected static void registerAlias(String original, String alias) {
  37. FUNCTIONS.put(alias, FUNCTIONS.get(original));
  38. }
  39. public static NamedFunction getFunction(String f) {
  40. final String function = f.toLowerCase();
  41. return FUNCTIONS.getOrDefault(function, new NamedFunction(function, (sc, in) -> {
  42. sc.handleFunction(function, in);
  43. return Void.TYPE;
  44. }));
  45. }
  46. static {
  47. registerFunction("nothing", (sc, in) -> Void.TYPE);
  48. registerConsumer("error", (sc, in) -> sc.setStackTrace(in[0].getBoolean(sc)));
  49. registerConsumer("event.load", (sc, in) -> {
  50. String event = in[0].getString(sc);
  51. sc.loadEvent(event);
  52. sc.getScriptManager().loadEvent(event, sc);
  53. });
  54. registerConsumer("event.unload", (sc, in) -> {
  55. String event = in[0].getString(sc);
  56. sc.unloadEvent(in[0].getString(sc));
  57. sc.getScriptManager().unloadEvent(event, sc);
  58. });
  59. registerFunction("event.isloaded", (sc, in) -> sc.isEventLoaded(in[0].getString(sc)));
  60. registerFunction("script.get", (sc, in) -> {
  61. if(in.length == 0) {
  62. return sc;
  63. }
  64. String name = in[0].getString(sc);
  65. for(Script script : sc.getScriptManager().getScripts()) {
  66. if(script.getName().equals(name)) {
  67. return script;
  68. }
  69. }
  70. return null;
  71. });
  72. registerFunction("script.getfromid", (sc, in) -> sc.getScriptManager().getScript(in[0].getInt(sc)));
  73. registerFunction("script.getid", (sc, in) -> (double) ((Script) in[0].get(sc)).getId());
  74. registerFunction("script.getvar", (sc, in) -> {
  75. Script other = (Script) in[0].get(sc);
  76. Variable v = other.getVar(in[1].getString(sc));
  77. if(v == null) {
  78. return null;
  79. }
  80. return v.get(other);
  81. });
  82. registerConsumer("script.setvar", (sc, in) -> {
  83. Script other = (Script) in[0].get(sc);
  84. other.getVar(in[1].getString(sc)).set(other, in[2].get(sc));
  85. });
  86. registerFunction("script.getall", (sc, in) -> {
  87. String name = in[0].getString(sc);
  88. return sc.getScriptManager().getScripts().stream()
  89. .filter(script -> script.getName().equals(name))
  90. .collect(Collectors.toList());
  91. });
  92. registerConsumer("script.term", (sc, in) -> {
  93. Script other = (Script) in[0].get(sc);
  94. other.term();
  95. sc.getScriptManager().removeScript(other);
  96. });
  97. registerFunction(">>", (sc, in) -> (double) (in[0].getInt(sc) >> in[1].getInt(sc)));
  98. registerFunction("<<", (sc, in) -> (double) (in[0].getInt(sc) << in[1].getInt(sc)));
  99. registerFunction("&", (sc, in) -> (double) (in[0].getInt(sc) & in[1].getInt(sc)));
  100. registerFunction("|", (sc, in) -> (double) (in[0].getInt(sc) | in[1].getInt(sc)));
  101. registerFunction("^", (sc, in) -> (double) (in[0].getInt(sc) ^ in[1].getInt(sc)));
  102. registerFunction("~", (sc, in) -> (double) (~in[0].getInt(sc)));
  103. registerFunction("bit.set", (sc, in) -> (double) (in[0].getInt(sc) | (1 << (in[1].getInt(sc)))));
  104. registerFunction("bit.unset", (sc, in) -> (double) (in[0].getInt(sc) & (~(1 << (in[1].getInt(sc))))));
  105. registerFunction("bit.get", (sc, in) -> (in[0].getInt(sc) & (1 << (in[1].getInt(sc)))) != 0);
  106. registerFunction("%", (sc, in) -> (double) (in[0].getInt(sc) % in[1].getInt(sc)));
  107. registerAlias("%", "math.mod");
  108. registerFunction("math.abs", (sc, in) -> Math.abs(in[0].getDouble(sc)));
  109. registerFunction("math.pow", (sc, in) -> Math.pow(in[0].getDouble(sc), in[1].getDouble(sc)));
  110. registerFunction("math.root", (sc, in) -> Math.pow(in[0].getDouble(sc), 1.0 / in[1].getDouble(sc)));
  111. registerFunction("math.sqrt", (sc, in) -> Math.sqrt(in[0].getDouble(sc)));
  112. registerFunction("math.hypot", (sc, in) -> Math.hypot(in[0].getDouble(sc), in[1].getDouble(sc)));
  113. registerFunction("math.sin", (sc, in) -> Math.sin(in[0].getDouble(sc)));
  114. registerFunction("math.cos", (sc, in) -> Math.cos(in[0].getDouble(sc)));
  115. registerFunction("math.tan", (sc, in) -> Math.tan(in[0].getDouble(sc)));
  116. registerFunction("math.asin", (sc, in) -> Math.asin(in[0].getDouble(sc)));
  117. registerFunction("math.acos", (sc, in) -> Math.acos(in[0].getDouble(sc)));
  118. registerFunction("math.atan", (sc, in) -> Math.atan(in[0].getDouble(sc)));
  119. registerFunction("math.e", (sc, in) -> Math.E);
  120. registerFunction("math.pi", (sc, in) -> Math.PI);
  121. registerFunction("math.ln", (sc, in) -> Math.log(in[0].getDouble(sc)));
  122. registerFunction("math.log", (sc, in) -> Math.log10(in[0].getDouble(sc)));
  123. registerFunction("math.random", (sc, in) -> (double) SnuviUtils.randomInt(in[0].getInt(sc), in[1].getInt(sc)));
  124. registerFunction("math.round", (sc, in) -> (double) Math.round(in[0].getDouble(sc)));
  125. registerFunction("math.rounddown", (sc, in) -> Math.floor(in[0].getDouble(sc)));
  126. registerFunction("math.roundup", (sc, in) -> Math.ceil(in[0].getDouble(sc)));
  127. registerFunction("math.roundcomma", (sc, in) -> {
  128. double d = in[0].getDouble(sc);
  129. int factor = (int) Math.pow(10, in[1].getInt(sc));
  130. return (double) (((double) Math.round(d * factor)) / factor);
  131. });
  132. registerFunction("math.min", (sc, in) -> Math.min(in[0].getDouble(sc), in[1].getDouble(sc)));
  133. registerFunction("math.max", (sc, in) -> Math.max(in[0].getDouble(sc), in[1].getDouble(sc)));
  134. registerFunction("matrix.new", (sc, in) -> new Matrix());
  135. registerFunction("matrix.newrotationy", (sc, in) -> Matrix.getRotationY(in[0].getDouble(sc)));
  136. registerFunction("matrix.newrotationx", (sc, in) -> Matrix.getRotationX(in[0].getDouble(sc)));
  137. registerFunction("matrix.mul", (sc, in) -> ((Matrix) in[0].get(sc)).mul((Matrix) in[1].get(sc)));
  138. registerConsumer("matrix.mulvector", (sc, in) -> ((Matrix) in[0].get(sc)).mul((Vector) in[1].get(sc)));
  139. registerFunction("vector.new", (sc, in) -> new Vector(in[0].getDouble(sc), in[1].getDouble(sc), in[2].getDouble(sc)));
  140. registerConsumer("vector.set", (sc, in) -> ((Vector) in[0].get(sc)).set(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
  141. registerFunction("vector.getx", (sc, in) -> ((Vector) in[0].get(sc)).getX());
  142. registerFunction("vector.gety", (sc, in) -> ((Vector) in[0].get(sc)).getY());
  143. registerFunction("vector.getz", (sc, in) -> ((Vector) in[0].get(sc)).getZ());
  144. registerFunction("list.new", (sc, in) -> new ArrayList<>());
  145. registerFunction("list.exists", (sc, in) -> in[0].get(sc) instanceof List);
  146. registerFunction("list.add", (sc, in) -> ((List) in[0].get(sc)).add(in[1].get(sc)));
  147. registerConsumer("list.addall", (sc, in) -> {
  148. List list = ((List) in[0].get(sc));
  149. for(int i = 1; i < in.length; i++) {
  150. list.add(in[i].get(sc));
  151. }
  152. });
  153. registerFunction("list.remove", (sc, in) -> ((List) in[0].get(sc)).remove(in[1].get(sc)));
  154. registerFunction("list.removeindex", (sc, in) -> ((List) in[0].get(sc)).remove(in[1].getInt(sc)));
  155. registerFunction("list.contains", (sc, in) -> ((List) in[0].get(sc)).contains(in[1].get(sc)));
  156. registerFunction("list.getsize", (sc, in) -> (double) ((List) in[0].get(sc)).size());
  157. registerFunction("list.getindex", (sc, in) -> ((List) in[0].get(sc)).get(in[1].getInt(sc)));
  158. registerAlias("list.getindex", "list.get");
  159. registerFunction("list.setindex", (sc, in) -> ((List) in[0].get(sc)).set(in[1].getInt(sc), in[2].get(sc)));
  160. registerConsumer("list.clear", (sc, in) -> ((List) in[0].get(sc)).clear());
  161. registerFunction("list.getindexof", (sc, in) -> (double) ((List) in[0].get(sc)).indexOf(in[1].get(sc)));
  162. registerConsumer("list.sort", (sc, in) -> {
  163. Collections.sort(((List<Object>) in[0].get(sc)), (o1, o2) -> ((Comparable) o1).compareTo(o2));
  164. });
  165. registerConsumer("list.reverse", (sc, in) -> Collections.reverse((List<Object>) in[0].get(sc)));
  166. registerConsumer("list.shuffle", (sc, in) -> Collections.shuffle((List<Object>) in[0].get(sc)));
  167. registerFunction("list.iterator", (sc, in) -> ((List) in[0].get(sc)).iterator());
  168. registerFunction("array.new", (sc, in) -> {
  169. if(in.length == 0) {
  170. throw new ArrayIndexOutOfBoundsException("missing array dimension");
  171. }
  172. int[] dim = new int[in.length];
  173. for(int i = 0; i < in.length; i++) {
  174. dim[i] = in[i].getInt(sc);
  175. }
  176. return Array.newInstance(Object.class, dim);
  177. });
  178. registerFunction("array.getsize", (sc, in) -> (double) Array.getLength(in[0].get(sc)));
  179. registerAlias("array.getsize", "array.length");
  180. registerFunction("map.new", (sc, in) -> new HashMap<>());
  181. registerFunction("map.exists", (sc, in) -> in[0].get(sc) instanceof Map);
  182. registerFunction("map.add", (sc, in) -> ((Map) in[0].get(sc)).put(in[1].get(sc), in[2].get(sc)));
  183. registerFunction("map.remove", (sc, in) -> ((Map) in[0].get(sc)).remove(in[1].get(sc)));
  184. registerFunction("map.contains", (sc, in) -> ((Map) in[0].get(sc)).containsKey(in[1].get(sc)));
  185. registerFunction("map.getsize", (sc, in) -> (double) ((Map) in[0].get(sc)).size());
  186. registerFunction("map.get", (sc, in) -> ((Map) in[0].get(sc)).get(in[1].get(sc)));
  187. registerFunction("map.getordefault", (sc, in) -> ((Map) in[0].get(sc)).getOrDefault(in[1].get(sc), in[2].get(sc)));
  188. registerConsumer("map.clear", (sc, in) -> ((Map) in[0].get(sc)).clear());
  189. registerFunction("map.iterator", (sc, in) -> ((Map) in[0].get(sc)).entrySet().iterator());
  190. registerFunction("map.getkey", (sc, in) -> ((Map.Entry) in[0].get(sc)).getKey());
  191. registerFunction("map.getvalue", (sc, in) -> ((Map.Entry) in[0].get(sc)).getValue());
  192. registerFunction("map.setvalue", (sc, in) -> ((Map.Entry) in[0].get(sc)).setValue(in[1].get(sc)));
  193. registerFunction("set.new", (sc, in) -> new HashSet<>());
  194. registerFunction("set.exists", (sc, in) -> in[0].get(sc) instanceof Set);
  195. registerFunction("set.add", (sc, in) -> ((Set) in[0].get(sc)).add(in[1].get(sc)));
  196. registerConsumer("set.addall", (sc, in) -> {
  197. Set set = ((Set) in[0].get(sc));
  198. for(int i = 1; i < in.length; i++) {
  199. set.add(in[i].get(sc));
  200. }
  201. });
  202. registerFunction("set.remove", (sc, in) -> ((Set) in[0].get(sc)).remove(in[1].get(sc)));
  203. registerFunction("set.contains", (sc, in) -> ((Set) in[0].get(sc)).contains(in[1].get(sc)));
  204. registerFunction("set.getsize", (sc, in) -> (double) ((Set) in[0].get(sc)).size());
  205. registerConsumer("set.clear", (sc, in) -> ((Set) in[0].get(sc)).clear());
  206. registerFunction("set.iterator", (sc, in) -> ((Set) in[0].get(sc)).iterator());
  207. registerFunction("time.new", (sc, in) -> {
  208. GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
  209. cal.setTimeInMillis(in[0].getLong(sc));
  210. return cal;
  211. });
  212. registerFunction("time.getmillis", (sc, in) -> (double) System.currentTimeMillis());
  213. registerFunction("time.getnanos", (sc, in) -> (double) System.nanoTime());
  214. registerFunction("time.from", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).getTimeInMillis());
  215. registerConsumer("time.nextday", (sc, in) -> {
  216. GregorianCalendar cal = (GregorianCalendar) in[0].get(sc);
  217. cal.add(Calendar.DAY_OF_YEAR, 1);
  218. cal.set(Calendar.HOUR_OF_DAY, 0);
  219. cal.set(Calendar.SECOND, 0);
  220. cal.set(Calendar.MINUTE, 0);
  221. cal.set(Calendar.MILLISECOND, 0);
  222. });
  223. registerFunction("time.getyear", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.YEAR));
  224. registerFunction("time.getmonth", (sc, in) -> (double) (((GregorianCalendar) in[0].get(sc)).get(Calendar.MONTH) + 1));
  225. registerFunction("time.getday", (sc, in) -> (double) (((GregorianCalendar) in[0].get(sc)).get(Calendar.DAY_OF_MONTH)));
  226. registerFunction("time.gethour", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.HOUR_OF_DAY));
  227. registerFunction("time.getminute", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.MINUTE));
  228. registerFunction("time.getsecond", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.SECOND));
  229. registerFunction("text.matches", (sc, in) -> in[0].getString(sc).matches(in[1].getString(sc)));
  230. registerFunction("text.number", (sc, in) -> SnuviUtils.toString(in[0].getDouble(sc)));
  231. registerFunction("text.class", (sc, in) -> in[0].get(sc).getClass().getSimpleName());
  232. registerFunction("text.tolowercase", (sc, in) -> SnuviUtils.connect(sc, in, 0).toLowerCase());
  233. registerAlias("text.tolowercase", "tolowercase");
  234. registerFunction("text.touppercase", (sc, in) -> SnuviUtils.connect(sc, in, 0).toUpperCase());
  235. registerAlias("text.touppercase", "touppercase");
  236. registerFunction("text.split", (sc, in) -> {
  237. String[] parts = in[1].getString(sc).split(in[0].getString(sc));
  238. ArrayList<Object> list = new ArrayList<>();
  239. for(String part : parts) {
  240. list.add(SnuviUtils.convert(part));
  241. }
  242. return list;
  243. });
  244. registerAlias("text.split", "split");
  245. registerFunction("text.convert", (sc, in) -> SnuviUtils.convert(in[0].getString(sc)));
  246. registerFunction("text.concatlist", (sc, in) -> {
  247. StringBuilder sb = new StringBuilder();
  248. List<Object> list = (List<Object>) in[0].get(sc);
  249. String splitter = in[1].getString(sc);
  250. Iterator<Object> iter = list.iterator();
  251. int from = in[2].getInt(sc);
  252. int to = Math.min(in[3].getInt(sc), list.size() - 1);
  253. to -= from;
  254. while(iter.hasNext() && from > 0) {
  255. iter.next();
  256. from--;
  257. }
  258. while(iter.hasNext() && to > 0) {
  259. sb.append(iter.next());
  260. sb.append(splitter);
  261. to--;
  262. }
  263. if(iter.hasNext() && to == 0) {
  264. sb.append(iter.next());
  265. }
  266. return sb.toString();
  267. });
  268. registerAlias("text.concatlist", "concatlist");
  269. registerFunction("text.concat", (sc, in) -> SnuviUtils.connect(sc, in, 0));
  270. registerAlias("text.concat", "concat");
  271. registerFunction("text.concatspace", (sc, in) -> SnuviUtils.connect(sc, in, " ", 0));
  272. registerFunction("text", (sc, in) -> String.valueOf(in[0].get(sc)));
  273. registerFunction("text.substring", (sc, in) -> in[0].getString(sc).substring(in[1].getInt(sc), in[2].getInt(sc)));
  274. registerFunction("text.length", (sc, in) -> (double) in[0].getString(sc).length());
  275. registerFunction("text.startswith", (sc, in) -> in[0].getString(sc).startsWith(in[1].getString(sc), in[2].getInt(sc)));
  276. registerFunction("text.endswith", (sc, in) -> in[0].getString(sc).endsWith(in[1].getString(sc)));
  277. registerFunction("text.contains", (sc, in) -> in[0].getString(sc).contains(in[1].getString(sc)));
  278. registerFunction("text.indexof", (sc, in) -> (double) in[0].getString(sc).indexOf(in[1].getString(sc), in[2].getInt(sc)));
  279. registerFunction("text.lastindexof", (sc, in) -> (double) in[0].getString(sc).lastIndexOf(in[1].getString(sc), in[2].getInt(sc)));
  280. registerFunction("text.replace", (sc, in) -> in[0].getString(sc).replace(in[1].getString(sc), in[2].getString(sc)));
  281. registerFunction("text.trim", (sc, in) -> in[0].getString(sc).trim());
  282. registerFunction("text.charat", (sc, in) -> String.valueOf(in[0].getString(sc).charAt(in[1].getInt(sc))));
  283. registerFunction("text.charcode", (sc, in) -> (double) in[0].getString(sc).charAt(in[1].getInt(sc)));
  284. registerFunction("text.fromcode", (sc, in) -> String.valueOf((char) in[0].getInt(sc)));
  285. registerFunction("text.onlyletters", (sc, in) -> {
  286. for(char c : in[0].getString(sc).toCharArray()) {
  287. if(!Character.isLetter(c)) {
  288. return false;
  289. }
  290. }
  291. return true;
  292. });
  293. registerFunction("file.new", (sc, in) -> new File(in[0].getString(sc)));
  294. registerFunction("file.exists", (sc, in) -> ((File) in[0].get(sc)).exists());
  295. registerFunction("file.isfile", (sc, in) -> ((File) in[0].get(sc)).isFile());
  296. registerFunction("file.isdirectory", (sc, in) -> ((File) in[0].get(sc)).isDirectory());
  297. registerFunction("file.delete", (sc, in) -> ((File) in[0].get(sc)).delete());
  298. registerFunction("file.getname", (sc, in) -> ((File) in[0].get(sc)).getName());
  299. registerFunction("file.getlist", (sc, in) -> Arrays.asList(((File) in[0].get(sc)).listFiles()));
  300. registerFunction("file.read", (sc, in) -> Files.readAllLines(((File) in[0].get(sc)).toPath()));
  301. registerConsumer("file.write", (sc, in) -> {
  302. File f = (File) in[0].get(sc);
  303. if(f.getParentFile() != null) {
  304. f.getParentFile().mkdirs();
  305. }
  306. if(!f.exists()) {
  307. f.createNewFile();
  308. }
  309. Files.write(Paths.get(f.toURI()), ((List<Object>) in[1].get(sc))
  310. .stream().map(o -> String.valueOf(o)).collect(Collectors.toList()), StandardCharsets.UTF_8);
  311. });
  312. registerFunction("config.new", (sc, in) -> new SnuviConfig(in[0].getString(sc), in[1].getString(sc)));
  313. registerFunction("config.exists", (sc, in) -> ((SnuviConfig) in[0].get(sc)).exists());
  314. registerFunction("config.save", (sc, in) -> ((SnuviConfig) in[0].get(sc)).save(sc));
  315. registerConsumer("config.load", (sc, in) -> ((SnuviConfig) in[0].get(sc)).load(sc));
  316. registerFunction("config.delete", (sc, in) -> ((SnuviConfig) in[0].get(sc)).delete());
  317. registerConsumer("config.set", (sc, in) -> ((SnuviConfig) in[0].get(sc)).set(in[1].getString(sc), in[2].get(sc)));
  318. registerFunction("config.getbool", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getBoolean(sc, in[1].getString(sc), in[2].getBoolean(sc)));
  319. registerFunction("config.getdouble", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getDouble(sc, in[1].getString(sc), in[2].getDouble(sc)));
  320. registerFunction("config.getstring", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getString(sc, in[1].getString(sc), in[2].getString(sc)));
  321. registerFunction("read.number", (sc, in) -> Double.parseDouble(in[0].getString(sc)));
  322. registerFunction("+", (sc, in) -> in[0].getDouble(sc) + in[1].getDouble(sc));
  323. registerAlias("+", "add");
  324. registerFunction("-", (sc, in) -> in.length == 1 ? -in[0].getDouble(sc) : in[0].getDouble(sc) - in[1].getDouble(sc));
  325. registerAlias("-", "sub");
  326. registerFunction("*", (sc, in) -> in[0].getDouble(sc) * in[1].getDouble(sc));
  327. registerAlias("*", "mul");
  328. registerFunction("/", (sc, in) -> in[0].getDouble(sc) / in[1].getDouble(sc));
  329. registerAlias("/", "div");
  330. registerFunction("=", (sc, in) -> {
  331. Object o = in[1].get(sc);
  332. in[0].set(sc, o);
  333. return o;
  334. });
  335. registerFunction("+=", (sc, in) -> {
  336. Object o = in[0].getDouble(sc) + in[1].getDouble(sc);
  337. in[0].set(sc, o);
  338. return o;
  339. });
  340. registerFunction("p++", (sc, in) -> {
  341. double d = in[0].getDouble(sc);
  342. in[0].set(sc, d + 1.0);
  343. return d;
  344. });
  345. registerAlias("p++", "inc");
  346. registerFunction("++", (sc, in) -> {
  347. double d = in[0].getDouble(sc) + 1.0;
  348. in[0].set(sc, d);
  349. return d;
  350. });
  351. registerFunction("-=", (sc, in) -> {
  352. Object o = in[0].getDouble(sc) - in[1].getDouble(sc);
  353. in[0].set(sc, o);
  354. return o;
  355. });
  356. registerFunction("p--", (sc, in) -> {
  357. double d = in[0].getDouble(sc);
  358. in[0].set(sc, d - 1.0);
  359. return d;
  360. });
  361. registerAlias("p--", "dec");
  362. registerFunction("--", (sc, in) -> {
  363. double d = in[0].getDouble(sc) - 1.0;
  364. in[0].set(sc, d);
  365. return d;
  366. });
  367. registerFunction("*=", (sc, in) -> {
  368. Object o = in[0].getDouble(sc) * in[1].getDouble(sc);
  369. in[0].set(sc, o);
  370. return o;
  371. });
  372. registerFunction("/=", (sc, in) -> {
  373. Object o = in[0].getDouble(sc) / in[1].getDouble(sc);
  374. in[0].set(sc, o);
  375. return o;
  376. });
  377. registerFunction("%=", (sc, in) -> {
  378. Object o = (double) (in[0].getInt(sc) % in[1].getInt(sc));
  379. in[0].set(sc, o);
  380. return o;
  381. });
  382. registerFunction("<<=", (sc, in) -> {
  383. Object o = (double) (in[0].getInt(sc) << in[1].getInt(sc));
  384. in[0].set(sc, o);
  385. return o;
  386. });
  387. registerFunction(">>=", (sc, in) -> {
  388. Object o = (double) (in[0].getInt(sc) >> in[1].getInt(sc));
  389. in[0].set(sc, o);
  390. return o;
  391. });
  392. registerFunction("&=", (sc, in) -> {
  393. Object o = (double) (in[0].getInt(sc) & in[1].getInt(sc));
  394. in[0].set(sc, o);
  395. return o;
  396. });
  397. registerFunction("^=", (sc, in) -> {
  398. Object o = (double) (in[0].getInt(sc) ^ in[1].getInt(sc));
  399. in[0].set(sc, o);
  400. return o;
  401. });
  402. registerFunction("|=", (sc, in) -> {
  403. Object o = (double) (in[0].getInt(sc) | in[1].getInt(sc));
  404. in[0].set(sc, o);
  405. return o;
  406. });
  407. registerFunction("getvar", (sc, in) -> sc.getVar(in[0].getString(sc)).get(sc));
  408. registerConsumer("setvar", (sc, in) -> sc.getVar(in[0].getString(sc)).set(sc, in[1].get(sc)));
  409. registerConsumer("removevar", (sc, in) -> sc.getVar(in[0].getString(sc)).set(sc, null));
  410. registerConsumer("wait", (sc, in) -> sc.setWaiting());
  411. registerConsumer("goto", (sc, in) -> sc.gotoLabel(in[0].getString(sc), true));
  412. registerConsumer("ignoregoto", (sc, in) -> sc.gotoLabel(in[0].getString(sc), false));
  413. registerAlias("ignoregoto", "igoto");
  414. registerConsumer("sgoto", (sc, in) -> {
  415. int time = in[0].getInt(sc);
  416. if(time < 0) {
  417. throw new IllegalArgumentException("time units can't be negative");
  418. }
  419. String label = in[1].getString(sc);
  420. sc.getScriptManager().getScheduler().scheduleTask(() -> {
  421. if(sc.shouldTerm() || sc.isHolded()) {
  422. return;
  423. }
  424. sc.gotoLabel(label, true, 1);
  425. sc.run();
  426. if(sc.shouldTerm()) {
  427. sc.getScriptManager().removeScript(sc);
  428. }
  429. }, time);
  430. });
  431. registerConsumer("gosub", (sc, in) -> sc.goSub(in[0].getString(sc)));
  432. registerFunction("==", (sc, in) -> Objects.equals(in[0].get(sc), in[1].get(sc)));
  433. registerAlias("==", "equal");
  434. registerAlias("==", "equals");
  435. registerFunction("!=", (sc, in) -> !Objects.equals(in[0].get(sc), in[1].get(sc)));
  436. registerAlias("!=", "notequal");
  437. registerFunction("<", (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) < 0);
  438. registerAlias("<", "less");
  439. registerFunction(">", (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) > 0);
  440. registerAlias(">", "greater");
  441. registerFunction("<=", (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) <= 0);
  442. registerAlias("<=", "lessequal");
  443. registerFunction(">=", (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) >= 0);
  444. registerAlias(">=", "greaterequal");
  445. registerFunction("!", (sc, in) -> !in[0].getBoolean(sc));
  446. registerAlias("!", "invert");
  447. registerFunction("&&", (sc, in) -> {
  448. for(InputProvider i : in) {
  449. if(!i.getBoolean(sc)) {
  450. return false;
  451. }
  452. }
  453. return true;
  454. });
  455. registerAlias("&&", "and");
  456. registerFunction("||", (sc, in) -> {
  457. for(InputProvider i : in) {
  458. if(i.getBoolean(sc)) {
  459. return true;
  460. }
  461. }
  462. return false;
  463. });
  464. registerAlias("||", "or");
  465. registerFunction("getscriptvar", (sc, in) -> GLOBAL_VARS.get(in[0].getString(sc)));
  466. registerFunction("setscriptvar", (sc, in) -> GLOBAL_VARS.put(in[0].getString(sc), in[1].get(sc)));
  467. registerFunction("delscriptvar", (sc, in) -> GLOBAL_VARS.remove(in[0].getString(sc)));
  468. registerConsumer("clearscriptvars", (sc, in) -> GLOBAL_VARS.clear());
  469. registerFunction("hasnext", (sc, in) -> ((Iterator) in[0].get(sc)).hasNext());
  470. registerFunction("next", (sc, in) -> ((Iterator) in[0].get(sc)).next());
  471. registerConsumer("remove", (sc, in) -> ((Iterator) in[0].get(sc)).remove());
  472. registerConsumer("swap", (sc, in) -> {
  473. Object o = in[0].get(sc);
  474. in[0].set(sc, in[1].get(sc));
  475. in[1].set(sc, o);
  476. });
  477. registerConsumer("print", (sc, in) -> {
  478. sc.getScriptManager().getLogger().print(SnuviUtils.connect(sc, in, 0), null, "print", sc.getName(), sc, sc.getStackTrace());
  479. });
  480. registerConsumer("rprint", (sc, in) -> System.out.println(SnuviUtils.connect(sc, in, 0)));
  481. registerConsumer("waitfor", (sc, in) -> {
  482. long l = in[0].getInt(sc);
  483. if(l < 0) {
  484. throw new IllegalArgumentException("time units can't be negative");
  485. }
  486. sc.setHolded(true);
  487. sc.setWaiting();
  488. sc.getScriptManager().getScheduler().scheduleTask(() -> {
  489. if(sc.shouldTerm()) {
  490. return;
  491. }
  492. sc.setHolded(false);
  493. sc.run();
  494. if(sc.shouldTerm()) {
  495. sc.getScriptManager().removeScript(sc);
  496. }
  497. }, l);
  498. });
  499. registerConsumer("term", (sc, in) -> {
  500. sc.term();
  501. sc.getScriptManager().removeScript(sc);
  502. });
  503. registerFunction("isbool", (sc, in) -> (in[0].get(sc) instanceof Boolean));
  504. registerFunction("isdouble", (sc, in) -> (in[0].get(sc) instanceof Double));
  505. registerFunction("islong", (sc, in) -> {
  506. Object o = in[0].get(sc);
  507. if(o instanceof Double) {
  508. double d = (Double) o;
  509. return d == (long) d;
  510. }
  511. return false;
  512. });
  513. registerConsumer("assert", (sc, in) -> {
  514. if(!in[0].getBoolean(sc)) {
  515. throw new IllegalArgumentException("assertion failed");
  516. }
  517. });
  518. registerFunction("class", (sc, in) -> in[0].get(sc).getClass());
  519. registerFunction("usedmemory", (sc, in) -> {
  520. Runtime runtime = Runtime.getRuntime();
  521. double usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / 1048576;
  522. return usedMemory;
  523. });
  524. registerFunction("allocatedmemory", (sc, in) -> Runtime.getRuntime().totalMemory() / 1048576.0);
  525. }
  526. }