You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScriptPluginManager.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.scriptplugin;
  18. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  19. import com.dmdirc.events.eventbus.BaseEvent;
  20. import com.dmdirc.events.PluginLoadedEvent;
  21. import com.dmdirc.events.PluginUnloadedEvent;
  22. import com.dmdirc.events.eventbus.EventBus;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.lang.reflect.Method;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import javax.inject.Inject;
  31. import javax.script.ScriptEngineManager;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import net.engio.mbassy.listener.Handler;
  35. import static com.dmdirc.util.LogUtils.USER_ERROR;
  36. public class ScriptPluginManager {
  37. private static final Logger LOG = LoggerFactory.getLogger(ScriptPluginManager.class);
  38. private final EventBus eventBus;
  39. private final String scriptDir;
  40. private final ScriptManager scriptManager;
  41. private final TypedProperties globalVariables;
  42. @Inject
  43. public ScriptPluginManager(final EventBus eventBus,
  44. @Directory(ScriptModule.SCRIPTS) final String scriptDir,
  45. final ScriptManager scriptManager,
  46. final ScriptEngineManager scriptEngineManager) {
  47. this.scriptDir = scriptDir;
  48. this.scriptManager = scriptManager;
  49. this.eventBus = eventBus;
  50. globalVariables = (TypedProperties) scriptEngineManager.get("globalVariables");
  51. }
  52. public void onLoad() {
  53. // Register the plugin_loaded action initially, this will be called
  54. // after this method finishes for us to register the rest.
  55. eventBus.subscribe(this);
  56. // Make sure our scripts dir exists
  57. final File newDir = new File(scriptDir);
  58. if (!newDir.exists()) {
  59. newDir.mkdirs();
  60. }
  61. final File savedVariables = new File(scriptDir + "storedVariables");
  62. if (savedVariables.exists()) {
  63. try (FileInputStream fis = new FileInputStream(savedVariables)) {
  64. globalVariables.load(fis);
  65. } catch (IOException e) {
  66. LOG.info(USER_ERROR, "Error reading savedVariables from '{}': {}",
  67. savedVariables.getPath(), e.getMessage(), e);
  68. }
  69. }
  70. }
  71. public void onUnLoad() {
  72. eventBus.unsubscribe(this);
  73. final File savedVariables = new File(scriptDir + "storedVariables");
  74. try (FileOutputStream fos = new FileOutputStream(savedVariables)) {
  75. globalVariables.store(fos, "# DMDirc Script Plugin savedVariables");
  76. } catch (IOException e) {
  77. LOG.info(USER_ERROR, "Error writing savedVariables to '{}': {}",
  78. savedVariables.getPath(), e.getMessage(), e);
  79. }
  80. }
  81. @Handler
  82. public void handlePluginLoadEvent(final BaseEvent event) throws ReflectiveOperationException {
  83. if (event instanceof PluginLoadedEvent || event instanceof PluginUnloadedEvent) {
  84. return;
  85. }
  86. final String name = event.getClass().getSimpleName()
  87. .replaceAll("Event$", "")
  88. .replaceAll("(.)([A-Z])", "$1_$2")
  89. .toUpperCase();
  90. final List<Object> arguments = new ArrayList<>();
  91. for (Method method : event.getClass().getMethods()) {
  92. if (method.getName().startsWith("get") && method.getParameterTypes().length == 0 &&
  93. !"getDisplayFormat".equals(method.getName())) {
  94. arguments.add(method.invoke(event));
  95. }
  96. }
  97. scriptManager.callFunctionAll("action_" + name, arguments.toArray());
  98. }
  99. }