[FEATURE] Новые состояния модулей

This commit is contained in:
Gravit 2019-08-28 21:04:44 +07:00
parent f6e2df3e1c
commit 856fe8797d
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
5 changed files with 39 additions and 22 deletions

View file

@ -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<T extends Event>
@ -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

View file

@ -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);

View file

@ -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<LauncherModule> 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;
}

View file

@ -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;

View file

@ -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());
}