ScriptManager.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package me.hammerle.snuviscript.code;
  2. import java.util.ArrayList;
  3. import me.hammerle.snuviscript.inputprovider.InputProvider;
  4. import java.util.Collection;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.function.Consumer;
  8. import me.hammerle.snuviscript.exceptions.PreScriptException;
  9. public class ScriptManager
  10. {
  11. private final ISnuviLogger logger;
  12. private final ISnuviScheduler scheduler;
  13. private final HashMap<Integer, Script> scripts = new HashMap<>();
  14. private final HashMap<String, HashSet<Script>> loadedEvents = new HashMap<>();
  15. private boolean isIterating = false;
  16. private final ArrayList<Script> addList = new ArrayList<>();
  17. private final ArrayList<Script> removeList = new ArrayList<>();
  18. private final ArrayList<Runnable> eventAddList = new ArrayList<>();
  19. public ScriptManager(ISnuviLogger logger, ISnuviScheduler scheduler)
  20. {
  21. this.logger = logger;
  22. this.scheduler = scheduler;
  23. }
  24. public ISnuviLogger getLogger()
  25. {
  26. return logger;
  27. }
  28. public ISnuviScheduler getScheduler()
  29. {
  30. return scheduler;
  31. }
  32. // -------------------------------------------------------------------------
  33. // function registry
  34. // -------------------------------------------------------------------------
  35. public void registerFunction(String s, ExceptionBiFunction<Script, InputProvider[], Object> f)
  36. {
  37. FunctionRegistry.registerFunction(s, f);
  38. }
  39. public void registerAlias(String original, String alias)
  40. {
  41. FunctionRegistry.registerAlias(original, alias);
  42. }
  43. // -------------------------------------------------------------------------
  44. // script controller
  45. // -------------------------------------------------------------------------
  46. public Script getScript(int id)
  47. {
  48. return scripts.get(id);
  49. }
  50. private void removeScriptUnsafe(Script sc)
  51. {
  52. sc.onTerm();
  53. scripts.remove(sc.getId());
  54. loadedEvents.values().forEach(list -> list.remove(sc));
  55. }
  56. private void addScript(Script sc)
  57. {
  58. scripts.put(sc.getId(), sc);
  59. sc.onStart();
  60. sc.run();
  61. if(sc.shouldTerm())
  62. {
  63. removeScriptUnsafe(sc);
  64. }
  65. }
  66. private void handleQueues()
  67. {
  68. if(!removeList.isEmpty())
  69. {
  70. removeList.forEach(sc -> removeScriptUnsafe(sc));
  71. removeList.clear();
  72. }
  73. if(!addList.isEmpty())
  74. {
  75. addList.forEach(sc -> addScript(sc));
  76. addList.clear();
  77. }
  78. if(!eventAddList.isEmpty())
  79. {
  80. eventAddList.forEach(r -> r.run());
  81. eventAddList.clear();
  82. }
  83. }
  84. public void removeScriptSafe(Script sc)
  85. {
  86. if(isIterating)
  87. {
  88. removeList.add(sc);
  89. }
  90. else
  91. {
  92. removeScriptUnsafe(sc);
  93. }
  94. }
  95. public boolean removeScriptsSafe()
  96. {
  97. if(isIterating)
  98. {
  99. return false;
  100. }
  101. scripts.values().forEach(sc -> sc.onTerm());
  102. scripts.clear();
  103. loadedEvents.values().forEach(list -> list.clear());
  104. return true;
  105. }
  106. public Collection<Script> getScripts()
  107. {
  108. return scripts.values();
  109. }
  110. public Script startScript(boolean rEventBroadcast, Consumer<Script> onStart, Consumer<Script> onTerm, String name, String... paths)
  111. {
  112. if(paths.length == 0)
  113. {
  114. return null;
  115. }
  116. try
  117. {
  118. Script sc = new Script(this, onStart, onTerm, name, paths);
  119. sc.setEventBroadcast(rEventBroadcast);
  120. if(isIterating)
  121. {
  122. addList.add(sc);
  123. }
  124. else
  125. {
  126. addScript(sc);
  127. }
  128. return sc;
  129. }
  130. catch(PreScriptException ex)
  131. {
  132. logger.print(ex.getLocalizedMessage(), ex, null, paths[0], null, ex.getLine());
  133. return null;
  134. }
  135. }
  136. public Script startScript(boolean rEventBroadcast, String name, String... paths)
  137. {
  138. return startScript(rEventBroadcast, null, null, name, paths);
  139. }
  140. // -------------------------------------------------------------------------
  141. // event
  142. // -------------------------------------------------------------------------
  143. private void loadEventUnsafe(String event, Script sc)
  144. {
  145. HashSet<Script> set = loadedEvents.get(event);
  146. if(set == null)
  147. {
  148. set = new HashSet<>();
  149. loadedEvents.put(event, set);
  150. }
  151. set.add(sc);
  152. }
  153. private void unloadEventUnsafe(String event, Script sc)
  154. {
  155. HashSet<Script> set = loadedEvents.get(event);
  156. if(set != null)
  157. {
  158. set.remove(sc);
  159. }
  160. }
  161. public void loadEventSafe(String event, Script sc)
  162. {
  163. if(isIterating)
  164. {
  165. eventAddList.add(() -> loadEventUnsafe(event, sc));
  166. }
  167. else
  168. {
  169. loadEventUnsafe(event, sc);
  170. }
  171. }
  172. public void unloadEventSafe(String event, Script sc)
  173. {
  174. if(isIterating)
  175. {
  176. eventAddList.add(() -> unloadEventUnsafe(event, sc));
  177. }
  178. else
  179. {
  180. unloadEventUnsafe(event, sc);
  181. }
  182. }
  183. public void callEvent(String name, Consumer<Script> before, Consumer<Script> after)
  184. {
  185. /*if(isIterating)
  186. {
  187. logger.print(String.format("'%s' called while executing another event", name), null, null, null, null, -1);
  188. return;
  189. }*/
  190. HashSet<Script> set = loadedEvents.get(name);
  191. if(set == null)
  192. {
  193. return;
  194. }
  195. isIterating = true;
  196. set.stream().filter(sc -> sc.shouldReceiveEventBroadcast() && !sc.isHolded() && sc.isWaiting())
  197. .forEach(sc ->
  198. {
  199. sc.setVar("event", name);
  200. if(before != null)
  201. {
  202. before.accept(sc);
  203. }
  204. sc.run();
  205. if(after != null)
  206. {
  207. after.accept(sc);
  208. }
  209. });
  210. isIterating = false;
  211. handleQueues();
  212. }
  213. public boolean callEvent(String name, Script sc, Consumer<Script> before, Consumer<Script> after)
  214. {
  215. if(sc.isEventLoaded(name) && !sc.isHolded() && sc.isWaiting())
  216. {
  217. sc.setVar("event", name);
  218. if(before != null)
  219. {
  220. before.accept(sc);
  221. }
  222. sc.run();
  223. if(after != null)
  224. {
  225. after.accept(sc);
  226. }
  227. if(sc.shouldTerm())
  228. {
  229. removeScriptUnsafe(sc);
  230. }
  231. return true;
  232. }
  233. return false;
  234. }
  235. }