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

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