FunctionRegistry.java 30 KB

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