FunctionRegistry.java 30 KB

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