FunctionRegistry.java 29 KB

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