Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. public class CoreModuleManager implements ModuleManager {
  2. private final List<PluginModule> pluginModules;
  3. private final Path path;
  4. private final Gson gson;
  5.  
  6. public CoreModuleManager(final Path path) {
  7. this.pluginModules = new ArrayList<>();
  8. this.path = path;
  9. this.gson = new GsonBuilder().setPrettyPrinting().create();
  10. loadModules();
  11. }
  12.  
  13. private void loadModules() {
  14. Path modulePath = Paths.get(path.toAbsolutePath().toString(), "modules");
  15.  
  16. FileUtil.createDirIfNotExists(modulePath);
  17.  
  18. try (Stream<Path> pathStream = Files.walk(modulePath)) {
  19. pathStream.filter(path1 -> path1.toString().endsWith(".jar")).forEach(p -> {
  20. try {
  21. PluginModule pluginModule = loadModule(p);
  22. enableModule(pluginModule);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. });
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. public PluginModule loadModule(final Path path) throws Exception {
  33. if (!path.toFile().exists()) {
  34. throw new Exception(String.format("Could not find module at %s", path.toAbsolutePath()));
  35. }
  36.  
  37. JarFile file = new JarFile(path.toFile());
  38. ZipEntry moduleJson = file.getEntry("module.json");
  39.  
  40. System.out.println(path.toUri().toURL());
  41.  
  42. if (moduleJson == null) {
  43. throw new Exception(String.format("Could not find module json at %s", path.toAbsolutePath()));
  44. }
  45.  
  46. String moduleJsonSTR = CharStreams.toString(new InputStreamReader(file.getInputStream(moduleJson), Charsets.UTF_8));
  47. ModuleConfig config = this.gson.fromJson(moduleJsonSTR, ModuleConfig.class);
  48.  
  49. // URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.toUri().toURL()});
  50. URLClassLoader classLoader = new URLClassLoader(new URL[]{path.toUri().toURL()}, getClass().getClassLoader());
  51. Class mainClass = classLoader.loadClass(config.getMain());
  52.  
  53. PluginModule module = (PluginModule) mainClass.newInstance();
  54.  
  55. Field nameField = module.getClass().getSuperclass().getDeclaredField("name");
  56. nameField.setAccessible(true);
  57. nameField.set(module, config.getName());
  58. nameField.setAccessible(false);
  59.  
  60. Field versionField = module.getClass().getSuperclass().getDeclaredField("version");
  61. versionField.setAccessible(true);
  62. versionField.set(module, config.getVersion());
  63. versionField.setAccessible(false);
  64.  
  65. Field descField = module.getClass().getSuperclass().getDeclaredField("description");
  66. descField.setAccessible(true);
  67. descField.set(module, config.getDescription());
  68. versionField.setAccessible(false);
  69.  
  70. Field commandField = module.getClass().getSuperclass().getDeclaredField("commands");
  71. commandField.setAccessible(true);
  72. commandField.set(module, config.getCommands());
  73. versionField.setAccessible(false);
  74.  
  75. file.close();
  76. classLoader.close();
  77. return module;
  78. }
  79.  
  80. public List<PluginModule> findAllModules() {
  81. return pluginModules;
  82. }
  83.  
  84. public PluginModule findByName(String name) {
  85. return this.pluginModules.stream().filter(p -> p.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
  86. }
  87.  
  88. public void enableModule(PluginModule module) {
  89. enableModule(module, true);
  90. }
  91.  
  92. @Override
  93. public void enableModule(PluginModule module, boolean add) {
  94. module.onEnable();
  95. module.setEnabled(true);
  96. if (add)
  97. pluginModules.add(module);
  98. }
  99.  
  100. @Override
  101. public void disableModule(PluginModule module) {
  102. disableModule(module, true);
  103. }
  104.  
  105. public void disableModule(PluginModule module, boolean remove) {
  106. module.disable();
  107. module.setEnabled(false);
  108. if (remove)
  109. pluginModules.remove(module);
  110. }
  111.  
  112. public Path getPath() {
  113. return path;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement