FunctionRegistry.java 31 KB

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