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 0323b629..7878bb00 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java @@ -33,9 +33,22 @@ public LauncherModuleInfo getModuleInfo() { */ public enum InitStatus { - CREATED, - INIT, - FINISH + CREATED(false), + PRE_INIT_WAIT(true), + PRE_INIT(false), + INIT_WAIT(true), + INIT(false), + FINISH(true); + + InitStatus(boolean b) { + isAvailable = b; + } + + public boolean isAvailable() { + return isAvailable; + } + + private final boolean isAvailable; } @FunctionalInterface public interface EventHandler @@ -76,6 +89,7 @@ public void setContext(LauncherModulesContext context) this.context = context; this.modulesManager = context.getModulesManager(); this.modulesConfigManager = context.getModulesConfigManager(); + this.setInitStatus(InitStatus.PRE_INIT_WAIT); } /** @@ -89,9 +103,17 @@ public void setContext(LauncherModulesContext context) * Use API Launcher, LaunchServer, ServerWrapper * Change the names of any modules */ - public void preInit() { + public void preInitAction() { //NOP } + public LauncherModule preInit() + { + if(!initStatus.equals(InitStatus.PRE_INIT_WAIT)) throw new IllegalStateException("PreInit not allowed in current state"); + initStatus = InitStatus.PRE_INIT; + preInitAction(); + initStatus = InitStatus.INIT_WAIT; + return this; + } /** * Basic module initialization method diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java index 78983692..8aa2f774 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java @@ -7,6 +7,7 @@ import java.util.function.Predicate; public interface LauncherModulesManager { + LauncherModule loadModule(LauncherModule module); LauncherModule loadModule(Path file) throws IOException; LauncherModule getModule(String name); 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 80c037b1..e565e025 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 @@ -16,7 +16,6 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.function.Predicate; import java.util.jar.JarFile; @@ -54,39 +53,30 @@ public void autoload(Path dir) throws IOException { } public void initModules(LauncherInitContext initContext) { - List startedModules = Collections.unmodifiableList(new ArrayList<>(modules)); - for(LauncherModule module : startedModules) - { - module.preInit(); - } boolean isAnyModuleLoad = true; modules.sort((m1, m2) -> { int priority1 = m1.getModuleInfo().priority; int priority2 = m2.getModuleInfo().priority; return Integer.compare(priority1, priority2); }); - int modules_size = modules.size(); - int loaded = 0; while(isAnyModuleLoad) { isAnyModuleLoad = false; for(LauncherModule module : modules) { - if(!module.getInitStatus().equals(LauncherModule.InitStatus.CREATED)) continue; + if(!module.getInitStatus().equals(LauncherModule.InitStatus.INIT_WAIT)) continue; if(checkDepend(module)) { isAnyModuleLoad = true; module.setInitStatus(LauncherModule.InitStatus.INIT); module.init(initContext); module.setInitStatus(LauncherModule.InitStatus.FINISH); - loaded++; } } - //if(loaded >= modules_size) return; } for(LauncherModule module : modules) { - if(module.getInitStatus().equals(LauncherModule.InitStatus.CREATED)) + if(module.getInitStatus().equals(LauncherModule.InitStatus.INIT_WAIT)) { LauncherModuleInfo info = module.getModuleInfo(); LogHelper.warning("Module %s required %s. Cyclic dependencies?", info.name, Arrays.toString(info.dependencies)); @@ -94,6 +84,11 @@ public void initModules(LauncherInitContext initContext) { module.init(initContext); module.setInitStatus(LauncherModule.InitStatus.FINISH); } + else if(module.getInitStatus().equals(LauncherModule.InitStatus.PRE_INIT_WAIT)) + { + LauncherModuleInfo info = module.getModuleInfo(); + LogHelper.error("Module %s skip pre-init phase. This module NOT finish loading", info.name, Arrays.toString(info.dependencies)); + } } } @@ -104,7 +99,7 @@ private boolean checkDepend(LauncherModule module) { LauncherModule depModule = getModule(dep); if(depModule == null) throw new RuntimeException(String.format("Module %s required %s. %s not found", info.name, dep, dep)); - if(depModule.getInitStatus().equals(LauncherModule.InitStatus.CREATED)) return false; + if(!depModule.getInitStatus().equals(LauncherModule.InitStatus.FINISH)) return false; } return true; } @@ -122,10 +117,12 @@ public LauncherModule loadModule(LauncherModule module) { LauncherModuleInfo info = module.getModuleInfo(); moduleNames.add(info.name); module.setContext(context); + module.preInit(); if(initContext != null) { - module.preInit(); + module.setInitStatus(LauncherModule.InitStatus.INIT); module.init(initContext); + module.setInitStatus(LauncherModule.InitStatus.FINISH); } return module; } diff --git a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend1Module.java b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend1Module.java index 918f9abc..ed4a03dd 100644 --- a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend1Module.java +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend1Module.java @@ -1,7 +1,6 @@ package pro.gravit.launcher.impl; import org.junit.jupiter.api.Assertions; -import pro.gravit.launcher.ModulesTest; import pro.gravit.launcher.modules.LauncherInitContext; import pro.gravit.launcher.modules.LauncherModule; import pro.gravit.launcher.modules.LauncherModuleInfo; diff --git a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend2Module.java b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend2Module.java index 510a3a1d..a5e592c3 100644 --- a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend2Module.java +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend2Module.java @@ -1,6 +1,5 @@ package pro.gravit.launcher.impl; -import pro.gravit.launcher.ModulesTest; import pro.gravit.launcher.modules.LauncherInitContext; import pro.gravit.launcher.modules.LauncherModule; import pro.gravit.launcher.modules.LauncherModuleInfo; @@ -11,8 +10,7 @@ public Depend2Module() { } @Override - public void preInit() { - super.preInit(); + public void preInitAction() { modulesManager.loadModule(new InternalModule()); }