FunctionRegistry.java 31 KB

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