FunctionRegistry.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. try {
  350. Files.setPosixFilePermissions(p, FunctionRegistry.FILE_ACCESS);
  351. } catch(Exception ex) {
  352. }
  353. });
  354. registerConsumer("file.createfolder", (sc, in) -> {
  355. Path p = Paths.get(((File) in[0].get(sc)).toURI());
  356. Files.createDirectory(p, PosixFilePermissions.asFileAttribute(FILE_ACCESS));
  357. try {
  358. Files.setPosixFilePermissions(p, FunctionRegistry.FILE_ACCESS);
  359. } catch(Exception ex) {
  360. }
  361. });
  362. registerFunction("config.new",
  363. (sc, in) -> new SnuviConfig(in[0].getString(sc), in[1].getString(sc)));
  364. registerFunction("config.exists", (sc, in) -> ((SnuviConfig) in[0].get(sc)).exists());
  365. registerFunction("config.save", (sc, in) -> ((SnuviConfig) in[0].get(sc)).save(sc));
  366. registerConsumer("config.load", (sc, in) -> ((SnuviConfig) in[0].get(sc)).load(sc));
  367. registerFunction("config.delete", (sc, in) -> ((SnuviConfig) in[0].get(sc)).delete());
  368. registerConsumer("config.set",
  369. (sc, in) -> ((SnuviConfig) in[0].get(sc)).set(in[1].getString(sc), in[2].get(sc)));
  370. registerFunction("config.getbool", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getBoolean(sc,
  371. in[1].getString(sc), in[2].getBoolean(sc)));
  372. registerFunction("config.getdouble", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getDouble(sc,
  373. in[1].getString(sc), in[2].getDouble(sc)));
  374. registerFunction("config.getstring", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getString(sc,
  375. in[1].getString(sc), in[2].getString(sc)));
  376. registerFunction("read.number", (sc, in) -> {
  377. try {
  378. return Double.parseDouble(in[0].getString(sc));
  379. } catch(NumberFormatException ex) {
  380. return null;
  381. }
  382. });
  383. registerFunction("+", (sc, in) -> in[0].getDouble(sc) + in[1].getDouble(sc));
  384. registerAlias("+", "add");
  385. registerFunction("-", (sc, in) -> in.length == 1 ? -in[0].getDouble(sc)
  386. : in[0].getDouble(sc) - in[1].getDouble(sc));
  387. registerAlias("-", "sub");
  388. registerFunction("*", (sc, in) -> in[0].getDouble(sc) * in[1].getDouble(sc));
  389. registerAlias("*", "mul");
  390. registerFunction("/", (sc, in) -> in[0].getDouble(sc) / in[1].getDouble(sc));
  391. registerAlias("/", "div");
  392. registerFunction("=", (sc, in) -> {
  393. Object o = in[1].get(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("p++", (sc, in) -> {
  403. double d = in[0].getDouble(sc);
  404. in[0].set(sc, d + 1.0);
  405. return d;
  406. });
  407. registerAlias("p++", "inc");
  408. registerFunction("++", (sc, in) -> {
  409. double d = in[0].getDouble(sc) + 1.0;
  410. in[0].set(sc, d);
  411. return d;
  412. });
  413. registerFunction("-=", (sc, in) -> {
  414. Object o = in[0].getDouble(sc) - in[1].getDouble(sc);
  415. in[0].set(sc, o);
  416. return o;
  417. });
  418. registerFunction("p--", (sc, in) -> {
  419. double d = in[0].getDouble(sc);
  420. in[0].set(sc, d - 1.0);
  421. return d;
  422. });
  423. registerAlias("p--", "dec");
  424. registerFunction("--", (sc, in) -> {
  425. double d = in[0].getDouble(sc) - 1.0;
  426. in[0].set(sc, d);
  427. return d;
  428. });
  429. registerFunction("*=", (sc, in) -> {
  430. Object o = in[0].getDouble(sc) * in[1].getDouble(sc);
  431. in[0].set(sc, o);
  432. return o;
  433. });
  434. registerFunction("/=", (sc, in) -> {
  435. Object o = in[0].getDouble(sc) / in[1].getDouble(sc);
  436. in[0].set(sc, o);
  437. return o;
  438. });
  439. registerFunction("%=", (sc, in) -> {
  440. Object o = (double) (in[0].getInt(sc) % in[1].getInt(sc));
  441. in[0].set(sc, o);
  442. return o;
  443. });
  444. registerFunction("<<=", (sc, in) -> {
  445. Object o = (double) (in[0].getInt(sc) << in[1].getInt(sc));
  446. in[0].set(sc, o);
  447. return o;
  448. });
  449. registerFunction(">>=", (sc, in) -> {
  450. Object o = (double) (in[0].getInt(sc) >> in[1].getInt(sc));
  451. in[0].set(sc, o);
  452. return o;
  453. });
  454. registerFunction("&=", (sc, in) -> {
  455. Object o = (double) (in[0].getInt(sc) & in[1].getInt(sc));
  456. in[0].set(sc, o);
  457. return o;
  458. });
  459. registerFunction("^=", (sc, in) -> {
  460. Object o = (double) (in[0].getInt(sc) ^ in[1].getInt(sc));
  461. in[0].set(sc, o);
  462. return o;
  463. });
  464. registerFunction("|=", (sc, in) -> {
  465. Object o = (double) (in[0].getInt(sc) | in[1].getInt(sc));
  466. in[0].set(sc, o);
  467. return o;
  468. });
  469. registerFunction("getvar", (sc, in) -> sc.getVar(in[0].getString(sc)).get(sc));
  470. registerConsumer("setvar",
  471. (sc, in) -> sc.getVar(in[0].getString(sc)).set(sc, in[1].get(sc)));
  472. registerConsumer("removevar", (sc, in) -> sc.getVar(in[0].getString(sc)).set(sc, null));
  473. registerConsumer("wait", (sc, in) -> sc.setWaiting());
  474. registerConsumer("goto", (sc, in) -> sc.gotoLabel(in[0].getString(sc), true));
  475. registerConsumer("ignoregoto", (sc, in) -> sc.gotoLabel(in[0].getString(sc), false));
  476. registerAlias("ignoregoto", "igoto");
  477. registerConsumer("sgoto", (sc, in) -> {
  478. int time = in[0].getInt(sc);
  479. if(time < 0) {
  480. throw new IllegalArgumentException("time units can't be negative");
  481. }
  482. String label = in[1].getString(sc);
  483. int line = sc.getLine();
  484. sc.getScriptManager().getScheduler().scheduleTask("sgoto",
  485. new ScheduledGoto("sgoto", sc, label, line), time);
  486. });
  487. registerConsumer("gosub", (sc, in) -> sc.goSub(in[0].getString(sc)));
  488. registerFunction("==", (sc, in) -> Objects.equals(in[0].get(sc), in[1].get(sc)));
  489. registerAlias("==", "equal");
  490. registerAlias("==", "equals");
  491. registerFunction("!=", (sc, in) -> !Objects.equals(in[0].get(sc), in[1].get(sc)));
  492. registerAlias("!=", "notequal");
  493. registerFunction("<",
  494. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) < 0);
  495. registerAlias("<", "less");
  496. registerFunction(">",
  497. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) > 0);
  498. registerAlias(">", "greater");
  499. registerFunction("<=",
  500. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) <= 0);
  501. registerAlias("<=", "lessequal");
  502. registerFunction(">=",
  503. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) >= 0);
  504. registerAlias(">=", "greaterequal");
  505. registerFunction("!", (sc, in) -> !in[0].getBoolean(sc));
  506. registerAlias("!", "invert");
  507. registerFunction("&&", (sc, in) -> {
  508. for(InputProvider i : in) {
  509. if(!i.getBoolean(sc)) {
  510. return false;
  511. }
  512. }
  513. return true;
  514. });
  515. registerAlias("&&", "and");
  516. registerFunction("||", (sc, in) -> {
  517. for(InputProvider i : in) {
  518. if(i.getBoolean(sc)) {
  519. return true;
  520. }
  521. }
  522. return false;
  523. });
  524. registerAlias("||", "or");
  525. registerFunction("getscriptvar", (sc, in) -> GLOBAL_VARS.get(in[0].getString(sc)));
  526. registerFunction("setscriptvar",
  527. (sc, in) -> GLOBAL_VARS.put(in[0].getString(sc), in[1].get(sc)));
  528. registerFunction("delscriptvar", (sc, in) -> GLOBAL_VARS.remove(in[0].getString(sc)));
  529. registerConsumer("clearscriptvars", (sc, in) -> GLOBAL_VARS.clear());
  530. registerFunction("hasnext", (sc, in) -> ((Iterator) in[0].get(sc)).hasNext());
  531. registerFunction("next", (sc, in) -> ((Iterator) in[0].get(sc)).next());
  532. registerConsumer("remove", (sc, in) -> ((Iterator) in[0].get(sc)).remove());
  533. registerFunction("iterator", (sc, in) -> {
  534. Object o = in[0].get(sc);
  535. if(o instanceof Iterable) {
  536. return ((Iterable) o).iterator();
  537. }
  538. return new ArrayIterator((Object[]) in[0].get(sc));
  539. });
  540. registerConsumer("swap", (sc, in) -> {
  541. Object o = in[0].get(sc);
  542. in[0].set(sc, in[1].get(sc));
  543. in[1].set(sc, o);
  544. });
  545. registerConsumer("print", (sc, in) -> {
  546. sc.getScriptManager().getLogger().print(SnuviUtils.connect(sc, in, 0), null, "print",
  547. sc.getName(), sc, sc.getStackTrace());
  548. });
  549. registerConsumer("waitfor", (sc, in) -> {
  550. long l = in[0].getInt(sc);
  551. if(l < 0) {
  552. throw new IllegalArgumentException("time units can't be negative");
  553. }
  554. sc.setHolded(true);
  555. sc.setWaiting();
  556. sc.getScriptManager().getScheduler().scheduleTask("waitfor", () -> {
  557. if(sc.shouldTerm()) {
  558. return;
  559. }
  560. sc.setHolded(false);
  561. sc.run();
  562. if(sc.shouldTerm()) {
  563. sc.getScriptManager().removeScript(sc);
  564. }
  565. }, l);
  566. });
  567. registerConsumer("term", (sc, in) -> {
  568. sc.term();
  569. sc.getScriptManager().removeScript(sc);
  570. });
  571. registerFunction("isbool", (sc, in) -> (in[0].get(sc) instanceof Boolean));
  572. registerFunction("isdouble", (sc, in) -> (in[0].get(sc) instanceof Double));
  573. registerFunction("islong", (sc, in) -> {
  574. Object o = in[0].get(sc);
  575. if(o instanceof Double) {
  576. double d = (Double) o;
  577. return d == (long) d;
  578. }
  579. return false;
  580. });
  581. registerConsumer("assert", (sc, in) -> {
  582. if(!in[0].getBoolean(sc)) {
  583. throw new IllegalArgumentException("assertion failed");
  584. }
  585. });
  586. registerFunction("class", (sc, in) -> in[0].get(sc).getClass());
  587. registerFunction("usedmemory", (sc, in) -> {
  588. Runtime runtime = Runtime.getRuntime();
  589. double usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / 1048576;
  590. return usedMemory;
  591. });
  592. registerFunction("allocatedmemory",
  593. (sc, in) -> Runtime.getRuntime().totalMemory() / 1048576.0);
  594. }
  595. }