FunctionRegistry.java 30 KB

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