FunctionRegistry.java 29 KB

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