From 114cd2f8c626c7e06c627342908a9a1d83c2a2e2 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 26 Aug 2019 16:42:04 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE][EXPERIMENTAL]=20LauncherInitContext?= =?UTF-8?q?=20=D0=B8=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BC=D0=BE=D0=B4=D1=83?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=B2=D0=BE=20=D0=B2=D1=80=D0=B5=D0=BC=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launcher/modules/LauncherInitContext.java | 4 +++ .../launcher/modules/LauncherModule.java | 2 +- .../modules/impl/SimpleModuleManager.java | 27 ++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherInitContext.java diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherInitContext.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherInitContext.java new file mode 100644 index 00000000..7738c826 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherInitContext.java @@ -0,0 +1,4 @@ +package pro.gravit.launcher.modules; + +public interface LauncherInitContext { +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java index 1f785e81..bef778b4 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java @@ -66,7 +66,7 @@ public void preInit() { //NOP } - public abstract void init(); + public abstract void init(LauncherInitContext initContext); protected boolean registerEvent(EventHandler handle, Class tClass) diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/impl/SimpleModuleManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/impl/SimpleModuleManager.java index a189d2c2..98ccf6b1 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/impl/SimpleModuleManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/impl/SimpleModuleManager.java @@ -1,10 +1,7 @@ package pro.gravit.launcher.modules.impl; import pro.gravit.launcher.managers.SimpleModulesConfigManager; -import pro.gravit.launcher.modules.LauncherModule; -import pro.gravit.launcher.modules.LauncherModuleInfo; -import pro.gravit.launcher.modules.LauncherModulesManager; -import pro.gravit.launcher.modules.ModulesConfigManager; +import pro.gravit.launcher.modules.*; import pro.gravit.utils.PublicURLClassLoader; import pro.gravit.utils.Version; import pro.gravit.utils.helper.IOHelper; @@ -29,6 +26,8 @@ public class SimpleModuleManager implements LauncherModulesManager { protected final List moduleNames = new ArrayList<>(); protected final SimpleModuleContext context; protected final ModulesConfigManager modulesConfigManager; + protected final Path modulesDir; + protected LauncherInitContext initContext; protected PublicURLClassLoader classLoader = new PublicURLClassLoader(new URL[]{}); @@ -43,6 +42,10 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO } } + public void autoload() throws IOException { + autoload(modulesDir); + } + public void autoload(Path dir) throws IOException { if (Files.notExists(dir)) Files.createDirectory(dir); else { @@ -50,7 +53,7 @@ public void autoload(Path dir) throws IOException { } } - public void initModules() { + public void initModules(LauncherInitContext initContext) { List startedModules = Collections.unmodifiableList(new ArrayList<>(modules)); for(LauncherModule module : startedModules) { @@ -69,14 +72,13 @@ public void initModules() { { isAnyModuleLoad = true; module.setInitPhase(LauncherModule.InitPhase.INIT); - module.init(); + module.init(initContext); module.setInitPhase(LauncherModule.InitPhase.FINISH); loaded++; } } + if(modules_size >= loaded) return; } - if(modules_size == loaded) return; - if(loaded > modules_size) throw new IllegalStateException(String.format("Module loading error. %d > %d", loaded, modules_size)); //WTF? for(LauncherModule module : modules) { if(module.getInitPhase().equals(LauncherModule.InitPhase.CREATED)) @@ -84,7 +86,7 @@ public void initModules() { LauncherModuleInfo info = module.getModuleInfo(); LogHelper.warning("Module %s required %s. Cyclic dependencies?", info.name, Arrays.toString(info.dependencies)); module.setInitPhase(LauncherModule.InitPhase.INIT); - module.init(); + module.init(initContext); module.setInitPhase(LauncherModule.InitPhase.FINISH); } } @@ -105,14 +107,21 @@ private boolean checkDepend(LauncherModule module) public SimpleModuleManager(Path modulesDir, Path configDir) { modulesConfigManager = new SimpleModulesConfigManager(configDir); context = new SimpleModuleContext(this, modulesConfigManager); + this.modulesDir = modulesDir; } @Override public LauncherModule loadModule(LauncherModule module) { + if(modules.contains(module)) return module; modules.add(module); LauncherModuleInfo info = module.getModuleInfo(); moduleNames.add(info.name); module.setContext(context); + if(initContext != null) + { + module.preInit(); + module.init(initContext); + } return module; }