diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerBuilder.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerBuilder.java index 0175b555..4a42c70b 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerBuilder.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerBuilder.java @@ -2,7 +2,6 @@ import pro.gravit.launchserver.config.LaunchServerConfig; import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; -import pro.gravit.launchserver.manangers.ModulesManager; import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager; import pro.gravit.utils.command.CommandHandler; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java index 5e0f9898..132f8d6f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java @@ -14,7 +14,6 @@ import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; import pro.gravit.launchserver.dao.provider.DaoProvider; import pro.gravit.launchserver.manangers.LaunchServerGsonManager; -import pro.gravit.launchserver.manangers.ModulesManager; import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager; import pro.gravit.launchserver.socket.WebSocketService; import pro.gravit.utils.command.CommandHandler; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/ModulesManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/ModulesManager.java deleted file mode 100644 index 0dbcb0c7..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/ModulesManager.java +++ /dev/null @@ -1,32 +0,0 @@ -package pro.gravit.launchserver.manangers; - -import java.net.URL; -import java.nio.file.Path; -import java.util.ArrayList; - -import pro.gravit.launcher.managers.SimpleModuleManager; -import pro.gravit.launcher.managers.SimpleModulesConfigManager; -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.modules.CoreModule; -import pro.gravit.launchserver.modules.LaunchServerModuleContext; -import pro.gravit.utils.PublicURLClassLoader; - -public class ModulesManager extends SimpleModuleManager { - public SimpleModulesConfigManager configManager; - - public ModulesManager(Path configDir) { - modules = new ArrayList<>(1); - configManager = new SimpleModulesConfigManager(configDir); - classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader()); - registerCoreModule(); - } - - public void initContext(LaunchServer server) - { - context = new LaunchServerModuleContext(server, classloader, configManager); - } - - private void registerCoreModule() { - load(new CoreModule()); - } -} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java deleted file mode 100644 index e66d88ee..00000000 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java +++ /dev/null @@ -1,158 +0,0 @@ -package pro.gravit.launcher.managers; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.jar.JarFile; - -import pro.gravit.launcher.modules.Module; -import pro.gravit.launcher.modules.ModuleContext; -import pro.gravit.launcher.modules.ModulesManager; -import pro.gravit.utils.PublicURLClassLoader; -import pro.gravit.utils.helper.IOHelper; -import pro.gravit.utils.helper.LogHelper; -@Deprecated -public class SimpleModuleManager implements ModulesManager { - protected final class ModulesVisitor extends SimpleFileVisitor { - private ModulesVisitor() { - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (file.toFile().getName().endsWith(".jar")) - try (JarFile f = new JarFile(file.toFile())) { - String mainclass = f.getManifest().getMainAttributes().getValue("Main-Class"); - if(mainclass == null) - { - LogHelper.error("In module %s Main-Class not found", file.toString()); - return super.visitFile(file, attrs); - } - loadModule(file.toUri().toURL(), mainclass); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { - LogHelper.error(e); - } - return super.visitFile(file, attrs); - } - } - - public ArrayList modules; - public PublicURLClassLoader classloader; - protected ModuleContext context; - - - public void autoload(Path dir) throws IOException { - LogHelper.info("Load modules"); - if (Files.notExists(dir)) Files.createDirectory(dir); - else { - IOHelper.walk(dir, new ModulesVisitor(), true); - } - sort(); - LogHelper.info("Loaded %d modules", modules.size()); - } - - public void sort() { - modules.sort((m1, m2) -> { - int p1 = m1.getPriority(); - int p2 = m2.getPriority(); - return Integer.compare(p2, p1); - }); - } - - @Override - public void close() { - for (Module m : modules) - try { - m.close(); - } catch (Throwable t) { - if (m.getName() != null) LogHelper.error("Error in stopping module: %s", m.getName()); - else LogHelper.error("Error in stopping one of modules"); - LogHelper.error(t); - } - } - - @Override - public void initModules() { - for (Module m : modules) { - m.init(context); - LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion()); - } - } - - @Override - public void load(Module module) { - modules.add(module); - } - - public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException { - try (JarFile f = new JarFile(Paths.get(jarpath.toURI()).toFile())) { - classloader.addURL(jarpath); - Module module = (Module) Class.forName(f.getManifest().getMainAttributes().getValue("Main-Class"), true, classloader).newInstance(); - modules.add(module); - module.preInit(context); - module.init(context); - module.postInit(context); - module.finish(context); - LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion()); - } - } - - @Override - public void loadModule(URL jarpath) throws Exception { - try (JarFile f = new JarFile(Paths.get(jarpath.toURI()).toFile())) { - loadModule(jarpath, f.getManifest().getMainAttributes().getValue("Main-Class")); - } - } - - @Override - public void loadModule(URL jarpath, String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException { - classloader.addURL(jarpath); - Module module = (Module) Class.forName(classname, true, classloader).newInstance(); - modules.add(module); - LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion()); - } - - @Override - public void postInitModules() { - for (Module m : modules) { - m.postInit(context); - LogHelper.info("Module %s version: %s post-init", m.getName(), m.getVersion()); - } - } - - - @Override - public void preInitModules() { - for (Module m : modules) { - m.preInit(context); - LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion()); - } - } - - @Override - public void printModules() { - for (Module m : modules) - LogHelper.info("Module %s version: %s", m.getName(), m.getVersion()); - LogHelper.info("Loaded %d modules", modules.size()); - } - - @Override - public void registerModule(Module module) { - modules.add(module); - LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion()); - } - - @Override - public void finishModules() { - for (Module m : modules) { - m.finish(context); - LogHelper.info("Module %s version: %s finished initialization", m.getName(), m.getVersion()); - } - } -} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java index ef8e3673..54b1754c 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java @@ -16,14 +16,12 @@ public SimpleModulesConfigManager(Path configDir) { } public Path getModuleConfig(String moduleName) { - if (!IOHelper.isDir(configDir)) { - try { - Files.createDirectories(configDir); - } catch (IOException e) { - LogHelper.error(e); - } - } - return configDir.resolve(moduleName.concat("Config.json")); + return getModuleConfig(moduleName, "Config"); + } + + @Override + public Path getModuleConfig(String moduleName, String configName) { + return getModuleConfigDir(moduleName).resolve(moduleName.concat(configName.concat(".json"))); } public Path getModuleConfigDir(String moduleName) { diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java index 89f1c0de..3d5522e5 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java @@ -5,5 +5,7 @@ public interface ModulesConfigManager { Path getModuleConfig(String moduleName); + Path getModuleConfig(String moduleName, String configName); + Path getModuleConfigDir(String moduleName); } 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 d80d3884..5d50f7c0 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 @@ -60,6 +60,11 @@ public void initModules(LauncherInitContext initContext) { 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)