From 04f147e3681dccbb05d12c4eb09aa9b2bcd07b5c Mon Sep 17 00:00:00 2001 From: Gravit Date: Thu, 29 Aug 2019 18:33:27 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=9F=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=BF=D0=BE=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=D1=83,=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BE=D0=BD=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D1=8B=D0=B2=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launcher/modules/LauncherModuleInfo.java | 13 +++++++++++ .../modules/LauncherModulesManager.java | 3 +++ .../modules/impl/SimpleModuleManager.java | 23 ++++++++++++++++++- .../java/pro/gravit/launcher/ModulesTest.java | 1 + .../gravit/launcher/impl/Depend1Module.java | 4 +++- .../gravit/launcher/impl/Depend2Module.java | 1 + .../gravit/launcher/impl/ProvidedModule.java | 18 +++++++++++++++ .../launcher/impl/VirtualInterface.java | 4 ++++ 8 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 LauncherAPI/src/test/java/pro/gravit/launcher/impl/ProvidedModule.java create mode 100644 LauncherAPI/src/test/java/pro/gravit/launcher/impl/VirtualInterface.java diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java index 2a39328b..26c0d298 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java @@ -7,12 +7,14 @@ public class LauncherModuleInfo { public final Version version; public final int priority; public final String[] dependencies; + public final String[] providers; public LauncherModuleInfo(String name, Version version) { this.name = name; this.version = version; this.priority = 0; this.dependencies = new String[]{}; + providers = new String[0]; } public LauncherModuleInfo(String name) { @@ -20,6 +22,7 @@ public LauncherModuleInfo(String name) { this.version = new Version(1,0,0); this.priority = 0; this.dependencies = new String[]{}; + providers = new String[0]; } public LauncherModuleInfo(String name, Version version, String[] dependencies) { @@ -27,6 +30,7 @@ public LauncherModuleInfo(String name, Version version, String[] dependencies) { this.version = version; this.priority = 0; this.dependencies = dependencies; + providers = new String[0]; } public LauncherModuleInfo(String name, Version version, int priority, String[] dependencies) { @@ -34,5 +38,14 @@ public LauncherModuleInfo(String name, Version version, int priority, String[] d this.version = version; this.priority = priority; this.dependencies = dependencies; + providers = new String[0]; + } + + public LauncherModuleInfo(String name, Version version, int priority, String[] dependencies, String[] providers) { + this.name = name; + this.version = version; + this.priority = priority; + this.dependencies = dependencies; + this.providers = providers; } } 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 8aa2f774..b85380d5 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.nio.file.Path; +import java.util.List; import java.util.function.Predicate; public interface LauncherModulesManager { @@ -23,6 +24,8 @@ default boolean containsModule(Class cl } ClassLoader getModuleClassLoader(); T getModule(Class clazz); + T getModuleByInterface(Class clazz); + List getModulesByInterface(Class clazz); T findModule(Class clazz, Predicate versionPredicate); /** 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 026bfd8e..e4acf821 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 @@ -153,7 +153,7 @@ public LauncherModule getModule(String name) { for(LauncherModule module : modules) { LauncherModuleInfo info = module.getModuleInfo(); - if(info.name.equals(name)) return module; + if(info.name.equals(name) || ( info.providers.length > 0 && Arrays.asList(info.providers).contains(name))) return module; } return null; } @@ -178,6 +178,27 @@ public T getModule(Class clazz) { return null; } + @Override + @SuppressWarnings("unchecked") + public T getModuleByInterface(Class clazz) { + for(LauncherModule module : modules) + { + if(clazz.isAssignableFrom(module.getClass())) return (T) module; + } + return null; + } + + @Override + @SuppressWarnings("unchecked") + public List getModulesByInterface(Class clazz) { + List list = new ArrayList<>(); + for(LauncherModule module : modules) + { + if(clazz.isAssignableFrom(module.getClass())) list.add((T) module); + } + return list; + } + @Override @SuppressWarnings("unchecked") public T findModule(Class clazz, Predicate versionPredicate) { diff --git a/LauncherAPI/src/test/java/pro/gravit/launcher/ModulesTest.java b/LauncherAPI/src/test/java/pro/gravit/launcher/ModulesTest.java index 0c98ea46..f2a6a98a 100644 --- a/LauncherAPI/src/test/java/pro/gravit/launcher/ModulesTest.java +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/ModulesTest.java @@ -53,6 +53,7 @@ public void dependenciesTest() Assertions.assertEquals(moduleManager.getModule("depend2").getInitStatus(), LauncherModule.InitStatus.FINISH); Assertions.assertEquals(moduleManager.getModule("depend3").getInitStatus(), LauncherModule.InitStatus.FINISH); Assertions.assertEquals(moduleManager.getModule("internal").getInitStatus(), LauncherModule.InitStatus.FINISH); + Assertions.assertEquals(moduleManager.getModule("virtual").getInitStatus(), LauncherModule.InitStatus.FINISH); Assertions.assertEquals(moduleManager.getModule("main").getInitStatus(), LauncherModule.InitStatus.FINISH); } @Test 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 ed4a03dd..107df843 100644 --- a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend1Module.java +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend1Module.java @@ -10,7 +10,7 @@ public class Depend1Module extends LauncherModule { public Depend1Module() { super(new LauncherModuleInfo("depend1", new Version(1,0,0), 0, - new String[]{"depend3", "internal"})); + new String[]{"depend3", "internal", "virtual"})); } @Override @@ -19,5 +19,7 @@ public void init(LauncherInitContext initContext) { Assertions.assertEquals(module.getInitStatus(), InitStatus.FINISH); Depend3Module module1 = modulesManager.getModule(Depend3Module.class); Assertions.assertEquals(module1.getInitStatus(), InitStatus.FINISH); + VirtualInterface virtualInterface = modulesManager.getModuleByInterface(VirtualInterface.class); + Assertions.assertEquals(((LauncherModule) virtualInterface).getInitStatus(), InitStatus.FINISH); } } 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 a5e592c3..d19246ea 100644 --- a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend2Module.java +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/Depend2Module.java @@ -12,6 +12,7 @@ public Depend2Module() { @Override public void preInitAction() { modulesManager.loadModule(new InternalModule()); + modulesManager.loadModule(new ProvidedModule()); } @Override diff --git a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/ProvidedModule.java b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/ProvidedModule.java new file mode 100644 index 00000000..826d768d --- /dev/null +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/ProvidedModule.java @@ -0,0 +1,18 @@ +package pro.gravit.launcher.impl; + +import pro.gravit.launcher.modules.LauncherInitContext; +import pro.gravit.launcher.modules.LauncherModule; +import pro.gravit.launcher.modules.LauncherModuleInfo; +import pro.gravit.utils.Version; + +public class ProvidedModule extends LauncherModule implements VirtualInterface { + public ProvidedModule() { + super(new LauncherModuleInfo("provided", new Version(1,0,0), + 0, new String[]{}, new String[]{"virtual"})); + } + + @Override + public void init(LauncherInitContext initContext) { + + } +} diff --git a/LauncherAPI/src/test/java/pro/gravit/launcher/impl/VirtualInterface.java b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/VirtualInterface.java new file mode 100644 index 00000000..affb5f45 --- /dev/null +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/impl/VirtualInterface.java @@ -0,0 +1,4 @@ +package pro.gravit.launcher.impl; + +public interface VirtualInterface { +}