[FEATURE][EXPERIMENTAL] LauncherInitContext и возможность инициализации модуля во время работы

This commit is contained in:
Gravit 2019-08-26 16:42:04 +07:00
parent 1646d8b473
commit 114cd2f8c6
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 23 additions and 10 deletions

View file

@ -0,0 +1,4 @@
package pro.gravit.launcher.modules;
public interface LauncherInitContext {
}

View file

@ -66,7 +66,7 @@ public void preInit() {
//NOP //NOP
} }
public abstract void init(); public abstract void init(LauncherInitContext initContext);
protected <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass) protected <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)

View file

@ -1,10 +1,7 @@
package pro.gravit.launcher.modules.impl; package pro.gravit.launcher.modules.impl;
import pro.gravit.launcher.managers.SimpleModulesConfigManager; import pro.gravit.launcher.managers.SimpleModulesConfigManager;
import pro.gravit.launcher.modules.LauncherModule; import pro.gravit.launcher.modules.*;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.launcher.modules.LauncherModulesManager;
import pro.gravit.launcher.modules.ModulesConfigManager;
import pro.gravit.utils.PublicURLClassLoader; import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.Version; import pro.gravit.utils.Version;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
@ -29,6 +26,8 @@ public class SimpleModuleManager implements LauncherModulesManager {
protected final List<String> moduleNames = new ArrayList<>(); protected final List<String> moduleNames = new ArrayList<>();
protected final SimpleModuleContext context; protected final SimpleModuleContext context;
protected final ModulesConfigManager modulesConfigManager; protected final ModulesConfigManager modulesConfigManager;
protected final Path modulesDir;
protected LauncherInitContext initContext;
protected PublicURLClassLoader classLoader = new PublicURLClassLoader(new URL[]{}); 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 { public void autoload(Path dir) throws IOException {
if (Files.notExists(dir)) Files.createDirectory(dir); if (Files.notExists(dir)) Files.createDirectory(dir);
else { else {
@ -50,7 +53,7 @@ public void autoload(Path dir) throws IOException {
} }
} }
public void initModules() { public void initModules(LauncherInitContext initContext) {
List<LauncherModule> startedModules = Collections.unmodifiableList(new ArrayList<>(modules)); List<LauncherModule> startedModules = Collections.unmodifiableList(new ArrayList<>(modules));
for(LauncherModule module : startedModules) for(LauncherModule module : startedModules)
{ {
@ -69,14 +72,13 @@ public void initModules() {
{ {
isAnyModuleLoad = true; isAnyModuleLoad = true;
module.setInitPhase(LauncherModule.InitPhase.INIT); module.setInitPhase(LauncherModule.InitPhase.INIT);
module.init(); module.init(initContext);
module.setInitPhase(LauncherModule.InitPhase.FINISH); module.setInitPhase(LauncherModule.InitPhase.FINISH);
loaded++; 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) for(LauncherModule module : modules)
{ {
if(module.getInitPhase().equals(LauncherModule.InitPhase.CREATED)) if(module.getInitPhase().equals(LauncherModule.InitPhase.CREATED))
@ -84,7 +86,7 @@ public void initModules() {
LauncherModuleInfo info = module.getModuleInfo(); LauncherModuleInfo info = module.getModuleInfo();
LogHelper.warning("Module %s required %s. Cyclic dependencies?", info.name, Arrays.toString(info.dependencies)); LogHelper.warning("Module %s required %s. Cyclic dependencies?", info.name, Arrays.toString(info.dependencies));
module.setInitPhase(LauncherModule.InitPhase.INIT); module.setInitPhase(LauncherModule.InitPhase.INIT);
module.init(); module.init(initContext);
module.setInitPhase(LauncherModule.InitPhase.FINISH); module.setInitPhase(LauncherModule.InitPhase.FINISH);
} }
} }
@ -105,14 +107,21 @@ private boolean checkDepend(LauncherModule module)
public SimpleModuleManager(Path modulesDir, Path configDir) { public SimpleModuleManager(Path modulesDir, Path configDir) {
modulesConfigManager = new SimpleModulesConfigManager(configDir); modulesConfigManager = new SimpleModulesConfigManager(configDir);
context = new SimpleModuleContext(this, modulesConfigManager); context = new SimpleModuleContext(this, modulesConfigManager);
this.modulesDir = modulesDir;
} }
@Override @Override
public LauncherModule loadModule(LauncherModule module) { public LauncherModule loadModule(LauncherModule module) {
if(modules.contains(module)) return module;
modules.add(module); modules.add(module);
LauncherModuleInfo info = module.getModuleInfo(); LauncherModuleInfo info = module.getModuleInfo();
moduleNames.add(info.name); moduleNames.add(info.name);
module.setContext(context); module.setContext(context);
if(initContext != null)
{
module.preInit();
module.init(initContext);
}
return module; return module;
} }