Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CoreModuleManager implements ModuleManager {
- private final List<PluginModule> pluginModules;
- private final Path path;
- private final Gson gson;
- public CoreModuleManager(final Path path) {
- this.pluginModules = new ArrayList<>();
- this.path = path;
- this.gson = new GsonBuilder().setPrettyPrinting().create();
- loadModules();
- }
- private void loadModules() {
- Path modulePath = Paths.get(path.toAbsolutePath().toString(), "modules");
- FileUtil.createDirIfNotExists(modulePath);
- try (Stream<Path> pathStream = Files.walk(modulePath)) {
- pathStream.filter(path1 -> path1.toString().endsWith(".jar")).forEach(p -> {
- try {
- PluginModule pluginModule = loadModule(p);
- enableModule(pluginModule);
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public PluginModule loadModule(final Path path) throws Exception {
- if (!path.toFile().exists()) {
- throw new Exception(String.format("Could not find module at %s", path.toAbsolutePath()));
- }
- JarFile file = new JarFile(path.toFile());
- ZipEntry moduleJson = file.getEntry("module.json");
- System.out.println(path.toUri().toURL());
- if (moduleJson == null) {
- throw new Exception(String.format("Could not find module json at %s", path.toAbsolutePath()));
- }
- String moduleJsonSTR = CharStreams.toString(new InputStreamReader(file.getInputStream(moduleJson), Charsets.UTF_8));
- ModuleConfig config = this.gson.fromJson(moduleJsonSTR, ModuleConfig.class);
- // URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{path.toUri().toURL()});
- URLClassLoader classLoader = new URLClassLoader(new URL[]{path.toUri().toURL()}, getClass().getClassLoader());
- Class mainClass = classLoader.loadClass(config.getMain());
- PluginModule module = (PluginModule) mainClass.newInstance();
- Field nameField = module.getClass().getSuperclass().getDeclaredField("name");
- nameField.setAccessible(true);
- nameField.set(module, config.getName());
- nameField.setAccessible(false);
- Field versionField = module.getClass().getSuperclass().getDeclaredField("version");
- versionField.setAccessible(true);
- versionField.set(module, config.getVersion());
- versionField.setAccessible(false);
- Field descField = module.getClass().getSuperclass().getDeclaredField("description");
- descField.setAccessible(true);
- descField.set(module, config.getDescription());
- versionField.setAccessible(false);
- Field commandField = module.getClass().getSuperclass().getDeclaredField("commands");
- commandField.setAccessible(true);
- commandField.set(module, config.getCommands());
- versionField.setAccessible(false);
- file.close();
- classLoader.close();
- return module;
- }
- public List<PluginModule> findAllModules() {
- return pluginModules;
- }
- public PluginModule findByName(String name) {
- return this.pluginModules.stream().filter(p -> p.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
- }
- public void enableModule(PluginModule module) {
- enableModule(module, true);
- }
- @Override
- public void enableModule(PluginModule module, boolean add) {
- module.onEnable();
- module.setEnabled(true);
- if (add)
- pluginModules.add(module);
- }
- @Override
- public void disableModule(PluginModule module) {
- disableModule(module, true);
- }
- public void disableModule(PluginModule module, boolean remove) {
- module.disable();
- module.setEnabled(false);
- if (remove)
- pluginModules.remove(module);
- }
- public Path getPath() {
- return path;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement